-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 NEFARIOUSPLAN-CANONICAL-V1 {"body_md":"## The block was a list, not a policy\n\n`ClientSession._request` is a `while True:` loop with redirect-handling logic at its tail. Every iteration sends one HTTP request; if the response is a redirect, the loop parses the redirect URL and continues with the new URL in hand. Loop-scoped variables persist across iterations unless the loop explicitly clears them.\n\nPre-patch, the cross-origin block at the end of the iteration looked like this:\n\n```python\nif url.origin() != redirect_origin:\n auth = None\n headers.pop(hdrs.AUTHORIZATION, None)\n headers.pop(hdrs.COOKIE, None)\n headers.pop(hdrs.PROXY_AUTHORIZATION, None)\n\nurl = parsed_redirect_url\nparams = {}\nresp.release()\ncontinue\n```\n\nThe block is a list. It enumerates the credential-bearing inputs to the request, by hand, and clears each one. `auth` is the `BasicAuth` object the loop carries across iterations. `Authorization`, `Cookie`, and `Proxy-Authorization` are the three credential-bearing HTTP headers the caller might have passed via the `headers=` keyword argument. There is no `for carrier in CARRIERS:` loop here. There is no policy that asks the loop to forget every input whose authority was scoped to the previous host. The block names four things and clears them.\n\nThe patch for CVE-2026-47265 adds a fifth line:\n\n```diff\n if url.origin() != redirect_origin:\n auth = None\n+ cookies = None\n headers.pop(hdrs.AUTHORIZATION, None)\n headers.pop(hdrs.COOKIE, None)\n headers.pop(hdrs.PROXY_AUTHORIZATION, None)\n```\n\n`cookies` is the loop-scoped variable that holds the dict the caller passed as the `cookies=` keyword argument on the request. Before the patch, that variable persisted across redirect iterations and was used to construct the outbound `Cookie` header on every one of them. The block knew to drop the header. It did not know to drop the source the header was reconstructed from.\n\n## The fifth carrier was the per-request cookies parameter\n\nEarlier in the same loop body, the request-construction block builds the outbound cookie set:\n\n```python\nall_cookies = self._cookie_jar.filter_cookies(url)\n\nif cookies is not None:\n tmp_cookie_jar = CookieJar(\n unsafe=self._cookie_jar.unsafe,\n quote_cookie=self._cookie_jar.quote_cookie,\n )\n tmp_cookie_jar.update_cookies(cookies)\n req_cookies = tmp_cookie_jar.filter_cookies(url)\n if req_cookies:\n all_cookies.load(req_cookies)\n```\n\n`self._cookie_jar` is the session-level `CookieJar`. Its `filter_cookies(url)` applies RFC 6265 domain matching: cookies whose `Domain` attribute does not match `url.host` are not returned. When the loop iterates with a new host after a cross-origin redirect, `self._cookie_jar.filter_cookies(new_url)` correctly returns only the cookies whose domain matches the new host. The session jar is fine.\n\nThe `cookies` keyword argument is the problem. Per-request cookies are loaded into a fresh, ad-hoc `tmp_cookie_jar` and the filtered result is merged into `all_cookies`. The pre-patch code re-ran this block on every redirect iteration, because the loop body runs from the top each time. On the second iteration, the `cookies` parameter still pointed at the dict the caller passed for the original request. The temp jar got loaded again. The outbound `Cookie` header got built again. The redirected host received the developer's per-request session cookie.\n\nA user-facing call that triggers the leak looks like this:\n\n```python\nasync with aiohttp.ClientSession() as session:\n resp = await session.get(\n \"https://victim.example.com/account\",\n cookies={\"session\": \"REDACTED_SESSION_TOKEN\"},\n )\n```\n\nIf `victim.example.com/account` responds with `Location: https://attacker.example.com/`, pre-patch the redirected request carries `Cookie: session=REDACTED_SESSION_TOKEN` to `attacker.example.com`. The developer set the cookie on a request to one host. The redirect handler reused it on a request to another.\n\n## The temp jar was built without a response URL, and a cookie without a host is shared\n\nThe mechanism by which the temp jar treats per-request cookies as send-to-everywhere is worth tracing, because it is what makes the convenient API the unsafe one.\n\n`CookieJar.update_cookies` has this branch:\n\n```python\ndef update_cookies(self, cookies: LooseCookies, response_url: URL = URL()) -> None:\n hostname = response_url.raw_host\n ...\n if not domain and hostname is not None:\n # Set the cookie's domain to the response hostname\n # and set its host-only-flag\n self._host_only_cookies.add((hostname, name))\n domain = cookie[\"domain\"] = hostname\n```\n\nThe session jar receives cookies via `update_cookies(set_cookie_morsels, response_url=response.url)`. The response URL is always present, so cookies that arrive without an explicit `Domain` attribute get their domain set to the responding host. They become host-only. A later `filter_cookies(other_host)` will not return them.\n\n`tmp_cookie_jar.update_cookies(cookies)` is called with no `response_url`. The default is `URL()`, whose `raw_host` is `None`. The host-fallback branch is skipped. The cookie's domain stays empty.\n\nThen `filter_cookies` runs. Cookies with empty domain land in the `(\"\", \"\")` bucket. `filter_cookies` returns the contents of that bucket unconditionally:\n\n```python\n# Send shared cookie\nkey = (\"\", \"\")\nfor c in self._cookies[key].values():\n ...\n filtered[c.key] = mrsl_val\n```\n\nThe \"shared cookie\" path exists for cookies the application explicitly wants on every host. The per-request `cookies=` dict is not that. It is the set of cookies the caller wanted on this one call. The temp jar treats them as shared cookies because the only signal it has that a cookie should be scoped to a specific host is the `response_url` it was never given.\n\nThe result is that the per-request cookies become host-agnostic for the lifetime of the redirect chain. Each iteration's `tmp_cookie_jar.filter_cookies(url)` returns every cookie in the dict, no matter what host `url` points at. The cleanup block at the end of the iteration is the only thing that could have prevented this. Pre-patch, the cleanup block did not.\n\nThe patch did not change any of this. `update_cookies` still accepts a default empty `response_url`. The temp jar still stores per-request cookies in the `(\"\", \"\")` bucket. The \"send shared cookie\" branch still emits everything in that bucket on every request. The fix is to drop the `cookies` variable before the next iteration starts, so the temp jar never gets constructed against a new host. Any future call site that passes a user-controlled dict through `CookieJar(...).update_cookies(...)` without a `response_url` and then asks for cookies on an attacker-controlled URL inherits the same shared-cookie path. The cookie jar's behavior is unchanged; the redirect loop has stopped reaching it on cross-origin transitions.\n\n## The mitigation routes you to the carrier the block already covered\n\nThe GHSA advisory's \"if unable to upgrade\" guidance reads, verbatim:\n\n> If unable to upgrade, using a `Cookie` header in the `headers` parameter is not vulnerable.\n\nThe advisory does not explain why. The reason is in the cleanup block. `headers.pop(hdrs.COOKIE, None)` has been there since well before CVE-2026-47265. A developer who passed their cookie as `headers={\"Cookie\": \"session=abc\"}` was triggering that existing line on every cross-origin redirect. The header was already in the list.\n\nThe aiohttp client API offers two ways to send a cookie on a single request:\n\n- `session.get(url, cookies={\"session\": \"abc\"})`, the structured parameter that auto-encodes the value and reads to code review as the safe option.\n- `session.get(url, headers={\"Cookie\": \"session=abc\"})`, the manual header the developer assembled by hand.\n\nThe structured parameter is the one that leaked. The manual header was already covered.\n\nThe mitigation tells developers to stop using the structured carrier and go back to assembling the `Cookie` header by hand. The reason the manual header is safe is that the block listed it. The reason the structured parameter was not safe is that the block did not. The mitigation is the proof that the cleanup is by enumeration, not by policy: if a name appears in the block, the carrier is safe across redirects; if it does not, it is not.\n\n## Three carriers, three patches, the same block\n\nThe same cleanup block has been patched three times in the ten weeks ending June 2026. Each patch added a previously-unenumerated carrier, or rewrote the scope of a variable the block could not reach.\n\n| Date | PR | Author | What the block missed |\n|---|---|---|---|\n| 2026-03-27 | [#12275](https://github.com/aio-libs/aiohttp/pull/12275) | Rodrigo Nogueira | An `is_same_host_https_redirect` carve-out skipped the cleanup entirely on same-host HTTP-to-HTTPS upgrades. Authorization, Cookie, and Proxy-Authorization headers persisted across that class of cross-origin redirect because the block was specifically declined. |\n| 2026-05-19 | [#12550](https://github.com/aio-libs/aiohttp/pull/12550) (CVE-2026-47265) | Sam Bull | The per-request `cookies` keyword argument persisted across cross-origin redirects because the block did not name it. |\n| 2026-06-06 | [#12829](https://github.com/aio-libs/aiohttp/pull/12829) | J. Nick Koston | The environment-resolved proxy auth header (from `HTTPS_PROXY` with credentials in the URL) was set once before the loop and persisted across iterations. The fix moves resolution inside the loop and nulls the carried headers at the top of each iteration. |\n\nThe three patches are not the same fix. They are three different ways of saying the same thing: a credential-bearing input to the loop body was persisting across redirect iterations, and the cleanup block did not name it. Each fix was authored by a different contributor. Each fix was a small diff to `aiohttp/client.py`. Each fix added a missing entry to a list that the redirect handler treats as authoritative.\n\nThis is Wide Default, Narrow Doc. The redirect loop's local-variable scope is the wide default: every keyword argument the caller passes to `_request` persists across iterations because Python does not unbind them. The cleanup block is the narrow doc: a hand-curated enumeration of which carriers are credential-bearing. The same shape, [Traefik's error-page middleware copying every inbound header by default](/posts/traefik-cve-2026-41181-forward-all-headers-by-default) while the documentation page only ever discussed `Host`, is the canonical case. aiohttp's redirect loop is the same shape with a different rendering of the wide default: Python's lexical scoping instead of Go's `utils.CopyHeaders`. The block is still a list. The next credential-bearing keyword argument added to `_request`, or the next runtime side channel that injects a header into the outbound request, will not appear in the block until someone files the next CVE.\n\nPoC: [aio-libs/aiohttp](https://github.com/aio-libs/aiohttp), patch [f54c408](https://github.com/aio-libs/aiohttp/commit/f54c40851b0d6c4bbdab97ba518a223adda32478) (PR [#12550](https://github.com/aio-libs/aiohttp/pull/12550)).","closing_line":"The cleanup block enumerated four credential carriers. The redirect loop carried five. The patch added the fifth name. The block is still the only thing keeping the sixth from leaking.","hook_md":"Inside `aiohttp/client.py`, the `ClientSession._request` redirect loop ends each iteration with a block that runs on cross-origin redirects. Pre-patch, the block does four things. It sets `auth = None`. It pops `Authorization` from the request headers. It pops `Cookie` from the request headers. It pops `Proxy-Authorization` from the request headers. CVE-2026-47265 is the one line PR #12550 added to that block on May 19, 2026: `cookies = None`.\n\nThe block was a hand-written list of credential carriers the redirect loop knew to drop before the next iteration. The list had four entries. The loop carried five.","post_id":599,"slug":"aiohttp-cve-2026-47265-cleanup-block-was-a-list","title":"CVE-2026-47265: aiohttp's Cross-Origin Redirect Cleanup Enumerated Four Credential Carriers. The Loop Carried Five.","type":"initial","unreadable_sentence":"The cleanup block enumerated four credential carriers. The redirect loop carried five."} -----BEGIN PGP SIGNATURE----- iHUEARYIAB0WIQRf0htP5+SjynlxywneZjl4jgkQJgUCasaoPQAKCRDeZjl4jgkQ JjU1AQDIrD/q+gHaDFys1aebhTmnBW+YAJElISdoOn5iHGC/yAEA+sqbkjDsMqWc 15QgtuGkk/VSK71dQ9QFnpnPInZYpQQ= =4iD7 -----END PGP SIGNATURE-----