security: add verified central client IP resolution with regression tests
This commit is contained in:
1 parent
5b5526d0df
commit
021aa95012
1 file changed
+34
@@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRequestClientIPRejectsSpoofedForwardHeaders(t *testing.T){
|
||||
t.Setenv("LANDWELLE_TRUST_PROXY","false")
|
||||
r:=httptest.NewRequest("GET","/",nil)
|
||||
r.RemoteAddr="198.51.100.8:1234"
|
||||
r.Header.Set("X-Forwarded-For","203.0.113.15")
|
||||
if got:=requestClientIP(r); got!="198.51.100.8" {t.Fatalf("spoofable IP: %q",got)}
|
||||
}
|
||||
func TestRequestClientIPRequiresTrustedPeer(t *testing.T){
|
||||
t.Setenv("LANDWELLE_TRUST_PROXY","true")
|
||||
t.Setenv("LANDWELLE_TRUSTED_PROXY_CIDRS","192.0.2.0/24")
|
||||
r:=httptest.NewRequest("GET","/",nil)
|
||||
r.RemoteAddr="198.51.100.8:1234"
|
||||
r.Header.Set("X-Forwarded-For","203.0.113.15")
|
||||
if got:=requestClientIP(r); got!="198.51.100.8" {t.Fatalf("accepted untrusted peer: %q",got)}
|
||||
r.RemoteAddr="192.0.2.7:5555"
|
||||
if got:=requestClientIP(r); got!="203.0.113.15" {t.Fatalf("ignored trusted peer: %q",got)}
|
||||
}
|
||||
func TestRequestClientIPUsesClosestUntrustedHop(t *testing.T){
|
||||
t.Setenv("LANDWELLE_TRUST_PROXY","true")
|
||||
t.Setenv("LANDWELLE_TRUSTED_PROXY_CIDRS","")
|
||||
r:=httptest.NewRequest("GET","/",nil)
|
||||
r.RemoteAddr="127.0.0.1:5500"
|
||||
r.Header.Set("X-Forwarded-For","192.0.2.25,198.51.100.5")
|
||||
if got:=requestClientIP(r);got!="198.51.100.5"{t.Fatalf("trusted spoofed address: %q",got)}
|
||||
r.Header.Set("X-Forwarded-For","unparseable")
|
||||
if got:=requestClientIP(r);got!="127.0.0.1"{t.Fatalf("invalid chain not ignored: %q",got)}
|
||||
}
|
||||
Reference in new issue
Block a user