Skip to content

Official Crawl4AI Cloud: a hosted web scraping, extraction, and search API for AI agents. Free API key, 1,000 credits a month, no card. Fully open source: this repo is the Apache-2.0 crawler that powers it, and you can self-host it.

fix(security): block IPv6-mapped IPv4 SSRF bypass

unclecode1e25edcb5c80
diff --git a/deploy/docker/tests/test_security_ssrf_crawl.py b/deploy/docker/tests/test_security_ssrf_crawl.py
index 7e13362b..6da09fc1 100644
--- a/deploy/docker/tests/test_security_ssrf_crawl.py
+++ b/deploy/docker/tests/test_security_ssrf_crawl.py
@@ -37,6 +37,19 @@ _BLOCKED_HOSTNAMES = {
}
+def _expand_ip_candidates(ip):
+ """Mirror of utils.py: unwrap IPv4 from IPv4-mapped/compat IPv6."""
+ candidates = [ip]
+ if isinstance(ip, ipaddress.IPv6Address):
+ if ip.ipv4_mapped is not None:
+ candidates.append(ip.ipv4_mapped)
+ else:
+ as_int = int(ip)
+ if 0 < as_int < 2**32:
+ candidates.append(ipaddress.IPv4Address(as_int))
+ return candidates
+
+
def _validate_webhook_url(url):
parsed = urlparse(str(url))
hostname = parsed.hostname
@@ -52,9 +65,10 @@ def _validate_webhook_url(url):
raise ValueError(f"Cannot resolve hostname '{hostname}'")
for _, _, _, _, sockaddr in resolved:
ip = ipaddress.ip_address(sockaddr[0])
- for network in _BLOCKED_NETWORKS:
- if ip in network:
- raise ValueError(f"URL resolves to blocked address: {ip}")
+ for candidate in _expand_ip_candidates(ip):
+ for network in _BLOCKED_NETWORKS:
+ if candidate in network:
+ raise ValueError(f"URL resolves to blocked address: {ip}")
def validate_url_destination(url, allow_internal=False):
@@ -130,6 +144,46 @@ class TestCrawlURLSSRF(unittest.TestCase):
validate_url_destination("http://169.254.169.254/meta", allow_internal=True)
+# ============================================================================
+# IPv6-mapped IPv4 bypass (caught in internal security review)
+# Prior code only checked blocked networks directly; ::ffff:127.0.0.1
+# parses as ::ffff:7f00:1 which is not in 127.0.0.0/8, bypassing the guard.
+# ============================================================================
+
+class TestIPv6MappedBypass(unittest.TestCase):
+ """Test the IPv4-mapped IPv6 SSRF bypass class."""
+
+ def test_mapped_loopback_blocked(self):
+ """http://[::ffff:127.0.0.1]/ must be blocked (maps to 127.0.0.1)."""
+ with self.assertRaises(ValueError):
+ validate_url_destination("http://[::ffff:127.0.0.1]/admin")
+
+ def test_mapped_private_10_blocked(self):
+ """::ffff:10.0.0.1 must be blocked (maps to RFC 1918)."""
+ with self.assertRaises(ValueError):
+ validate_url_destination("http://[::ffff:10.0.0.1]/")
+
+ def test_mapped_aws_metadata_blocked(self):
+ """::ffff:169.254.169.254 must be blocked (maps to cloud metadata)."""
+ with self.assertRaises(ValueError):
+ validate_url_destination("http://[::ffff:169.254.169.254]/latest/meta-data/")
+
+ def test_mapped_192_168_blocked(self):
+ """::ffff:192.168.1.1 must be blocked."""
+ with self.assertRaises(ValueError):
+ validate_url_destination("http://[::ffff:192.168.1.1]/")
+
+ def test_ipv4_compatible_loopback_blocked(self):
+ """IPv4-compatible IPv6 (deprecated but still exists): ::127.0.0.1."""
+ with self.assertRaises(ValueError):
+ validate_url_destination("http://[::127.0.0.1]/")
+
+ def test_regular_ipv6_loopback_still_blocked(self):
+ """::1 must remain blocked (IPv6 loopback)."""
+ with self.assertRaises(ValueError):
+ validate_url_destination("http://[::1]:8080/")
+
+
# ============================================================================
# Source-level verification
# ============================================================================
diff --git a/deploy/docker/utils.py b/deploy/docker/utils.py
index 3a43ce31..881d6d2e 100644
--- a/deploy/docker/utils.py
+++ b/deploy/docker/utils.py
@@ -355,6 +355,21 @@ def validate_url_destination(url: str) -> None:
raise HTTPException(status_code=400, detail=f"URL blocked (SSRF protection): {e}")
+def _expand_ip_candidates(ip):
+ """Return [ip] plus any IPv4 form wrapped inside the IPv6 address.
+ SSRF guards must check the unwrapped form because ::ffff:127.0.0.1 and
+ ::127.0.0.1 route to 127.0.0.1 but would not match IPv4 blocklists directly."""
+ candidates = [ip]
+ if isinstance(ip, ipaddress.IPv6Address):
+ if ip.ipv4_mapped is not None:
+ candidates.append(ip.ipv4_mapped)
+ else:
+ as_int = int(ip)
+ if 0 < as_int < 2**32:
+ candidates.append(ipaddress.IPv4Address(as_int))
+ return candidates
+
+
def validate_webhook_url(url: str) -> None:
"""Reject webhook URLs targeting internal/private/reserved networks (SSRF protection)."""
parsed = urlparse(str(url))
@@ -372,9 +387,10 @@ def validate_webhook_url(url: str) -> None:
raise ValueError(f"Cannot resolve webhook hostname '{hostname}'")
for _, _, _, _, sockaddr in resolved:
ip = ipaddress.ip_address(sockaddr[0])
- for network in _BLOCKED_NETWORKS:
- if ip in network:
- raise ValueError(f"Webhook URL resolves to blocked address: {ip}")
+ for candidate in _expand_ip_candidates(ip):
+ for network in _BLOCKED_NETWORKS:
+ if candidate in network:
+ raise ValueError(f"Webhook URL resolves to blocked address: {ip}")
def verify_email_domain(email: str) -> bool:
diff --git a/tests/test_eval_security_adversarial.py b/tests/test_eval_security_adversarial.py
index 8539b364..7ec5f995 100644
--- a/tests/test_eval_security_adversarial.py
+++ b/tests/test_eval_security_adversarial.py
@@ -428,12 +428,17 @@ class TestHookManagerBuiltins(unittest.TestCase):
elt.value for elt in node.value.elts
if isinstance(elt, ast.Constant)
]
+ # Batch 2 hardening: hasattr, type, __build_class__ also removed
self.assertNotIn("getattr", values,
"getattr must not be in hook allowed_builtins (sandbox escape)")
self.assertNotIn("setattr", values,
"setattr must not be in hook allowed_builtins (sandbox escape)")
- self.assertIn("hasattr", values,
- "hasattr should remain (read-only, safe)")
+ self.assertNotIn("hasattr", values,
+ "hasattr removed in batch 2 (info disclosure via probing)")
+ self.assertNotIn("type", values,
+ "type removed in batch 2 (__subclasses__ MRO chain escape)")
+ self.assertNotIn("__build_class__", values,
+ "__build_class__ removed in batch 2 (__init_subclass__ abuse)")
return
self.fail("Could not find allowed_builtins in hook_manager.py")