From db912d800c70cea93f96aa3c026708ac15ef16f9 Mon Sep 17 00:00:00 2001 From: Konano Date: Tue, 27 Aug 2024 18:45:23 +0000 Subject: [PATCH 1/9] pythongh-123401: Fix http.cookies module to support obsolete RFC 850 date format --- Lib/http/cookies.py | 4 +++- Lib/test/test_http_cookies.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/Lib/http/cookies.py b/Lib/http/cookies.py index 6b9ed24ad8ec78..2dcc8ef16647c9 100644 --- a/Lib/http/cookies.py +++ b/Lib/http/cookies.py @@ -426,7 +426,9 @@ def OutputString(self, attrs=None): (?P # 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 + # Special case for "expires" attr + (\w{3,6}day|\w{3}),\s # Day of the week or abbreviated day + [\w\d\s-]{9,11}\s[\d:]{8}\sGMT # Date and time in specific format | # or [""" + _LegalValueChars + r"""]* # Any word or empty string ) # End of group 'val' diff --git a/Lib/test/test_http_cookies.py b/Lib/test/test_http_cookies.py index 8879902a6e2f41..9243c7b5e5119b 100644 --- a/Lib/test/test_http_cookies.py +++ b/Lib/test/test_http_cookies.py @@ -59,6 +59,39 @@ def test_basic(self): for k, v in sorted(case['dict'].items()): self.assertEqual(C[k].value, v) + def test_obsolete_rfc850_date_format(self): + # Test cases with different days and dates in obsolete RFC 850 format + test_cases = [ + { + 'data': 'key=value; expires=Sunday, 06-Nov-94 08:49:37 GMT', + 'output': 'Sunday, 06-Nov-94 08:49:37 GMT' + }, + { + 'data': 'key=value; expires=Wednesday, 09-Nov-94 08:49:37 GMT', + 'output': 'Wednesday, 09-Nov-94 08:49:37 GMT' + }, + { + 'data': 'key=value; expires=Friday, 11-Nov-94 08:49:37 GMT', + 'output': 'Friday, 11-Nov-94 08:49:37 GMT' + }, + { + 'data': 'key=value; expires=Monday, 14-Nov-94 08:49:37 GMT', + 'output': 'Monday, 14-Nov-94 08:49:37 GMT' + }, + ] + + for case in test_cases: + with self.subTest(data=case['data']): + C = cookies.SimpleCookie() + C.load(case['data']) + + # Extract the cookie name from the data string + cookie_name = case['data'].split('=')[0] + + # Check if the cookie is loaded correctly + self.assertIn(cookie_name, C) + self.assertEqual(C[cookie_name].get('expires'), case['output']) + def test_unquote(self): cases = [ (r'a="b=\""', 'b="'), From 57904eb2910b4d63299ee573bab0c3ab801c6264 Mon Sep 17 00:00:00 2001 From: Konano Date: Tue, 27 Aug 2024 18:59:26 +0000 Subject: [PATCH 2/9] Add NEWS entry for http.cookies RFC 850 date format support --- .../next/Library/2024-08-27-18-58-01.gh-issue-123401.t4-FpI.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-08-27-18-58-01.gh-issue-123401.t4-FpI.rst diff --git a/Misc/NEWS.d/next/Library/2024-08-27-18-58-01.gh-issue-123401.t4-FpI.rst b/Misc/NEWS.d/next/Library/2024-08-27-18-58-01.gh-issue-123401.t4-FpI.rst new file mode 100644 index 00000000000000..6bc209e9d5068d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-08-27-18-58-01.gh-issue-123401.t4-FpI.rst @@ -0,0 +1,2 @@ +The `http.cookies` module now supports parsing obsolete RFC 850 date +formats, in accordance with RFC 9110 requirements. From d8cd225b8476549a686570c2660b7f78d01611f4 Mon Sep 17 00:00:00 2001 From: Konano Date: Tue, 27 Aug 2024 19:03:56 +0000 Subject: [PATCH 3/9] Update NEWS entry to use double backticks for inline literals --- .../next/Library/2024-08-27-18-58-01.gh-issue-123401.t4-FpI.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-08-27-18-58-01.gh-issue-123401.t4-FpI.rst b/Misc/NEWS.d/next/Library/2024-08-27-18-58-01.gh-issue-123401.t4-FpI.rst index 6bc209e9d5068d..be9ed0074aa465 100644 --- a/Misc/NEWS.d/next/Library/2024-08-27-18-58-01.gh-issue-123401.t4-FpI.rst +++ b/Misc/NEWS.d/next/Library/2024-08-27-18-58-01.gh-issue-123401.t4-FpI.rst @@ -1,2 +1,2 @@ -The `http.cookies` module now supports parsing obsolete RFC 850 date +The ``http.cookies`` module now supports parsing obsolete RFC 850 date formats, in accordance with RFC 9110 requirements. From a3737180f3f0e3f3c016887905f44fe5345d98f8 Mon Sep 17 00:00:00 2001 From: Nano Date: Wed, 28 Aug 2024 11:17:29 +0800 Subject: [PATCH 4/9] style(http/cookies): correct typo in regex comment Fix a minor typo in the regex comment by adding a hyphen to "double-quoted" for consistency and clarity. No functional changes to the code logic. Co-authored-by: Wulian <1055917385@qq.com> --- Lib/http/cookies.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/http/cookies.py b/Lib/http/cookies.py index 2dcc8ef16647c9..57791c6ab0886d 100644 --- a/Lib/http/cookies.py +++ b/Lib/http/cookies.py @@ -424,7 +424,7 @@ def OutputString(self, attrs=None): ( # Optional group: there may not be a value. \s*=\s* # Equal Sign (?P # Start of group 'val' - "(?:[^\\"]|\\.)*" # Any doublequoted 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 From 5cf347dbaebf7bcdde3474225f93ca1b6ad08f8c Mon Sep 17 00:00:00 2001 From: Nano Date: Fri, 6 Sep 2024 01:32:35 +0800 Subject: [PATCH 5/9] Update Misc/NEWS.d/next/Library/2024-08-27-18-58-01.gh-issue-123401.t4-FpI.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- .../Library/2024-08-27-18-58-01.gh-issue-123401.t4-FpI.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2024-08-27-18-58-01.gh-issue-123401.t4-FpI.rst b/Misc/NEWS.d/next/Library/2024-08-27-18-58-01.gh-issue-123401.t4-FpI.rst index be9ed0074aa465..a646b3b94c4e8d 100644 --- a/Misc/NEWS.d/next/Library/2024-08-27-18-58-01.gh-issue-123401.t4-FpI.rst +++ b/Misc/NEWS.d/next/Library/2024-08-27-18-58-01.gh-issue-123401.t4-FpI.rst @@ -1,2 +1,2 @@ -The ``http.cookies`` module now supports parsing obsolete RFC 850 date -formats, in accordance with RFC 9110 requirements. +The :mod:`http.cookies` module now supports parsing obsolete :rfc:`850` +date formats, in accordance with :rfc:`9110` requirements. From 592da513efcf76740b033544f4a1e959ce4c2728 Mon Sep 17 00:00:00 2001 From: Nano Date: Fri, 6 Sep 2024 01:38:23 +0800 Subject: [PATCH 6/9] Include two examples from RFC 850 --- Lib/test/test_http_cookies.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Lib/test/test_http_cookies.py b/Lib/test/test_http_cookies.py index 9243c7b5e5119b..c4467fa4b5fdd5 100644 --- a/Lib/test/test_http_cookies.py +++ b/Lib/test/test_http_cookies.py @@ -62,6 +62,14 @@ def test_basic(self): def test_obsolete_rfc850_date_format(self): # Test cases with different days and dates in obsolete RFC 850 format test_cases = [ + { + 'data': 'key=value; expires=Saturday, 01-Jan-83 00:00:00 EST', + 'output': 'Saturday, 01-Jan-83 00:00:00 EST' + }, + { + 'data': 'key=value; expires=Friday, 19-Nov-82 16:59:30 EST', + 'output': 'Friday, 19-Nov-82 16:59:30 EST' + }, { 'data': 'key=value; expires=Sunday, 06-Nov-94 08:49:37 GMT', 'output': 'Sunday, 06-Nov-94 08:49:37 GMT' From 8cdf6c54ebe59dd1ef378d14eb15307dce1f5bdb Mon Sep 17 00:00:00 2001 From: Nano Date: Fri, 6 Sep 2024 01:56:24 +0800 Subject: [PATCH 7/9] Add comments for rfc850 test cases --- Lib/test/test_http_cookies.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/test/test_http_cookies.py b/Lib/test/test_http_cookies.py index c4467fa4b5fdd5..bb47fb869aa927 100644 --- a/Lib/test/test_http_cookies.py +++ b/Lib/test/test_http_cookies.py @@ -62,6 +62,8 @@ def test_basic(self): def test_obsolete_rfc850_date_format(self): # Test cases with different days and dates in obsolete RFC 850 format test_cases = [ + # test cases from RFC 850 + # https://datatracker.ietf.org/doc/html/rfc850#section-2 { 'data': 'key=value; expires=Saturday, 01-Jan-83 00:00:00 EST', 'output': 'Saturday, 01-Jan-83 00:00:00 EST' @@ -70,6 +72,7 @@ def test_obsolete_rfc850_date_format(self): 'data': 'key=value; expires=Friday, 19-Nov-82 16:59:30 EST', 'output': 'Friday, 19-Nov-82 16:59:30 EST' }, + # other test cases { 'data': 'key=value; expires=Sunday, 06-Nov-94 08:49:37 GMT', 'output': 'Sunday, 06-Nov-94 08:49:37 GMT' From 1692faf81de347fe456a860cdbf648ad7adb82b8 Mon Sep 17 00:00:00 2001 From: Nano Date: Fri, 6 Sep 2024 02:02:32 +0800 Subject: [PATCH 8/9] change EST to GMT in test cases --- Lib/test/test_http_cookies.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_http_cookies.py b/Lib/test/test_http_cookies.py index bb47fb869aa927..7b3dc0fdaedc3b 100644 --- a/Lib/test/test_http_cookies.py +++ b/Lib/test/test_http_cookies.py @@ -62,21 +62,23 @@ def test_basic(self): def test_obsolete_rfc850_date_format(self): # Test cases with different days and dates in obsolete RFC 850 format test_cases = [ - # test cases from RFC 850 + # from RFC 850, change EST to GMT # https://datatracker.ietf.org/doc/html/rfc850#section-2 { - 'data': 'key=value; expires=Saturday, 01-Jan-83 00:00:00 EST', - 'output': 'Saturday, 01-Jan-83 00:00:00 EST' + 'data': 'key=value; expires=Saturday, 01-Jan-83 00:00:00 GMT', + 'output': 'Saturday, 01-Jan-83 00:00:00 GMT' }, { - 'data': 'key=value; expires=Friday, 19-Nov-82 16:59:30 EST', - 'output': 'Friday, 19-Nov-82 16:59:30 EST' + 'data': 'key=value; expires=Friday, 19-Nov-82 16:59:30 GMT', + 'output': 'Friday, 19-Nov-82 16:59:30 GMT' }, - # other test cases + # from RFC 9110 + # https://www.rfc-editor.org/rfc/rfc9110.html#section-5.6.7-6 { 'data': 'key=value; expires=Sunday, 06-Nov-94 08:49:37 GMT', 'output': 'Sunday, 06-Nov-94 08:49:37 GMT' }, + # other test cases { 'data': 'key=value; expires=Wednesday, 09-Nov-94 08:49:37 GMT', 'output': 'Wednesday, 09-Nov-94 08:49:37 GMT' From f9dd82b00c9ce870a430cd1c15e29f03ca6b5282 Mon Sep 17 00:00:00 2001 From: Nano Date: Fri, 6 Sep 2024 02:07:44 +0800 Subject: [PATCH 9/9] Update Misc/NEWS.d/next/Library/2024-08-27-18-58-01.gh-issue-123401.t4-FpI.rst --- .../next/Library/2024-08-27-18-58-01.gh-issue-123401.t4-FpI.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Misc/NEWS.d/next/Library/2024-08-27-18-58-01.gh-issue-123401.t4-FpI.rst b/Misc/NEWS.d/next/Library/2024-08-27-18-58-01.gh-issue-123401.t4-FpI.rst index a646b3b94c4e8d..638f3f76239ca6 100644 --- a/Misc/NEWS.d/next/Library/2024-08-27-18-58-01.gh-issue-123401.t4-FpI.rst +++ b/Misc/NEWS.d/next/Library/2024-08-27-18-58-01.gh-issue-123401.t4-FpI.rst @@ -1,2 +1,3 @@ The :mod:`http.cookies` module now supports parsing obsolete :rfc:`850` date formats, in accordance with :rfc:`9110` requirements. +Patch by Nano Zheng.