diff --git a/Lib/http/cookies.py b/Lib/http/cookies.py index 556d101fb0e..00082a6740f 100644 --- a/Lib/http/cookies.py +++ b/Lib/http/cookies.py @@ -428,12 +428,13 @@ def OutputString(self, attrs=None): # result, the parsing rules here are less strict. # -_LegalCharsPatt = r"[\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\=]" +_LegalKeyChars = r"\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\=" +_LegalValueChars = _LegalKeyChars + '\[\]' _CookiePattern = re.compile(r""" (?x) # This is a verbose pattern \s* # Optional whitespace at start of cookie (?P # Start of group 'key' - """ + _LegalCharsPatt + r"""+? # Any word of at least one letter + [""" + _LegalKeyChars + r"""]+? # Any word of at least one letter ) # End of group 'key' ( # Optional group: there may not be a value. \s*=\s* # Equal Sign @@ -442,7 +443,7 @@ def OutputString(self, attrs=None): | # or \w{3},\s[\w\d\s-]{9,11}\s[\d:]{8}\sGMT # Special case for "expires" attr | # or - """ + _LegalCharsPatt + r"""* # Any word or empty string + [""" + _LegalValueChars + r"""]* # Any word or empty string ) # End of group 'val' )? # End of optional value group \s* # Any number of spaces. diff --git a/Lib/test/test_http_cookies.py b/Lib/test/test_http_cookies.py index 76e5e9c4dad..8d7c220c5b6 100644 --- a/Lib/test/test_http_cookies.py +++ b/Lib/test/test_http_cookies.py @@ -43,6 +43,19 @@ def test_basic(self): 'repr': "", 'output': 'Set-Cookie: key:term=value:term'}, + # issue22931 - Adding '[' and ']' as valid characters in cookie + # values as defined in RFC 6265 + { + 'data': 'a=b; c=[; d=r; f=h', + 'dict': {'a':'b', 'c':'[', 'd':'r', 'f':'h'}, + 'repr': "", + 'output': '\n'.join(( + 'Set-Cookie: a=b', + 'Set-Cookie: c=[', + 'Set-Cookie: d=r', + 'Set-Cookie: f=h' + )) + } ] for case in cases: diff --git a/Misc/NEWS b/Misc/NEWS index 827d761bf9f..0f969207d43 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -22,6 +22,8 @@ Core and Builtins Library ------- +- Issue #22931: Allow '[' and ']' in cookie values. + - Issue #24094: Fix possible crash in json.encode with poorly behaved dict subclasses.