Skip to content

Commit 5258f0e

Browse files
KonanoWulian233picnixzvstinner
authored andcommitted
pythongh-123401: Fix http.cookies module to support obsolete RFC 850 date format (python#123405)
Co-authored-by: Wulian <[email protected]> Co-authored-by: Bénédikt Tran <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
1 parent 584906a commit 5258f0e

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

Lib/http/cookies.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,11 @@ def OutputString(self, attrs=None):
425425
( # Optional group: there may not be a value.
426426
\s*=\s* # Equal Sign
427427
(?P<val> # Start of group 'val'
428-
"(?:[^\\"]|\\.)*" # Any doublequoted string
428+
"(?:[^\\"]|\\.)*" # Any double-quoted string
429429
| # or
430-
\w{3},\s[\w\d\s-]{9,11}\s[\d:]{8}\sGMT # Special case for "expires" attr
430+
# Special case for "expires" attr
431+
(\w{3,6}day|\w{3}),\s # Day of the week or abbreviated day
432+
[\w\d\s-]{9,11}\s[\d:]{8}\sGMT # Date and time in specific format
431433
| # or
432434
[""" + _LegalValueChars + r"""]* # Any word or empty string
433435
) # End of group 'val'

Lib/test/test_http_cookies.py

+46
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,52 @@ def test_basic(self):
5959
for k, v in sorted(case['dict'].items()):
6060
self.assertEqual(C[k].value, v)
6161

62+
def test_obsolete_rfc850_date_format(self):
63+
# Test cases with different days and dates in obsolete RFC 850 format
64+
test_cases = [
65+
# from RFC 850, change EST to GMT
66+
# https://datatracker.ietf.org/doc/html/rfc850#section-2
67+
{
68+
'data': 'key=value; expires=Saturday, 01-Jan-83 00:00:00 GMT',
69+
'output': 'Saturday, 01-Jan-83 00:00:00 GMT'
70+
},
71+
{
72+
'data': 'key=value; expires=Friday, 19-Nov-82 16:59:30 GMT',
73+
'output': 'Friday, 19-Nov-82 16:59:30 GMT'
74+
},
75+
# from RFC 9110
76+
# https://www.rfc-editor.org/rfc/rfc9110.html#section-5.6.7-6
77+
{
78+
'data': 'key=value; expires=Sunday, 06-Nov-94 08:49:37 GMT',
79+
'output': 'Sunday, 06-Nov-94 08:49:37 GMT'
80+
},
81+
# other test cases
82+
{
83+
'data': 'key=value; expires=Wednesday, 09-Nov-94 08:49:37 GMT',
84+
'output': 'Wednesday, 09-Nov-94 08:49:37 GMT'
85+
},
86+
{
87+
'data': 'key=value; expires=Friday, 11-Nov-94 08:49:37 GMT',
88+
'output': 'Friday, 11-Nov-94 08:49:37 GMT'
89+
},
90+
{
91+
'data': 'key=value; expires=Monday, 14-Nov-94 08:49:37 GMT',
92+
'output': 'Monday, 14-Nov-94 08:49:37 GMT'
93+
},
94+
]
95+
96+
for case in test_cases:
97+
with self.subTest(data=case['data']):
98+
C = cookies.SimpleCookie()
99+
C.load(case['data'])
100+
101+
# Extract the cookie name from the data string
102+
cookie_name = case['data'].split('=')[0]
103+
104+
# Check if the cookie is loaded correctly
105+
self.assertIn(cookie_name, C)
106+
self.assertEqual(C[cookie_name].get('expires'), case['output'])
107+
62108
def test_unquote(self):
63109
cases = [
64110
(r'a="b=\""', 'b="'),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The :mod:`http.cookies` module now supports parsing obsolete :rfc:`850`
2+
date formats, in accordance with :rfc:`9110` requirements.
3+
Patch by Nano Zheng.

0 commit comments

Comments
 (0)