#16611: BaseCookie now parses 'secure' and 'httponly' flags.
Previously it generated them if they were given a value, but completely ignored them if they were present in the string passed in to be parsed. Now if the flag appears on a cookie, the corresponding Morsel key will reference a True value. Other pre-existing behavior is retained in this maintenance patch: if the source contains something like 'secure=foo', morsel['secure'] will return 'foo'. Since such a value doesn't round trip and never did (and would be a surprising occurrence) a subsequent non-bug-fix patch may change this behavior. Inspired by a patch from Julien Phalip, who reviewed this one.
This commit is contained in:
parent
f1fe159822
commit
cd0f74b1e0
@ -338,6 +338,8 @@ class Morsel(dict):
|
|||||||
"version" : "Version",
|
"version" : "Version",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_flags = {'secure', 'httponly'}
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# Set defaults
|
# Set defaults
|
||||||
self.key = self.value = self.coded_value = None
|
self.key = self.value = self.coded_value = None
|
||||||
@ -435,15 +437,18 @@ _CookiePattern = re.compile(r"""
|
|||||||
(?P<key> # Start of group 'key'
|
(?P<key> # 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'
|
) # End of group 'key'
|
||||||
\s*=\s* # Equal Sign
|
( # Optional group: there may not be a value.
|
||||||
(?P<val> # Start of group 'val'
|
\s*=\s* # Equal Sign
|
||||||
"(?:[^\\"]|\\.)*" # Any doublequoted string
|
(?P<val> # Start of group 'val'
|
||||||
| # or
|
"(?:[^\\"]|\\.)*" # Any doublequoted string
|
||||||
|
| # or
|
||||||
\w{3},\s[\w\d\s-]{9,11}\s[\d:]{8}\sGMT # Special case for "expires" attr
|
\w{3},\s[\w\d\s-]{9,11}\s[\d:]{8}\sGMT # Special case for "expires" attr
|
||||||
| # or
|
| # or
|
||||||
""" + _LegalCharsPatt + r"""* # Any word or empty string
|
""" + _LegalCharsPatt + r"""* # Any word or empty string
|
||||||
) # End of group 'val'
|
) # End of group 'val'
|
||||||
\s*;? # Probably ending in a semi-colon
|
)? # End of optional value group
|
||||||
|
\s* # Any number of spaces.
|
||||||
|
(\s+|;|$) # Ending either at space, semicolon, or EOS.
|
||||||
""", re.ASCII) # May be removed if safe.
|
""", re.ASCII) # May be removed if safe.
|
||||||
|
|
||||||
|
|
||||||
@ -549,8 +554,12 @@ class BaseCookie(dict):
|
|||||||
M[key[1:]] = value
|
M[key[1:]] = value
|
||||||
elif key.lower() in Morsel._reserved:
|
elif key.lower() in Morsel._reserved:
|
||||||
if M:
|
if M:
|
||||||
M[key] = _unquote(value)
|
if value is None:
|
||||||
else:
|
if key.lower() in Morsel._flags:
|
||||||
|
M[key] = True
|
||||||
|
else:
|
||||||
|
M[key] = _unquote(value)
|
||||||
|
elif value is not None:
|
||||||
rval, cval = self.value_decode(value)
|
rval, cval = self.value_decode(value)
|
||||||
self.__set(key, rval, cval)
|
self.__set(key, rval, cval)
|
||||||
M = self[key]
|
M = self[key]
|
||||||
|
@ -109,13 +109,51 @@ class CookieTests(unittest.TestCase):
|
|||||||
self.assertEqual(C.output(),
|
self.assertEqual(C.output(),
|
||||||
'Set-Cookie: Customer="WILE_E_COYOTE"; Max-Age=10')
|
'Set-Cookie: Customer="WILE_E_COYOTE"; Max-Age=10')
|
||||||
|
|
||||||
# others
|
def test_set_secure_httponly_attrs(self):
|
||||||
C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"')
|
C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"')
|
||||||
C['Customer']['secure'] = True
|
C['Customer']['secure'] = True
|
||||||
C['Customer']['httponly'] = True
|
C['Customer']['httponly'] = True
|
||||||
self.assertEqual(C.output(),
|
self.assertEqual(C.output(),
|
||||||
'Set-Cookie: Customer="WILE_E_COYOTE"; httponly; secure')
|
'Set-Cookie: Customer="WILE_E_COYOTE"; httponly; secure')
|
||||||
|
|
||||||
|
def test_secure_httponly_false_if_not_present(self):
|
||||||
|
C = cookies.SimpleCookie()
|
||||||
|
C.load('eggs=scrambled; Path=/bacon')
|
||||||
|
self.assertFalse(C['eggs']['httponly'])
|
||||||
|
self.assertFalse(C['eggs']['secure'])
|
||||||
|
|
||||||
|
def test_secure_httponly_true_if_present(self):
|
||||||
|
# Issue 16611
|
||||||
|
C = cookies.SimpleCookie()
|
||||||
|
C.load('eggs=scrambled; httponly; secure; Path=/bacon')
|
||||||
|
self.assertTrue(C['eggs']['httponly'])
|
||||||
|
self.assertTrue(C['eggs']['secure'])
|
||||||
|
|
||||||
|
def test_secure_httponly_true_if_have_value(self):
|
||||||
|
# This isn't really valid, but demonstrates what the current code
|
||||||
|
# is expected to do in this case.
|
||||||
|
C = cookies.SimpleCookie()
|
||||||
|
C.load('eggs=scrambled; httponly=foo; secure=bar; Path=/bacon')
|
||||||
|
self.assertTrue(C['eggs']['httponly'])
|
||||||
|
self.assertTrue(C['eggs']['secure'])
|
||||||
|
# Here is what it actually does; don't depend on this behavior. These
|
||||||
|
# checks are testing backward compatibility for issue 16611.
|
||||||
|
self.assertEqual(C['eggs']['httponly'], 'foo')
|
||||||
|
self.assertEqual(C['eggs']['secure'], 'bar')
|
||||||
|
|
||||||
|
def test_bad_attrs(self):
|
||||||
|
# issue 16611: make sure we don't break backward compatibility.
|
||||||
|
C = cookies.SimpleCookie()
|
||||||
|
C.load('cookie=with; invalid; version; second=cookie;')
|
||||||
|
self.assertEqual(C.output(),
|
||||||
|
'Set-Cookie: cookie=with\r\nSet-Cookie: second=cookie')
|
||||||
|
|
||||||
|
def test_extra_spaces(self):
|
||||||
|
C = cookies.SimpleCookie()
|
||||||
|
C.load('eggs = scrambled ; secure ; path = bar ; foo=foo ')
|
||||||
|
self.assertEqual(C.output(),
|
||||||
|
'Set-Cookie: eggs=scrambled; Path=bar; secure\r\nSet-Cookie: foo=foo')
|
||||||
|
|
||||||
def test_quoted_meta(self):
|
def test_quoted_meta(self):
|
||||||
# Try cookie with quoted meta-data
|
# Try cookie with quoted meta-data
|
||||||
C = cookies.SimpleCookie()
|
C = cookies.SimpleCookie()
|
||||||
|
@ -66,6 +66,9 @@ Core and Builtins
|
|||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #16611: http.cookie now correctly parses the 'secure' and 'httponly'
|
||||||
|
cookie flags.
|
||||||
|
|
||||||
- Issue #11973: Fix a problem in kevent. The flags and fflags fields are now
|
- Issue #11973: Fix a problem in kevent. The flags and fflags fields are now
|
||||||
properly handled as unsigned.
|
properly handled as unsigned.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user