Repository navigation
Give Max-Age precedence over Expires (RFC 6265 4.1.2.2) - #8415
Open
dylanpulver wants to merge 1 commit into
Open
dylanpulver wants to merge 1 commit into
dylanpulver wants to merge 1 commit into
Conversation
get_expiration_ts checked Expires first and only reached Max-Age in an elif, so whenever a Set-Cookie carried both attributes the Max-Age branch was unreachable. RFC 6265 4.1.2.2 says the opposite: "If a cookie has both the Max-Age and the Expires attribute, the Max-Age attribute has precedence and controls the expiration date of the cookie." Section 5.3 step 3 states the same ordering normatively. The visible consequence is in the stickycookie addon, the only consumer of is_expired(): a server deleting a cookie with `Max-Age=0` while leaving the original far-future Expires in place was judged not expired, so the jar kept the value and replayed it on later requests. The two branches are now independent ifs rather than if/elif, so a Max-Age that cannot be parsed falls back to Expires instead of discarding both. Second defect in the same statement: _read_set_cookie_pairs stores None for an attribute with no value, and int(None) raises TypeError, which the bare `except ValueError` did not catch. `Set-Cookie: foo=bar; Max-Age` therefore raised out of is_expired(). TypeError is now caught. The lookup also used the key "Max-Age" while the guard tested "max-age"; both now use "max-age".
dylanpulver
force-pushed
the
cookie-max-age-precedence
branch
from
September 10, 2026 15:25
4767d8c to
284e14c
Compare
2 tasks done
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
get_expiration_ts()checksExpiresfirst and only reachesMax-Agein anelif, so on anySet-Cookiecarrying both attributes theMax-Agebranch is unreachable. RFC 6265 has it the other way round.RFC 6265 §4.1.2.2: "If a cookie has both the Max-Age and the Expires attribute, the Max-Age attribute has precedence and controls the expiration date of the cookie."
Mechanism:
mitmproxy/net/http/cookies.py:331-341. The only consumer isis_expired()→mitmproxy/addons/stickycookie.py:69, so with--stickycookiea server that deletes a cookie by re-sending it withMax-Age=0(while leaving the original far-futureExpiresin place — the common delete idiom) is judged not expired. The jar keeps the value and replays it on subsequent requests.Measured on
main, driving realSet-Cookieheaders throughparse_set_cookie_header+is_expired; same script, same env, before and after:The fourth line is the one the module already knows about: the comment at
cookies.py:312-318records that real sites send unparseableexpiresvalues, and withelifan unparseableExpiresreturnsNonewithout ever looking at a perfectly goodMax-Age. The two branches are now independentifs.Second defect, same statement.
_read_set_cookie_pairsstoresNonefor an attribute with no value (cookies.py:174-175), andint(None)raisesTypeError, which the bareexcept ValueErrordoes not catch. SoSet-Cookie: foo=bar; Max-Ageraises out ofis_expired()and takes the addon hook with it.TypeErroris now caught. The lookup also readcookie_attrs["Max-Age"]while the guard tested"max-age"; that only works becauseCookieAttrs._kconvlowercases, so both now use"max-age".Why the tests did not catch it
test_is_expiredhas two cases with both attributes present, and both agree under either precedence order (Expiresin the past +Max-Age=0;Expiresin 2133 +Max-Age=1).test_get_expiration_tsnever sets both at once.grep -rn "Max-Age" test/ | grep -i expiresreturns only those two lines. The added assertions are all disagreeing pairs.Mutants
test_get_expiration_tsfailsassert 5164128000 == 17,test_is_expiredfails the delete-me case.elifandexcept ValueError): still fails, onF(Max-Age=nan, Expires=<past>) == 0→assert None == 0, and on the valueless-Max-Agecase withTypeError. So the tests pin the fall-through structure and the exception type, not just the ordering.except ValueErroronly: fails only the valueless-Max-Ageassertions, isolating that line.What I ran
uv run pytestfull suite: 2000 passed, 5 skipped — identical tomain, i.e. the behaviour flips four ways and no existing test notices.uv run tox -e lintanduv run ruff format --check: clean.Not tested: no end-to-end proxy run against a live site, and no Windows/macOS run — Linux/macOS
pytestonly. I did not touchrefresh_set_cookie_header, which deletes an unparseableexpiresoutright; that looks deliberate per its comment.Not from a user report; found auditing cookie handling against RFC 6265. AI assistance: this change was written with an AI coding assistant.
Checklist