Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,13 @@ func parserFor(canon string) func(string) string {
func parseForwarded(v string) string {
if m := forwardedRegex.FindStringSubmatch(v); len(m) > 1 {
// An IPv6 address (and any node-port) MUST be quoted, so trim the quotes.
return strings.Trim(m[1], `"`)
node := strings.Trim(m[1], `"`)
// A bracketed IPv6 address without a port is not a valid IP, so strip the brackets.
if strings.HasPrefix(node, "[") && strings.HasSuffix(node, "]") {
return node[1 : len(node)-1]
}

return node
}

return ""
Expand Down
1 change: 1 addition & 0 deletions tests/proxy_ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func TestForwarded(t *testing.T) {
want string
}{
{"for element is extracted", "by=foo;for=3.11.0.1;host=foo.workstation;proto=http", "3.11.0.1"},
{"bracketed IPv6 without port is unbracketed", `for="[2001:db8:cafe::17]";proto=http`, "2001:db8:cafe::17"},
{"trusted loopback stays loopback", "by=foo;for=127.0.0.1;host=foo.workstation;proto=http", loopback},
{"value without for= falls back to the peer", "foo.workstation", loopback},
}
Expand Down
1 change: 1 addition & 0 deletions trusted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestIP(t *testing.T) {
{tcip, "8.8.8.8", "8.8.8.8"}, // Single address
{forwarded, `for="_foo"`, "_foo"}, // Hostname
{forwarded, `For="[2001:db8:cafe::17]:4711`, `[2001:db8:cafe::17]:4711`}, // IPv6 address
{forwarded, `for="[2001:db8:cafe::17]"`, `2001:db8:cafe::17`}, // IPv6 address without port
{forwarded, `for=192.0.2.60;proto=http;by=203.0.113.43`, `192.0.2.60`}, // Multiple params
{forwarded, `for=192.0.2.43, for=198.51.100.17`, "192.0.2.43"}, // Multiple params
{forwarded, `for="workstation.local",for=198.51.100.17`, "workstation.local"}, // Hostname
Expand Down