Title: SimpleCookie() fails for json-like values with embedded double-quotes · Issue #92936 · python/cpython · GitHub
Open Graph Title: SimpleCookie() fails for json-like values with embedded double-quotes · Issue #92936 · python/cpython
X Title: SimpleCookie() fails for json-like values with embedded double-quotes · Issue #92936 · python/cpython
Description: 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 n...
Open Graph Description: 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 e...
X Description: 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 e...
Opengraph URL: https://github.com/python/cpython/issues/92936
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"SimpleCookie() fails for json-like values with embedded double-quotes","articleBody":"\r\n**Bug report**\r\n`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:\r\n```\r\n>>> import http.cookies.SimpleCookie('a1=b;a2=\"c d\";')\r\n\u003cSimpleCookie: a1='b' a2='c d'>\r\n```\r\n\r\nCookies 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.\r\n```\r\n# Pattern for finding cookie\r\n#\r\n# This used to be strict parsing based on the RFC2109 and RFC2068\r\n# specifications. I have since discovered that MSIE 3.0x doesn't\r\n# follow the character rules outlined in those specs. As a\r\n# result, the parsing rules here are less strict.\r\n```\r\nCool. 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):\r\n\r\n```\r\n>>> import http.cookies.SimpleCookie('a1={\"b\":c};')\r\n\u003cSimpleCookie: >\r\n```\r\n\r\n**Bug Fix/Modest Proposal**\r\nRather 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\".\r\n\r\nThe change would simply allow any number of embedded double quotes. The following snippet adds two lines to the existing `_CookiePattern` as found in `cpython/Lib/http/cookies.py`:\r\n```\r\n_CookiePattern = re.compile(r\"\"\"\r\n \\s* # Optional whitespace at start of cookie\r\n (?P\u003ckey> # Start of group 'key'\r\n [\"\"\" + _LegalKeyChars + r\"\"\"]+? # Any word of at least one letter\r\n ) # End of group 'key'\r\n ( # Optional group: there may not be a value.\r\n \\s*=\\s* # Equal Sign\r\n (?P\u003cval> # Start of group 'val'\r\n \"(?:[^\\\\\"]|\\\\.)*\" # Any doublequoted string\r\n | # or\r\n \\w{3},\\s[\\w\\d\\s-]{9,11}\\s[\\d:]{8}\\sGMT # Special case for \"expires\" attr\r\n | # or\r\n [\"\"\" + _LegalValueChars + r\"\"\"]* # Any word or empty string\r\n# additional clause vvvvv\r\n | \r\n [\"\"\" + _LegalValueChars + r\"\"\"]+[\"\"\" + _LegalValueChars + r'\"]*[' + _LegalValueChars + r\"\"\"]+ # Any word with internal quotes \r\n# end ^^^^\r\n ) # End of group 'val'\r\n )? # End of optional value group\r\n \\s* # Any number of spaces.\r\n (\\s+|;|$) # Ending either at space, semicolon, or EOS.\r\n \"\"\", re.ASCII | re.VERBOSE) # re.ASCII may be removed if safe.\r\n```\r\nThe 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.\r\n\r\n**Your environment**\r\nObserved initially in 3.8.10 (linux), confirmed 3.10.1 (Mac) and observed the code in `cpython/Lib/http/cookies.py:437`.\r\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-113663\n* gh-137566\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/pbuckner","@type":"Person","name":"pbuckner"},"datePublished":"2022-05-18T23:26:12Z","url":"https://github.com/python/cpython/issues/92936"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:e6994914-320f-839f-60f0-909948cbbda9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 89EA:97D39:1049352:1576EAC:6B020F38 |
| html-safe-nonce | a3c50bbe8ce85ccedc92e8eba6723949202c13a59777668369ac979c359a1c2e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4OUVBOjk3RDM5OjEwNDkzNTI6MTU3NkVBQzo2QjAyMEYzOCIsInZpc2l0b3JfaWQiOiIyMTc1NDEzMjkzODQwMjA3NjcyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 4774e7da7068311c5bb5ffb6e61b9407d807f91a0be2af3b49e09bfb2cafe770 |
| hovercard-subject-tag | issue:1240710790 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/92936/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ba2349d095f898c3b417c7261dd84c50107780c003687d8e1b3cfe6c7ac8a8a5/python/cpython/issues/92936 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ba2349d095f898c3b417c7261dd84c50107780c003687d8e1b3cfe6c7ac8a8a5/python/cpython/issues/92936 |
| og:image:alt | 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 e... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | pbuckner |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width