3x-ui/internal/web/controller/util_test.go
PluS98 968f8d0071 fix(clean-ip): run only the test script the panel made
The one-day command piped whatever the script URL returned into Python. On a
panel reached over plain HTTP that fetch crosses the ISP being tested, so anyone
on the path could swap in their own code. The script (and the pinned variant
fetched through a page IP) is now fixed when the command is made, and each
command is a python -c that fetches it and runs it only if its SHA-256 matches.

- The pinned command maps the panel's domain to the page IP for the fetch, as
  curl --resolve did, so it no longer needs curl and works the same in
  PowerShell, where a pipe re-encodes the script and would break any hash.
- The command's base URL honours X-Forwarded-Proto/-Host only from a trusted
  proxy, like resolveHost, and keeps the port the browser used.
2026-11-03 01:14:52 +03:00

119 lines
4.4 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)
}
}
// Caddy and HAProxy set X-Forwarded-For but pass the client's own X-Real-IP
// through, so a client could pick its login-limiter bucket per request.
func TestGetRemoteIpDistrustsRealIPThatContradictsForwardedFor(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", "203.0.113.5")
c.Request.Header.Set("X-Real-IP", "198.51.100.1")
if got := getRemoteIp(c); got != "127.0.0.1" {
t.Fatalf("remote IP = %q, want the proxy address when the forwarded headers disagree", got)
}
}
// An appending proxy keeps whatever X-Forwarded-For the client sent in front of
// the address it saw, so only the hop next to the trusted proxies is real.
func TestGetRemoteIpReadsForwardedForFromTheProxyEnd(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.1, 203.0.113.5")
if got := getRemoteIp(c); got != "203.0.113.5" {
t.Fatalf("remote IP = %q, want the hop the trusted proxy appended", 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)
}
}
// The clean IP command is fetched from this URL: forwarded headers only count from a
// trusted proxy, and its Host must be the one the browser used (with its port).
func TestPanelBaseURLTrustsForwardedHeadersOnlyFromAProxy(t *testing.T) {
gin.SetMode(gin.TestMode)
for _, tc := range []struct {
name, remote, proto, fwdHost, want string
}{
{"untrusted caller", "203.0.113.10:5555", "https", "evil.example.net", "http://panel.example.com:2053/x/"},
{"trusted proxy", "127.0.0.1:5555", "https", "panel.example.net", "https://panel.example.net/x/"},
{"trusted proxy, no host", "127.0.0.1:5555", "https", "", "https://panel.example.com:2053/x/"},
} {
t.Run(tc.name, func(t *testing.T) {
c, _ := gin.CreateTestContext(httptest.NewRecorder())
c.Request = httptest.NewRequest(http.MethodGet, "/", nil)
c.Request.Host = "panel.example.com:2053"
c.Request.RemoteAddr = tc.remote
c.Request.Header.Set("X-Forwarded-Proto", tc.proto)
if tc.fwdHost != "" {
c.Request.Header.Set("X-Forwarded-Host", tc.fwdHost)
}
c.Set("base_path", "/x/")
if got := panelBaseURL(c); got != tc.want {
t.Errorf("panelBaseURL = %q, want %q", got, tc.want)
}
})
}
}