Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2d7a489f58 |
@@ -32,9 +32,6 @@ const (
|
||||
forwardedIP = "203.0.113.7"
|
||||
// realIP is the client address presented via X-Real-IP.
|
||||
realIP = "203.0.113.9"
|
||||
// appendedIP is the X-Forwarded-For entry a trusted proxy appended
|
||||
// after the entries the client wrote.
|
||||
appendedIP = "198.51.100.7"
|
||||
)
|
||||
|
||||
func mustPrefixes(t *testing.T, cidrs ...string) []netip.Prefix {
|
||||
@@ -93,8 +90,8 @@ func clientIPCases() []clientIPCase {
|
||||
{
|
||||
name: "trusted proxy uses right-most of chain",
|
||||
remoteAddr: loopbackPeer,
|
||||
xff: "203.0.113.9, " + appendedIP,
|
||||
want: appendedIP,
|
||||
xff: "203.0.113.9, 198.51.100.7",
|
||||
want: "198.51.100.7",
|
||||
},
|
||||
{
|
||||
name: "trusted proxy entries are skipped from the right",
|
||||
@@ -108,6 +105,24 @@ func clientIPCases() []clientIPCase {
|
||||
xff: "10.0.0.5, 10.1.2.3",
|
||||
want: loopbackIP,
|
||||
},
|
||||
{
|
||||
name: "entry with a port is read as its address",
|
||||
remoteAddr: loopbackPeer,
|
||||
xff: "203.0.113.9, 198.51.100.7:5000",
|
||||
want: "198.51.100.7",
|
||||
},
|
||||
{
|
||||
name: "ipv6 entry with a port is read as its address",
|
||||
remoteAddr: loopbackPeer,
|
||||
xff: "203.0.113.9, [2001:db8::1]:443",
|
||||
want: "2001:db8::1",
|
||||
},
|
||||
{
|
||||
name: "trusted proxy entry with a port is skipped",
|
||||
remoteAddr: loopbackPeer,
|
||||
xff: "203.0.113.9, 10.0.0.5:5000",
|
||||
want: "203.0.113.9",
|
||||
},
|
||||
{
|
||||
name: "entry that is not an address uses peer",
|
||||
remoteAddr: loopbackPeer,
|
||||
@@ -187,36 +202,12 @@ func TestClientIPReadsEveryForwardedForLine(t *testing.T) {
|
||||
|
||||
header := http.Header{}
|
||||
header.Add("X-Forwarded-For", "203.0.113.9")
|
||||
header.Add("X-Forwarded-For", appendedIP)
|
||||
header.Add("X-Forwarded-For", "198.51.100.7")
|
||||
|
||||
got := middleware.ClientIP(loopbackPeer, header,
|
||||
mustPrefixes(t, "127.0.0.1/32"))
|
||||
if got != appendedIP {
|
||||
t.Errorf("ClientIP() = %q, want %q", got, appendedIP)
|
||||
}
|
||||
}
|
||||
|
||||
// TestClientIPReadsAddressWithPort checks that an X-Forwarded-For entry
|
||||
// written as an address with a port is read as that address, both for
|
||||
// the trust check and as the result.
|
||||
func TestClientIPReadsAddressWithPort(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
trusted := mustPrefixes(t, "127.0.0.1/32", "10.0.0.0/8")
|
||||
|
||||
for xff, want := range map[string]string{
|
||||
"203.0.113.9, " + appendedIP + ":5000": appendedIP,
|
||||
"203.0.113.9, [2001:db8::1]:443": "2001:db8::1",
|
||||
"203.0.113.9, 10.0.0.5:5000": "203.0.113.9",
|
||||
} {
|
||||
header := http.Header{}
|
||||
header.Set("X-Forwarded-For", xff)
|
||||
|
||||
got := middleware.ClientIP(loopbackPeer, header, trusted)
|
||||
if got != want {
|
||||
t.Errorf("X-Forwarded-For %q: ClientIP() = %q, want %q",
|
||||
xff, got, want)
|
||||
}
|
||||
if got != "198.51.100.7" {
|
||||
t.Errorf("ClientIP() = %q, want %q", got, "198.51.100.7")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user