Skip to content

Personal AI, On Personal Devices

Python

security(ssrf): normalize disguised IPv4 and fail closed on unresolvable host (#1001)

clawdovil-agent-bvb75eca2e3e0f
diff --git a/src/openjarvis/security/ssrf.py b/src/openjarvis/security/ssrf.py
index fb8866f1..332067f0 100644
--- a/src/openjarvis/security/ssrf.py
+++ b/src/openjarvis/security/ssrf.py
@@ -117,6 +117,20 @@ def _check_ssrf_python(url: str) -> Optional[str]:
return f"URL resolves to private IP: {hostname}"
return None
+ # Catch IPv4 "in disguise": decimal (2130706433), hex (0x7f000001),
+ # octal, and short-dotted (127.1) forms are rejected by
+ # ``ipaddress.ip_address`` above but ARE accepted by the C resolver the
+ # HTTP client ultimately uses, so they'd otherwise slip past the DNS
+ # branch and reach loopback/metadata. ``inet_aton`` mirrors that resolver.
+ disguised = _disguised_ipv4(hostname)
+ if disguised is not None:
+ disguised_str = str(disguised)
+ if disguised_str in _BLOCKED_HOSTS:
+ return f"Blocked host: {disguised_str} (cloud metadata endpoint)"
+ if is_private_ip(disguised_str):
+ return f"URL resolves to private IP: {disguised_str}"
+ return None
+
# DNS resolution check
try:
resolved = socket.getaddrinfo(
@@ -125,14 +139,49 @@ def _check_ssrf_python(url: str) -> Optional[str]:
socket.AF_UNSPEC,
socket.SOCK_STREAM,
)
- for family, stype, proto, canonname, sockaddr in resolved:
- ip = sockaddr[0]
- if is_private_ip(ip):
- return f"URL resolves to private IP: {ip}"
except socket.gaierror:
- pass # DNS resolution failed — allow (will fail at request time)
+ # Fail CLOSED: an unresolvable name must not be waved through. On an
+ # exposed server, a permissive fallback is an SSRF hole; a legitimate
+ # transient DNS failure is better surfaced as a block than as a
+ # silent bypass. Override with OPENJARVIS_SSRF_FAIL_OPEN=1 only for
+ # trusted, non-exposed local use.
+ import os as _os
+
+ if _os.environ.get("OPENJARVIS_SSRF_FAIL_OPEN", "").lower() in (
+ "1",
+ "true",
+ "yes",
+ ):
+ return None
+ return f"Could not resolve host (blocked, fail-closed): {hostname}"
+
+ for family, stype, proto, canonname, sockaddr in resolved:
+ ip = sockaddr[0]
+ if is_private_ip(ip):
+ return f"URL resolves to private IP: {ip}"
return None # Safe
+def _disguised_ipv4(host: str) -> ipaddress.IPv4Address | None:
+ """Return the canonical IPv4 for a non-standard textual form, else None.
+
+ Uses ``socket.inet_aton``, which — like glibc's resolver — accepts decimal
+ (``2130706433``), hex (``0x7f000001``), octal, and short-dotted (``127.1``)
+ encodings of an IPv4 address. Plain dotted-quad and non-IP hostnames return
+ None (dotted-quad is already handled by ``ipaddress.ip_address``).
+ """
+ try:
+ packed = socket.inet_aton(host)
+ except OSError:
+ return None
+ # inet_aton also accepts canonical dotted-quad; that path is already
+ # covered by the ip_address() literal check, so only report when the
+ # input text differs from the canonical form (i.e. it was "disguised").
+ addr = ipaddress.IPv4Address(packed)
+ if str(addr) == host:
+ return None
+ return addr
+
+
__all__ = ["check_ssrf", "is_private_ip"]
diff --git a/tests/security/test_ssrf.py b/tests/security/test_ssrf.py
index 5b673c7f..dd27a4cf 100644
--- a/tests/security/test_ssrf.py
+++ b/tests/security/test_ssrf.py
@@ -136,10 +136,27 @@ class TestCheckSsrf:
assert result is not None
assert "Invalid URL" in result or "No hostname" in result
- def test_dns_failure_allowed(self):
- """DNS resolution failure should not block — request will fail at HTTP time."""
+ def test_dns_failure_fails_closed(self):
+ """DNS resolution failure must fail CLOSED (block), not silently allow.
+
+ A permissive fallback here is an SSRF hole on an exposed server: a name
+ the validator cannot resolve may still be resolvable by the HTTP
+ client's own resolver (or via rebinding), so we block by default.
+ """
+ import socket
+
+ with patch(
+ "openjarvis.security.ssrf.socket.getaddrinfo",
+ side_effect=socket.gaierror("Name resolution failed"),
+ ):
+ result = _check_ssrf_python("https://nonexistent.example.com")
+ assert result is not None
+
+ def test_dns_failure_allowed_with_fail_open_env(self, monkeypatch):
+ """Opt-in escape hatch for trusted, non-exposed local use only."""
import socket
+ monkeypatch.setenv("OPENJARVIS_SSRF_FAIL_OPEN", "1")
with patch(
"openjarvis.security.ssrf.socket.getaddrinfo",
side_effect=socket.gaierror("Name resolution failed"),
diff --git a/tests/security/test_ssrf_disguised.py b/tests/security/test_ssrf_disguised.py
new file mode 100644
index 00000000..8d00087d
--- /dev/null
+++ b/tests/security/test_ssrf_disguised.py
@@ -0,0 +1,39 @@
+"""SSRF: disguised-IPv4 forms are normalized and unresolvable hosts fail closed.
+
+Regression tests for the ``_check_ssrf_python`` fallback used when the compiled
+Rust backend is unavailable.
+"""
+
+from __future__ import annotations
+
+import pytest
+
+from openjarvis.security.ssrf import _check_ssrf_python
+
+
+class TestSSRFDisguisedForms:
+ @pytest.mark.parametrize(
+ "url",
+ [
+ "http://2130706433/", # decimal 127.0.0.1
+ "http://0x7f000001/", # hex 127.0.0.1
+ "http://0x7f.0x0.0x0.0x1/", # dotted hex
+ "http://127.1/", # short-dotted loopback
+ ],
+ )
+ def test_disguised_loopback_blocked(self, url):
+ assert _check_ssrf_python(url) is not None
+
+ def test_plain_loopback_still_blocked(self):
+ assert _check_ssrf_python("http://127.0.0.1/") is not None
+
+ def test_metadata_endpoint_blocked(self):
+ assert _check_ssrf_python("http://169.254.169.254/") is not None
+
+ def test_unresolvable_host_fails_closed(self):
+ result = _check_ssrf_python("http://this-host-does-not-exist-zzz.invalid/")
+ assert result is not None # blocked, not silently allowed
+
+ def test_fail_open_override(self, monkeypatch):
+ monkeypatch.setenv("OPENJARVIS_SSRF_FAIL_OPEN", "1")
+ assert _check_ssrf_python("http://another-nonexistent-zzz.invalid/") is None