-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
unquote double-quotes cookie values #1440
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
|
@@ -131,3 +131,4 @@ Patches and Suggestions | |
- Dave Shawley <[email protected]> | ||
- James Clarke (jam) | ||
- Kevin Burke <[email protected]> | ||
- Flavio Curella |
This file contains 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's this code doing now? It looks like you're going through cookies and removing all instances of
\"
...is that the right behaviour?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Lukasa going by what you quoted, I suspect not. Going one step further, the
*cookie-octent
you mention looks to be a URL-encoded string that doesn't allow"
, controls, whitespace,,
,;
, and\
. The real problem then becomes, how do we add back the"
s (when necessary)?Also @fcurella do you have an example of where the current implementation breaks (or rather a server breaks due to it)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Lukasa currently, the
cookie.value
will be'"\\"bar:baz\\""'
(to clarify: double-quote, slash, double-quote, cookie_value, slash, double-quote, double-quote).If we want to keep the quotes, we can't use
.strip()
. Maybe we can use a regex likere.sub(r'"\\"(.*)\\""', '"$1"', cookie.value)
, to make sure we remove only the two\"
at the ends of the string.@sigmavirus24 the problem came up on a django project i'm working on, where one service would use
request.set_cookie(key, "param1:param2")
, and we're using requests on a different service and we need to forward the cookie to the browser.I've already verified that keeping the quotes (but not escaping them) produces the correct cookie.
Moreover, when removing the quotes altogether (my previous solution), django will recognize the
:
and will add the quotes back again. I've done a quick test with Flask, and in this last scenario (unquoted cookie containing:
) it doesn't quote the cookie value automatically.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha, that makes sense to me. It would be awesome if you could add a test just to verify that this is actuall what happens.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fcurella you mean
request.set_cookie(key, '"param1:param2"')
right? If so that makes sense. But yes, adding a test and ensuring this doesn't break anything else would be awesome.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sigmavirus24 no. if you call
response.set_cookie(key, 'param1:param2')
django will add the double-quotes implicitly because of the:
(and other special characters, see https://github.com/django/django/blob/master/django/http/cookie.py#L23)Looking at the django code and its comments, this seems to be the desired behaviour and not due to a bug in django.
Re tests: I'm already adding a new test for the parsing. Do you want me to include additional weirder cookie values?
I'd also like your and @Lukasa's opinion on
.replace()
vsre.sub()
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mine has always been straight-forward, never use regex. Replace works well and
it it is all we need currently.
re
is not the best implementation of regularexpressions in python and the others are not in the standard library. Beyond
that, string methods are fast.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sigmavirus24 +1. Regexes are only worth using if you're actually defining a complicated grammar. If you don't use a single regex special character, you didn't need a regex. =)