Skip to content

fix(download): scope browser session cookies to the fetch target origin - #124

Open
Xtacy856X wants to merge 3 commits into
eellak:masterfrom
Xtacy856X:fix/121-origin-scoped-browser-cookies
Open

Xtacy856X wants to merge 3 commits into
eellak:masterfrom
Xtacy856X:fix/121-origin-scoped-browser-cookies

Conversation

@Xtacy856X

Copy link
Copy Markdown

Problem

_bootstrap_browser_session_state() collects cookies with context.cookies() — every cookie in the browser context, whatever origin set it — and packs them all into one Cookie header stored in BrowserSessionState. When the first accepted response lives on a different origin than the page that produced it, _fetch_with_browser_session_state() re-fetches the candidate through aiohttp with that full header, so cookies belonging to the source host get disclosed to the candidate host. Closes #121 (the issue's reporter reproduced this locally with a real Playwright browser and two HTTP hosts).

Approach

BrowserSessionState now keeps the raw cookie list instead of a pre-joined header, and the header is built per request URL by a new _cookie_header_for_url() with RFC 6265-style matching:

  • domain cookies (leading dot in Playwright's cookie dict) match the domain and its subdomains; host-only cookies match the exact host only
  • cookie path must prefix-match the URL path
  • cookies flagged secure are skipped for non-HTTPS fetches

The session cache is unaffected: state is still cached per domain key, and the filtering happens at fetch time so a reused session also gets origin-scoped cookies per URL.

Tests

Four new tests in tests/test_browser_gloss_downloader.py covering the issue scenario (a source-host cookie is not forwarded to a cross-origin candidate, and a CDN-origin cookie still is), domain vs host-only matching, the secure flag over HTTP vs HTTPS, and cookie path scoping. The existing cached-session test was updated for the new cookies field. Verified the tests bite: with the scoping temporarily disabled they all fail. tests/test_browser_gloss_downloader.py: 21 passed. tests/test_corpus_phases_lightweight.py unchanged from baseline (the remaining failures are Windows-local environment issues: ledger/writer.py does a module-level import fcntl and os.open() on a directory, both POSIX-only — reported separately from this PR's scope).

Risk and exclusions

  • Any cookie whose Playwright record lacks a domain is now dropped rather than sent anywhere; previously such a cookie would have been forwarded to every refetch, which is exactly the behavior under discussion.
  • Other refetch paths that already reuse _fetch_with_browser_session_state get the scoped header automatically; no public Corpus API changed.

Browser bootstrap collected every cookie in the Playwright context and
sent the joined header to whichever candidate URL was refetched, so a
cross-origin candidate could receive the source host's session cookies.
BrowserSessionState now stores the raw cookie list and the header is
built per URL with RFC 6265-style domain/path/secure matching.

Closes eellak#121
@Princess0407

Princess0407 commented Nov 21, 2026 •

Copy link
Copy Markdown

Hello @Xtacy856X !!
I actually went through the implementation and the originally raised host only cookie disclosure scenario looks addressed. Good approach keeping the cookie records structured and filtering them at fetch time. However I noticed two edge cases which are worth considering I think!!
Currently, _cookie_header_for_url() forwards domain cookies to matching subdomains. Like for example, a cookie with Domain=.example.org will be included when the candidate is cdn.example.org.So, if the candidate response originated from a cross origin fetch() using the default credentials: "same-origin" mode, the browser would not have sent that cookie in the first place, even though the domain matches....since, the refetch reconstructs the Cookie header from the entire context rather than the actual candidate request, could this potentially preserve a similar disclosure path for sibling subdomains? You can try considering the cookies actually sent with the recorded browser request, or otherwise making the intended cross origin credential policy explicit ig. A regression case involving www.example.org and cdn.example.org might help clarify the expected behaviour.

@Princess0407

Copy link
Copy Markdown

andd, one more smaller edge case in the path matcher:
The current path.startswith(cookie_path) check would allow a cookie scoped to /download to match /download-private/file.pdf as well. RFC 6265 path matching requires either an exact match, a cookie path ending in /, or a / boundary immediately after the matching prefix...try tightening this check and add a small regression test for that boundary case. That would make the RFC 6265 style matching claim more precise I feel.

A cookie scoped to `/download` was forwarded to `/download-private/file.pdf`
because the scope check was a bare prefix test. RFC 6265 section 5.1.4 wants an
exact match, a cookie path ending in `/`, or a `/` boundary right after the
matching prefix.
The refetch rebuilt the Cookie header from the whole browser context, so a
`Domain=.example.org` cookie still reached `cdn.example.org` even when the
browser's own same-origin request carried no cookie at all. Browser bootstrap
now records the Cookie header of each candidate request through Playwright's
`all_headers()` (context cookies never appear in `request.headers`) and the
refetch replays it verbatim; RFC 6265 matching stays only as the fallback when
nothing was captured.
@Xtacy856X

Copy link
Copy Markdown
Author

Heyy @Princess0407, thanks for digging in, both of those were real.

The path one was a plain bug: startswith let a /download cookie ride along to /download-private/file.pdf, so it now does the RFC 6265 5.1.4 matching (exact match, cookie path ending in /, or / right after the prefix) with a test for that boundary.

Your first point I fixed at the root instead of adding another filter. The bootstrap now keeps the Cookie header the browser actually sent on each candidate request and the refetch replays it as-is, so a Domain=.example.org cookie doesn't turn up at cdn.example.org when the browser's own same-origin request didn't carry it. Context cookies don't show up in request.headers, they're only in all_headers(), that was the fiddly bit. The RFC matching stays as the fallback for cached sessions where nothing was captured.

Both cases have a regression test now. I ran the downloader tests locally (24 passed, 3 failures that are the POSIX-only directory lock tests on Windows, same three on a clean checkout); the workflows on this PR are sitting at action_required, so CI hasn't picked the new commits up yet.

@Princess0407

Copy link
Copy Markdown

Heyy @Xtacy856X again!!
that's really nice of you! I went through your explanation, and I especially like that you addressed the subdomain case at the abstraction level!! The all_headers() detail is interesting too. I hadn't considered that distinction when I was looking at the Playwright request API and the passing regression coverage is a huge plus. Really appreciate the collaboration! :)) Now let's wait for the maintainer's review!!

@Xtacy856X

Copy link
Copy Markdown
Author

@Princess0407 The pleasure is all mine! Greetings from Bologna

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.

[Security]: Potential cross origin cookie disclosure in browser assisted downloads

2 participants