The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
fix(core): harden scoped and compatible IPv6 SSRF checks (#41151)
ccurme2813f7a2fd3a
diff --git a/libs/core/langchain_core/_security/_policy.py b/libs/core/langchain_core/_security/_policy.pyindex 458101dbd8..1191af62fc 100644--- a/libs/core/langchain_core/_security/_policy.py+++ b/libs/core/langchain_core/_security/_policy.py@@ -88,6 +88,7 @@ _K8S_SUFFIX = ".svc.cluster.local"_LOOPBACK_IPV4 = ipaddress.IPv4Network("127.0.0.0/8")_LOOPBACK_IPV6 = ipaddress.IPv6Address("::1")+_IPV4_COMPATIBLE_PREFIX = ipaddress.IPv6Network("::/96")# NAT64 well-known prefixes_NAT64_PREFIX = ipaddress.IPv6Network("64:ff9b::/96")@@ -125,7 +126,7 @@ DEFAULT_SSRF_POLICY = SSRFPolicy()def _extract_embedded_ipv4(addr: ipaddress.IPv6Address,) -> ipaddress.IPv4Address | None:- """Extract an embedded IPv4 from IPv4-mapped or NAT64 IPv6 addresses."""+ """Extract IPv4 from mapped, compatible, or NAT64 IPv6 addresses."""# Check ipv4_mapped first (covers ::ffff:x.x.x.x)if addr.ipv4_mapped is not None:return addr.ipv4_mapped@@ -135,6 +136,9 @@ def _extract_embedded_ipv4(raw = addr.packedreturn ipaddress.IPv4Address(raw[-4:])+ if addr in _IPV4_COMPATIBLE_PREFIX and int(addr) > 1:+ return ipaddress.IPv4Address(int(addr))+return None@@ -203,6 +207,7 @@ def validate_resolved_ip(ip_str: str, policy: SSRFPolicy) -> None:raise SSRFBlockedError(msg) from excif isinstance(addr, ipaddress.IPv6Address):+ addr = ipaddress.IPv6Address(int(addr))inner = _extract_embedded_ipv4(addr)if inner is not None:addr = innerdiff --git a/libs/core/tests/unit_tests/test_ssrf_policy_transport.py b/libs/core/tests/unit_tests/test_ssrf_policy_transport.pyindex 3f182422b5..0e8ae9de25 100644--- a/libs/core/tests/unit_tests/test_ssrf_policy_transport.py+++ b/libs/core/tests/unit_tests/test_ssrf_policy_transport.py@@ -37,6 +37,40 @@ def test_validate_resolved_ip_blocks_nat64_embedded_private_ip() -> None:validate_resolved_ip("64:ff9b::c0a8:101", policy)[email protected]("block_private_ips", [True, False])+ "ip",+ [+ "fd00:ec2::254%eth0",+ "fd00:ec2::23%eth0",+ "fe80::a9fe:a9fe%eth0",+ "::1%eth0",+ "::169.254.169.254",+ "::169.254.170.2",+ "::100.100.100.200",+ "::127.0.0.1",+ ],+)+def test_scoped_and_compatible_ipv6_blocked(+ ip: str, *, block_private_ips: bool+) -> None:+ policy = SSRFPolicy(block_private_ips=block_private_ips)+ with pytest.raises(SSRFBlockedError):+ validate_resolved_ip(ip, policy)+ with pytest.raises(SSRFBlockedError):+ validate_url_sync(f"http://[{ip}]/", policy)++[email protected]("ip", ["::8.8.8.8", "::192.168.1.1", "fd00::123%eth0", "::"])+def test_relaxed_policy_allows_non_metadata_ipv6(ip: str) -> None:+ validate_resolved_ip(ip, SSRFPolicy(block_private_ips=False))++[email protected]("ip", ["::1", "::1%eth0", "::127.0.0.1"])+def test_relaxed_policy_can_allow_ipv6_localhost(ip: str) -> None:+ validate_resolved_ip(ip, SSRFPolicy(block_private_ips=False, block_localhost=False))++def test_validate_resolved_ip_blocks_cgnat() -> None:policy = SSRFPolicy()