Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 3 additions & 9 deletions req_resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,9 @@ func apiGatewayV2Headers(r *http.Request) map[string]string {
if _, ok := headers["host"]; !ok {
headers["host"] = r.Host
}
if _, ok := headers["x-forwarded-proto"]; !ok {
headers["x-forwarded-proto"] = requestScheme(r)
}
if _, ok := headers["x-forwarded-for"]; !ok {
headers["x-forwarded-for"] = requestSourceIP(r)
}
if _, ok := headers["x-forwarded-port"]; !ok {
headers["x-forwarded-port"] = requestPort(r)
}
headers["x-forwarded-proto"] = requestScheme(r)
headers["x-forwarded-for"] = requestSourceIP(r)
headers["x-forwarded-port"] = requestPort(r)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

requestPort is derived from the Host header which is set by the client. So this is spoofable.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/oc see the above comment.

return headers
}

Expand Down
20 changes: 20 additions & 0 deletions req_resp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ func TestNewRequestForFormatAPIGatewayV2(t *testing.T) {
}
}

func TestAPIGatewayV2HeadersOverwriteSpoofedForwardedHeaders(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "http://example.test/", nil)
r.RemoteAddr = "192.0.2.1:1234"
r.Header.Set("X-Forwarded-For", "203.0.113.42")
r.Header.Set("X-Forwarded-Proto", "https")
r.Header.Set("X-Forwarded-Port", "8443")

headers := apiGatewayV2Headers(r)

if got, want := headers["x-forwarded-for"], "192.0.2.1"; got != want {
t.Errorf("x-forwarded-for = %q, want %q", got, want)
}
if got, want := headers["x-forwarded-proto"], "http"; got != want {
t.Errorf("x-forwarded-proto = %q, want %q", got, want)
}
if got, want := headers["x-forwarded-port"], "80"; got != want {
t.Errorf("x-forwarded-port = %q, want %q", got, want)
}
}

func TestRequestSourceIPUsesCaddyClientIP(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/", nil)
r.RemoteAddr = "192.0.2.1:1234"
Expand Down
Loading