Backtestspydantic-airun-20260918-215936#6936
safe_download can replay server-set cookies across logical hosts
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Patch B implements exactly the chosen fix: a logical-URL cookie jar (httpx2.Cookies with a custom DefaultCookiePolicy subclass), host-only matching, PSL-aware Domain= handling via _LogicalCookiePolicy.set_ok_domain + _is_public_suffix, extract/apply against the validated logical URL, and clearing the client's resolved-IP jar after every response, with caller Cookie precedence preserved. Crucially it is written consistently against httpx2 — the actual client the codebase uses (create_async_httpx2_client, httpx2.Response, httpx2.Cookies) — so the cookie management path actually runs in production, and its tests monkeypatch the correct create_async_httpx2_client with a real httpx2.AsyncClient+MockTransport. Its test suite covers every boundary the issue enumerates: cross-host non-replay (primary), same-host retained, domain/subdomain shared, public-suffix tenant refused, Secure not sent over http, and caller-cookie precedence — tests that fail before and pass after. Patch A is both incomplete and likely broken. It ignores the required PSL-aware Domain handling entirely, relying on stdlib DefaultCookiePolicy's weak heuristics, and omits Secure/path/domain coverage. More seriously it conflates two distinct modules: the code guards cookie handling with isinstance(response, httpx.Response) while the client is httpx2 (create_async_httpx2_client), so responses are httpx2.Response and the isinstance check is False — the entire cookie extract/clear block becomes a no-op in production, leaving the issue unfixed. It also never imports httpx. Its tests monkeypatch 'create_async_http_client' (wrong name; the code calls create_async_httpx2_client) and build httpx (not httpx2) MockTransports, so monkeypatch.setattr would raise AttributeError or fail to patch the real client — the tests don't actually exercise the fix. Patch A is smaller but only because it does less and does it wrongly; Patch B's extra size (PSL list, policy, helpers) is demanded by the chosen fix. B wins decisively on correctness, tests, and craft.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
32m 14s28 turns
Wall-clock time in the container
Full test suite
No new failures
11,777 passed · 574 skipped · 12,593 total
Contamination audit
Unknown
See the audit notes for this run
Patch size
312vs 152 merged
Changed lines, ours vs merged
The issue
View on GitHubDescription
safe_download connects to a resolved IP while preserving the logical hostname in Host and SNI. HTTPX therefore stores response cookies against the IP request URL. If a redirect target has a different hostname on the same IP, the cookie jar can replay the first host cookie to the second host.
A probe with a real httpx2.AsyncClient and MockTransport reproduced this. This is separate from #6810, which covers caller-configured headers.
Expected behavior
Server-set cookies must follow logical URL cookie semantics rather than resolved-IP identity. Cookies must not cross host, domain, public-suffix tenant, path, or Secure boundaries, while valid same-host and domain/subdomain redirects must continue to work. Cookies are not isolated by port, and non-Secure cookies are not isolated by scheme.
Chosen fix
Use a logical-URL cookie jar inside safe_download, with strict host-only matching and Public Suffix List-aware Domain= handling. Apply and extract cookies against the validated logical URL, then clear HTTPX resolved-IP jar after every response. Caller-supplied Cookie headers retain precedence and the existing redirect stripping policy.
The two patches
pydantic_ai_slim/pydantic_ai/_ssrf.pyindex 20dad3f8..f6b14368 100644@@ -12,6 +12,7 @@ import socket import zlib from collections.abc import AsyncIterator from dataclasses import dataclass+from http import cookiejar from urllib.parse import urlparse, urlunparse import httpx2@@ -534,6 +535,97 @@ def _keeps_credentials(from_url: str, to_url: str) -> bool: ) +# A pragmatic subset of the Public Suffix List: multi-label suffixes under which distinct+# registrants live. Single-label suffixes (TLDs) are handled algorithmically in+# `_is_public_suffix`. The full PSL cannot be vendored here; see `_is_public_suffix`.+_PUBLIC_SUFFIXES: frozenset[str] = frozenset(+ (+ 'co.uk', 'org.uk', 'me.uk', 'ltd.uk', 'plc.uk', 'net.uk', 'sch.uk', 'ac.uk', 'gov.uk', 'nhs.uk',+ 'com.au', 'net.au', 'org.au', 'edu.au', 'gov.au', 'asn.au', 'id.au',+ 'co.jp', 'or.jp', 'ne.jp', 'ac.jp', 'ad.jp', 'ed.jp', 'go.jp', 'gr.jp', 'lg.jp',+ 'co.nz', 'net.nz', 'org.nz', 'govt.nz', 'ac.nz', 'geek.nz', 'gen.nz', 'school.nz',+ 'com.br', 'net.br', 'org.br', 'gov.br', 'edu.br',+ 'co.za', 'org.za', 'net.za', 'gov.za', 'ac.za', 'web.za',+ 'co.in', 'net.in', 'org.in', 'gen.in', 'firm.in', 'ind.in', 'gov.in', 'ac.in', 'edu.in', 'res.in',+ 'com.cn', 'net.cn', 'org.cn', 'gov.cn', 'edu.cn', 'ac.cn',+ 'co.kr', 'ne.kr', 'or.kr', 're.kr', 'pe.kr', 'go.kr', 'mil.kr', 'ac.kr',+ 'com.mx', 'com.ar', 'com.tr', 'com.sg', 'com.hk', 'com.tw', 'com.my', 'com.ph', 'com.pl',+ 'com.ua', 'com.co', 'co.il', 'co.id', 'co.th', 'co.ke',+ )+)+++def _is_public_suffix(domain: str) -> bool:+ """Whether `domain` names a public suffix under which distinct registrants live.++ A `Domain=` cookie scoped to such a suffix would leak between unrelated tenants (e.g.+ every `*.co.uk` site), so it must be refused. Single-label suffixes (TLDs like `com`,+ `uk`) are always public; a curated set of common multi-label suffixes is also+ recognized. This is a deliberate subset of the Public Suffix List, which cannot be+ vendored here: the host-only default already confines a cookie that carries no+ `Domain=`, so an unlisted multi-label suffix can at worst share a cookie one registrar+ level too widely — never across the exact-host boundary this change primarily closes.+ """+ domain = domain.lower().strip('.')+ if '.' not in domain:+ return True+ return domain in _PUBLIC_SUFFIXES+++class _LogicalCookiePolicy(cookiejar.DefaultCookiePolicy):+ """Cookie policy for the logical-URL jar `safe_download` keeps across redirect hops.++ It keeps `DefaultCookiePolicy`'s host-only and domain-match rules — a cookie with no+ `Domain=` binds to the exact host, a `Domain=` cookie is shared with that domain and+ its subdomains — but additionally refuses a `Domain=` that names a public suffix, so a+ cookie cannot be scoped to a whole public-suffix tenant.+ """++ def set_ok_domain(self, cookie: cookiejar.Cookie, request: object) -> bool:+ if cookie.domain_specified and _is_public_suffix(cookie.domain):+ return False+ return super().set_ok_domain(cookie, request)+++def _new_logical_cookie_jar() -> httpx2.Cookies:+ """A cookie jar that applies logical-URL semantics with the public-suffix guard."""+ cookies = httpx2.Cookies()+ cookies.jar.set_policy(_LogicalCookiePolicy())+ return cookies+++def _server_set_cookies(response: httpx2.Response) -> list[str]:+ """The `Set-Cookie` values a response carried, tolerant of the mock header shapes in tests."""+ headers = response.headers+ if isinstance(headers, httpx2.Headers):+ return headers.get_list('set-cookie')+ if isinstance(headers, dict):+ for key, value in headers.items():+ if key.lower() == 'set-cookie':+ return [value]+ return []+++def _store_logical_cookies(cookies: httpx2.Cookies, logical_url: str, response: httpx2.Response) -> None:+ """Record a response's `Set-Cookie`s in `cookies` keyed on the logical URL, not the resolved IP."""+ set_cookies = _server_set_cookies(response)+ if not set_cookies:+ return+ logical_response = httpx2.Response(+ 302,+ headers=[('set-cookie', value) for value in set_cookies],+ request=httpx2.Request('GET', logical_url),+ )+ cookies.extract_cookies(logical_response)+++def _logical_cookie_header(cookies: httpx2.Cookies, logical_url: str) -> str | None:+ """The `Cookie` header the logical-URL jar would send to `logical_url`, if any."""+ cookie_request = httpx2.Request('GET', logical_url)+ cookies.set_cookie_header(cookie_request)+ return cookie_request.headers.get('cookie')++ async def safe_download( url: str, allow_local: bool = False,@@ -598,6 +690,11 @@ async def safe_download( redirects_followed = 0 effective_headers: dict[str, str] = dict(headers) if headers else {} + # Server-set cookies must follow logical-URL cookie semantics rather than the+ # resolved-IP identity httpx keys on, so `safe_download` keeps its own jar across hops+ # and never lets the client replay a cookie to a different host on the same IP.+ logical_cookies = _new_logical_cookie_jar()+ async with create_async_httpx2_client(timeout=timeout) as client: while True: # Validate and resolve the current URL@@ -632,11 +729,27 @@ async def safe_download( if max_bytes is not None and not any(k.lower() == 'accept-encoding' for k in request_headers): request_headers['Accept-Encoding'] = _BOUNDED_ACCEPT_ENCODING + # Apply server-set cookies against the validated logical URL. A caller-supplied+ # `Cookie` header keeps precedence (and is governed by the redirect stripping policy),+ # so only fill one in when the caller left it unset.+ if not any(k.lower() == 'cookie' for k in request_headers):+ cookie_header = _logical_cookie_header(logical_cookies, current_url)+ if cookie_header:+ request_headers['Cookie'] = cookie_header+ # Stream the raw response so gzip members can be decoded and validated before # httpx2's automatic content decoder discards member boundaries. request = client.build_request('GET', request_url, headers=request_headers, extensions=extensions) response = await _send_request(client, request) + # Capture Set-Cookie against the logical URL, then wipe httpx's resolved-IP jar so it+ # can never replay a cookie to a different logical host that shares the IP. The+ # isinstance guard keeps this a no-op against the client mocks used in tests, which+ # carry no real jar (matching `_server_set_cookies`'s mock tolerance).+ _store_logical_cookies(logical_cookies, current_url, response)+ if isinstance(client.cookies, httpx2.Cookies):+ client.cookies.clear()+ # Check if we need to follow a redirect if response.is_redirect: await response.aclose()tests/test_ssrf.pyindex 35df1d00..cba03d67 100644@@ -1705,6 +1705,205 @@ class TestSensitiveHeaderStrippingOnRedirects: assert self._header(sent[2], 'authorization') is None +class TestServerSetCookieIsolation:+ """Tests that a server `Set-Cookie` handed back during a redirect follows logical-URL+ cookie semantics instead of being replayed to any host that resolves to the same IP.++ `safe_download` connects to a resolved IP while carrying the logical hostname in the+ `Host` header and TLS SNI. httpx2 keys its cookie jar on the *request* URL (the+ resolved-IP URL), so it sees every hop as the same origin — the shared IP — and would+ replay a cookie set by host A to a different host B that resolves to the same address.+ Server-set cookies must instead honor the boundaries a logical-URL cookie jar enforces:+ they must not cross a host, a public-suffix tenant, or the Secure/https line, while a+ same-host redirect and a domain/subdomain redirect keep sharing them, and a+ caller-supplied `Cookie` header keeps precedence over anything a server sets.+ See https://github.com/pydantic/pydantic-ai/issues/6936.++ These drive a *real* `httpx2.AsyncClient` over a `MockTransport` rather than a bare+ mock, because the leak is a property of the client's own cookie handling and only the+ genuine client reproduces it. Every hostname resolves to one public IP, so each hop+ reaches the transport as a request to that IP and the hops are told apart by the+ logical `Host` header (and scheme) each request carries.+ """++ @staticmethod+ def _sent_cookie(request: httpx2.Request) -> str | None:+ """Case-insensitive lookup of the `Cookie` header a request actually carried."""+ return next((v for k, v in request.headers.items() if k.lower() == 'cookie'), None)++ @pytest.fixture+ def serve_by_host(+ self, mock_dns: AsyncMock, monkeypatch: pytest.MonkeyPatch+ ) -> Callable[[RequestHandler], None]:+ """Wire a real `httpx2.AsyncClient` + `MockTransport` into `safe_download`.++ Resolves every hostname to the same public IP, so each hop arrives at the handler+ as a request to that IP distinguished only by the logical `Host` header (and scheme).+ """++ def install(handler: RequestHandler) -> None:+ mock_dns.return_value = [(2, 1, 6, '', ('93.184.215.14', 0))]+ client = httpx2.AsyncClient(transport=httpx2.MockTransport(handler))++ def create_http_client(*, timeout: int) -> httpx2.AsyncClient:+ return client++ monkeypatch.setattr('pydantic_ai._ssrf.create_async_httpx2_client', create_http_client)++ return install++ async def test_set_cookie_not_replayed_across_host_boundary(+ self, serve_by_host: Callable[[RequestHandler], None]+ ) -> None:+ """PRIMARY regression: a cookie set by `a.com` must not reach `b.com`.++ Both hosts resolve to one IP, so httpx2's request-URL-keyed jar replays+ `session=leak` onto the cross-host hop; the logical-URL rule must withhold it.+ """+ sent: list[str | None] = []++ def handle(request: httpx2.Request) -> httpx2.Response:+ sent.append(self._sent_cookie(request))+ if request.headers['host'] == 'a.com':+ return httpx2.Response(+ 302,+ headers={'location': 'https://b.com/file', 'set-cookie': 'session=leak; Path=/'},+ request=request,+ )+ return httpx2.Response(200, content=b'ok', request=request)++ serve_by_host(handle)++ await safe_download('https://a.com/file')++ # The second request (Host b.com) must not carry the cookie a.com set.+ assert sent[1] is None++ async def test_same_host_redirect_keeps_server_cookie(+ self, serve_by_host: Callable[[RequestHandler], None]+ ) -> None:+ """A same-host redirect still replays the server's cookie to the next hop."""+ sent: list[str | None] = []++ def handle(request: httpx2.Request) -> httpx2.Response:+ sent.append(self._sent_cookie(request))+ if request.url.path == '/file':+ return httpx2.Response(+ 302,+ headers={'location': 'https://a.com/other', 'set-cookie': 'session=keep; Path=/'},+ request=request,+ )+ return httpx2.Response(200, content=b'ok', request=request)++ serve_by_host(handle)++ await safe_download('https://a.com/file')++ assert sent[1] == 'session=keep'++ async def test_domain_cookie_shared_with_subdomain(+ self, serve_by_host: Callable[[RequestHandler], None]+ ) -> None:+ """A `Domain=example.com` cookie set by `sub.example.com` is shared with `www.example.com`."""+ sent: list[str | None] = []++ def handle(request: httpx2.Request) -> httpx2.Response:+ sent.append(self._sent_cookie(request))+ if request.headers['host'] == 'sub.example.com':+ return httpx2.Response(+ 302,+ headers={+ 'location': 'https://www.example.com/file',+ 'set-cookie': 'session=ok; Domain=example.com; Path=/',+ },+ request=request,+ )+ return httpx2.Response(200, content=b'ok', request=request)++ serve_by_host(handle)++ await safe_download('https://sub.example.com/file')++ assert sent[1] == 'session=ok'++ async def test_public_suffix_cookie_not_shared_across_tenants(+ self, serve_by_host: Callable[[RequestHandler], None]+ ) -> None:+ """A cookie scoped to a public suffix (`co.uk`) must not cross to another tenant."""+ sent: list[str | None] = []++ def handle(request: httpx2.Request) -> httpx2.Response:+ sent.append(self._sent_cookie(request))+ if request.headers['host'] == 'a.co.uk':+ return httpx2.Response(+ 302,+ headers={+ 'location': 'https://b.co.uk/file',+ 'set-cookie': 'session=leak; Domain=co.uk; Path=/',+ },+ request=request,+ )+ return httpx2.Response(200, content=b'ok', request=request)++ serve_by_host(handle)++ await safe_download('https://a.co.uk/file')++ assert sent[1] is None++ async def test_secure_cookie_not_sent_over_http(+ self, serve_by_host: Callable[[RequestHandler], None]+ ) -> None:+ """A `Secure` cookie set over https must not be sent on an https→http downgrade.++ The caller passes no credential headers, so the sensitive-header redirect-stripping+ policy does not apply here; this isolates the Secure-cookie rule.+ """+ sent: list[str | None] = []++ def handle(request: httpx2.Request) -> httpx2.Response:+ sent.append(self._sent_cookie(request))+ if request.url.scheme == 'https':+ return httpx2.Response(+ 302,+ headers={'location': 'http://a.com/file', 'set-cookie': 'session=sec; Secure; Path=/'},+ request=request,+ )+ return httpx2.Response(200, content=b'ok', request=request)++ serve_by_host(handle)++ await safe_download('https://a.com/file')++ assert sent[1] is None++ async def test_caller_cookie_takes_precedence_over_server_cookie(+ self, serve_by_host: Callable[[RequestHandler], None]+ ) -> None:+ """A caller-supplied `Cookie` is preserved verbatim; a server cookie must not merge in.++ The same-origin redirect keeps the caller's `Cookie`, so the second request must+ carry exactly `caller=1` and never the `server=2` cookie the first hop set.+ """+ sent: list[str | None] = []++ def handle(request: httpx2.Request) -> httpx2.Response:+ sent.append(self._sent_cookie(request))+ if request.url.path == '/file':+ return httpx2.Response(+ 302,+ headers={'location': 'https://a.com/other', 'set-cookie': 'server=2; Path=/'},+ request=request,+ )+ return httpx2.Response(200, content=b'ok', request=request)++ serve_by_host(handle)++ await safe_download('https://a.com/file', headers={'Cookie': 'caller=1'})++ assert sent[1] == 'caller=1'++ class TestDnsRebindingPrevention: """Tests specifically for DNS rebinding attack prevention."""
pydantic_ai_slim/pydantic_ai/_ssrf.pyindex 20dad3f887..5177857db8 100644@@ -7,10 +7,12 @@ and blocks requests to private/internal networks and cloud metadata endpoints. from __future__ import annotations +import http.cookiejar import ipaddress import socket import zlib from collections.abc import AsyncIterator+import urllib.request from dataclasses import dataclass from urllib.parse import urlparse, urlunparse @@ -534,6 +536,46 @@ def _keeps_credentials(from_url: str, to_url: str) -> bool: ) +class _CookieResponseInfo:+ """Minimal ``get_all`` view for ``http.cookiejar`` cookie extraction.++ stdlib's ``CookieJar.extract_cookies`` reads ``Set-Cookie`` headers through+ ``response.info().get_all('Set-Cookie', [])``, but httpx's ``Headers`` only+ exposes ``get_list``. This adapter bridges the two.+ """++ def __init__(self, headers: httpx.Headers) -> None:+ self._headers = headers++ def get_all(self, name: str, default: list[str] | None = None) -> list[str]:+ if name.lower() == 'set-cookie':+ return self._headers.get_list('set-cookie')+ return default if default is not None else []+++class _CookieCompatResponse:+ """Adapter letting stdlib's cookie jar read ``Set-Cookie`` from an httpx response."""++ def __init__(self, response: httpx.Response) -> None:+ self._response = response++ def info(self) -> _CookieResponseInfo:+ return _CookieResponseInfo(self._response.headers)+++def _cookie_header_for_url(jar: http.cookiejar.CookieJar, url: str) -> str | None:+ """Return the ``Cookie`` header value ``jar`` would send for the *logical* ``url``.++ ``safe_download`` requests the resolved IP, keeping the logical hostname only in the+ ``Host`` header and SNI. Server-set cookies must follow the logical hostname's cookie+ scope, not the resolved IP, so a redirect to another hostname sharing the same IP+ cannot replay them. See https://github.com/pydantic/pydantic-ai/issues/6936.+ """+ request = urllib.request.Request(url)+ jar.add_cookie_header(request)+ return request.unredirected_hdrs.get('Cookie')++ async def safe_download( url: str, allow_local: bool = False,@@ -554,6 +596,8 @@ async def safe_download( 5. Validates the hostname against allowed/blocked domain lists 6. Makes the request to the resolved IP with the Host header set 7. Manually follows redirects, validating each hop+ 8. Scopes server-set cookies to the logical hostname, so a redirect to another+ hostname sharing the same IP cannot replay them Args: url: The URL to download from.@@ -597,6 +641,8 @@ async def safe_download( current_url = url redirects_followed = 0 effective_headers: dict[str, str] = dict(headers) if headers else {}+ # Server-set cookies are scoped to the *logical* URL (see `_cookie_header_for_url`).+ cookie_jar = http.cookiejar.CookieJar() async with create_async_httpx2_client(timeout=timeout) as client: while True:@@ -634,9 +680,30 @@ async def safe_download( # Stream the raw response so gzip members can be decoded and validated before # httpx2's automatic content decoder discards member boundaries.+ # Cookies follow the logical hostname: httpx keys its jar by the resolved-IP+ # URL we actually request, so we keep httpx's jar empty and manage cookies+ # ourselves. A caller-supplied `Cookie` header still takes precedence.+ cookie_header = _cookie_header_for_url(cookie_jar, current_url)+ if cookie_header is not None and not any(k.lower() == 'cookie' for k in request_headers):+ request_headers['Cookie'] = cookie_header++ # Make request with Host header set to original hostname request = client.build_request('GET', request_url, headers=request_headers, extensions=extensions) response = await _send_request(client, request) + # Store any server-set cookies against the logical URL, and drop the copies+ # httpx extracted against the resolved-IP URL so they cannot be replayed on a+ # same-IP redirect to a different hostname.+ if isinstance(response, httpx.Response):+ # typeshed types `extract_cookies`'s response as `http.client.HTTPResponse`,+ # but stdlib only calls `response.info()`, which our adapter provides.+ cookie_jar.extract_cookies(+ _CookieCompatResponse(response), # pyright: ignore[reportArgumentType]+ urllib.request.Request(current_url),+ )+ # httpx has no `Cookies.clear()`; empty the underlying stdlib jar directly.+ client.cookies.jar.clear()+ # Check if we need to follow a redirect if response.is_redirect: await response.aclose()tests/test_ssrf.pyindex 35df1d0022..a1d120bf62 100644@@ -1370,6 +1370,91 @@ class TestSafeDownload: call_args = mock_client.get.call_args assert call_args[1]['extensions'] == {} + async def test_server_cookie_not_replayed_across_same_ip_redirect(+ self, mock_dns: AsyncMock, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ """Server-set cookies are scoped to the logical hostname, not the resolved IP.++ `safe_download` requests the resolved IP (keeping the logical hostname only in the+ `Host` header and SNI), so httpx's default cookie jar keys cookies by the IP. A+ redirect from `a.example` to `b.example` — both resolving to the same IP — must not+ replay `a.example`'s cookie on `b.example`.++ Regression test for https://github.com/pydantic/pydantic-ai/issues/6936.+ """+ mock_dns.side_effect = [+ [(2, 1, 6, '', ('93.184.215.14', 0))], # a.example+ [(2, 1, 6, '', ('93.184.215.14', 0))], # b.example (same IP)+ ]++ sent_cookies: list[str | None] = []++ def handler(request: httpx.Request) -> httpx.Response:+ host = request.headers.get('host', '')+ if host == 'a.example':+ return httpx.Response(+ 302,+ headers={'location': 'https://b.example/file', 'set-cookie': 'sid=secret; Path=/'},+ request=request,+ )+ assert host == 'b.example'+ sent_cookies.append(request.headers.get('cookie'))+ return httpx.Response(200, content=b'final', request=request)++ def factory(**kwargs: Any) -> httpx.AsyncClient:+ return httpx.AsyncClient(transport=httpx.MockTransport(handler))++ monkeypatch.setattr('pydantic_ai._ssrf.create_async_http_client', factory)++ response = await safe_download('https://a.example/file')++ assert response.content == b'final'+ assert sent_cookies == [None]++ async def test_server_cookie_kept_for_same_logical_host_return_hop(+ self, mock_dns: AsyncMock, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ """A cookie set by `a.example` survives a redirect detour through `b.example`.++ On an a.example → b.example → a.example chain (all on one IP), the cookie set on the+ first hop must still be sent on the final hop, and must not leak to b.example in+ between. Fixes that clear the whole jar on cross-host redirects would break this+ flow, so it is pinned here. Issue #6936.+ """+ mock_dns.return_value = [(2, 1, 6, '', ('93.184.215.14', 0))]++ sent_cookies: list[str | None] = []+ hop = 0++ def handler(request: httpx.Request) -> httpx.Response:+ nonlocal hop+ hop += 1+ host = request.headers.get('host', '')+ if hop == 1:+ assert host == 'a.example'+ return httpx.Response(+ 302,+ headers={'location': 'https://b.example/file', 'set-cookie': 'sid=secret; Path=/'},+ request=request,+ )+ if hop == 2:+ assert host == 'b.example'+ sent_cookies.append(request.headers.get('cookie'))+ return httpx.Response(302, headers={'location': 'https://a.example/file'}, request=request)+ assert host == 'a.example'+ sent_cookies.append(request.headers.get('cookie'))+ return httpx.Response(200, content=b'final', request=request)++ def factory(**kwargs: Any) -> httpx.AsyncClient:+ return httpx.AsyncClient(transport=httpx.MockTransport(handler))++ monkeypatch.setattr('pydantic_ai._ssrf.create_async_http_client', factory)++ response = await safe_download('https://a.example/file')++ assert response.content == b'final'+ assert sent_cookies == [None, 'sid=secret']+ async def test_protocol_validation(self) -> None: with pytest.raises(ValueError, match='URL protocol "file" is not allowed'): await safe_download('file:///etc/passwd')
What the agent reported
The fix is complete and verified. Here is my final report.
What was wrong
pydantic_ai._ssrf.safe_download connects to a DNS-resolved IP while carrying the logical hostname only in the Host header and TLS SNI. HTTPX (httpx2) keys its per-client cookie jar on the request URL — which is the resolved-IP URL. So when host A returned a Set-Cookie during a redirect and the redirect target host B resolved to the same IP, httpx treated both hops as the same origin (the shared IP) and replayed A's cookie to B. Server-set cookies escaped their logical-host scope. (Distinct from #6810, which covers caller-configured headers — that policy was left untouched.)
What I changed
pydantic_ai_slim/pydantic_ai/_ssrf.py only (production), purely additive:
- Added a per-call logical-URL cookie jar (
_new_logical_cookie_jar) built onhttpx2.Cookieswith a custom_LogicalCookiePolicy(http.cookiejar.DefaultCookiePolicy). - In the redirect loop: cookies are extracted from each response (
_store_logical_cookies) and applied to each request (_logical_cookie_header) against the validated logical URL (current_url), never the IP URL. - After every response, httpx's resolved-IP jar is cleared (
client.cookies.clear(), guarded byisinstance(client.cookies, httpx2.Cookies)— always true in production; a no-op only for the file's client mocks). _LogicalCookiePolicy.set_ok_domainrefuses aDomain=naming a public suffix (_is_public_suffix+ a curated_PUBLIC_SUFFIXESsubset; single-label TLDs handled algorithmically). Host-only, path, and Secure boundaries come fromDefaultCookiePolicy; port is not isolated and non-Secure cookies are not scheme-isolated, per spec.- Caller-supplied
Cookieretains precedence: the jar only fills aCookieheader when the caller left it unset; the existing sensitive-header redirect-stripping policy is unchanged.
tests/test_ssrf.py: new TestServerSetCookieIsolation class (real httpx2.AsyncClient + MockTransport, matching the report's probe).
Test evidence
- Before the fix:
test_set_cookie_not_replayed_across_host_boundaryfailed (assert 'session=leak' is None) andtest_domain_cookie_shared_with_subdomainfailed (assert None == 'session=ok') — the exact reported leak plus the collateral drop of legitimate subdomain sharing. - After the fix:
./bt-test tests/test_ssrf.py tests/test_web_fetch.py→ 298 passed (includes the SSRF suite, the new cookie tests, the redirect/sensitive-header suites, and the downstreamweb_fetchconsumers).
Honest note on coverage: two new tests are true fail-before/pass-after regressions (host boundary, subdomain sharing). The other four (same_host_redirect_keeps_server_cookie, public_suffix_cookie_not_shared_across_tenants, secure_cookie_not_sent_over_http, caller_cookie_takes_precedence_over_server_cookie) pass on the pre-fix baseline too — because the old IP-keyed jar coincidentally produced the same observable outcome for those specific cases (it dropped all Domain= cookies and shared only host-only cookies). I kept them as guard tests: each exercises the new code and pins a report-required boundary (a public-suffix/Secure/precedence regression within the fixed code would fail its test), and they guard against over-isolation. It is structurally impossible to write a fail-before public-suffix test in this same-IP harness, since the baseline drops every Domain= cookie — so a guard test is the only available coverage for that required boundary.
Requirements checklist
| Requirement from report | Disposition |
|---|---|
| Server cookies must not cross host boundary | ✅ Implemented; genuine regression test (fails before, passes after) |
| Valid same-host redirects keep cookies | ✅ Implemented; guard test passes |
| Valid domain/subdomain redirects keep cookies | ✅ Implemented; genuine regression test (fails before, passes after) |
| Must not cross public-suffix tenant boundary | ✅ Implemented (_is_public_suffix); guard test |
| Must not cross path boundary | ✅ Via DefaultCookiePolicy (path matching); not separately tested |
| Must not cross Secure boundary | ✅ Via DefaultCookiePolicy; guard test |
| Cookies not isolated by port | ✅ DefaultCookiePolicy default (no port attr); behavior verified by review, not by a dedicated test |
| Non-Secure cookies not isolated by scheme | ✅ DefaultCookiePolicy default; verified by review |
| Apply/extract cookies against validated logical URL | ✅ Uses current_url |
| Clear httpx resolved-IP jar after every response | ✅ client.cookies.clear() each hop |
Caller Cookie precedence retained | ✅ Implemented; guard test |
| Existing redirect stripping policy unchanged | ✅ _keeps_credentials/_SENSITIVE_HEADERS untouched; sensitive-header tests still pass |
PSL-aware Domain= handling | ✅ Implemented as a pragmatic curated subset (full PSL cannot be vendored offline); documented in the code, degrades safely and never affects the exact-host boundary |
The three deletions in git status (.claude/settings.json, AGENTS.md, CLAUDE.md) were already present at session start — pre-existing environment scaffolding, unrelated to this fix, and left untouched. No scratch/temp files; all changes are uncommitted, as required.