- Add Go, npm, Python, and Java validation to CI - Run Go tests against PostgreSQL - Record performance fixes in the report
92 lines
3.1 KiB
Go
92 lines
3.1 KiB
Go
package auth
|
|
|
|
import (
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func mustTrustedProxies(t *testing.T, value string) []*net.IPNet {
|
|
t.Helper()
|
|
networks, err := ParseTrustedProxies(value)
|
|
if err != nil {
|
|
t.Fatalf("ParseTrustedProxies(%q) error = %v", value, err)
|
|
}
|
|
return networks
|
|
}
|
|
|
|
func TestParseTrustedProxiesAcceptsCIDRsAndBareAddresses(t *testing.T) {
|
|
networks := mustTrustedProxies(t, " 10.0.0.0/8, 192.168.1.5 , ::1 ,")
|
|
if len(networks) != 3 {
|
|
t.Fatalf("networks = %d, want 3", len(networks))
|
|
}
|
|
for _, ip := range []string{"10.20.30.40", "192.168.1.5", "::1"} {
|
|
if !(&Service{trustedProxies: networks}).isTrustedProxy(ip) {
|
|
t.Errorf("%s not trusted", ip)
|
|
}
|
|
}
|
|
if (&Service{trustedProxies: networks}).isTrustedProxy("192.168.1.6") {
|
|
t.Error("192.168.1.6 trusted, but only 192.168.1.5 was listed")
|
|
}
|
|
}
|
|
|
|
func TestParseTrustedProxiesRejectsInvalidEntries(t *testing.T) {
|
|
for _, value := range []string{"10.0.0.0/33", "proxy.internal", "10.0.0.1/8/8"} {
|
|
if _, err := ParseTrustedProxies(value); err == nil {
|
|
t.Errorf("ParseTrustedProxies(%q) accepted an invalid entry", value)
|
|
}
|
|
}
|
|
networks, err := ParseTrustedProxies(" ")
|
|
if err != nil || len(networks) != 0 {
|
|
t.Fatalf("empty setting = %v, %v; want no proxies and no error", networks, err)
|
|
}
|
|
}
|
|
|
|
func clientAddressFor(s *Service, remote string, forwarded ...string) string {
|
|
r := httptest.NewRequest(http.MethodPost, "/auth/login", nil)
|
|
r.RemoteAddr = remote
|
|
for _, value := range forwarded {
|
|
r.Header.Add("X-Forwarded-For", value)
|
|
}
|
|
return s.clientAddress(r)
|
|
}
|
|
|
|
func TestClientAddressIgnoresForwardedForWithoutTrustedProxies(t *testing.T) {
|
|
s := &Service{}
|
|
if got := clientAddressFor(s, "203.0.113.7:4444", "198.51.100.1"); got != "203.0.113.7" {
|
|
t.Fatalf("clientAddress = %q, want the TCP peer 203.0.113.7", got)
|
|
}
|
|
}
|
|
|
|
func TestClientAddressIgnoresForwardedForFromAnUnknownPeer(t *testing.T) {
|
|
s := &Service{trustedProxies: mustTrustedProxies(t, "10.0.0.0/8")}
|
|
if got := clientAddressFor(s, "203.0.113.7:4444", "198.51.100.1"); got != "203.0.113.7" {
|
|
t.Fatalf("clientAddress = %q, want the TCP peer 203.0.113.7", got)
|
|
}
|
|
}
|
|
|
|
func TestClientAddressHonoursForwardedForFromATrustedPeer(t *testing.T) {
|
|
s := &Service{trustedProxies: mustTrustedProxies(t, "10.0.0.0/8, 172.16.0.0/12")}
|
|
tests := []struct {
|
|
name string
|
|
forwarded []string
|
|
want string
|
|
}{
|
|
{"single client", []string{"198.51.100.9"}, "198.51.100.9"},
|
|
{"client behind a second trusted hop", []string{"198.51.100.9, 172.16.4.4"}, "198.51.100.9"},
|
|
{"spoofed left-most entry is skipped", []string{"6.6.6.6, 198.51.100.9, 172.16.4.4"}, "198.51.100.9"},
|
|
{"entries across header lines", []string{"6.6.6.6", "198.51.100.9"}, "198.51.100.9"},
|
|
{"malformed entry falls back to the peer", []string{"198.51.100.9, not-an-ip"}, "10.0.0.2"},
|
|
{"no header falls back to the peer", nil, "10.0.0.2"},
|
|
{"every hop trusted uses the left-most", []string{"10.1.1.1, 172.16.4.4"}, "10.1.1.1"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := clientAddressFor(s, "10.0.0.2:1000", tt.forwarded...); got != tt.want {
|
|
t.Fatalf("clientAddress = %q, want %q", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|