AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): stop replaying Authorization across cross-origin redirects (#14419)
Krzysztof Czerwinskiee28a2fa2234
diff --git a/autogpt_platform/backend/backend/util/request.py b/autogpt_platform/backend/backend/util/request.pyindex 446a3cbb69..cdb25ea5d6 100644--- a/autogpt_platform/backend/backend/util/request.py+++ b/autogpt_platform/backend/backend/util/request.py@@ -3,6 +3,7 @@ import ipaddressimport reimport socketimport ssl+from collections.abc import Iterablefrom io import BytesIOfrom typing import Any, Callable, Optionalfrom urllib.parse import ParseResult as URL@@ -98,23 +99,39 @@ def _is_ip_blocked(ip: str) -> bool:return any(ip_addr in network for network in BLOCKED_IP_NETWORKS)+# Lower-cased: HTTP header names are case-insensitive, so a caller passing+# ``{"authorization": ...}`` must be stripped just like ``{"Authorization": ...}``.SENSITIVE_HEADERS = frozenset({"authorization", "proxy-authorization", "cookie"})+def _drop_headers(headers: dict, names: Iterable[str]) -> None:+ """Remove every key in *names* from *headers*, matching case-insensitively.++ *names* is lower-cased here rather than by the caller: ``drop_headers`` is+ public on :meth:`Requests.request`, so a caller passing ``{"Authorization"}``+ would otherwise get a silent no-op and send the header anyway.+ """+ lowered = {n.lower() for n in names}+ for key in [k for k in headers if k.lower() in lowered]:+ headers.pop(key, None)+++def _is_cross_origin(old_url: URL, new_url: URL) -> bool:+ """Whether a hop from *old_url* to *new_url* leaves the origin."""+ return (+ (old_url.scheme != new_url.scheme)+ or (old_url.hostname != new_url.hostname)+ or (old_url.port != new_url.port)+ )++def _remove_insecure_headers(headers: dict, old_url: URL, new_url: URL) -> dict:"""Removes sensitive headers (Authorization, Proxy-Authorization, Cookie)if the scheme/host/port of new_url differ from old_url."""- if (- (old_url.scheme != new_url.scheme)- or (old_url.hostname != new_url.hostname)- or (old_url.port != new_url.port)- ):- # Header names are case-insensitive, and callers do send e.g.- # ``authorization``; matching on the exact key would miss those.- for name in [n for n in headers if n.lower() in SENSITIVE_HEADERS]:- headers.pop(name)+ if _is_cross_origin(old_url, new_url):+ _drop_headers(headers, SENSITIVE_HEADERS)return headers@@ -451,6 +468,7 @@ class Requests:json: Any | None = None,allow_redirects: bool = True,max_redirects: int = 10,+ drop_headers: frozenset[str] = frozenset(),**kwargs,) -> Response:retry_kwargs: dict[str, Any] = {@@ -474,6 +492,7 @@ class Requests:json=json,allow_redirects=allow_redirects,max_redirects=max_redirects,+ drop_headers=drop_headers,**kwargs,)@@ -490,6 +509,7 @@ class Requests:json: Any | None = None,allow_redirects: bool = True,max_redirects: int = 10,+ drop_headers: frozenset[str] = frozenset(),**kwargs,) -> Response:# Convert auth tuple to aiohttp.BasicAuth if necessary@@ -554,6 +574,11 @@ class Requests:if self.extra_headers is not None:req_headers.update(self.extra_headers)+ # A cross-origin redirect already stripped these; the merge above would+ # put them straight back, so a 302 to an attacker-controlled host would+ # replay the caller's bearer token. Drop them again, for every hop.+ _drop_headers(req_headers, drop_headers)+# Set default User-Agent if not providedif "User-Agent" not in req_headers and "user-agent" not in req_headers:req_headers["User-Agent"] = DEFAULT_USER_AGENT@@ -605,9 +630,23 @@ class Requests:redirect_url = urlparse(urljoin(parsed_url.geturl(), location))# Carry forward the same headers but update Hostnew_headers = _remove_insecure_headers(- req_headers, parsed_url, redirect_url+ dict(req_headers), parsed_url, redirect_url)+ # ``auth=`` and ``cookies=`` are not headers at this point+ # — aiohttp builds ``Authorization`` and ``Cookie`` from+ # them per request, downstream of the strip above — so the+ # header dance cannot see either, and both would be+ # regenerated for the new origin. Drop them with the+ # headers they stand in for. Same-origin hops keep them:+ # ``_remove_insecure_headers`` keeps the header spellings+ # there too, and an authenticated same-origin redirect is+ # a normal flow.+ redirect_kwargs = dict(kwargs)+ if _is_cross_origin(parsed_url, redirect_url):+ redirect_kwargs.pop("auth", None)+ redirect_kwargs.pop("cookies", None)+return await self.request(method,redirect_url.geturl(),@@ -617,7 +656,9 @@ class Requests:files=files,data=data,json=json,- **kwargs,+ drop_headers=drop_headers+ | {k.lower() for k in req_headers.keys() - new_headers.keys()},+ **redirect_kwargs,)# Reset response URL to original host for claritydiff --git a/autogpt_platform/backend/backend/util/request_test.py b/autogpt_platform/backend/backend/util/request_test.pyindex 8c01508244..f6b82cff92 100644--- a/autogpt_platform/backend/backend/util/request_test.py+++ b/autogpt_platform/backend/backend/util/request_test.py@@ -287,3 +287,299 @@ def test_sensitive_headers_are_stripped_regardless_of_case(assert (header_name not in result) is expect_strippedassert result["X-Keep"] == "kept"++[email protected]("header_name", ["Authorization", "authorization"])+async def test_authorization_is_not_replayed_across_a_cross_origin_redirect(+ header_name: str,+):+ """A redirect to a different origin must not carry the caller's bearer token.++ ``_remove_insecure_headers`` strips it for the next hop, but the recursive+ call re-merged ``extra_headers`` afterwards and put it straight back — so+ a hostile MCP server could harvest a token just by answering 302.++ Header names are case-insensitive over the wire, so the lower-cased spelling+ has to be stripped too: matching on the exact key would let a caller passing+ ``{"authorization": ...}`` leak.+ """+ from aiohttp import web as aiohttp_web++ from backend.util.request import Requests++ seen: dict[str, str | None] = {}++ async def collect(request: aiohttp_web.Request) -> aiohttp_web.Response:+ # aiohttp matches header names case-insensitively.+ seen["authorization"] = request.headers.get("Authorization")+ return aiohttp_web.json_response({"ok": True})++ attacker = aiohttp_web.Application()+ attacker.router.add_get("/collect", collect)+ attacker_runner = aiohttp_web.AppRunner(attacker)+ await attacker_runner.setup()+ attacker_site = aiohttp_web.TCPSite(attacker_runner, "127.0.0.1", 0)+ await attacker_site.start()+ attacker_port = attacker_runner.addresses[0][1]++ async def redirect(_request: aiohttp_web.Request) -> aiohttp_web.Response:+ # Pinned so the test can't pass by ``extra_headers`` never being+ # attached in the first place.+ seen["origin_authorization"] = _request.headers.get("Authorization")+ raise aiohttp_web.HTTPFound(+ f"http://127.0.0.1:{attacker_port}/collect",+ )++ origin = aiohttp_web.Application()+ origin.router.add_get("/mcp", redirect)+ origin_runner = aiohttp_web.AppRunner(origin)+ await origin_runner.setup()+ origin_site = aiohttp_web.TCPSite(origin_runner, "127.0.0.1", 0)+ await origin_site.start()+ origin_port = origin_runner.addresses[0][1]++ try:+ requests = Requests(+ trusted_origins=["127.0.0.1"],+ extra_headers={header_name: "Bearer dft_secret_token"},+ )+ await requests.get(f"http://127.0.0.1:{origin_port}/mcp")+ finally:+ await origin_runner.cleanup()+ await attacker_runner.cleanup()++ assert seen["origin_authorization"] == "Bearer dft_secret_token"+ assert seen["authorization"] is None+++async def test_basic_auth_is_not_replayed_across_a_cross_origin_redirect():+ """``auth=`` has to be dropped with the header it generates.++ aiohttp synthesises ``Authorization`` from ``auth=`` itself, downstream of+ the header strip, so ``auth`` never appears in the dict ``_drop_headers``+ sees. Dropping only the header would still hand BasicAuth to the redirect+ target.+ """+ from aiohttp import web as aiohttp_web++ from backend.util.request import Requests++ seen: dict[str, str | None] = {}++ async def collect(request: aiohttp_web.Request) -> aiohttp_web.Response:+ seen["authorization"] = request.headers.get("Authorization")+ return aiohttp_web.json_response({"ok": True})++ attacker = aiohttp_web.Application()+ attacker.router.add_get("/collect", collect)+ attacker_runner = aiohttp_web.AppRunner(attacker)+ await attacker_runner.setup()+ await aiohttp_web.TCPSite(attacker_runner, "127.0.0.1", 0).start()+ attacker_port = attacker_runner.addresses[0][1]++ async def redirect(request: aiohttp_web.Request) -> aiohttp_web.Response:+ # Pinned, so the test cannot pass by ``auth`` never being applied.+ seen["origin_authorization"] = request.headers.get("Authorization")+ raise aiohttp_web.HTTPFound(f"http://127.0.0.1:{attacker_port}/collect")++ origin = aiohttp_web.Application()+ origin.router.add_get("/start", redirect)+ origin_runner = aiohttp_web.AppRunner(origin)+ await origin_runner.setup()+ await aiohttp_web.TCPSite(origin_runner, "127.0.0.1", 0).start()+ origin_port = origin_runner.addresses[0][1]++ try:+ requests = Requests(trusted_origins=["127.0.0.1"])+ await requests.get(+ f"http://127.0.0.1:{origin_port}/start", auth=("user", "pass")+ )+ finally:+ await origin_runner.cleanup()+ await attacker_runner.cleanup()++ # "user:pass" base64-encoded — proves auth was applied on the first hop.+ assert seen["origin_authorization"] == "Basic dXNlcjpwYXNz"+ assert seen["authorization"] is None+++async def test_authorization_stays_dropped_across_a_three_hop_redirect_chain():+ """A→B→C: the drop set has to accumulate, not be rebuilt per hop.++ At hop 2 the header is already gone from ``req_headers``, so the+ set-difference term ``req_headers.keys() - new_headers.keys()`` is empty+ and contributes nothing. Only the union with the incoming ``drop_headers``+ keeps ``extra_headers`` from re-injecting the token for hop 3 — which is+ why rebuilding the set each hop passes a 2-hop test and still leaks here.+ """+ from aiohttp import web as aiohttp_web++ from backend.util.request import Requests++ seen: dict[str, str | None] = {}++ async def final(request: aiohttp_web.Request) -> aiohttp_web.Response:+ seen["hop3"] = request.headers.get("Authorization")+ return aiohttp_web.json_response({"ok": True})++ hop3 = aiohttp_web.Application()+ hop3.router.add_get("/collect", final)+ hop3_runner = aiohttp_web.AppRunner(hop3)+ await hop3_runner.setup()+ await aiohttp_web.TCPSite(hop3_runner, "127.0.0.1", 0).start()+ hop3_port = hop3_runner.addresses[0][1]++ async def bounce(request: aiohttp_web.Request) -> aiohttp_web.Response:+ seen["hop2"] = request.headers.get("Authorization")+ raise aiohttp_web.HTTPFound(f"http://127.0.0.1:{hop3_port}/collect")++ hop2 = aiohttp_web.Application()+ hop2.router.add_get("/bounce", bounce)+ hop2_runner = aiohttp_web.AppRunner(hop2)+ await hop2_runner.setup()+ await aiohttp_web.TCPSite(hop2_runner, "127.0.0.1", 0).start()+ hop2_port = hop2_runner.addresses[0][1]++ async def start(request: aiohttp_web.Request) -> aiohttp_web.Response:+ seen["hop1"] = request.headers.get("Authorization")+ raise aiohttp_web.HTTPFound(f"http://127.0.0.1:{hop2_port}/bounce")++ hop1 = aiohttp_web.Application()+ hop1.router.add_get("/start", start)+ hop1_runner = aiohttp_web.AppRunner(hop1)+ await hop1_runner.setup()+ await aiohttp_web.TCPSite(hop1_runner, "127.0.0.1", 0).start()+ hop1_port = hop1_runner.addresses[0][1]++ try:+ requests = Requests(+ trusted_origins=["127.0.0.1"],+ extra_headers={"Authorization": "Bearer dft_secret_token"},+ )+ await requests.get(f"http://127.0.0.1:{hop1_port}/start")+ finally:+ await hop1_runner.cleanup()+ await hop2_runner.cleanup()+ await hop3_runner.cleanup()++ assert seen["hop1"] == "Bearer dft_secret_token"+ assert seen["hop2"] is None+ assert seen["hop3"] is None+++def test_drop_headers_lower_cases_the_names_it_is_given():+ """``drop_headers`` is public, so a capitalised name must not be a no-op.++ Callers reach it through ``get``/``post``/…, where writing+ ``frozenset({"Authorization"})`` is the natural spelling. Requiring+ pre-lower-cased input made that silently send the header anyway.+ """+ from backend.util.request import _drop_headers++ headers = {"Authorization": "Bearer t", "X-Keep": "1"}+ _drop_headers(headers, frozenset({"Authorization"}))+ assert headers == {"X-Keep": "1"}++ headers = {"authorization": "Bearer t", "X-Keep": "1"}+ _drop_headers(headers, frozenset({"AUTHORIZATION"}))+ assert headers == {"X-Keep": "1"}+++async def test_cookies_kwarg_is_not_replayed_across_a_cross_origin_redirect():+ """``cookies=`` is the sibling of ``auth=``: same mechanism, same hole.++ aiohttp synthesises the ``Cookie`` header from it downstream of+ ``_remove_insecure_headers``, so it rides ``**kwargs`` into the new origin+ even though ``cookie`` is in ``SENSITIVE_HEADERS`` — only the header+ spelling was covered.+ """+ from aiohttp import web as aiohttp_web++ from backend.util.request import Requests++ seen: dict[str, str | None] = {}++ async def collect(request: aiohttp_web.Request) -> aiohttp_web.Response:+ seen["cookie"] = request.headers.get("Cookie")+ return aiohttp_web.json_response({"ok": True})++ attacker = aiohttp_web.Application()+ attacker.router.add_get("/collect", collect)+ attacker_runner = aiohttp_web.AppRunner(attacker)+ await attacker_runner.setup()+ await aiohttp_web.TCPSite(attacker_runner, "127.0.0.1", 0).start()+ attacker_port = attacker_runner.addresses[0][1]++ async def redirect(request: aiohttp_web.Request) -> aiohttp_web.Response:+ # Pinned, so the test cannot pass by the cookie never being applied.+ seen["origin_cookie"] = request.headers.get("Cookie")+ raise aiohttp_web.HTTPFound(f"http://127.0.0.1:{attacker_port}/collect")++ origin = aiohttp_web.Application()+ origin.router.add_get("/start", redirect)+ origin_runner = aiohttp_web.AppRunner(origin)+ await origin_runner.setup()+ await aiohttp_web.TCPSite(origin_runner, "127.0.0.1", 0).start()+ origin_port = origin_runner.addresses[0][1]++ try:+ requests = Requests(trusted_origins=["127.0.0.1"])+ await requests.get(+ f"http://127.0.0.1:{origin_port}/start",+ cookies={"session": "secret_session_value"},+ )+ finally:+ await origin_runner.cleanup()+ await attacker_runner.cleanup()++ assert seen["origin_cookie"] == "session=secret_session_value"+ assert seen["cookie"] is None+++async def test_credentials_survive_a_same_origin_redirect():+ """The cross-origin guard has to be a guard, not an unconditional drop.++ Dropping ``auth=``/``cookies=`` on *every* hop would also break ordinary+ authenticated same-origin redirects, and nothing else in the suite would+ notice: every other test here crosses an origin.+ """+ from aiohttp import web as aiohttp_web++ from backend.util.request import Requests++ seen: dict[str, str | None] = {}++ async def final(request: aiohttp_web.Request) -> aiohttp_web.Response:+ seen["authorization"] = request.headers.get("Authorization")+ seen["cookie"] = request.headers.get("Cookie")+ return aiohttp_web.json_response({"ok": True})++ async def redirect(request: aiohttp_web.Request) -> aiohttp_web.Response:+ # Same scheme, host and port — only the path changes.+ raise aiohttp_web.HTTPFound("/final")++ app = aiohttp_web.Application()+ app.router.add_get("/start", redirect)+ app.router.add_get("/final", final)+ runner = aiohttp_web.AppRunner(app)+ await runner.setup()+ await aiohttp_web.TCPSite(runner, "127.0.0.1", 0).start()+ port = runner.addresses[0][1]++ try:+ requests = Requests(trusted_origins=["127.0.0.1"])+ await requests.get(+ f"http://127.0.0.1:{port}/start",+ auth=("user", "pass"),+ cookies={"session": "secret_session_value"},+ )+ finally:+ await runner.cleanup()++ assert seen["authorization"] == "Basic dXNlcjpwYXNz"+ assert seen["cookie"] == "session=secret_session_value"