Trim spaces from elements of split XFF string (#459)

* Trim spaces from elements of split XFF string

* Adds test to verify that spaces in original XFF header are handled
This commit is contained in:
David Chandek-Stark 2025-05-07 23:35:42 -04:00 committed by GitHub
parent 1c6c07939a
commit 7f0f691ba5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 0 deletions

View File

@ -134,6 +134,9 @@ func computeXFFHeader(remoteAddr string, origXFFHeader string, pref XFFComputePr
origForwardedList := make([]string, 0, 4)
if origXFFHeader != "" {
origForwardedList = strings.Split(origXFFHeader, ",")
for i := range origForwardedList {
origForwardedList[i] = strings.TrimSpace(origForwardedList[i])
}
}
origForwardedList = append(origForwardedList, parsedRemoteIP.String())
forwardedList := make([]string, 0, len(origForwardedList))

View File

@ -130,6 +130,19 @@ func TestComputeXFFHeader(t *testing.T) {
},
result: "1.1.1.1",
},
{
name: "TrimSpaces",
remoteAddr: "127.0.0.1:80",
origXFFHeader: "1.1.1.1, 10.0.0.1, fe80::, 100.64.0.1, 169.254.0.1",
pref: XFFComputePreferences{
StripPrivate: true,
StripLoopback: true,
StripCGNAT: true,
StripLLU: true,
Flatten: true,
},
result: "1.1.1.1",
},
{
name: "invalid-ip-port",
remoteAddr: "fe80::",