Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(security): harden SSRF metadata blocks and redact log userinfo (#3882)

LearningCircuit4ae0041f630e
diff --git a/SECURITY.md b/SECURITY.md
index fc0944886f..d952c1b654 100644
--- a/SECURITY.md
+++ b/SECURITY.md
@@ -138,6 +138,22 @@ A reporter ([@Fushuling](https://github.com/Fushuling), [@RacerZ-fighting](https
Both `ssrf_validator.validate_url` and `NotificationURLValidator.validate_service_url` (HTTP/HTTPS branch) carry the fix. Future edits to the SSRF path should preserve `RFC_FORBIDDEN_URL_CHARS_RE` and the `urllib3.util.parse_url` host extraction — reverting either reintroduces the bypass.
+### Cloud Metadata Endpoint Block List
+
+`ssrf_validator.ALWAYS_BLOCKED_METADATA_IPS` is a frozenset of cloud-provider metadata IPs that are blocked under every flag combination, including `allow_localhost=True` and `allow_private_ips=True`. These IPs expose IAM / instance-role credentials and are never legitimate destinations for outbound HTTP. The current set is:
+
+| IP | Provider |
+| --- | --- |
+| `169.254.169.254` | AWS IMDSv1/v2, Azure, OCI, DigitalOcean (shared) |
+| `169.254.170.2` | AWS ECS task metadata v3 |
+| `169.254.170.23` | AWS ECS task metadata v4 |
+| `169.254.0.23` | Tencent Cloud |
+| `100.100.100.200` | AlibabaCloud |
+
+Future contributors must not remove entries from this set. Adding a new cloud provider's metadata IP is encouraged when a new public-cloud target appears.
+
+URL rejection log lines route through `ssrf_validator.redact_url_for_log` to drop userinfo (RFC 3986 §3.2.1 allows credentials in the URL), path, and query — operators see `scheme://host:port` only. Operators with grep/regex tooling on the rejection log lines will see authority-only strings instead of full URLs.
+
## Supported Versions
Security fixes are only provided for the latest release. Please upgrade to receive patches.
diff --git a/changelog.d/+ssrf-metadata-block-log-hygiene.security.md b/changelog.d/+ssrf-metadata-block-log-hygiene.security.md
new file mode 100644
index 0000000000..0f4abc64dd
--- /dev/null
+++ b/changelog.d/+ssrf-metadata-block-log-hygiene.security.md
@@ -0,0 +1,8 @@
+Hardened SSRF defenses against AWS ECS task metadata
+(`169.254.170.2`, `169.254.170.23`), Tencent Cloud (`169.254.0.23`),
+and AlibabaCloud (`100.100.100.200`) metadata endpoints — these are
+now always blocked alongside the existing AWS IMDS / Azure / OCI /
+DigitalOcean entry (`169.254.169.254`). Redacted credentials, path,
+and query from URL-rejection logs (operators with grep/regex tooling
+on the rejection log lines will see authority-only `scheme://host:port`
+instead of full URLs going forward).
diff --git a/docs/SearXNG-Setup.md b/docs/SearXNG-Setup.md
index e0eb8db31a..b5e143bd28 100644
--- a/docs/SearXNG-Setup.md
+++ b/docs/SearXNG-Setup.md
@@ -111,7 +111,7 @@ SearXNG is designed for self-hosting, so Local Deep Research allows SearXNG to a
This is intentional and secure because:
1. The SearXNG URL is **admin-configured**, not user input
2. Private IPs are only accessible from your local network
-3. The **AWS metadata endpoint** (169.254.169.254) is always blocked to prevent credential theft in cloud environments
+3. **Cloud metadata endpoints** (AWS IMDS / ECS, Azure, OCI, DigitalOcean, AlibabaCloud, Tencent Cloud — see `ssrf_validator.ALWAYS_BLOCKED_METADATA_IPS`) are always blocked to prevent credential theft in cloud environments
## Troubleshooting
diff --git a/src/local_deep_research/security/notification_validator.py b/src/local_deep_research/security/notification_validator.py
index 6f2b4987f3..08a4f83fe2 100644
--- a/src/local_deep_research/security/notification_validator.py
+++ b/src/local_deep_research/security/notification_validator.py
@@ -15,7 +15,7 @@ from urllib3.exceptions import LocationParseError
from urllib3.util import parse_url
from .ip_ranges import PRIVATE_IP_RANGES as _PRIVATE_IP_RANGES
-from .ssrf_validator import RFC_FORBIDDEN_URL_CHARS_RE
+from .ssrf_validator import RFC_FORBIDDEN_URL_CHARS_RE, redact_url_for_log
class NotificationURLValidationError(ValueError):
@@ -211,14 +211,14 @@ class NotificationURLValidator:
# Check for blocked schemes
if scheme in NotificationURLValidator.BLOCKED_SCHEMES:
logger.warning(
- f"Blocked unsafe notification protocol: {scheme} in URL: {url[:50]}..."
+ f"Blocked unsafe notification protocol: {scheme} in URL: {redact_url_for_log(url)}"
)
return False, f"Blocked unsafe protocol: {scheme}"
# Check for allowed schemes
if scheme not in NotificationURLValidator.ALLOWED_SCHEMES:
logger.warning(
- f"Unknown notification protocol: {scheme} in URL: {url[:50]}..."
+ f"Unknown notification protocol: {scheme} in URL: {redact_url_for_log(url)}"
)
return (
False,
@@ -332,7 +332,10 @@ class NotificationURLValidator:
if not is_valid:
# Return first error found
- return False, f"Invalid URL '{url[:50]}...': {error_message}"
+ return (
+ False,
+ f"Invalid URL '{redact_url_for_log(url)}': {error_message}",
+ )
# All URLs passed validation
return True, None
diff --git a/src/local_deep_research/security/safe_requests.py b/src/local_deep_research/security/safe_requests.py
index 995b62cada..038c4d630d 100644
--- a/src/local_deep_research/security/safe_requests.py
+++ b/src/local_deep_research/security/safe_requests.py
@@ -161,7 +161,9 @@ def safe_get(
used by Podman/rootless containers), link-local (169.254.x.x), and IPv6
private ranges (fc00::/7, fe80::/10). Use for trusted self-hosted services
like SearXNG or Ollama in containerized environments.
- Note: AWS metadata endpoint (169.254.169.254) is ALWAYS blocked.
+ Note: cloud metadata endpoints (AWS / Azure / OCI / DigitalOcean /
+ AlibabaCloud / Tencent / ECS) are ALWAYS blocked — see
+ ``ssrf_validator.ALWAYS_BLOCKED_METADATA_IPS``.
**kwargs: Additional arguments to pass to requests.get()
Returns:
@@ -296,7 +298,9 @@ def safe_post(
used by Podman/rootless containers), link-local (169.254.x.x), and IPv6
private ranges (fc00::/7, fe80::/10). Use for trusted self-hosted services
like SearXNG or Ollama in containerized environments.
- Note: AWS metadata endpoint (169.254.169.254) is ALWAYS blocked.
+ Note: cloud metadata endpoints (AWS / Azure / OCI / DigitalOcean /
+ AlibabaCloud / Tencent / ECS) are ALWAYS blocked — see
+ ``ssrf_validator.ALWAYS_BLOCKED_METADATA_IPS``.
**kwargs: Additional arguments to pass to requests.post()
Returns:
@@ -461,7 +465,9 @@ class SafeSession(requests.Session):
This includes RFC1918, CGNAT (100.64.x.x used by Podman), link-local, and
IPv6 private ranges. Use for trusted self-hosted services like SearXNG or
Ollama in containerized environments.
- Note: AWS metadata endpoint (169.254.169.254) is ALWAYS blocked.
+ Note: cloud metadata endpoints (AWS / Azure / OCI / DigitalOcean /
+ AlibabaCloud / Tencent / ECS) are ALWAYS blocked — see
+ ``ssrf_validator.ALWAYS_BLOCKED_METADATA_IPS``.
"""
super().__init__()
self.max_redirects = _MAX_REDIRECTS
diff --git a/src/local_deep_research/security/ssrf_validator.py b/src/local_deep_research/security/ssrf_validator.py
index 3591b862ad..5c041c9c72 100644
--- a/src/local_deep_research/security/ssrf_validator.py
+++ b/src/local_deep_research/security/ssrf_validator.py
@@ -16,9 +16,19 @@ from urllib3.util import parse_url
from .ip_ranges import PRIVATE_IP_RANGES as BLOCKED_IP_RANGES
-# AWS metadata endpoint (commonly targeted in SSRF attacks)
-# nosec B104 - Hardcoded IP is intentional for SSRF prevention (blocking AWS metadata endpoint)
-AWS_METADATA_IP = "169.254.169.254"
+# Cloud-provider metadata endpoints — always blocked, even with
+# allow_localhost=True or allow_private_ips=True. These IPs expose IAM /
+# instance-role credentials and are never legitimate destinations.
+# nosec B104 - Hardcoded IPs are intentional for SSRF prevention
+ALWAYS_BLOCKED_METADATA_IPS = frozenset(
+ {
+ "169.254.169.254", # AWS IMDSv1/v2, Azure, OCI, DigitalOcean
+ "169.254.170.2", # AWS ECS task metadata v3
+ "169.254.170.23", # AWS ECS task metadata v4
+ "169.254.0.23", # Tencent Cloud
+ "100.100.100.200", # AlibabaCloud
+ }
+)
# Allowed URL schemes
ALLOWED_SCHEMES = {"http", "https"}
@@ -46,7 +56,9 @@ def is_ip_blocked(
used by Podman/rootless containers), link-local (169.254.x.x), and IPv6
private ranges (fc00::/7, fe80::/10). Use for trusted self-hosted services
like SearXNG or Ollama in containerized environments.
- Note: AWS metadata endpoint (169.254.169.254) is ALWAYS blocked.
+ Note: cloud metadata endpoints in ``ALWAYS_BLOCKED_METADATA_IPS``
+ (AWS / Azure / OCI / DigitalOcean / AlibabaCloud / Tencent / ECS)
+ are ALWAYS blocked regardless of these flags.
Returns:
True if IP is blocked, False otherwise
@@ -71,7 +83,7 @@ def is_ip_blocked(
), # CGNAT - used by Podman/rootless containers
ipaddress.ip_network(
"169.254.0.0/16"
- ), # Link-local (AWS metadata blocked separately)
+ ), # Link-local (cloud metadata IPs blocked separately via ALWAYS_BLOCKED_METADATA_IPS)
# IPv6 Private Ranges
ipaddress.ip_network("fc00::/7"), # IPv6 Unique Local Addresses
ipaddress.ip_network("fe80::/10"), # IPv6 Link-Local
@@ -85,8 +97,11 @@ def is_ip_blocked(
if isinstance(ip, ipaddress.IPv6Address) and ip.ipv4_mapped:
ip = ip.ipv4_mapped
- # ALWAYS block AWS metadata endpoint - critical SSRF target for credential theft
- if str(ip) == AWS_METADATA_IP:
+ # ALWAYS block cloud-metadata endpoints - critical SSRF target
+ # for credential theft (AWS IMDS/ECS, Azure, OCI, DigitalOcean,
+ # AlibabaCloud, Tencent Cloud). These are never legitimate
+ # destinations regardless of allow_localhost / allow_private_ips.
+ if str(ip) in ALWAYS_BLOCKED_METADATA_IPS:
return True
# Check if IP is in any blocked range
@@ -135,7 +150,9 @@ def validate_url(
used by Podman/rootless containers), link-local (169.254.x.x), and IPv6
private ranges (fc00::/7, fe80::/10). Use for trusted self-hosted services
like SearXNG or Ollama in containerized environments.
- Note: AWS metadata endpoint (169.254.169.254) is ALWAYS blocked.
+ Note: cloud metadata endpoints in ``ALWAYS_BLOCKED_METADATA_IPS``
+ (AWS / Azure / OCI / DigitalOcean / AlibabaCloud / Tencent / ECS)
+ are ALWAYS blocked regardless of these flags.
Returns:
True if URL is safe, False otherwise
@@ -157,7 +174,7 @@ def validate_url(
# Check scheme
if parsed.scheme.lower() not in ALLOWED_SCHEMES:
logger.warning(
- f"Blocked URL with invalid scheme: {parsed.scheme} - {url}"
+ f"Blocked URL with invalid scheme: {parsed.scheme} - {redact_url_for_log(url)}"
)
return False
@@ -189,7 +206,9 @@ def validate_url(
if hostname:
hostname = hostname.rstrip(".")
if not hostname:
- logger.warning(f"Blocked URL with no hostname: {url}")
+ logger.warning(
+ f"Blocked URL with no hostname: {redact_url_for_log(url)}"
+ )
return False
# Check if hostname is an IP address
@@ -201,7 +220,7 @@ def validate_url(
allow_private_ips=allow_private_ips,
):
logger.warning(
- f"Blocked URL with internal/private IP: {hostname} - {url}"
+ f"Blocked URL with internal/private IP: {hostname} - {redact_url_for_log(url)}"
)
return False
except ValueError:
@@ -238,7 +257,7 @@ def validate_url(
):
logger.warning(
f"Blocked URL - hostname {hostname} resolves to "
- f"internal/private IP: {ip_str} - {url}"
+ f"internal/private IP: {ip_str} - {redact_url_for_log(url)}"
)
return False
@@ -253,7 +272,7 @@ def validate_url(
return True
except Exception:
- logger.exception(f"Error validating URL {url}")
+ logger.exception(f"Error validating URL {redact_url_for_log(url)}")
return False
@@ -276,5 +295,28 @@ def get_safe_url(
if validate_url(url):
return url
- logger.warning(f"Unsafe URL rejected: {url}")
+ logger.warning(f"Unsafe URL rejected: {redact_url_for_log(url)}")
return default
+
+
+def redact_url_for_log(url: str) -> str:
+ """Return ``scheme://host:port`` (no userinfo, path, query, fragment).
+
+ For log output only. Drops everything except scheme + authority host
+ + port to minimise the chance of leaking credentials, tokens, or
+ sensitive paths into logs while still giving operators enough to
+ distinguish ``http://10.0.0.1:80`` from ``https://10.0.0.1:443``.
+
+ RFC 3986 §3.2.1 allows credentials in URL userinfo
+ (``http://user:pass@host/``). A rejected URL is by definition
+ adversarial-shaped, but it may still carry the operator's real
+ credentials if a misconfiguration produced it.
+ """
+ try:
+ u = parse_url(url)
+ scheme = u.scheme or "?"
+ host = u.host or "<no-host>"
+ host_port = f"{host}:{u.port}" if u.port else host
+ return f"{scheme}://{host_port}"
+ except (LocationParseError, ValueError):
+ return "<unparseable>"
diff --git a/src/local_deep_research/web/services/pdf_service.py b/src/local_deep_research/web/services/pdf_service.py
index 961c5113a0..5c2e9fa924 100644
--- a/src/local_deep_research/web/services/pdf_service.py
+++ b/src/local_deep_research/web/services/pdf_service.py
@@ -42,9 +42,9 @@ class UnsafePDFResourceURLError(ValueError):
# Module-level URLFetcher preserves the allow_redirects=False posture that
# default_url_fetcher hard-coded. Redirects disabled keeps the SSRF guard
-# airtight — validate_url only inspects the initial URL, so a 30x to the
-# AWS metadata endpoint (see ssrf_validator.AWS_METADATA_IP) would
-# otherwise slip past.
+# airtight — validate_url only inspects the initial URL, so a 30x to a
+# cloud metadata endpoint (see ssrf_validator.ALWAYS_BLOCKED_METADATA_IPS)
+# would otherwise slip past.
_URL_FETCHER = (
URLFetcher(allow_redirects=False) if WEASYPRINT_AVAILABLE else None
)
diff --git a/tests/security/test_ip_ranges.py b/tests/security/test_ip_ranges.py
index 7c9858a5db..88001470f3 100644
--- a/tests/security/test_ip_ranges.py
+++ b/tests/security/test_ip_ranges.py
@@ -97,6 +97,23 @@ class TestPrivateIPRanges:
unique_local = ipaddress.ip_network("fc00::/7")
assert unique_local in PRIVATE_IP_RANGES
+ def test_contains_ipv4_unspecified(self):
+ """Should contain 0.0.0.0/8 ('this' network — IPv4 unspecified).
+ Linux routes connect() to 0.0.0.0 to local host."""
+ from local_deep_research.security.ip_ranges import PRIVATE_IP_RANGES
+
+ unspecified_v4 = ipaddress.ip_network("0.0.0.0/8")
+ assert unspecified_v4 in PRIVATE_IP_RANGES
+
+ def test_contains_ipv6_unspecified(self):
+ """Should contain ::/128 (IPv6 unspecified). Linux routes
+ connect() to [::] to local host (same semantics as 0.0.0.0).
+ Added in PR #3873 after empirical bypass discovery."""
+ from local_deep_research.security.ip_ranges import PRIVATE_IP_RANGES
+
+ unspecified_v6 = ipaddress.ip_network("::/128")
+ assert unspecified_v6 in PRIVATE_IP_RANGES
+
class TestPrivateIPDetection:
"""Tests for using PRIVATE_IP_RANGES to detect private IPs."""
diff --git a/tests/security/test_ssrf_validator.py b/tests/security/test_ssrf_validator.py
index 9dae48a6cb..f51d33a1ad 100644
--- a/tests/security/test_ssrf_validator.py
+++ b/tests/security/test_ssrf_validator.py
@@ -10,10 +10,11 @@ Security model:
- allow_private_ips=True: Allow all private/internal IPs + localhost:
- RFC1918: 10.x.x.x, 172.16-31.x.x, 192.168.x.x
- CGNAT: 100.64.x.x (used by Podman/rootless containers)
- - Link-local: 169.254.x.x (except AWS metadata)
+ - Link-local: 169.254.x.x (except cloud metadata endpoints)
- IPv6 ULA: fc00::/7
- IPv6 Link-local: fe80::/10
-- AWS metadata endpoint (169.254.169.254) is ALWAYS blocked
+- Cloud metadata endpoints (AWS IMDS / ECS, Azure, OCI, DigitalOcean,
+ AlibabaCloud, Tencent — see ALWAYS_BLOCKED_METADATA_IPS) are ALWAYS blocked
The allow_private_ips parameter is designed for trusted self-hosted services like
SearXNG or Ollama that may be running in containerized environments (Docker, Podman)
@@ -933,6 +934,138 @@ class TestAllowFlagMatrix:
)
+class TestAlwaysBlockedMetadataIPs:
+ """Cloud-metadata IPs blocked under every flag combination."""
+
+ def test_metadata_ip_blocked_under_all_flags(self):
+ """Every IP in the always-blocked set must be blocked under all
+ allow-flag combinations."""
+ from local_deep_research.security.ssrf_validator import (
+ ALWAYS_BLOCKED_METADATA_IPS,
+ is_ip_blocked,
+ )
+
+ for ip in sorted(ALWAYS_BLOCKED_METADATA_IPS):
+ assert is_ip_blocked(ip) is True
+ assert is_ip_blocked(ip, allow_localhost=True) is True
+ assert is_ip_blocked(ip, allow_private_ips=True) is True
+
+ def test_validate_url_blocks_all_metadata_ips_under_allow_private_ips(self):
+ """Same coverage end-to-end through validate_url."""
+ from local_deep_research.security.ssrf_validator import (
+ ALWAYS_BLOCKED_METADATA_IPS,
+ validate_url,
+ )
+
+ for ip in sorted(ALWAYS_BLOCKED_METADATA_IPS):
+ assert (
+ validate_url(f"http://{ip}/", allow_private_ips=True) is False
+ )
+
+ def test_dns_resolution_to_metadata_ip_blocked(self):
+ """A hostname that resolves to a metadata IP must also be blocked
+ even when allow_private_ips=True."""
+ from local_deep_research.security.ssrf_validator import (
+ validate_url,
+ )
+
+ with patch(
+ "socket.getaddrinfo",
+ return_value=[(2, 1, 6, "", ("169.254.170.2", 0))],
+ ):
+ assert (
+ validate_url("http://attacker.example/", allow_private_ips=True)
+ is False
+ )
+
+
+class TestRedactUrlForLog:
+ """The redact_url_for_log helper used at all log sites."""
+
+ def test_strips_userinfo(self):
+ from local_deep_research.security.ssrf_validator import (
+ redact_url_for_log,
+ )
+
+ assert (
+ redact_url_for_log("http://user:[email protected]/path?token=x")
+ == "http://example.com"
+ )
+
+ def test_strips_percent_encoded_password(self):
+ from local_deep_research.security.ssrf_validator import (
+ redact_url_for_log,
+ )
+
+ assert (
+ redact_url_for_log("http://u:p%[email protected]/")
+ == "http://example.com"
+ )
+
+ def test_keeps_port(self):
+ from local_deep_research.security.ssrf_validator import (
+ redact_url_for_log,
+ )
+
+ assert (
+ redact_url_for_log("http://example.com:8080/path")
+ == "http://example.com:8080"
+ )
+
+ def test_ipv6_host_keeps_brackets(self):
+ from local_deep_research.security.ssrf_validator import (
+ redact_url_for_log,
+ )
+
+ assert redact_url_for_log("http://[::1]:8080/") == "http://[::1]:8080"
+
+ def test_no_scheme_uses_question_mark(self):
+ """Scheme-relative URLs use '?' as the scheme sentinel."""
+ from local_deep_research.security.ssrf_validator import (
+ redact_url_for_log,
+ )
+
+ # urllib3 may parse '//example.com/path' with scheme=None.
+ result = redact_url_for_log("//example.com/path")
+ assert result.startswith("?://") or result == "<unparseable>"
+
+ def test_unparseable_returns_sentinel(self):
+ """urllib3 rejects malformed IPv6 brackets and out-of-range
+ ports; helper falls back to <unparseable>."""
+ from local_deep_research.security.ssrf_validator import (
+ redact_url_for_log,
+ )
+
+ assert redact_url_for_log("http://[::") == "<unparseable>"
+ assert redact_url_for_log("http://1.2.3.4:99999") == "<unparseable>"
+
+ def test_validate_url_log_does_not_leak_userinfo(self, loguru_caplog):
+ """End-to-end: validate_url's rejection log must not contain the
+ password from the URL's userinfo. Also assert at least one log
+ record was emitted, otherwise the not-in assertion is vacuously
+ true and we'd have false confidence."""
+ from local_deep_research.security.ssrf_validator import (
+ validate_url,
+ )
+
+ # Mock DNS to a public IP so the URL passes Layer 1+2 and reaches
+ # the IP-block log site (which does log the URL).
+ with (
+ loguru_caplog.at_level("WARNING"),
+ patch(
+ "socket.getaddrinfo",
+ return_value=[(2, 1, 6, "", ("127.0.0.1", 0))],
+ ),
+ ):
+ validate_url("http://user:[email protected]/")
+ assert "supersecret123" not in loguru_caplog.text, (
+ "Password leaked into log output"
+ )
+ # Anti-silent-pass: verify we actually did log (otherwise the
+ # not-in assertion above is trivially true on empty text).
+ assert len(loguru_caplog.records) > 0, "No log records emitted"
+
+
class TestSchemeRejection:
"""Non-http(s) schemes must be rejected outright (not just the host check)."""
diff --git a/tests/security/test_ssrf_validator_behavior.py b/tests/security/test_ssrf_validator_behavior.py
index e4602cbf1b..ed0a8b1a26 100644
--- a/tests/security/test_ssrf_validator_behavior.py
+++ b/tests/security/test_ssrf_validator_behavior.py
@@ -255,14 +255,16 @@ class TestAllowedSchemes:
assert "https" in ALLOWED_SCHEMES
-class TestAWSMetadataIP:
- """Tests for AWS_METADATA_IP constant."""
+class TestAlwaysBlockedMetadataIPs:
+ """Tests for ALWAYS_BLOCKED_METADATA_IPS constant."""
- def test_aws_metadata_ip_value(self):
- """AWS_METADATA_IP has correct value."""
- from local_deep_research.security.ssrf_validator import AWS_METADATA_IP
+ def test_aws_imds_in_always_blocked(self):
+ """AWS / Azure / OCI / DigitalOcean shared IMDS IP is in the set."""
+ from local_deep_research.security.ssrf_validator import (
+ ALWAYS_BLOCKED_METADATA_IPS,
+ )
- assert AWS_METADATA_IP == "169.254.169.254"
+ assert "169.254.169.254" in ALWAYS_BLOCKED_METADATA_IPS
class TestGetSafeUrl:
diff --git a/tests/security/test_ssrf_validator_extended.py b/tests/security/test_ssrf_validator_extended.py
index fa04e1e2b3..b12822cc15 100644
--- a/tests/security/test_ssrf_validator_extended.py
+++ b/tests/security/test_ssrf_validator_extended.py
@@ -2,7 +2,7 @@
Extended tests for the SSRF validator module.
Provides comprehensive coverage of is_ip_blocked, get_safe_url, validate_url,
-and module constants (AWS_METADATA_IP, ALLOWED_SCHEMES).
+and module constants (ALWAYS_BLOCKED_METADATA_IPS, ALLOWED_SCHEMES).
"""
from unittest.mock import patch
@@ -10,7 +10,7 @@ from unittest.mock import patch
import pytest
from local_deep_research.security.ssrf_validator import (
- AWS_METADATA_IP,
+ ALWAYS_BLOCKED_METADATA_IPS,
ALLOWED_SCHEMES,
get_safe_url,
is_ip_blocked,
@@ -23,16 +23,24 @@ from local_deep_research.security.ssrf_validator import (
# ---------------------------------------------------------------------------
-class TestAWSMetadataIPConstant:
- """Verify the AWS_METADATA_IP constant value."""
+class TestAlwaysBlockedMetadataIPsConstant:
+ """Verify the ALWAYS_BLOCKED_METADATA_IPS frozenset."""
- def test_aws_metadata_ip_is_correct_string(self):
- """AWS_METADATA_IP must be the well-known metadata endpoint."""
- assert AWS_METADATA_IP == "169.254.169.254"
+ def test_contains_aws_imds(self):
+ """AWS IMDS / Azure / OCI / DigitalOcean shared endpoint."""
+ assert "169.254.169.254" in ALWAYS_BLOCKED_METADATA_IPS
- def test_aws_metadata_ip_type(self):
- """AWS_METADATA_IP must be a plain string."""
- assert isinstance(AWS_METADATA_IP, str)
+ def test_contains_aws_ecs_v3_and_v4(self):
+ assert "169.254.170.2" in ALWAYS_BLOCKED_METADATA_IPS
+ assert "169.254.170.23" in ALWAYS_BLOCKED_METADATA_IPS
+
+ def test_contains_alibaba_and_tencent(self):
+ assert "100.100.100.200" in ALWAYS_BLOCKED_METADATA_IPS
+ assert "169.254.0.23" in ALWAYS_BLOCKED_METADATA_IPS
+
+ def test_is_frozenset(self):
+ """Must be immutable so tests can't accidentally mutate it."""
+ assert isinstance(ALWAYS_BLOCKED_METADATA_IPS, frozenset)
class TestAllowedSchemesConstant:
diff --git a/tests/security/test_ssrf_validator_high_value.py b/tests/security/test_ssrf_validator_high_value.py
index 5d22482333..76f41008c1 100644
--- a/tests/security/test_ssrf_validator_high_value.py
+++ b/tests/security/test_ssrf_validator_high_value.py
@@ -3,6 +3,8 @@
import socket
from unittest.mock import patch
+import pytest
+
from local_deep_research.security import ssrf_validator
from local_deep_research.security.ssrf_validator import (
@@ -10,7 +12,7 @@ from local_deep_research.security.ssrf_validator import (
validate_url,
get_safe_url,
ALLOWED_SCHEMES,
- AWS_METADATA_IP,
+ ALWAYS_BLOCKED_METADATA_IPS,
)
@@ -85,17 +87,20 @@ class TestIsIpBlockedSpecialRanges:
assert is_ip_blocked("127.0.0.1", allow_private_ips=True) is False
-class TestIsIpBlockedAWSMetadata:
- """AWS metadata endpoint always blocked."""
+class TestIsIpBlockedCloudMetadata:
+ """Cloud-provider metadata endpoints always blocked under all flags."""
- def test_aws_metadata_blocked_default(self):
- assert is_ip_blocked(AWS_METADATA_IP) is True
+ @pytest.mark.parametrize("ip", sorted(ALWAYS_BLOCKED_METADATA_IPS))
+ def test_metadata_ip_blocked_default(self, ip):
+ assert is_ip_blocked(ip) is True
- def test_aws_metadata_blocked_even_with_allow_localhost(self):
- assert is_ip_blocked(AWS_METADATA_IP, allow_localhost=True) is True
+ @pytest.mark.parametrize("ip", sorted(ALWAYS_BLOCKED_METADATA_IPS))
+ def test_metadata_ip_blocked_with_allow_localhost(self, ip):
+ assert is_ip_blocked(ip, allow_localhost=True) is True
- def test_aws_metadata_blocked_even_with_allow_private(self):
- assert is_ip_blocked(AWS_METADATA_IP, allow_private_ips=True) is True
+ @pytest.mark.parametrize("ip", sorted(ALWAYS_BLOCKED_METADATA_IPS))
+ def test_metadata_ip_blocked_with_allow_private_ips(self, ip):
+ assert is_ip_blocked(ip, allow_private_ips=True) is True
class TestIsIpBlockedIPv4Mapped:
@@ -275,5 +280,15 @@ class TestConstants:
def test_allowed_schemes_http_https(self):
assert ALLOWED_SCHEMES == {"http", "https"}
- def test_aws_metadata_ip_value(self):
- assert AWS_METADATA_IP == "169.254.169.254"
+ def test_always_blocked_metadata_ips_membership(self):
+ """Lock in the exact membership of the always-blocked set so a
+ future contributor accidentally removing an IP fails loudly."""
+ assert ALWAYS_BLOCKED_METADATA_IPS == frozenset(
+ {
+ "169.254.169.254",
+ "169.254.170.2",
+ "169.254.170.23",
+ "169.254.0.23",
+ "100.100.100.200",
+ }
+ )