Harden client IP resolution in RateLimitMiddleware - #19567
Merged
Merged
Conversation
Follow-up to the ipHeader default change: - Extract the left-most entry from a forwarded chain again. Using the raw header line keyed the limit on the whole "client, proxy1, ..." string, so an attacker could vary downstream hops to bypass the limit. - Handle the 'remote_addr' default explicitly instead of relying on it not matching any header name and falling through. - Document that forwarded headers are spoofable and only safe when a trusted proxy overwrites them. - Assert the resolved identifier in the trust-proxy test and switch the status assertions to assertSame(200, ...).
markstory
reviewed
Jul 27, 2026
Member
|
Thanks for catching the mistake I had with remote_addr ❤️ |
markstory
added a commit
that referenced
this pull request
Jul 28, 2026
The previous defaults were not safe, and I don't think we should have released this like this. I've also cleaned up some of the implementation as ServerRequestInterface has methods for this. Thank you to `@yousukezan` for reporting this issue through our security mailing list. * Harden client IP resolution in RateLimitMiddleware (#19567) - Extract the left-most entry from a forwarded chain again. Using the raw header line keyed the limit on the whole "client, proxy1, ..." string, so an attacker could vary downstream hops to bypass the limit. - Handle the 'remote_addr' default explicitly instead of relying on it not matching any header name and falling through. - Document that forwarded headers are spoofable and only safe when a trusted proxy overwrites them. - Assert the resolved identifier in the trust-proxy test and switch the status assertions to assertSame(200, ...). --------- Co-authored-by: Kevin Pfeifer <[email protected]> Co-authored-by: othercorey <[email protected]> Co-authored-by: Mark Scherer <[email protected]>
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.
Follow-up to #19565, targeting that branch.
The default change from
x-forwarded-fortoremote_addris the right fix. This adds three hardening/correctness follow-ups on top of it.1. Restore left-most IP extraction (behavior regression)
#19565 replaced the
explode(',', ...)+trim($ips[0])extraction with the rawgetHeaderLine()value. For anyone opting back intoipHeader => 'x-forwarded-for', the rate-limit identifier became the entire forwarded chain (192.168.1.101, 10.0.0.1) instead of the client IP.That is a bypass: an attacker who controls XFF can vary the downstream hops (
client, junk1,client, junk2, ...) and get a fresh key each request. This restores taking the left-most (originating client) entry.2. Explicit
remote_addrhandlingThe new default only worked because
getHeaderLine('remote_addr')returns''and falls through to$params['REMOTE_ADDR']. That is implicit - any nonexistent header name behaves the same. Handle theremote_addrsentinel explicitly so intent is clear.3. Docs + tests
testProxyHeadersWithTrustProxynow asserts the resolved identifier: it primes on client192.168.1.101via the chain, then re-requests with the same client but a different proxy hop and asserts the limit is hit. The previous version only asserted200twice and would have passed even with the chain-keying bug.assertSame(200, ...)(expected-first, type-strict).Gates:
RateLimitMiddlewareTest10/10 green, phpstan clean, phpcs clean.