From 423efc50f9e44632ecea00123011c25b553f917a Mon Sep 17 00:00:00 2001 From: Nick Burns Date: Tue, 2 Jan 2024 19:40:07 +0000 Subject: [PATCH 1/2] allow double quote in cookie values --- Lib/http/cookies.py | 2 +- Lib/test/test_http_cookies.py | 12 ++++++++++++ .../2025-08-08-21-20-14.gh-issue-92936.rOgG1S.rst | 2 ++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2025-08-08-21-20-14.gh-issue-92936.rOgG1S.rst diff --git a/Lib/http/cookies.py b/Lib/http/cookies.py index 694b1b09a0567c..74349bb63d66e2 100644 --- a/Lib/http/cookies.py +++ b/Lib/http/cookies.py @@ -426,7 +426,7 @@ def OutputString(self, attrs=None): ( # Optional group: there may not be a value. \s*=\s* # Equal Sign (?P # Start of group 'val' - "(?:[^\\"]|\\.)*" # Any double-quoted string + "(?:\\"|.)*?" # Any double-quoted string | # or # Special case for "expires" attr (\w{3,6}day|\w{3}),\s # Day of the week or abbreviated day diff --git a/Lib/test/test_http_cookies.py b/Lib/test/test_http_cookies.py index 2fbc142de2fd34..b5d8a2b11b49e1 100644 --- a/Lib/test/test_http_cookies.py +++ b/Lib/test/test_http_cookies.py @@ -48,6 +48,18 @@ def test_basic(self): 'Set-Cookie: d=r', 'Set-Cookie: f=h' )) + }, + + {'data': 'cookie="{"key": "value"}"', + 'dict': {'cookie': '{"key": "value"}'}, + 'repr': "", + 'output': 'Set-Cookie: cookie="{"key": "value"}"', + }, + + {'data': 'key="some value; surrounded by quotes"', + 'dict': {'key': 'some value; surrounded by quotes'}, + 'repr': "", + 'output': 'Set-Cookie: key="some value; surrounded by quotes"', } ] diff --git a/Misc/NEWS.d/next/Library/2025-08-08-21-20-14.gh-issue-92936.rOgG1S.rst b/Misc/NEWS.d/next/Library/2025-08-08-21-20-14.gh-issue-92936.rOgG1S.rst new file mode 100644 index 00000000000000..906c442b64f438 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-08-08-21-20-14.gh-issue-92936.rOgG1S.rst @@ -0,0 +1,2 @@ +Update regex used by ``http.cookies.SimpleCookie`` to handle values containing +double quotes. From 081ace233f6bf22f80a8f6794fd731a8d61f8351 Mon Sep 17 00:00:00 2001 From: Nick Burns Date: Fri, 8 Aug 2025 09:13:50 -0700 Subject: [PATCH 2/2] Update Lib/test/test_http_cookies.py Co-authored-by: Senthil Kumaran --- Lib/test/test_http_cookies.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/Lib/test/test_http_cookies.py b/Lib/test/test_http_cookies.py index b5d8a2b11b49e1..c2ed30831b2e0e 100644 --- a/Lib/test/test_http_cookies.py +++ b/Lib/test/test_http_cookies.py @@ -50,16 +50,27 @@ def test_basic(self): )) }, - {'data': 'cookie="{"key": "value"}"', - 'dict': {'cookie': '{"key": "value"}'}, - 'repr': "", - 'output': 'Set-Cookie: cookie="{"key": "value"}"', + # gh-92936: allow double quote in cookie values + { + 'data': 'cookie="{"key": "value"}"', + 'dict': {'cookie': '{"key": "value"}'}, + 'repr': "", + 'output': 'Set-Cookie: cookie="{"key": "value"}"', }, - - {'data': 'key="some value; surrounded by quotes"', - 'dict': {'key': 'some value; surrounded by quotes'}, - 'repr': "", - 'output': 'Set-Cookie: key="some value; surrounded by quotes"', + { + 'data': 'key="some value; surrounded by quotes"', + 'dict': {'key': 'some value; surrounded by quotes'}, + 'repr': "", + 'output': 'Set-Cookie: key="some value; surrounded by quotes"', + }, + { + 'data': 'session="user123"; preferences="{"theme": "dark"}"', + 'dict': {'session': 'user123', 'preferences': '{"theme": "dark"}'}, + 'repr': "", + 'output': '\n'.join(( + 'Set-Cookie: preferences="{"theme": "dark"}"', + 'Set-Cookie: session="user123"', + )) } ]