Skip to content

Commit 5f21f43

Browse files
committed
python#22758: fix regression in handling of secure cookies.
This backports the fix from python#16611, per discussion with the release manager.
1 parent 035583b commit 5f21f43

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
@@ -437,15 +439,18 @@ def OutputString(self, attrs=None):
437439
(?P<key> # Start of group 'key'
438440
[""" + _LegalKeyChars + r"""]+? # Any word of at least one letter
439441
) # End of group 'key'
440-
\s*=\s* # Equal Sign
441-
(?P<val> # Start of group 'val'
442-
"(?:[^\\"]|\\.)*" # Any doublequoted string
443-
| # or
442+
( # Optional group: there may not be a value.
443+
\s*=\s* # Equal Sign
444+
(?P<val> # Start of group 'val'
445+
"(?:[^\\"]|\\.)*" # Any doublequoted string
446+
| # or
444447
\w{3},\s[\w\d\s-]{9,11}\s[\d:]{8}\sGMT # Special case for "expires" attr
445-
| # or
446-
[""" + _LegalValueChars + r"""]* # Any word or empty string
447-
) # End of group 'val'
448-
\s*;? # Probably ending in a semi-colon
448+
| # or
449+
[""" + _LegalValueChars + r"""]* # Any word or empty string
450+
) # End of group 'val'
451+
)? # End of optional value group
452+
\s* # Any number of spaces.
453+
(\s+|;|$) # Ending either at space, semicolon, or EOS.
449454
""", re.ASCII) # May be removed if safe.
450455

451456

@@ -551,8 +556,12 @@ def __parse_string(self, str, patt=_CookiePattern):
551556
M[key[1:]] = value
552557
elif key.lower() in Morsel._reserved:
553558
if M:
554-
M[key] = _unquote(value)
555-
else:
559+
if value is None:
560+
if key.lower() in Morsel._flags:
561+
M[key] = True
562+
else:
563+
M[key] = _unquote(value)
564+
elif value is not None:
556565
rval, cval = self.value_decode(value)
557566
self.__set(key, rval, cval)
558567
M = self[key]

Lib/test/test_http_cookies.py

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

117-
# others
117+
def test_set_secure_httponly_attrs(self):
118118
C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"')
119119
C['Customer']['secure'] = True
120120
C['Customer']['httponly'] = True
121121
self.assertEqual(C.output(),
122122
'Set-Cookie: Customer="WILE_E_COYOTE"; httponly; secure')
123123

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

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ Tests
2828
self-signed.pythontest.net. This avoids relying on svn.python.org, which
2929
recently changed root certificate.
3030

31+
- Issue #22758: Fix a regression that no longer allowed secure and httponly
32+
cookies to be parsed.
33+
3134

3235
What's New in Python 3.2.6?
3336
===========================

0 commit comments

Comments
 (0)