Skip to content

Commit cd0f74b

Browse files
committed
#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.
1 parent f1fe159 commit cd0f74b

File tree

3 files changed

+61
-11
lines changed

3 files changed

+61
-11
lines changed

Lib/http/cookies.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,8 @@ class Morsel(dict):
338338
"version" : "Version",
339339
}
340340

341+
_flags = {'secure', 'httponly'}
342+
341343
def __init__(self):
342344
# Set defaults
343345
self.key = self.value = self.coded_value = None
@@ -435,15 +437,18 @@ def OutputString(self, attrs=None):
435437
(?P<key> # Start of group 'key'
436438
""" + _LegalCharsPatt + r"""+? # Any word of at least one letter
437439
) # End of group 'key'
438-
\s*=\s* # Equal Sign
439-
(?P<val> # Start of group 'val'
440-
"(?:[^\\"]|\\.)*" # Any doublequoted string
441-
| # or
440+
( # Optional group: there may not be a value.
441+
\s*=\s* # Equal Sign
442+
(?P<val> # Start of group 'val'
443+
"(?:[^\\"]|\\.)*" # Any doublequoted string
444+
| # or
442445
\w{3},\s[\w\d\s-]{9,11}\s[\d:]{8}\sGMT # Special case for "expires" attr
443-
| # or
444-
""" + _LegalCharsPatt + r"""* # Any word or empty string
445-
) # End of group 'val'
446-
\s*;? # Probably ending in a semi-colon
446+
| # or
447+
""" + _LegalCharsPatt + r"""* # Any word or empty string
448+
) # End of group 'val'
449+
)? # End of optional value group
450+
\s* # Any number of spaces.
451+
(\s+|;|$) # Ending either at space, semicolon, or EOS.
447452
""", re.ASCII) # May be removed if safe.
448453

449454

@@ -549,8 +554,12 @@ def __parse_string(self, str, patt=_CookiePattern):
549554
M[key[1:]] = value
550555
elif key.lower() in Morsel._reserved:
551556
if M:
552-
M[key] = _unquote(value)
553-
else:
557+
if value is None:
558+
if key.lower() in Morsel._flags:
559+
M[key] = True
560+
else:
561+
M[key] = _unquote(value)
562+
elif value is not None:
554563
rval, cval = self.value_decode(value)
555564
self.__set(key, rval, cval)
556565
M = self[key]

Lib/test/test_http_cookies.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,51 @@ def test_special_attrs(self):
109109
self.assertEqual(C.output(),
110110
'Set-Cookie: Customer="WILE_E_COYOTE"; Max-Age=10')
111111

112-
# others
112+
def test_set_secure_httponly_attrs(self):
113113
C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"')
114114
C['Customer']['secure'] = True
115115
C['Customer']['httponly'] = True
116116
self.assertEqual(C.output(),
117117
'Set-Cookie: Customer="WILE_E_COYOTE"; httponly; secure')
118118

119+
def test_secure_httponly_false_if_not_present(self):
120+
C = cookies.SimpleCookie()
121+
C.load('eggs=scrambled; Path=/bacon')
122+
self.assertFalse(C['eggs']['httponly'])
123+
self.assertFalse(C['eggs']['secure'])
124+
125+
def test_secure_httponly_true_if_present(self):
126+
# Issue 16611
127+
C = cookies.SimpleCookie()
128+
C.load('eggs=scrambled; httponly; secure; Path=/bacon')
129+
self.assertTrue(C['eggs']['httponly'])
130+
self.assertTrue(C['eggs']['secure'])
131+
132+
def test_secure_httponly_true_if_have_value(self):
133+
# This isn't really valid, but demonstrates what the current code
134+
# is expected to do in this case.
135+
C = cookies.SimpleCookie()
136+
C.load('eggs=scrambled; httponly=foo; secure=bar; Path=/bacon')
137+
self.assertTrue(C['eggs']['httponly'])
138+
self.assertTrue(C['eggs']['secure'])
139+
# Here is what it actually does; don't depend on this behavior. These
140+
# checks are testing backward compatibility for issue 16611.
141+
self.assertEqual(C['eggs']['httponly'], 'foo')
142+
self.assertEqual(C['eggs']['secure'], 'bar')
143+
144+
def test_bad_attrs(self):
145+
# issue 16611: make sure we don't break backward compatibility.
146+
C = cookies.SimpleCookie()
147+
C.load('cookie=with; invalid; version; second=cookie;')
148+
self.assertEqual(C.output(),
149+
'Set-Cookie: cookie=with\r\nSet-Cookie: second=cookie')
150+
151+
def test_extra_spaces(self):
152+
C = cookies.SimpleCookie()
153+
C.load('eggs = scrambled ; secure ; path = bar ; foo=foo ')
154+
self.assertEqual(C.output(),
155+
'Set-Cookie: eggs=scrambled; Path=bar; secure\r\nSet-Cookie: foo=foo')
156+
119157
def test_quoted_meta(self):
120158
# Try cookie with quoted meta-data
121159
C = cookies.SimpleCookie()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ Core and Builtins
6666
Library
6767
-------
6868

69+
- Issue #16611: http.cookie now correctly parses the 'secure' and 'httponly'
70+
cookie flags.
71+
6972
- Issue #11973: Fix a problem in kevent. The flags and fflags fields are now
7073
properly handled as unsigned.
7174

0 commit comments

Comments
 (0)