-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Fix cookie parsing issues #11112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix cookie parsing issues #11112
Conversation
Ready to go from my perspective. I want to sleep on it though |
Still happy with this after sleeping on it. Just landing in LHR now to make the OHF summit in Dublin. While I've done everything I can think of to write tests for it, I'm thinking it would be best to do a 3.12.7rc0 with this and run it through Home Assistant CI and a few other projects before pushing 3.12.7. |
Backport to 3.12: 💔 cherry-picking failed — conflicts found❌ Failed to cleanly apply 8edec63 on top of patchback/backports/3.12/8edec635b65035ad819cd98abc7bfeb192f788a3/pr-11112 Backporting merged PR #11112 into master
🤖 @patchback |
Backport to 3.13: 💔 cherry-picking failed — conflicts found❌ Failed to cleanly apply 8edec63 on top of patchback/backports/3.13/8edec635b65035ad819cd98abc7bfeb192f788a3/pr-11112 Backporting merged PR #11112 into master
🤖 @patchback |
(cherry picked from commit 8edec63)
(cherry picked from commit 8edec63)
I've done all the production testing I can do with the RC. All looks good |
What do these changes do?
Summary
This PR fixes multiple long-standing cookie parsing issues by implementing a more lenient cookie parser that handles real-world cookies while maintaining compatibility with the HTTP cookie specification.
Issues Fixed
expires
cookie directive #4493Key Changes
1. Use
parse_cookie_headers
for parsing cookie headersReplaced Python's strict
SimpleCookie
with the more lenientparse_cookie_headers
function in multiple places:Server-side (aiohttp/web_request.py):
{}[]()
that are commonly used by real-world websitesClient-side:
parse_cookie_headers
to handle malformed cookies gracefullyresponse.cookies
as aSimpleCookie
objectparse_cookie_headers
for the actual parsing, then updates the SimpleCookie with the resultsupdate_cookies()
method to useparse_cookie_headers
when parsing existing Cookie headersupdate_cookies_from_headers()
in AbstractCookieJarparse_cookie_headers
handles errors internally without raising exceptions2. Updated logger usage
client_logger
tointernal_logger
inparse_cookie_headers
since the function is now used by both client and server code3. Fixed inconsistent cookie quoting behavior in CookieJar
quote_cookie
settingquote_cookie
, but shared cookies were always sent unquoted_build_morsel()
method for both shared and non-shared cookiesself._quote_cookie
setting consistently4. Refactored cookie morsel building in CookieJar
_build_morsel()
method_cookie_helpers
module:make_non_quoted_morsel()
whenquote_cookie=False
preserve_morsel_with_coded_value()
when cookie is already properly quotedmake_quoted_morsel()
when cookie needs quotingBaseCookie
for the filtered result to ensure our quoting is always respected.5. Comprehensive test coverage
Added extensive tests to ensure:
Technical Details
The implementation aims to make aiohttp more robust when handling cookies from various web services and frameworks.
Are there changes in behavior for the user?
More lenient parser.
Is it a substantial burden for the maintainers to support this?
We have to maintain a cookie parser, however its not as bad as I was expecting since our use case is more narrow than everything
SimpleCookie
does