Skip to content

Commit a3eab6f

Browse files
authored
fix(proxy_handler): Handle spaces in x-forwarded-for header (#327)
Having spaces or no spaces results in equivalent headers in RFC 7239: As an example, the header field Forwarded: for=192.0.2.43,for="[2001:db8:cafe::17]",for=unknown is equivalent to the header field Forwarded: for=192.0.2.43, for="[2001:db8:cafe::17]", for=unknown This PR fixes #326 because we treated spaces as mandatory in the Forwarded Header. Signed-off-by: Lennard Eijsackers <[email protected]>
1 parent 0c6683c commit a3eab6f

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

proxy_handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func extractFirstMatchFromIPList(ipList string) string {
7474
if ipList == "" {
7575
return ""
7676
}
77-
s := strings.Index(ipList, ", ")
77+
s := strings.Index(ipList, ",")
7878
if s == -1 {
7979
s = len(ipList)
8080
}
@@ -91,7 +91,7 @@ func parseForwardedHeader(fwd string) string {
9191
for _, split := range splits {
9292
trimmed := strings.TrimSpace(split)
9393
if strings.HasPrefix(strings.ToLower(trimmed), "for=") {
94-
forSplits := strings.Split(trimmed, ", ")
94+
forSplits := strings.Split(trimmed, ",")
9595
if len(forSplits) == 0 {
9696
return ""
9797
}

proxy_handler_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ func TestProxyHandler(t *testing.T) {
7474
},
7575
expectedAddr: "10.0.0.1",
7676
},
77+
{
78+
name: "proxy should forward proxy header Forwarded if set and treat a lack of spaces as an equivalent (issue #326).",
79+
proxy: &config.Proxy{
80+
Enable: true,
81+
},
82+
r: &http.Request{
83+
RemoteAddr: "127.0.0.1:1234",
84+
Header: http.Header{
85+
"Forwarded": []string{"for=10.0.0.1,for=10.0.0.3"},
86+
},
87+
},
88+
expectedAddr: "10.0.0.1",
89+
},
7790
{
7891
name: "proxy should properly parse Forwarded header",
7992
proxy: &config.Proxy{

0 commit comments

Comments
 (0)