Skip to content

docs: warn about underscore header spoofing in NewRequestWithContext#2460

Merged
dunglas merged 1 commit into
mainfrom
docs/underscore-header-spoofing
Jun 3, 2026
Merged

docs: warn about underscore header spoofing in NewRequestWithContext#2460
dunglas merged 1 commit into
mainfrom
docs/underscore-header-spoofing

Conversation

@dunglas
Copy link
Copy Markdown
Member

@dunglas dunglas commented Jun 3, 2026

What

Document that NewRequestWithContext does not strip request headers whose name contains an underscore.

Why

CGI maps dashes to underscores (Foo-Bar becomes HTTP_FOO_BAR), so a client-supplied Foo_Bar header is indistinguishable from a legitimate Foo-Bar in $_SERVER and can spoof it. This affects any such header an application or upstream proxy trusts (forwarded-for, auth, etc.).

The Caddy-based server and reverse proxies like nginx (underscores_in_headers off) already drop these. Callers using the Go API directly must do it themselves unless they explicitly whitelist them.

Changes

  • Doc comment on NewRequestWithContext explaining the risk and mitigation.
  • ExampleServeHTTP now drops underscore headers before building the request.

CGI maps dashes to underscores, so a client-supplied Foo_Bar header is
indistinguishable from Foo-Bar in $_SERVER and can spoof it. Document
that callers must drop underscore headers (the Caddy server and proxies
like nginx already do) and show it in the ServeHTTP example.
Copilot AI review requested due to automatic review settings June 3, 2026 16:44
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR documents a header-spoofing risk when using the Go API NewRequestWithContext: HTTP header names containing underscores are not stripped, and due to CGI’s dash-to-underscore mapping, an attacker can spoof trusted headers once they are projected into $_SERVER. It also updates the ExampleServeHTTP snippet to demonstrate a safe mitigation (dropping underscore-containing headers) before constructing the FrankenPHP request.

Changes:

  • Add a doc comment to NewRequestWithContext warning about underscore header spoofing via CGI header mapping and recommending mitigation.
  • Update ExampleServeHTTP to remove request headers containing underscores prior to calling NewRequestWithContext.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
frankenphp_test.go Updates the public example to drop underscore-containing headers before creating the FrankenPHP request.
context.go Adds documentation warning callers about underscore header spoofing risk and recommended mitigation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@dunglas dunglas merged commit 30ec3e6 into main Jun 3, 2026
32 checks passed
@dunglas dunglas deleted the docs/underscore-header-spoofing branch June 3, 2026 20:49
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.

2 participants