Skip to content

fix(pdf): carry cookies across manual redirect hops - #2159

Merged
ntohidi merged 1 commit into
developfrom
fix/pdf-redirect-cookie-session
Aug 20, 2026
Merged

fix(pdf): carry cookies across manual redirect hops#2159
ntohidi merged 1 commit into
developfrom
fix/pdf-redirect-cookie-session

Conversation

@ntohidi

@ntohidi ntohidi commented Aug 20, 2026

Copy link
Copy Markdown
Collaborator

Regression from #2150, found while verifying that PR locally. Affects the library, not just the Docker server.

What broke

#2150 set allow_redirects=False and follows redirect hops by hand, so url_validator can vet each hop before it is fetched. Correct goal — but each hop calls a bare requests.get(), and every one of those starts with an empty cookie jar.

So a host that sets a cookie and then redirects never gets its own cookie back, and answers 403. That is the ordinary shape for gated and CDN-signed PDFs. It worked before #2150 because allow_redirects=True carried cookies for us.

Reproduced against a local server (302 + Set-Cookie, then a target that requires it):

tree result
fc6ec13 (before #2150) downloads the PDF
37ee60a (after #2150) RuntimeError: ... 403 Client Error: Forbidden

Fix

One requests.Session for the chain instead of a fresh requests.get per hop.

The SSRF guarantee is untouched: hops are still validated before the fetch, still not auto-followed, still capped. A Session only adds a cookie jar.

Testing

  • New test in tests/test_docker_pdf_crawler_pairing.py, reusing the existing redirect_server fixture with a /gated route.
  • Mutation check: reverting session.get to requests.get fails that test and nothing else.
  • deploy/docker/tests/test_security_*.py (the CI suite): 319 passed, 1 xfailed.
  • PDF tests: 9 passed.
  • tests/regression: 320 passed, 1 failed — test_soft_404_filters_probes, which hits a live site and fails identically on fc6ec13, so it is unrelated.

cc @SohamKukreti

#2150 switched the PDF download to allow_redirects=False and followed hops
by hand so url_validator can vet each one before it is fetched. Each hop
used a bare requests.get(), which starts with an empty cookie jar, so a
host that sets a cookie and then redirects never gets its own cookie back
and answers 403. That is the normal shape for gated and CDN-signed PDFs,
and it worked before #2150 because allow_redirects=True carried cookies
implicitly.

Use one requests.Session for the chain. The SSRF guarantee is unchanged:
hops are still validated before the fetch and still not auto-followed.

Verified by mutation: reverting session.get to requests.get fails the new
test and nothing else.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@ntohidi
ntohidi merged commit 133eff7 into develop Aug 20, 2026
1 check passed
@ntohidi
ntohidi deleted the fix/pdf-redirect-cookie-session branch August 20, 2026 09:23
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