Skip to content

Commit 9bd476e

Browse files
committed
allow square brackets in cookie values (closes python#22931)
1 parent 0823ffb commit 9bd476e

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

Lib/http/cookies.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,20 +429,21 @@ def OutputString(self, attrs=None):
429429
# result, the parsing rules here are less strict.
430430
#
431431

432-
_LegalCharsPatt = r"[\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\=]"
432+
_LegalKeyChars = r"\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\="
433+
_LegalValueChars = _LegalKeyChars + '\[\]'
433434
_CookiePattern = re.compile(r"""
434435
(?x) # This is a verbose pattern
435436
\s* # Optional whitespace at start of cookie
436437
(?P<key> # Start of group 'key'
437-
""" + _LegalCharsPatt + r"""+? # Any word of at least one letter
438+
[""" + _LegalKeyChars + r"""]+? # Any word of at least one letter
438439
) # End of group 'key'
439440
\s*=\s* # Equal Sign
440441
(?P<val> # Start of group 'val'
441442
"(?:[^\\"]|\\.)*" # Any doublequoted string
442443
| # or
443444
\w{3},\s[\w\d\s-]{9,11}\s[\d:]{8}\sGMT # Special case for "expires" attr
444445
| # or
445-
""" + _LegalCharsPatt + r"""* # Any word or empty string
446+
[""" + _LegalValueChars + r"""]* # Any word or empty string
446447
) # End of group 'val'
447448
\s*;? # Probably ending in a semi-colon
448449
""", re.ASCII) # May be removed if safe.

Lib/test/test_http_cookies.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ def test_basic(self):
3434
'dict': {'keebler' : 'E=mc2'},
3535
'repr': "<SimpleCookie: keebler='E=mc2'>",
3636
'output': 'Set-Cookie: keebler=E=mc2'},
37+
38+
# issue22931 - Adding '[' and ']' as valid characters in cookie
39+
# values as defined in RFC 6265
40+
{
41+
'data': 'a=b; c=[; d=r; f=h',
42+
'dict': {'a':'b', 'c':'[', 'd':'r', 'f':'h'},
43+
'repr': "<SimpleCookie: a='b' c='[' d='r' f='h'>",
44+
'output': '\n'.join((
45+
'Set-Cookie: a=b',
46+
'Set-Cookie: c=[',
47+
'Set-Cookie: d=r',
48+
'Set-Cookie: f=h'
49+
))
50+
}
3751
]
3852

3953
for case in cases:

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ Core and Builtins
1616
- Issue #23055: Fixed a buffer overflow in PyUnicode_FromFormatV. Analysis
1717
and fix by Guido Vranken.
1818

19+
Library
20+
-------
21+
22+
- Issue #22931: Allow '[' and ']' in cookie values.
23+
1924

2025
What's New in Python 3.2.6?
2126
===========================

0 commit comments

Comments
 (0)