|
| 1 | +package middleware |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestRealIP_NoTrustedProxies_IsNoOp(t *testing.T) { |
| 10 | + captured := "" |
| 11 | + h := RealIP(nil)(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { |
| 12 | + captured = r.RemoteAddr |
| 13 | + })) |
| 14 | + |
| 15 | + r := httptest.NewRequest(http.MethodGet, "/", nil) |
| 16 | + r.RemoteAddr = "1.2.3.4:5555" |
| 17 | + r.Header.Set("X-Forwarded-For", "9.9.9.9") |
| 18 | + h.ServeHTTP(httptest.NewRecorder(), r) |
| 19 | + |
| 20 | + if captured != "1.2.3.4:5555" { |
| 21 | + t.Fatalf("expected RemoteAddr untouched, got %q", captured) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +func TestRealIP_TrustedPeer_UsesXFF(t *testing.T) { |
| 26 | + trusted, err := ParseTrustedProxies([]string{"10.0.0.0/8"}) |
| 27 | + if err != nil { |
| 28 | + t.Fatal(err) |
| 29 | + } |
| 30 | + |
| 31 | + captured := "" |
| 32 | + h := RealIP(trusted)(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { |
| 33 | + captured = r.RemoteAddr |
| 34 | + })) |
| 35 | + |
| 36 | + r := httptest.NewRequest(http.MethodGet, "/", nil) |
| 37 | + r.RemoteAddr = "10.0.5.6:5555" |
| 38 | + r.Header.Set("X-Forwarded-For", "203.0.113.7") |
| 39 | + h.ServeHTTP(httptest.NewRecorder(), r) |
| 40 | + |
| 41 | + if captured != "203.0.113.7" { |
| 42 | + t.Fatalf("expected client IP from XFF, got %q", captured) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestRealIP_UntrustedPeer_IgnoresXFF(t *testing.T) { |
| 47 | + trusted, err := ParseTrustedProxies([]string{"10.0.0.0/8"}) |
| 48 | + if err != nil { |
| 49 | + t.Fatal(err) |
| 50 | + } |
| 51 | + |
| 52 | + captured := "" |
| 53 | + h := RealIP(trusted)(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { |
| 54 | + captured = r.RemoteAddr |
| 55 | + })) |
| 56 | + |
| 57 | + r := httptest.NewRequest(http.MethodGet, "/", nil) |
| 58 | + r.RemoteAddr = "8.8.8.8:5555" |
| 59 | + r.Header.Set("X-Forwarded-For", "203.0.113.7") |
| 60 | + h.ServeHTTP(httptest.NewRecorder(), r) |
| 61 | + |
| 62 | + if captured != "8.8.8.8:5555" { |
| 63 | + t.Fatalf("expected RemoteAddr untouched for untrusted peer, got %q", captured) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestRealIP_ChainOfTrustedProxies(t *testing.T) { |
| 68 | + trusted, err := ParseTrustedProxies([]string{"10.0.0.0/8", "172.16.0.0/12"}) |
| 69 | + if err != nil { |
| 70 | + t.Fatal(err) |
| 71 | + } |
| 72 | + |
| 73 | + captured := "" |
| 74 | + h := RealIP(trusted)(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { |
| 75 | + captured = r.RemoteAddr |
| 76 | + })) |
| 77 | + |
| 78 | + r := httptest.NewRequest(http.MethodGet, "/", nil) |
| 79 | + r.RemoteAddr = "10.0.5.6:5555" |
| 80 | + // Chain: client → ext LB (untrusted) → internal LB (trusted) → peer (trusted) |
| 81 | + r.Header.Set("X-Forwarded-For", "203.0.113.7, 172.16.0.1, 10.0.5.1") |
| 82 | + h.ServeHTTP(httptest.NewRecorder(), r) |
| 83 | + |
| 84 | + if captured != "203.0.113.7" { |
| 85 | + t.Fatalf("expected first untrusted IP from right, got %q", captured) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestRealIP_AllForwardersTrusted_NoMatch(t *testing.T) { |
| 90 | + trusted, err := ParseTrustedProxies([]string{"10.0.0.0/8"}) |
| 91 | + if err != nil { |
| 92 | + t.Fatal(err) |
| 93 | + } |
| 94 | + |
| 95 | + captured := "" |
| 96 | + h := RealIP(trusted)(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { |
| 97 | + captured = r.RemoteAddr |
| 98 | + })) |
| 99 | + |
| 100 | + r := httptest.NewRequest(http.MethodGet, "/", nil) |
| 101 | + r.RemoteAddr = "10.0.5.6:5555" |
| 102 | + r.Header.Set("X-Forwarded-For", "10.0.5.1, 10.0.5.2") |
| 103 | + h.ServeHTTP(httptest.NewRecorder(), r) |
| 104 | + |
| 105 | + if captured != "10.0.5.6:5555" { |
| 106 | + t.Fatalf("expected RemoteAddr untouched when no untrusted hop, got %q", captured) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestParseTrustedProxies(t *testing.T) { |
| 111 | + cases := []struct { |
| 112 | + in []string |
| 113 | + wantOK bool |
| 114 | + }{ |
| 115 | + {[]string{"10.0.0.0/8"}, true}, |
| 116 | + {[]string{"::1/128"}, true}, |
| 117 | + {[]string{"192.168.1.1"}, true}, // bare IP → /32 |
| 118 | + {[]string{"2001:db8::1"}, true}, // bare IPv6 → /128 |
| 119 | + {[]string{""}, true}, // empty entries skipped |
| 120 | + {[]string{"not-an-ip"}, false}, |
| 121 | + {[]string{"10.0.0.0/99"}, false}, |
| 122 | + } |
| 123 | + |
| 124 | + for _, c := range cases { |
| 125 | + _, err := ParseTrustedProxies(c.in) |
| 126 | + if (err == nil) != c.wantOK { |
| 127 | + t.Errorf("ParseTrustedProxies(%v): err=%v, wantOK=%v", c.in, err, c.wantOK) |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments