-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
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
SimpleCookie() fails for json-like values with embedded double-quotes #92936
Comments
how about
|
I think you're asking for trouble -- the idea is we cannot / should not attempt to interpret the 'value' part -- it's technically just a string. If the cookie value wants to be ("a","b","c") why shouldn't we return from python the string '("a","b","c")'. The "rule" for value is: quoted-string is double-quotes surrounding Here, we're more concerned with the 'token' definition. It is supposed to be any TEXT except most punctuation included braces, parens, angle brackets etc. However SimpleCookie already makes exceptions for many of these -- I'm just suggesting we add in the double quote as being "legal" character unless it begin or ends the value (... in which case the double-quote behaves as part of quoted-string definition and does not contribute to the actual value of the cookie. |
you mean treat |
Yes, I would -- in python. If a browser doesn't like it, let the browser say so, not our problem. Let the receiving application figure out what to do with it. We don't attempt to interpret numbers, It's parsable -- we need to be more careful about spaces, semicolons and equal sign, as they're used to determine boundaries. |
I wonder how browsers do |
Understood... but you only think it's json. Maybe the cookie maker has other ideas (given there's no way to specify cookie format such as Content-Type.) Note I can create a cookie (in browser):
and the string value is stored just like that... |
ok.. |
@pbuckner We'd need that handled correctly, wouldn't we? Example:
|
double quote only works for the full value of the cookie (everything right of the equal sing). In your example, the value is not double quoted, but is Yes, it's "obvious" that "my value" is a single construct, but only if you assume JSON-like semantics. Cookies aren't JSON. (Personally, I have no problem making the python cookie parser much smarter & think there's little risk, as long as we don't add semantics like converting dates or data-types, adding or removing spaces to look more JSON-like, etc.) In your case, adding a single character to my suggested added line:
would suffice. The difference is adding a single space in the middle clause, between the single-quote and double-quote: |
@pbuckner |
Took a shot at implementing this by modifying the existing regex for a string wrapped in double quotes: #113663 |
This discussion suggests that the safest option is probably just to split on semi-colon, then take the entire string (after the first In summary: The latest RFC considers The original netscape spec did allow There were 2 other specs in between those which introduce a quoted-string, but according to that discussion those specs were basically nonsense and nobody really implemented them. Note that both the original and the latest spec do not have any quoted-string (the latest simply allows So, for parsing values, it seems splitting on |
Bug report
http.cookies.SimpleCookie()
takes a string and should return a dict-like parse of the result. On some parse errors, it returns an empty dict, or one sparsely populated with values, for example on success on a cookie with two name-value pairs:Cookies consist of name-value pairs, both of which have legal character subsets as defined in RFC2068 and RFC2109. Actual browser / server implementations are more lenient and
cookies.py
source includes such acknowledgement.Cool. Modern times bring more exceptions. Specifically, Google's OAUTH implementation now includes a cookie (
g_state
) whose value appears to be JSON, and embedded double quotes cause SimpleCookie() to fail (or actually "succeed" in a useless way):Bug Fix/Modest Proposal
Rather than trying to get Google to change their cookie format (which is happily supported by common browsers), or requiring users of
http.cookie
module write their own parsers, I suggest simple augmentation of the regular expression used to "find cookies".The change would simply allow any number of embedded double quotes. The following snippet adds two lines to the existing
_CookiePattern
as found incpython/Lib/http/cookies.py
:The two added lines merely permit cookie values to contain any number of double quotes, as long as the first and last character of the value is not a double quote. No further interpretation of cookie value (such as json validation) is attempted or warranted.
Your environment
Observed initially in 3.8.10 (linux), confirmed 3.10.1 (Mac) and observed the code in
cpython/Lib/http/cookies.py:437
.Linked PRs
The text was updated successfully, but these errors were encountered: