Skip to content

Overwrite spoofed X-Forwarded-* headers - #59

Merged
danmoz merged 2 commits into
masterfrom
opencode/issue41-20260905061337
Sep 5, 2026
Merged

danmoz merged 2 commits into
masterfrom
opencode/issue41-20260905061337

Conversation

@opencode-agent

@opencode-agent opencode-agent Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Fixed. The x-forwarded-proto, x-forwarded-for, and x-forwarded-port headers are now always overwritten with trusted values derived from the request, preventing client spoofing. Added a regression test.

Closes #41

New%20session%20-%202026-09-05T06%3A13%3A36.786Z
opencode session  |  github run

Co-authored-by: danmoz <danmoz@users.noreply.github.com>
Comment thread req_resp.go
}
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.

Co-authored-by: danmoz <danmoz@users.noreply.github.com>
@opencode-agent

opencode-agent Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor Author

Fixed. The review comment is correct: requestPort previously derived the port from r.Host (client-controlled), making x-forwarded-port spoofable.

Changes:

  • req_resp.go:272 — requestPort now reads the port from the trusted local listening address (http.LocalAddrContextKey), matching how Caddy's own {http.request.local.port} replacer works, with a scheme-based fallback.
  • req_resp_test.go — strengthened the regression test to set a spoofed Host: example.test:8443 and a trusted local addr, asserting the port is derived from the latter.

All tests, gofmt, and go vet pass.

New%20session%20-%202026-09-05T06%3A42%3A15.281Z
opencode session  |  github run

@danmoz
danmoz merged commit 9d660b2 into master Sep 5, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Client-supplied X-Forwarded-For / -Proto / -Port pass through verbatim

1 participant