fix: preserve original URL string in Location header to prevent percent-encoding corruption (#5319) - #13357
Open
waterWang wants to merge 2 commits into
Open
fix: preserve original URL string in Location header to prevent percent-encoding corruption (#5319)#13357waterWang wants to merge 2 commits into
waterWang wants to merge 2 commits into
Conversation
…nt-encoding corruption (aio-libs#5319)
for more information, see https://pre-commit.ci
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #13357 +/- ##
=======================================
Coverage 98.99% 98.99%
=======================================
Files 132 132
Lines 49156 49156
Branches 2560 2560
=======================================
Hits 48661 48661
Misses 371 371
Partials 124 124
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
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.
Summary
Fixes #5319
When a server-side redirect (via
raise HTTPFound(...)or any otherHTTPMovesubclass) is sent with a location that contains percent-encoded characters (e.g.%3Ffor?in a query string argument that is itself a URL), aiohttp re-encoded the URL throughyarl.URL, decoding%3Fto?. Some downstream web servers reject the resulting URL because it then contains a second literal?.Root cause
In
HTTPMove.__init__, theLocationheader was set fromstr(self.location), whereself.locationis theyarl.URLparsed form.yarlnormalizes the URL, decoding percent-encoded query characters, so the header no longer matches the exact URL the user supplied.Fix
Locationheader from the original location string (str(location)) instead of the parsed-and-re-encodedURL, so percent-encoding is preserved exactly.URL(location)for validation and for the.locationproperty (which still returns the parsedURL).Testing
Added manual verification covering: basic string location, percent-encoding preservation, complex nested-URL query strings, CRLF stripping, empty/None location (
ValueError), the.locationproperty, and pickle round-trip for allHTTPMovesubclasses.