This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: http.cookies.SimpleCookie.parse error after [keys] or some JSON data values
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: xnovakj
Priority: normal Keywords:

Created on 2020-10-05 15:19 by xnovakj, last changed 2022-04-11 14:59 by admin.

Messages (3)
msg378041 - (view) Author: Jan Novak (xnovakj) Date: 2020-10-05 15:19
If brackets [] are around cookie name,
next cookie names are not loaded.

try:
  import http.cookies as Cookie
except ImportError:
  import Cookie
c = Cookie.SimpleCookie()
c.load('id=12345; [object Object]=data; something=not loaded')
print(c)

Note:
It could cause big problems with session etc.
We found that Chrome/Edge starts to save and send this type of cookies for some (couple) users. The origin of that [object Object]=... cookies are probably some implementation of
https://cookiepedia.co.uk/cookies/euconsent
and errors somewhere in external javascripts or browsers?

Related issues:
https://bugs.python.org/issue41695
https://bugs.python.org/issue27674

The same problem occures in P3.7, P2.7, six.moves.http_cookies etc.

I know RFT says that cookie-name can't use brackets.
But you can set them to browser cookies.

RFC 6265:
set-cookie-header = "Set-Cookie:" SP set-cookie-string
 set-cookie-string = cookie-pair *( ";" SP cookie-av )
 cookie-pair       = cookie-name "=" cookie-value
 cookie-name       = token
 token             = <token, defined in [RFC2616], Section 2.2>

RFC 2616:
token          = 1*<any CHAR except CTLs or separators>
       separators     = "(" | ")" | "<" | ">" | "@"
                      | "," | ";" | ":" | "\" | <">
                      | "/" | "[" | "]" | "?" | "="
                      | "{" | "}" | SP | HT
msg380403 - (view) Author: Jan Novak (xnovakj) Date: 2020-11-05 10:44
Possible patch, load parts one by one:

http_cookie = 'id=12345; [object Object]=data; something=not_loaded'
for cookie_key in http_cookie.split(';'):
  c.load(cookie_key)

print c
Set-Cookie: id=12345
Set-Cookie: something=not_loaded
msg409688 - (view) Author: Jan Novak (xnovakj) Date: 2022-01-04 16:01
New examples with the structured data.

Problems are with quotes and spaces inside { or [

cookie-script.com set those cookie data:
CookieScriptConsent={"action":"accept","categories":"[\\"performance\\"]"}

Python loads only cookies before that JSON structure

>>> from http.cookies import SimpleCookie
>>> ck = SimpleCookie()
>>> ck.load('id=12345; CookieScriptConsent={"action":"accept","categories":"[\\"performance\\"]"}; something="not loaded"')
>>> print(ck)
Set-Cookie: id=12345

This works:
>>> ck.load('id=12345; complex_data={1:[1,2]}; something="loaded"')
>>> print(ck)
Set-Cookie: complex_data={1:[1,2]}
Set-Cookie: id=12345
Set-Cookie: something="loaded"

This not works:
>>> ck.load('id=12345; complex_data={1:[1, 2]}; something="not loaded"')
>>> print(ck)
Set-Cookie: complex_data={1:[1,
Set-Cookie: id=12345

Conclusion:
Parsing JSON like cookie objects works, except quotes and without spaces. 

Exist some new RFC with JSON data support?
How implementation/support/solution in diferent languages?
Exist another Python library which support cookie with JSON data?
History
Date User Action Args
2022-04-11 14:59:36adminsetgithub: 86111
2022-01-04 16:01:46xnovakjsetversions: + Python 3.10, - Python 3.7
messages: + msg409688
title: http.cookies.SimpleCookie.parse error after [keys] -> http.cookies.SimpleCookie.parse error after [keys] or some JSON data values
2020-11-05 10:44:00xnovakjsetmessages: + msg380403
2020-10-05 15:19:46xnovakjcreate