Repository navigation
Scan every request cookie, including ones SimpleCookie rejected #769
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fed00a1
Parse request cookies without SimpleCookie
iacobdaniel ea70543
Read every Cookie header, not only the last one
iacobdaniel f5a7c97
Keep every value of a cookie name that appears more than once
iacobdaniel e991b45
Test cookies without a name=value pair
iacobdaniel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,29 @@ | ||
| """ | ||
| Exports `parse_cookies` function which parses WSGI cookies | ||
| Exports `parse_cookies` function which parses request cookies | ||
| """ | ||
|
|
||
| from http.cookies import SimpleCookie, CookieError | ||
| from http.cookies import _unquote | ||
|
|
||
|
|
||
| def parse_cookies(cookie_str): | ||
| """Parse cookie string from headers""" | ||
| cookie_dict = {} | ||
| cookies = SimpleCookie() | ||
| try: | ||
| cookies.load(cookie_str) | ||
| except CookieError: | ||
| # Invalid keys can cause errors, we want to ignore them | ||
| pass | ||
|
|
||
| for key, morsel in cookies.items(): | ||
| cookie_dict[key] = morsel.value | ||
|
|
||
| return cookie_dict | ||
| def parse_cookies(*cookie_strs): | ||
| """Parse cookie strings from headers""" | ||
| # Unlike SimpleCookie, this keeps every cookie, even one with a space, a comma or JSON, | ||
| # and every value of a repeated name, because the app may read any of them. | ||
| cookies = {} | ||
| for cookie_str in cookie_strs: | ||
| for chunk in cookie_str.split(";"): | ||
| if "=" in chunk: | ||
| key, val = chunk.split("=", 1) | ||
| else: | ||
| key, val = "", chunk | ||
| key, val = key.strip(), _unquote(val.strip()) | ||
| if not key and not val: | ||
| continue | ||
| if key not in cookies: | ||
| cookies[key] = val | ||
| elif isinstance(cookies[key], list): | ||
| if val not in cookies[key]: | ||
| cookies[key].append(val) | ||
| elif cookies[key] != val: | ||
| cookies[key] = [cookies[key], val] | ||
| return cookies |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.