mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-09-28 04:36:40 +08:00
169cd86e00
ResolveRequest and the panel's resolveHost fell back to X-Real-IP for the host when a trusted proxy sent no X-Forwarded-Host. X-Real-IP names the visitor, so behind nginx with only that header set, subscription and exported links advertised the subscriber's own public IP as the server. The host now comes from a trusted X-Forwarded-Host, else the dialed request Host. X-Real-IP stays a client-IP source only. Fixes #6589.
62 lines
2.0 KiB
Go
62 lines
2.0 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func TestGetRemoteIpIgnoresForwardedHeadersFromUntrustedRemote(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
|
c.Request = httptest.NewRequest(http.MethodGet, "/", nil)
|
|
c.Request.RemoteAddr = "203.0.113.10:12345"
|
|
c.Request.Header.Set("X-Real-IP", "198.51.100.9")
|
|
c.Request.Header.Set("X-Forwarded-For", "198.51.100.8")
|
|
|
|
if got := getRemoteIp(c); got != "203.0.113.10" {
|
|
t.Fatalf("remote IP = %q, want request remote address", got)
|
|
}
|
|
}
|
|
|
|
func TestGetRemoteIpHonorsForwardedHeadersFromTrustedLoopbackProxy(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
|
c.Request = httptest.NewRequest(http.MethodGet, "/", nil)
|
|
c.Request.RemoteAddr = "127.0.0.1:12345"
|
|
c.Request.Header.Set("X-Forwarded-For", "198.51.100.8, 127.0.0.1")
|
|
|
|
if got := getRemoteIp(c); got != "198.51.100.8" {
|
|
t.Fatalf("remote IP = %q, want forwarded client IP", got)
|
|
}
|
|
}
|
|
|
|
func TestResolveHostPrefersForwardedHostOverRealIP(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
|
c.Request = httptest.NewRequest(http.MethodGet, "/", nil)
|
|
c.Request.Host = "panel.example.com:2053"
|
|
c.Request.RemoteAddr = "127.0.0.1:12345"
|
|
c.Request.Header.Set("X-Forwarded-Host", "sub.example.net:443")
|
|
c.Request.Header.Set("X-Real-IP", "198.51.100.7")
|
|
|
|
if got := resolveHost(c); got != "sub.example.net" {
|
|
t.Fatalf("resolveHost = %q, want X-Forwarded-Host", got)
|
|
}
|
|
}
|
|
|
|
func TestResolveHostIgnoresRealIPFromTrustedProxy(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
|
c.Request = httptest.NewRequest(http.MethodGet, "/", nil)
|
|
c.Request.Host = "panel.example.com:2053"
|
|
c.Request.RemoteAddr = "127.0.0.1:12345"
|
|
c.Request.Header.Set("X-Real-IP", "198.51.100.7")
|
|
|
|
if got := resolveHost(c); got != "panel.example.com" {
|
|
t.Fatalf("resolveHost = %q, want request host (not X-Real-IP)", got)
|
|
}
|
|
}
|