diff -r 7ed567ad8b4c Lib/http/cookies.py --- a/Lib/http/cookies.py Tue Mar 31 22:03:59 2015 +0200 +++ b/Lib/http/cookies.py Tue Mar 31 16:58:33 2015 -0700 @@ -455,12 +455,13 @@ # result, the parsing rules here are less strict. # -_LegalCharsPatt = r"[\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\=]" +_LegalCharsPatt = r"\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\=" +_LegalValuesPatt = _LegalCharsPatt + '\[\]' _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 + [""" + _LegalCharsPatt + r"""]+? # Any word of at least one letter ) # End of group 'key' ( # Optional group: there may not be a value. \s*=\s* # Equal Sign @@ -469,7 +470,7 @@ | # 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 + [""" + _LegalValuesPatt + r"""]* # Any word or empty string ) # End of group 'val' )? # End of optional value group \s* # Any number of spaces. diff -r 7ed567ad8b4c Lib/test/test_http_cookies.py --- a/Lib/test/test_http_cookies.py Tue Mar 31 22:03:59 2015 +0200 +++ b/Lib/test/test_http_cookies.py Tue Mar 31 16:58:33 2015 -0700 @@ -44,6 +44,19 @@ '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: