Preserve the response when a redirect has an invalid Location and redirects are not followed#1064
Open
devl00p wants to merge 1 commit into
Open
Conversation
…ts are not followed When follow_redirects=False and a 3XX response carries a malformed Location header (e.g. a data: URI), _send_handling_redirects builds the redirect request before checking follow_redirects, so _redirect_url raises RemoteProtocolError and the response, along with its headers, is discarded. A caller that only wants to inspect the redirect without following it never sees the response. Defer building the redirect request to the branch that actually follows redirects. When not following, building next_request is best-effort: a malformed Location leaves next_request=None and the response is returned intact. Adds sync and async regression tests. Refs: wapiti-scanner/wapiti#690 Refs: encode/httpx#3706 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Merging this PR will not alter performance
Comparing Footnotes
|
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.
Description
When
follow_redirects=Falseand a 3XX response carries a malformedLocationheader (e.g. adata:URI),_send_handling_redirectsbuilds the redirect request before checkingfollow_redirects, so_redirect_urlraisesRemoteProtocolErrorand the response — along with its headers — is discarded. A caller that only wants to inspect the redirect without following it never gets the response.This is a real-world need: the Wapiti web scanner reads the
Locationheader of such responses to detect reflected payloads, and currently cannot — see wapiti-scanner/wapiti#690. A malformedLocationis still part of an otherwise valid HTTP response; not following the redirect shouldn't mean losing the response.The same fix was proposed upstream in encode/httpx#3706 (still open, refs discussion encode/httpx#3179) and is already shipped by the
httpxyzfork.The fix
Defer building the redirect request to the branch that actually follows redirects. When not following, building
next_requestis best-effort: a malformedLocationleavesnext_request=Noneand the response is returned intact. Applied to both the sync and async_send_handling_redirects.Following redirects (
follow_redirects=True) still raisesRemoteProtocolErroron an invalidLocation, unchanged.Tests
Adds sync and async regression tests (
test_invalid_redirect_not_followed) reusing the existing/invalid_redirectmock endpoint.scripts/check(ruff format, mypy, ruff check, unasync) and the redirect test suite pass.