Files
Pulse/internal/api/client_ip_test.go
courtmanr@gmail.com 3a75b0d746 Tighten local-client trust and forwarded client addresses
Loopback trust (no-auth access, recovery tokens and sessions,
unauthenticated config transfer, the rate-limit exemption) now needs more
than a loopback peer: no reverse-proxy forwarding header, a Host that is
localhost, *.localhost or a loopback literal, and a browser request that is
same-origin by Fetch Metadata or names exactly this origin. A same-host
proxy passing a public Host, a page whose name resolves to 127.0.0.1, or a
page on another origin no longer counts as a local user.

GetClientIP now takes the right-most X-Forwarded-For hop that is not in
PULSE_TRUSTED_PROXY_CIDRS, reading every header line and accepting entries
with ports, instead of the first entry. A proxy that appends keeps the
client's own entries on the left, so the first entry is client-chosen.
Lockout, rate limits, audit addresses and recovery-token binding use this.

The reverse-proxy guide explains multi-hop setups: list upstream hops as
trusted or have the final proxy replace the header.
2026-11-13 22:35:24 +00:00

141 lines
3.9 KiB
Go

package api
import (
"net/http/httptest"
"testing"
)
// A proxy that appends to X-Forwarded-For keeps whatever the client sent on
// the left, so the client address is the right-most hop that no trusted proxy
// added, never the first entry.
func TestGetClientIPUsesRightMostUntrustedForwardedHop(t *testing.T) {
cases := []struct {
name string
trusted string
remote string
xff []string
realIP string
expected string
}{
{
name: "appending proxy keeps client-supplied entries on the left",
trusted: "127.0.0.1/32",
remote: "127.0.0.1:50000",
xff: []string{"198.51.100.66, 203.0.113.9"},
expected: "203.0.113.9",
},
{
name: "single replaced entry",
trusted: "127.0.0.1/32",
remote: "127.0.0.1:50000",
xff: []string{"203.0.113.9"},
expected: "203.0.113.9",
},
{
name: "trusted inner hops are skipped",
trusted: "127.0.0.1/32,10.0.0.0/8",
remote: "127.0.0.1:50000",
xff: []string{"198.51.100.66, 203.0.113.9, 10.0.0.5"},
expected: "203.0.113.9",
},
{
name: "untrusted inner hop is the client",
trusted: "127.0.0.1/32",
remote: "127.0.0.1:50000",
xff: []string{"203.0.113.9, 10.0.0.5"},
expected: "10.0.0.5",
},
{
name: "all hops trusted returns the left-most",
trusted: "127.0.0.1/32,10.0.0.0/8",
remote: "127.0.0.1:50000",
xff: []string{"10.0.0.7, 10.0.0.5"},
expected: "10.0.0.7",
},
{
name: "proxy that adds a separate header line",
trusted: "127.0.0.1/32",
remote: "127.0.0.1:50000",
xff: []string{"198.51.100.66", "203.0.113.9"},
expected: "203.0.113.9",
},
{
name: "entry with a port",
trusted: "127.0.0.1/32",
remote: "127.0.0.1:50000",
xff: []string{"198.51.100.66, 203.0.113.9:5555"},
expected: "203.0.113.9",
},
{
name: "bracketed IPv6 entry with a port",
trusted: "127.0.0.1/32",
remote: "127.0.0.1:50000",
xff: []string{"198.51.100.66, [2001:db8::9]:443"},
expected: "2001:db8::9",
},
{
name: "unreadable hop stops the walk at the proxy",
trusted: "127.0.0.1/32",
remote: "127.0.0.1:50000",
xff: []string{"198.51.100.66, unknown"},
realIP: "198.51.100.67",
expected: "127.0.0.1",
},
{
name: "empty forwarded-for falls back to X-Real-IP",
trusted: "127.0.0.1/32",
remote: "127.0.0.1:50000",
xff: []string{" , "},
realIP: "203.0.113.9",
expected: "203.0.113.9",
},
{
name: "untrusted peer ignores forwarded-for",
trusted: "127.0.0.1/32",
remote: "198.51.100.10:50000",
xff: []string{"203.0.113.9"},
expected: "198.51.100.10",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Setenv("PULSE_TRUSTED_PROXY_CIDRS", tc.trusted)
resetTrustedProxyConfig()
t.Cleanup(resetTrustedProxyConfig)
req := httptest.NewRequest("GET", "/", nil)
req.RemoteAddr = tc.remote
for _, value := range tc.xff {
req.Header.Add("X-Forwarded-For", value)
}
if tc.realIP != "" {
req.Header.Set("X-Real-IP", tc.realIP)
}
if got := GetClientIP(req); got != tc.expected {
t.Fatalf("GetClientIP() = %q, want %q", got, tc.expected)
}
})
}
}
// Login lockout keys on the client address, so a client behind an appending
// proxy must not escape it by changing the forwarded-for value it sends.
func TestLoginLockoutIgnoresClientSuppliedForwardedEntries(t *testing.T) {
t.Setenv("PULSE_TRUSTED_PROXY_CIDRS", "127.0.0.1/32")
resetTrustedProxyConfig()
t.Cleanup(resetTrustedProxyConfig)
seen := map[string]bool{}
for _, spoofed := range []string{"198.51.100.1", "198.51.100.2", "198.51.100.3"} {
req := httptest.NewRequest("POST", "/api/login", nil)
req.RemoteAddr = "127.0.0.1:50000"
req.Header.Set("X-Forwarded-For", spoofed+", 203.0.113.9")
seen[GetClientIP(req)] = true
}
if len(seen) != 1 || !seen["203.0.113.9"] {
t.Fatalf("client addresses = %v, want only 203.0.113.9", seen)
}
}