Strip and re-author forwarding headers in Falcon::Middleware::Proxy#351
Open
benni-rogge wants to merge 1 commit into
Open
Strip and re-author forwarding headers in Falcon::Middleware::Proxy#351benni-rogge wants to merge 1 commit into
Falcon::Middleware::Proxy#351benni-rogge wants to merge 1 commit into
Conversation
As the trust boundary, Falcon must not trust client-supplied forwarding headers. `prepare_request` now discards every inbound `Forwarded` / `X-Forwarded-*` header and authors its own from connection-level facts, preventing a client from spoofing the forwarded address or scheme (CWE-290 / CWE-348). We emit both the modern RFC 7239 `Forwarded` header (with correctly bracketed and quoted IPv6) and the legacy `X-Forwarded-For` / `X-Forwarded-Proto` headers. The legacy headers are retained because they are still consumed downstream -- notably Rack's `Rack::Request#forwarded_for` (used by Rails' `ActionDispatch::RemoteIp`) falls back to `X-Forwarded-For`, and older Rack (< 3) and direct readers depend on it. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Signed-off-by: Benni Rogge <benni.rogge@shopify.com>
b77bae7 to
6d54a53
Compare
d98bae2 to
6d54a53
Compare
Author
|
I investigated the remaining
|
Author
|
Opened #352 as a separate baseline CI fix so this PR can stay focused on forwarding headers: #352 Validation evidence:
The upstream #352 PR workflows are still waiting for maintainer approval because it is a fork PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
As the trust boundary, Falcon must not trust client-supplied forwarding headers. Previously
Falcon::Middleware::Proxy#prepare_requestappended to whateverForwarded/X-Forwarded-*headers arrived on the inbound request, so a client could pre-seed those headers and have the spoofed values reach the upstream application (CWE-290 / CWE-348 — authentication/decision based on a spoofable, untrusted value).This change makes
prepare_request:forwarded,x-forwarded-for,x-forwarded-proto,x-forwarded-host,x-forwarded-port) so none can be spoofed.Forwardedheader and the legacyX-Forwarded-For/X-Forwarded-Protoheaders.Why keep the legacy
X-Forwarded-*headers?X-Forwarded-Foris still consumed widely downstream, so dropping it would break real consumers:Rack::Request#forwarded_for(used by Rails'ActionDispatch::RemoteIp) defaults toforwarded_priority = [:forwarded, :x_forwarded]— it prefers RFC 7239Forwardedbut falls back toX-Forwarded-For.Forwardedsupport) and a lot of application code readX-Forwarded-Fordirectly.So we emit both header families. This is documented inline on the
FORWARDING_HEADERSconstant.RFC 7239 correctness
Forwardednode identifiers for IPv6 are now correctly bracketed and quoted (for="[::1]"), via the newforwarded_nodehelper. The legacyX-Forwarded-Forcontinues to carry the bare address (::1), per its conventions.Tests
test/falcon/middleware/proxy.rb— 7 passing examples covering:forwarded,x-forwarded-for,x-forwarded-proto,x-forwarded-host,x-forwarded-port) are stripped and replaced with Falcon's own;Forwarded, bare inX-Forwarded-For);remote_address-unavailable branch emits neitherfor=norX-Forwarded-For, but still authorsprotoandvia.CI note
The remaining
Test Coverage / validatefailure is inherited frommain, not introduced by this PR:main(e7007f6):45 files checked; 755/882 lines executed; 85.6% covered.6d54a53):45 files checked; 761/888 lines executed; 85.7% covered.A separate baseline CI fix is open as #352. Its exact commit passes the fork workflow, and a temporary fork branch combining #352 + this PR also passes
Test Coverage / validatewith this PR's85.7%coverage.🤖 Generated with Claude Code