Skip to content

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): match sensitive header names case-insensitively when following redirects (#14394)

Reinier van der Leer30ea0d2e2d18
diff --git a/autogpt_platform/backend/backend/util/request.py b/autogpt_platform/backend/backend/util/request.py
index 20da177b32..5cbb357690 100644
--- a/autogpt_platform/backend/backend/util/request.py
+++ b/autogpt_platform/backend/backend/util/request.py
@@ -88,6 +88,9 @@ def _is_ip_blocked(ip: str) -> bool:
return any(ip_addr in network for network in BLOCKED_IP_NETWORKS)
+SENSITIVE_HEADERS = frozenset({"authorization", "proxy-authorization", "cookie"})
+
+
def _remove_insecure_headers(headers: dict, old_url: URL, new_url: URL) -> dict:
"""
Removes sensitive headers (Authorization, Proxy-Authorization, Cookie)
@@ -98,9 +101,10 @@ def _remove_insecure_headers(headers: dict, old_url: URL, new_url: URL) -> dict:
or (old_url.hostname != new_url.hostname)
or (old_url.port != new_url.port)
):
- headers.pop("Authorization", None)
- headers.pop("Proxy-Authorization", None)
- headers.pop("Cookie", None)
+ # 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)
return headers
diff --git a/autogpt_platform/backend/backend/util/request_test.py b/autogpt_platform/backend/backend/util/request_test.py
index 2d48d755eb..8c01508244 100644
--- a/autogpt_platform/backend/backend/util/request_test.py
+++ b/autogpt_platform/backend/backend/util/request_test.py
@@ -1,7 +1,14 @@
+from urllib.parse import urlparse
+
import pytest
from aiohttp import web
-from backend.util.request import _is_ip_blocked, pin_url, validate_url_host
+from backend.util.request import (
+ _is_ip_blocked,
+ _remove_insecure_headers,
+ pin_url,
+ validate_url_host,
+)
@pytest.mark.parametrize(
@@ -244,3 +251,39 @@ async def test_ipv4_mapped_ipv6_bypass(
else:
url, _, ip_addresses = await validate_url_host(raw_url)
assert ip_addresses # Should have resolved IPs
+
+
+ "header_name",
+ [
+ "Authorization",
+ "authorization",
+ "AUTHORIZATION",
+ "Proxy-Authorization",
+ "proxy-authorization",
+ "PROXY-AUTHORIZATION",
+ "Cookie",
+ "cookie",
+ "CoOkIe",
+ ],
+)
+ "new_url, expect_stripped",
+ [
+ ("https://example.com/b", False),
+ ("https://other.example.com/b", True),
+ ("http://example.com/b", True),
+ ("https://example.com:8443/b", True),
+ ],
+)
+def test_sensitive_headers_are_stripped_regardless_of_case(
+ header_name: str, new_url: str, expect_stripped: bool
+):
+ headers = {header_name: "secret", "X-Keep": "kept"}
+
+ result = _remove_insecure_headers(
+ headers, urlparse("https://example.com/a"), urlparse(new_url)
+ )
+
+ assert (header_name not in result) is expect_stripped
+ assert result["X-Keep"] == "kept"