Skip to content

Give Max-Age precedence over Expires (RFC 6265 4.1.2.2) - #8415

Open
dylanpulver wants to merge 1 commit into
mitmproxy:mainfrom
dylanpulver:cookie-max-age-precedence
Open

dylanpulver wants to merge 1 commit into
mitmproxy:mainfrom
dylanpulver:cookie-max-age-precedence

Conversation

@dylanpulver

@dylanpulver dylanpulver commented Sep 1, 2026 •

Copy link
Copy Markdown

Description

get_expiration_ts() checks Expires first and only reaches Max-Age in an elif, so on any Set-Cookie carrying both attributes the Max-Age branch 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 is is_expired() → mitmproxy/addons/stickycookie.py:69, so with --stickycookie a server that deletes a cookie by re-sending it with Max-Age=0 (while leaving the original far-future Expires in place — the common delete idiom) is judged not expired. The jar keeps the value and replays it on subsequent requests.

Measured on main, driving real Set-Cookie headers through parse_set_cookie_header + is_expired; same script, same env, before and after:

before                                                              after
is_expired("Expires=Mon, 24-Aug-2133...; Max-Age=0")     False       True    <- RFC: True
is_expired("Max-Age=0; Expires=Mon, 24-Aug-2133...")     False       True    <- RFC: True
is_expired("Expires=Thu, 01-Jan-1970...; Max-Age=99999") True        False   <- RFC: False
is_expired("Expires=totally-bogus; Max-Age=0")           False       True    <- RFC: True
is_expired("Expires=Thu, 01-Jan-1970...")   (control)    True        True
is_expired("Max-Age=0")                     (control)    True        True

The fourth line is the one the module already knows about: the comment at cookies.py:312-318 records that real sites send unparseable expires values, and with elif an unparseable Expires returns None without ever looking at a perfectly good Max-Age. The two branches are now independent ifs.

Second defect, same statement. _read_set_cookie_pairs stores None for an attribute with no value (cookies.py:174-175), and int(None) raises TypeError, which the bare except ValueError does not catch. So Set-Cookie: foo=bar; Max-Age raises out of is_expired() and takes the addon hook with it. TypeError is now caught. The lookup also read cookie_attrs["Max-Age"] while the guard tested "max-age"; that only works because CookieAttrs._kconv lowercases, so both now use "max-age".

Why the tests did not catch it

test_is_expired has two cases with both attributes present, and both agree under either precedence order (Expires in the past + Max-Age=0; Expires in 2133 + Max-Age=1). test_get_expiration_ts never sets both at once. grep -rn "Max-Age" test/ | grep -i expires returns only those two lines. The added assertions are all disagreeing pairs.

Mutants

  • A — revert the source, keep the new tests: test_get_expiration_ts fails assert 5164128000 == 17, test_is_expired fails the delete-me case.
  • B — the naive fix (swap the order, keep elif and except ValueError): still fails, on F(Max-Age=nan, Expires=<past>) == 0 → assert None == 0, and on the valueless-Max-Age case with TypeError. So the tests pin the fall-through structure and the exception type, not just the ordering.
  • C — correct order and fall-through, but except ValueError only: fails only the valueless-Max-Age assertions, isolating that line.

What I ran

uv run pytest full suite: 2000 passed, 5 skipped — identical to main, i.e. the behaviour flips four ways and no existing test notices. uv run tox -e lint and uv run ruff format --check: clean.

Not tested: no end-to-end proxy run against a live site, and no Windows/macOS run — Linux/macOS pytest only. I did not touch refresh_set_cookie_header, which deletes an unparseable expires outright; 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

  • I have updated tests where applicable.
  • I have added an entry to the CHANGELOG.

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".

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant