You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
http.cookies.SimpleCookie.load() fails to consistently handle malformed cookies #127195
However, http.cookies.SimpleCookie.load() processes only the valid portion before the malformed cookie and stops entirely:
>>> from http import cookies
>>> C = cookies.SimpleCookie()
>>> C.load('a=b; c={"d":"e"}; f=g')
>>> C.output()
'Set-Cookie: a=b'
It seems we should ensure consistent handling by (a) processing all valid cookies and discarding only invalid ones, or
(b) rejecting the entire cookie string if any invalid cookie is present.
After further investigation, it appears the module exhibits inconsistent behavior when encountering unexpected inputs and it is likely due to its regex-based parsing, which assumes very clean input.
["""+_LegalKeyChars+r"""]+? # Any word of at least one letter
) # End of group 'key'
( # Optional group: there may not be a value.
\s*=\s* # Equal Sign
(?P<val> # Start of group 'val'
"(?:[^\\"]|\\.)*" # Any doublequoted string
| # or
\w{3},\s[\w\d\s-]{9,11}\s[\d:]{8}\sGMT # Special case for "expires" attr
| # or
["""+_LegalValueChars+r"""]* # Any word or empty string
) # End of group 'val'
)? # End of optional value group
\s* # Any number of spaces.
(\s+|;|$) # Ending either at space, semicolon, or EOS.
""", re.ASCII|re.VERBOSE) # re.ASCII may be removed if safe.
If the maintainers are in agreement, I’d like to propose a patch to improve cookie parsing by following these steps:
Split the cookie into parts.
For each part:
(a) Validate the key. If invalid, issue a warning and skip the part.
(b) Validate the value. If invalid, issue a warning and skip the part.
Store only valid parts in the cookie jar.
Additionally, I’d like to gather opinions on whether we should allow invalid—but commonly occurring—characters in cookie values, such as those used in JSON. While the RFC advises against accepting these characters, they are widely accepted by major browsers and have become a common practice among web developers.
@picnixz, it seems you triaged my issue. If possible, could you notify the appropriate core maintainers?
Activity
moonsikpark commentedon Nov 24, 2024
After further investigation, it appears the module exhibits inconsistent behavior when encountering unexpected inputs and it is likely due to its regex-based parsing, which assumes very clean input.
cpython/Lib/http/cookies.py
Lines 420 to 437 in a4d4c1e
If the maintainers are in agreement, I’d like to propose a patch to improve cookie parsing by following these steps:
(a) Validate the key. If invalid, issue a warning and skip the part.
(b) Validate the value. If invalid, issue a warning and skip the part.
Additionally, I’d like to gather opinions on whether we should allow invalid—but commonly occurring—characters in cookie values, such as those used in JSON. While the RFC advises against accepting these characters, they are widely accepted by major browsers and have become a common practice among web developers.
@picnixz, it seems you triaged my issue. If possible, could you notify the appropriate core maintainers?
picnixz commentedon Nov 27, 2024
cc @serhiy-storchaka
vadmium commentedon Dec 12, 2024
The first case with the tab character looks the same as Issue #75637. For the second case with double quotes, there are a few related reports:
nburns commentedon Aug 6, 2025
Was wondering if someone would be willing to take a look at my issue/PR (and provide feedback) related to a similar issue with the cookies parser:
Browsers tolerate json values well, and for example Zoom sets cookies with json in them:
#92936
so it would be nice if the cookie parser could handle them (webob for example is able to do so): #113663