Skip to content

fix(python): follow RFC 6265 domain and path rules in the custom cookie jar - #636

Merged
B4nan merged 6 commits into
masterfrom
fix/python-cookie-domain
Nov 20, 2026
Merged

B4nan merged 6 commits into
masterfrom
fix/python-cookie-domain

Conversation

@B4nan

@B4nan B4nan commented Nov 20, 2026

Copy link
Copy Markdown
Member

The custom cookie jar in the Python bindings stored every Set-Cookie as it came in. It now follows the RFC 6265 domain rules: a cookie with a Domain is only stored when that domain matches the host that sent it, an empty Domain is ignored, and a cookie without Domain is only sent back to the host that set it, not to its subdomains.

A Domain made only of dots is ignored too. Public suffixes (com, co.uk, github.io) are handled the way browsers do it, using both sections of the Public Suffix List through the psl crate: such a cookie is dropped, unless it comes from the host of that exact name, where it is stored as host-only.

Cookie paths were compared as a plain string prefix, so a cookie for /foo was also sent to /foobar. They now have to match up to a segment boundary.

test_complex_cookies used Domain=.127.0.0.1 against httpbin, which is no longer stored, so it uses the httpbin host now.

A cookie with a Domain is stored with a leading dot, like http.cookiejar does it. File cookie jars such as MozillaCookieJar only keep domain_specified for those, so without the dot the cookie became host-only after a save and load.

A custom cookie jar received every `Set-Cookie` as it was. A cookie with a `Domain` attribute is now stored only when that domain matches the host that sent it, an empty `Domain` is ignored, and a cookie without `Domain` is sent back only to the host that set it, not to its subdomains.
A cookie path was compared as a plain string prefix, so a cookie for `/foo` was also sent to `/foobar`. The path now has to match up to a segment boundary, as described in RFC 6265.
A `Domain` of `..` was stored as `.` and matched every host, and a bare suffix like `com` was accepted from any host under it. Both are now ignored, the second the same way Python's `DefaultCookiePolicy` does it.
The dotless check let suffixes like `co.uk` or `github.io` through. The domain is now checked against the Public Suffix List instead.
A host whose name is itself a public suffix can set a cookie for that name. Browsers then store it as a host-only cookie, so it is not sent to the sites below it.
File cookie jars of `http.cookiejar` derive `domain_specified` from a leading dot when saving, so a cookie
with a `Domain` turned into a host-only one after a save and load.
@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 20, 2026
@B4nan
B4nan requested review from Pijukatel and barjin November 20, 2026 11:17

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

since my https://github.com/apify/impit-ghsa-g2m3-vx8j-88cw/pull/1 review has been addressed, I don't have anything to add.

thanks @B4nan !

@B4nan
B4nan merged commit e18213f into master Nov 20, 2026
35 checks passed
@B4nan
B4nan deleted the fix/python-cookie-domain 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