Skip to content

Commit d7dbde8

Browse files
nburnsorsenthil
andauthored
gh-92936: allow double quote in cookie values (#113663)
* allow double quote in cookie values * Update Lib/test/test_http_cookies.py Co-authored-by: Senthil Kumaran <[email protected]>
1 parent 34d7351 commit d7dbde8

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

Lib/http/cookies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ def OutputString(self, attrs=None):
426426
( # Optional group: there may not be a value.
427427
\s*=\s* # Equal Sign
428428
(?P<val> # Start of group 'val'
429-
"(?:[^\\"]|\\.)*" # Any double-quoted string
429+
"(?:\\"|.)*?" # Any double-quoted string
430430
| # or
431431
# Special case for "expires" attr
432432
(\w{3,6}day|\w{3}),\s # Day of the week or abbreviated day

Lib/test/test_http_cookies.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,29 @@ def test_basic(self):
4848
'Set-Cookie: d=r',
4949
'Set-Cookie: f=h'
5050
))
51+
},
52+
53+
# gh-92936: allow double quote in cookie values
54+
{
55+
'data': 'cookie="{"key": "value"}"',
56+
'dict': {'cookie': '{"key": "value"}'},
57+
'repr': "<SimpleCookie: cookie='{\"key\": \"value\"}'>",
58+
'output': 'Set-Cookie: cookie="{"key": "value"}"',
59+
},
60+
{
61+
'data': 'key="some value; surrounded by quotes"',
62+
'dict': {'key': 'some value; surrounded by quotes'},
63+
'repr': "<SimpleCookie: key='some value; surrounded by quotes'>",
64+
'output': 'Set-Cookie: key="some value; surrounded by quotes"',
65+
},
66+
{
67+
'data': 'session="user123"; preferences="{"theme": "dark"}"',
68+
'dict': {'session': 'user123', 'preferences': '{"theme": "dark"}'},
69+
'repr': "<SimpleCookie: preferences='{\"theme\": \"dark\"}' session='user123'>",
70+
'output': '\n'.join((
71+
'Set-Cookie: preferences="{"theme": "dark"}"',
72+
'Set-Cookie: session="user123"',
73+
))
5174
}
5275
]
5376

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Update regex used by ``http.cookies.SimpleCookie`` to handle values containing
2+
double quotes.

0 commit comments

Comments
 (0)