Do not raise exception for invalid redirect URLs #3706
+26
−10
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.
Related discussion: #3179
May also be related to several discussions tied to InvalidURL exceptions
Description:
Currently,
httpxraises anhttpx.InvalidURLexception when a response contains aLocationheader with a URL that is not considered valid, such as one with a non-HTTP scheme likedata:.This behavior is not ideal, as an HTTP response with a malformed
Locationheader is still a valid HTTP response. The client should not crash in this case, but rather allow the user to inspect the response, including the problematicLocationheader.How to Reproduce the Issue
The following code demonstrates the original issue. The target URL returns a 302 redirect where the
Locationheader contains adata:URI.The Fix
This MR modifies the redirect handling logic. Now, if a redirect URL is invalid,
httpxwill stop following redirects and return the original redirect response. Theresponse.next_requestproperty will be set toNoneto indicate that no valid redirect request could be constructed.The tests for invalid redirects have been updated to reflect this new, more robust behavior.
Use Case: Web Vulnerability Scanning
This change is motivated by the use of
httpxwithin the Wapiti web vulnerability scanner.A core function of such scanners is to send a wide variety of payloads, some of which are intentionally malformed, to probe for security flaws. This can cause servers to respond in unexpected or non-standard ways. For instance, a server might reply with a redirect containing an invalid
Locationheader, as demonstrated in this MR.In this scenario, the HTTP client must be robust and fault-tolerant. Instead of crashing with an
InvalidURLexception, it should gracefully handle the response and allow the calling application (the scanner) to inspect it. This change ensureshttpxbehaves more predictably and resiliently, making it a more reliable choice for security tooling and other applications that interact with potentially unpredictable endpoints.