Fix Set-Cookie attribute parsing for short Expires values (#8489) · mitmproxy/mitmproxy@c8a55ff · GitHub
Skip to content

Commit c8a55ff

Browse files
Fix Set-Cookie attribute parsing for short Expires values (#8489)
* Fix Set-Cookie attribute parsing for short Expires values Fixes #8488 * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent f9c59ef commit c8a55ff

3 files changed

Lines changed: 66 additions & 9 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions

‎mitmproxy/net/http/cookies.py‎

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,24 @@ def _read_cookie_pairs(s, off=0):
136136
return pairs, off
137137

138138

139+
_WEEKDAYS = {
140+
"mon",
141+
"tue",
142+
"wed",
143+
"thu",
144+
"fri",
145+
"sat",
146+
"sun",
147+
"monday",
148+
"tuesday",
149+
"wednesday",
150+
"thursday",
151+
"friday",
152+
"saturday",
153+
"sunday",
154+
}
155+
156+
139157
def _read_set_cookie_pairs(s: str, off=0) -> tuple[list[TPairs], int]:
140158
"""
141159
Read pairs of lhs=rhs values from SetCookie headers while handling multiple cookies.
@@ -156,15 +174,11 @@ def _read_set_cookie_pairs(s: str, off=0) -> tuple[list[TPairs], int]:
156174

157175
# Special handling of attributes
158176
if lhs.lower() == "expires":
159-
# 'expires' values can contain commas in them so they need to
160-
# be handled separately.
161-
162-
# We actually bank on the fact that the expires value WILL
163-
# contain a comma. Things will fail, if they don't.
164-
165-
# '3' is just a heuristic we use to determine whether we've
166-
# only read a part of the expires value and we should read more.
167-
if len(rhs) <= 3:
177+
# 'expires' date values can contain a comma after the weekday
178+
# (e.g. RFC 1123 'Wed, ...' or RFC 850 'Wednesday, ...').
179+
# We only consume across a comma if the delimiter encountered
180+
# is actually a comma and the value read so far is a weekday.
181+
if off < len(s) and s[off] == "," and rhs.strip().lower() in _WEEKDAYS:
168182
trail, off = _read_value(s, off + 1, ";,")
169183
rhs = rhs + "," + trail
170184

‎test/mitmproxy/net/http/test_cookies.py‎

Lines changed: 41 additions & 0 deletions

0 commit comments

Comments
 (0)