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.

feat(backend): apply the outbound address rules to every IPv6 form of a blocked address (#15926)

Nicholas Tindle84e023806d7b
diff --git a/autogpt_platform/backend/backend/util/request.py b/autogpt_platform/backend/backend/util/request.py
index 168579bc60..95a417630f 100644
--- a/autogpt_platform/backend/backend/util/request.py
+++ b/autogpt_platform/backend/backend/util/request.py
@@ -71,7 +71,11 @@ BLOCKED_IP_NETWORKS = [
ipaddress.ip_network("224.0.0.0/4"), # Multicast
ipaddress.ip_network("240.0.0.0/4"), # Reserved for Future Use
# IPv6 Ranges
+ ipaddress.ip_network("::/128"), # Unspecified; Linux connects it to ::1
ipaddress.ip_network("::1/128"), # Loopback
+ # Local-use NAT64 (RFC 8215): it translates to addresses inside some network,
+ # and not always with the IPv4 address in the last 32 bits.
+ ipaddress.ip_network("64:ff9b:1::/48"),
ipaddress.ip_network("fc00::/7"), # Unique local addresses (ULA)
ipaddress.ip_network("fe80::/10"), # Link-local
ipaddress.ip_network("ff00::/8"), # Multicast
@@ -81,22 +85,50 @@ BLOCKED_IP_NETWORKS = [
ALLOWED_SCHEMES = ["http", "https"]
HOSTNAME_REGEX = re.compile(r"^[A-Za-z0-9.-]+$") # Basic DNS-safe hostname pattern
+# IPv6 prefixes whose last 32 bits are an IPv4 address the network may route
+# the connection to: the well-known NAT64 prefix (RFC 6052), SIIT (RFC 2765)
+# and the deprecated IPv4-compatible form (RFC 4291).
+_IPV4_IN_LOW_BITS_NETWORKS = [
+ ipaddress.ip_network("64:ff9b::/96"),
+ ipaddress.ip_network("::ffff:0:0:0/96"),
+ ipaddress.ip_network("::/96"),
+]
+
+
+def _embedded_ipv4(ip_addr: ipaddress.IPv6Address) -> ipaddress.IPv4Address | None:
+ """The IPv4 address an IPv6 address encodes, if it is a form that embeds one."""
+ if ip_addr.ipv4_mapped:
+ return ip_addr.ipv4_mapped
+ if ip_addr.sixtofour:
+ return ip_addr.sixtofour
+ if ip_addr.teredo:
+ # (server, client): the client is the host the packets are delivered to
+ return ip_addr.teredo[1]
+ if any(ip_addr in network for network in _IPV4_IN_LOW_BITS_NETWORKS):
+ return ipaddress.IPv4Address(int(ip_addr) & 0xFFFFFFFF)
+ return None
+
def _is_ip_blocked(ip: str) -> bool:
"""
Checks if the IP address is in a blocked network.
- IPv4-mapped IPv6 addresses (e.g. ``::ffff:127.0.0.1``) are normalized to
- their IPv4 equivalent before checking, so the IPv4 blocklist cannot be
- bypassed by encoding a private IPv4 address as IPv6.
+ IPv6 addresses that embed an IPv4 address (IPv4-mapped ``::ffff:127.0.0.1``,
+ 6to4, Teredo, NAT64, SIIT and IPv4-compatible) are also checked as that IPv4
+ address, so the IPv4 blocklist cannot be bypassed by encoding a private IPv4
+ address as IPv6.
"""
ip_addr = ipaddress.ip_address(ip)
- # Normalize IPv4-mapped IPv6 → IPv4 so the IPv4 blocklist applies
- if isinstance(ip_addr, ipaddress.IPv6Address) and ip_addr.ipv4_mapped:
- ip_addr = ip_addr.ipv4_mapped
+ if any(ip_addr in network for network in BLOCKED_IP_NETWORKS):
+ return True
+
+ if isinstance(ip_addr, ipaddress.IPv6Address):
+ embedded = _embedded_ipv4(ip_addr)
+ if embedded is not None:
+ return any(embedded in network for network in BLOCKED_IP_NETWORKS)
- return any(ip_addr in network for network in BLOCKED_IP_NETWORKS)
+ return False
# Lower-cased: HTTP header names are case-insensitive, so a caller passing
diff --git a/autogpt_platform/backend/backend/util/request_test.py b/autogpt_platform/backend/backend/util/request_test.py
index 511a7bde41..be369f77aa 100644
--- a/autogpt_platform/backend/backend/util/request_test.py
+++ b/autogpt_platform/backend/backend/util/request_test.py
@@ -7,6 +7,7 @@ from backend.util.request import (
_is_ip_blocked,
_remove_insecure_headers,
pin_url,
+ resolve_and_check_blocked,
validate_url_host,
)
@@ -204,9 +205,22 @@ async def test_large_header_handling():
("1.1.1.1", False),
("::ffff:8.8.8.8", False), # IPv4-mapped but public
# Native IPv6 blocked ranges
+ ("::", True), # Unspecified; Linux connects it to ::1
("::1", True), # Loopback
("fe80::1", True), # Link-local
("fc00::1", True), # ULA
+ # Other IPv6 encodings of a blocked IPv4 address
+ ("2002:a9fe:a9fe::", True), # 6to4 of 169.254.169.254
+ ("2001:0:4136:e378:8000:63bf:80ff:fffe", True), # Teredo, client 127.0.0.1
+ ("64:ff9b::a9fe:a9fe", True), # NAT64 of 169.254.169.254
+ ("64:ff9b:1::a00:1", True), # Local-use NAT64 of 10.0.0.1
+ ("64:ff9b:1::808:808", True), # Local-use NAT64 is blocked whatever it embeds
+ ("::ffff:0:7f00:1", True), # SIIT of 127.0.0.1
+ ("::7f00:1", True), # IPv4-compatible 127.0.0.1
+ # The same encodings of a public IPv4 address (should NOT be blocked)
+ ("2002:808:808::", False), # 6to4 of 8.8.8.8
+ ("2001:0:4136:e378:8000:63bf:f7f7:f7f7", False), # Teredo, client 8.8.8.8
+ ("64:ff9b::808:808", False), # NAT64 of 8.8.8.8, as DNS64 networks resolve
# Public IPv6 (should NOT be blocked)
("2607:f8b0:4004:800::200e", False), # Google
],
@@ -228,17 +242,21 @@ def test_is_ip_blocked(ip: str, expected_blocked: bool):
("cgnat.example.com", ["100.64.0.1"], True),
# Mixed: one public, one mapped-private — must be blocked (any blocked = reject)
("mixed.example.com", ["8.8.8.8", "::ffff:192.168.1.1"], True),
+ # A lone AAAA record of :: — Linux connects it to ::1, so must be blocked
+ ("unspecified.example.com", ["::"], True),
+ # NAT64 of the cloud metadata address — must be blocked
+ ("nat64-metadata.example.com", ["64:ff9b::a9fe:a9fe"], True),
# All public — should pass
("public.example.com", ["8.8.8.8", "9.9.9.9"], False),
],
)
-async def test_ipv4_mapped_ipv6_bypass(
+async def test_ipv6_encoded_blocked_ip_bypass(
monkeypatch,
raw_url: str,
resolved_ips: list[str],
should_raise: bool,
):
- """Ensures IPv4-mapped IPv6 addresses are checked against IPv4 blocklist."""
+ """Ensures a host that resolves to a blocked address in any IPv6 form is rejected."""
def mock_getaddrinfo(host, port, *args, **kwargs):
return [(None, None, None, None, (ip, port)) for ip in resolved_ips]
@@ -253,6 +271,15 @@ async def test_ipv4_mapped_ipv6_bypass(
assert ip_addresses # Should have resolved IPs
[email protected]("host", ["::", "::1", "::ffff:127.0.0.1", "0.0.0.0"])
+async def test_literal_blocked_host_is_rejected(host: str):
+ """The SQL and email blocks pass a user's host to resolve_and_check_blocked
+ as typed, without validate_url_host's hostname check, so an IP literal must
+ be rejected here. getaddrinfo returns a literal as is, so no DNS is involved."""
+ with pytest.raises(ValueError):
+ await resolve_and_check_blocked(host)
+
+
@pytest.mark.parametrize(
"header_name",
[