middleware: test ClientIPFromHeader does not fall back to other headers (#1208)

This commit is contained in:
Renan Martins Pimentel 2026-10-13 18:37:10 -03:00 committed by GitHub
parent fff8bb28cb
commit c9744a3641
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -527,6 +527,18 @@ func TestAdvisory_GHSA_rjr7_jggh_pgcp(t *testing.T) {
t.Errorf("want 203.0.113.7 (the only trusted header), got %q", got)
}
})
// When the opted-in header is absent, ClientIPFromHeader must not fall
// back to other attacker-supplied headers; it leaves no client IP set.
t.Run("no_fallback_when_opted_in_header_missing", func(t *testing.T) {
got := run(t, ClientIPFromHeader("X-Real-IP"), func(r *http.Request) {
r.Header.Set("True-Client-IP", "1.1.1.1") // attacker-supplied; ignored.
r.Header.Set("X-Forwarded-For", "2.2.2.2") // attacker-supplied; ignored.
})
if got != "" {
t.Errorf("VULNERABLE: fell back to attacker-supplied header, got %q", got)
}
})
}
// GHSA-j6qf-3m9c-vwj3 (tenbbughunters): ClientIPFromXFF never checked that