Skip to content

fix(python): honor Max-Age and apply the Secure and prefix rules when storing cookies - #633

Merged
B4nan merged 3 commits into
masterfrom
fix/python-cookie-storage-rules
Nov 21, 2026
Merged

B4nan merged 3 commits into
masterfrom
fix/python-cookie-storage-rules

Conversation

@B4nan

@B4nan B4nan commented Nov 19, 2026

Copy link
Copy Markdown
Member

Brings the custom cookie jar of the Python bindings closer to RFC 6265 and its current draft update when storing cookies:

  • Max-Age was ignored, so such cookies never expired and Max-Age=0 did not remove a cookie. It now takes precedence over Expires, and an already expired cookie removes the stored one, the same way CookieJar.extract_cookies does.
  • A Set-Cookie header that fails to parse was stored as a <cookie-name> placeholder cookie. It is skipped now.
  • A Secure cookie is only stored from HTTPS or a loopback host, and a response from anywhere else can't replace an existing one.
  • The __Secure- and __Host- name prefixes are checked.

… storing cookies

- `Max-Age` was ignored, so such cookies never expired and `Max-Age=0` did not remove a cookie. It now takes precedence over `Expires`, and an already expired cookie removes the stored one.
- A `Set-Cookie` header that fails to parse was stored as a placeholder cookie. It is skipped now.
- A `Secure` cookie is only stored from HTTPS or a loopback host, and a response from elsewhere can't replace one.
- The `__Secure-` and `__Host-` name prefixes are checked.
@B4nan B4nan added t-tooling Issues with this label are in the ownership of the tooling team. adhoc Ad-hoc unplanned task added during the sprint. labels Nov 19, 2026

@barjin barjin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @B4nan , there's some small nits (see below), but approving now, as it's not really blocking-level bad.

Once these fixes are merged and out, I'd love to simplify all this by delegating all the calls to the standard Python CookieJar implementations. Hopefully, that will save us quite a few LOCs.

Comment thread impit-python/src/cookies.rs Outdated
Comment on lines +294 to +295
&& attr("domain").eq_ignore_ascii_case(domain)
&& attr("path") == path

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the Domain and Path attributes should be compared using the Domain-Match and Path-Match algos.

Otherwise, HTTP servers can still store a cookie that already exists in the jar w/ Secure attribute (using, e.g., Path: "", while the original has Path: "/" or sth like that).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, it uses domain-match (either direction) and path-match now, with an empty path treated as /.

@B4nan
B4nan merged commit 0f429d0 into master Nov 21, 2026
30 checks passed
@B4nan
B4nan deleted the fix/python-cookie-storage-rules branch December 4, 2026 09:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

adhoc Ad-hoc unplanned task added during the sprint. t-tooling Issues with this label are in the ownership of the tooling team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants