~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""SSRF / egress-policy census for the FastAPI port (PR #3299).
The SSRF machinery itself (``security/ssrf_validator.py``,
``security/safe_requests.py``, ``security/dns_pinning.py``,
``security/egress/``) carries no port-driven change on this branch — what it
has picked up from ``origin/main`` is additive hardening (``#5086``'s
pre-DNS legacy-numeric-host guard, ``validate_url``'s ``block_link_local``
opt-in) — so the question this file answers is NOT "is the validator
correct": ``test_ssrf_validator*.py`` and ``test_safe_requests_redirects.py``
already cover that. The question is **whether every call site still goes
through the gate after the Flask -> FastAPI rewrite**, and whether the gate
is re-applied where an attacker gets a second bite (redirects).
Four properties are asserted here:
1. **No ungated egress primitive exists in the package.** A repo-wide AST
sweep for ``requests`` / ``httpx`` / ``aiohttp`` / ``urllib3`` /
``urlopen`` call sites. Broader than the ``check-safe-requests``
pre-commit hook, which only knows about ``requests`` and only runs on
staged files.
2. **Every directly consumed user-controlled URL in the ported web layer
still names its gate** in the handler that consumes it. Model discovery
delegates validation to provider implementations and is covered by a
behavior-level route/provider regression instead of this syntax census.
3. **Redirect targets are re-validated per hop** — the initial URL passing
the gate does not license the hop that follows it.
4. **The most permissive user-facing gate**
(``is_safe_custom_llm_endpoint``, ``allow_private_ips=True``, reached
from three routers) rejects the classic bypass corpus: alternate IP
encodings, IPv4-mapped IPv6, userinfo host confusion, the backslash
parser differential, and non-HTTP schemes.
No test in this file opens a socket. Section 4 replaces the resolver with
one pinned to ``AI_NUMERICHOST``, so the alternate-encoding cases are
canonicalised by the platform's ``inet_aton`` with provably zero DNS
traffic; sections 3 stubs the HTTP client and the DNS pinner outright.
"""
import ast
import socket
import textwrap
from pathlib import Path
import pytest
import requests
import local_deep_research
from local_deep_research.security import safe_requests, ssrf_validator
from local_deep_research.security.legacy_ipv4 import (
is_ambiguous_numeric_ipv4_host,
)
from local_deep_research.security.ssrf_validator import (
ALWAYS_BLOCKED_METADATA_IPS,
validate_url,
)
from local_deep_research.utilities.url_utils import (
is_safe_custom_llm_endpoint,
)
PACKAGE_ROOT = Path(local_deep_research.__file__).resolve().parent
WEB_ROOT = PACKAGE_ROOT / "web"
# Payloads live in variables, never inline in comments (repo convention:
# docs/source scanners flag literal SSRF targets written as text).
IMDS_HOST = "169.254.169.254"
# Documentation-range addresses (RFC 5737 TEST-NET-2 / TEST-NET-3). Neither
# is inside any range in ``security.ip_ranges.PRIVATE_IP_RANGES``, so they
# are the "allowed" half of every positive control below. They are numeric,
# so resolving them never leaves the host.
PUBLIC_A = "203.0.113.9"
PUBLIC_B = "198.51.100.7"
RFC1918_HOST = "192.168.10.4"
def test_imds_host_constant_matches_production_blocklist():
"""Guard the payload constant against drifting away from the code it
is meant to probe. If the production frozenset ever renames or drops
this entry, every "blocked" assertion below would still pass while
silently testing nothing."""
assert IMDS_HOST in ALWAYS_BLOCKED_METADATA_IPS
# ---------------------------------------------------------------------
# 1. Egress-primitive census
# ---------------------------------------------------------------------
_HTTP_VERBS = frozenset(
{
"get",
"post",
"put",
"delete",
"patch",
"head",
"options",
"request",
"stream",
}
)
_CLIENT_CTORS = frozenset(
{"Session", "Client", "AsyncClient", "ClientSession", "PoolManager"}
)
_CLIENT_MODULES = frozenset({"requests", "httpx", "aiohttp", "urllib3"})
class _RawEgressScanner(ast.NodeVisitor):
"""Collect call sites that reach the network without the SSRF wrapper.
Deliberately structural rather than textual: it sees
``httpx.AsyncClient()`` and ``from requests import post`` — neither of
which the ``check-safe-requests`` hook's ``requests``-only patterns
catch — and it does not fire on ``safe_get`` / ``safe_post`` /
``SafeSession``, nor on ``.get()`` called on a *SafeSession* instance.
"""
def __init__(self) -> None:
self.hits: list[tuple[int, str]] = []
def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
if node.module in _CLIENT_MODULES:
for alias in node.names:
if alias.name in _HTTP_VERBS or alias.name in _CLIENT_CTORS:
self.hits.append(
(node.lineno, f"from {node.module} import {alias.name}")
)
self.generic_visit(node)
def visit_Call(self, node: ast.Call) -> None:
func = node.func
if isinstance(func, ast.Attribute):
if (
isinstance(func.value, ast.Name)
and func.value.id in _CLIENT_MODULES
and (func.attr in _HTTP_VERBS or func.attr in _CLIENT_CTORS)
):
self.hits.append(
(node.lineno, f"{func.value.id}.{func.attr}()")
)
elif func.attr == "urlopen":
self.hits.append((node.lineno, "urlopen()"))
elif isinstance(func, ast.Name) and func.id == "urlopen":
self.hits.append((node.lineno, "urlopen()"))
self.generic_visit(node)
def _scan_source(source: str, filename: str = "<probe>") -> list[str]:
scanner = _RawEgressScanner()
scanner.visit(ast.parse(source, filename=filename))
return [what for _lineno, what in scanner.hits]
def _scan_tree(root: Path) -> dict[str, list[str]]:
found: dict[str, list[str]] = {}
for path in sorted(root.rglob("*.py")):
hits = _scan_source(
path.read_text(encoding="utf-8"), filename=str(path)
)
if hits:
found[str(path.relative_to(PACKAGE_ROOT))] = hits
return found
_RAW_PRIMITIVE_PROBE = textwrap.dedent(
"""
import aiohttp
import httpx
import requests
import urllib.request
from requests import post
from urllib.request import urlopen
def go(url):
requests.get(url)
requests.Session().get(url)
httpx.AsyncClient()
aiohttp.ClientSession()
urllib.request.urlopen(url)
urlopen(url)
post(url)
"""
)
_GATED_PROBE = textwrap.dedent(
"""
from local_deep_research.security import SafeSession, safe_get, safe_post
def go(url):
safe_get(url)
safe_post(url, json={})
with SafeSession() as session:
session.get(url)
session.post(url)
"""
)
def test_scanner_detects_every_raw_egress_primitive():
"""Control for the sweep below: an empty result must mean "clean", not
"the detector is blind"."""
hits = _scan_source(_RAW_PRIMITIVE_PROBE)
assert "requests.get()" in hits
assert "requests.Session()" in hits
assert "httpx.AsyncClient()" in hits
assert "aiohttp.ClientSession()" in hits
assert "from requests import post" in hits
assert hits.count("urlopen()") >= 2
def test_scanner_ignores_wrapper_backed_calls():
"""Second control, the other direction: the detector must not fire on
the sanctioned helpers, or the sweep would be unsatisfiable and the
allowlist below meaningless."""
assert _scan_source(_GATED_PROBE) == []
# The only modules in the package allowed to touch a raw HTTP client.
# Anything else appearing here is a new ungated egress path.
_REVIEWED_RAW_EGRESS = {
# The SSRF wrapper itself: validates + DNS-pins, then calls requests.
"security/safe_requests.py": {"requests.get()", "requests.post()"},
# Constructs an aiohttp session in __aenter__ and closes it in
# __aexit__ but never issues a request through it — every pricing
# lookup is served from the in-process static table. Listed (not
# ignored) because a future edit that starts using this session would
# bypass both the SSRF validator and the check-safe-requests hook,
# which only understands `requests`.
"metrics/pricing/pricing_fetcher.py": {"aiohttp.ClientSession()"},
}
def test_only_reviewed_modules_hold_a_raw_http_client():
"""Package-wide census. Equality (not subset) in both directions: a new
offender fails, and an allowlist entry that no longer exists fails too,
so the allowlist cannot rot into a no-op."""
found = {
module: set(hits) for module, hits in _scan_tree(PACKAGE_ROOT).items()
}
assert found == _REVIEWED_RAW_EGRESS
def test_ported_web_layer_holds_no_raw_http_client():
"""PR-scoped restatement: the whole rewritten web layer (routers,
services, dependencies, fastapi_app) reaches the network only through
safe_get / safe_post / SafeSession."""
assert _scan_tree(WEB_ROOT) == {}
# ---------------------------------------------------------------------
# 2. Per-entry-point gate census
# ---------------------------------------------------------------------
def _called_names(source: str, func_name: str) -> set[str]:
"""Names invoked anywhere inside ``func_name``, nested defs included.
Nesting matters: ``api_add_resource`` does its work in a nested
``_impl()`` that it hands to a worker thread, so a body-only scan
would miss the gate. ``func_name`` may also name a class (a
callable-class WeasyPrint fetcher), scanning every method the same
way -- or, as ``"ClassName.method_name"``, a single method inside a
class, when a census entry needs to pin the gate to the one method
that actually runs it rather than to the class as a whole.
"""
tree = ast.parse(source)
if "." in func_name:
class_name, method_name = func_name.split(".", 1)
class_node = None
for node in ast.walk(tree):
if isinstance(node, ast.ClassDef) and node.name == class_name:
class_node = node
break
assert class_node is not None, f"class {class_name!r} not found"
target = None
for node in ast.walk(class_node):
if (
isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))
and node.name == method_name
):
target = node
break
assert target is not None, (
f"method {method_name!r} not found in class {class_name!r}"
)
else:
target = None
for node in ast.walk(tree):
if (
isinstance(
node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)
)
and node.name == func_name
):
target = node
break
assert target is not None, f"function {func_name!r} not found"
names: set[str] = set()
for node in ast.walk(target):
if isinstance(node, ast.Call):
func = node.func
if isinstance(func, ast.Name):
names.add(func.id)
elif isinstance(func, ast.Attribute):
names.add(func.attr)
return names
_UNGATED_PROBE = textwrap.dedent(
"""
def handler(url):
return fetch_it(url)
"""
)
_NESTED_GATE_PROBE = textwrap.dedent(
"""
def handler(url):
def _impl():
if not validate_url(url):
return None
return fetch_it(url)
return _impl()
"""
)
# A class with two methods, only one of which gates -- the shape
# ``_SafeUrlFetcher``/``_SkippingUrlFetcher`` share in pdf_service.py, and
# what the dotted "ClassName.method_name" form of ``_called_names`` needs
# to tell apart.
_CLASS_MIXED_METHODS_PROBE = textwrap.dedent(
"""
class Handler:
def gated(self, url):
if not validate_url(url):
return None
return fetch_it(url)
def ungated(self, url):
return fetch_it(url)
"""
)
def test_gate_locator_distinguishes_gated_from_ungated():
"""Control for the parametrised census below."""
assert "validate_url" not in _called_names(_UNGATED_PROBE, "handler")
assert "validate_url" in _called_names(_NESTED_GATE_PROBE, "handler")
def test_gate_locator_scopes_dotted_name_to_one_method():
"""Control for the dotted ``ClassName.method_name`` form.
Scanning the whole class (undotted) would find ``validate_url`` via
``gated`` even when asked about ``ungated`` -- exactly the false
pass a census entry pinned to a specific method (like
``_SafeUrlFetcher.__call__``) needs to avoid.
"""
assert "validate_url" in _called_names(
_CLASS_MIXED_METHODS_PROBE, "Handler.gated"
)
assert "validate_url" not in _called_names(
_CLASS_MIXED_METHODS_PROBE, "Handler.ungated"
)
# Sanity: the undotted, whole-class form still sees both.
assert "validate_url" in _called_names(
_CLASS_MIXED_METHODS_PROBE, "Handler"
)
# (module, handler, gate) for every place a user-supplied URL — request
# body, or a settings value the user edits through the UI — reaches an
# outbound request directly in the ported web layer. Available-model discovery
# is intentionally absent: the handler delegates to provider implementations,
# and ``test_force_refresh_uses_one_guarded_ollama_provider_path`` drives that
# route through a real provider with both allowed and metadata-target URLs.
_GATED_ENTRY_POINTS = [
# POST /api/resources/{id} — "url" straight out of the request body.
("web/routers/api.py", "api_add_resource", "validate_url"),
# GET /api/check/ollama_status and the model-availability probe both
# funnel through this single helper (llm.ollama.url).
("web/routers/api.py", "_probe_ollama_tags", "safe_get"),
# GET /settings/api/ollama-status (llm.ollama.url).
("web/routers/settings.py", "check_ollama_status", "safe_get"),
# POST /research/api/start — custom_endpoint (OpenAI-compatible
# base_url) from the request body or settings.
(
"web/routers/research.py",
"_extract_research_params",
"is_safe_custom_llm_endpoint",
),
(
"web/routers/research.py",
"_start_research_sync",
"is_safe_custom_llm_endpoint",
),
# POST /api/followup/start — same URL, read from the settings snapshot.
(
"web/routers/followup.py",
"_start_followup_sync",
"is_safe_custom_llm_endpoint",
),
# News subscription create/update — custom_endpoint.
(
"web/routers/news_flask_api.py",
"_reject_custom_endpoint",
"is_safe_custom_llm_endpoint",
),
# WeasyPrint resource fetching during PDF export. Pinned to
# ``__call__`` specifically, not the whole class: ``_SafeUrlFetcher``
# also carries a ``_fail_on_errors`` class attribute (see #6685 for
# why it's a class rather than a bare function), and a whole-class
# scan would keep passing even if a refactor moved the
# ``validate_url`` call out of ``__call__`` while leaving some other
# attribute or method referencing the name.
(
"web/services/pdf_service.py",
"_SafeUrlFetcher.__call__",
"validate_url",
),
]
@pytest.mark.parametrize(
"module_rel,handler,gate",
_GATED_ENTRY_POINTS,
ids=[f"{h}:{g}" for _m, h, g in _GATED_ENTRY_POINTS],
)
def test_user_url_entry_point_still_names_its_gate(module_rel, handler, gate):
source = (PACKAGE_ROOT / module_rel).read_text(encoding="utf-8")
assert gate in _called_names(source, handler), (
f"{module_rel}::{handler} no longer calls {gate}() — a "
f"user-supplied URL now reaches egress ungated"
)
def test_notification_test_endpoint_keeps_both_operator_gates():
"""POST /settings/api/notifications/test-url.
The Flask original built ``NotificationService`` with both env-only
switches wired in, so the "Send test notification" button could not
bypass the operator's risk decision: ``outbound_allowed`` is the
master kill switch (default off) and ``allow_private_ips`` decides
whether LAN webhooks are reachable. Dropping either keyword in the
port would silently flip the default — ``allow_private_ips`` to False
is merely broken, but a hardcoded True would open the whole private
network to an authenticated user.
"""
source = (PACKAGE_ROOT / "web/routers/settings.py").read_text(
encoding="utf-8"
)
tree = ast.parse(source)
handler = None
for node in ast.walk(tree):
if (
isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))
and node.name == "api_test_notification_url"
):
handler = node
break
assert handler is not None
constructions = [
node
for node in ast.walk(handler)
if isinstance(node, ast.Call)
and isinstance(node.func, ast.Name)
and node.func.id == "NotificationService"
]
assert len(constructions) == 1, (
"expected exactly one NotificationService construction in the "
"test-url handler"
)
keywords = {kw.arg for kw in constructions[0].keywords}
assert {"outbound_allowed", "allow_private_ips"} <= keywords
# Both must be *derived*, not literals: a bare `True` would hardcode
# the operator's decision into the code.
for keyword in constructions[0].keywords:
if keyword.arg in ("outbound_allowed", "allow_private_ips"):
assert not isinstance(keyword.value, ast.Constant), (
f"{keyword.arg} is hardcoded in the test-url handler; it "
f"must be read from the env registry"
)
# ---------------------------------------------------------------------
# 3. Redirect re-validation
# ---------------------------------------------------------------------
class _FakeRaw:
def read(self, amt=None, *args, **kwargs): # noqa: D102
return b""
class _FakeResponse:
"""Enough of ``requests.Response`` for safe_get's redirect loop and
``_check_response_size`` (which installs a body guard on ``.raw``
when Content-Length is absent)."""
def __init__(self, status_code, url, location=None, body=b"ok"):
self.status_code = status_code
self.url = url
self.headers = {}
if location is not None:
self.headers["Location"] = location
self.content = body
self.raw = _FakeRaw()
self.closed = False
def close(self):
self.closed = True
class _NullPin:
"""Stand-in for ``dns_pinning.pinned_request``. The real one resolves
the hostname; stubbing it keeps this section socket-free."""
def __init__(self, *args, **kwargs):
pass
def __enter__(self):
return self
def __exit__(self, *exc):
return False
@pytest.fixture
def stub_http(monkeypatch):
"""Replace safe_requests' HTTP client and DNS pinner. Returns the call
log so a test can assert which hops were actually dialled."""
calls: list[tuple[str, dict]] = []
queue: list[_FakeResponse] = []
def fake_get(url, **kwargs):
calls.append((url, kwargs))
assert queue, f"unexpected outbound request to {url}"
return queue.pop(0)
def fake_post(url, **kwargs):
calls.append((url, kwargs))
assert queue, f"unexpected outbound request to {url}"
return queue.pop(0)
monkeypatch.setattr(safe_requests.requests, "get", fake_get)
monkeypatch.setattr(safe_requests.requests, "post", fake_post)
monkeypatch.setattr(safe_requests.dns_pinning, "pinned_request", _NullPin)
return {"calls": calls, "queue": queue}
def test_redirect_from_public_host_to_metadata_is_blocked(stub_http):
"""The parent question, stated directly: the gate is re-applied after
the redirect, not only to the URL the caller passed in."""
start = f"http://{PUBLIC_A}/start"
target = f"http://{IMDS_HOST}/latest/meta-data/"
stub_http["queue"].append(_FakeResponse(302, url=start, location=target))
with pytest.raises(ValueError) as excinfo:
safe_requests.safe_get(start)
assert "Redirect target failed SSRF validation" in str(excinfo.value)
# The refused target is named by origin only (#6541): the exception
# text reaches caller logs, and a URL path/query can carry tokens.
assert f"http://{IMDS_HOST}" in str(excinfo.value)
assert "/latest/meta-data/" not in str(excinfo.value)
# The decisive assertion: the second hop was never dialled. A gate
# that raises *after* connecting has not prevented the SSRF.
assert [url for url, _kw in stub_http["calls"]] == [start]
def test_redirect_from_public_host_to_public_host_is_followed(stub_http):
"""Positive control for the test above. Without it, "blocked" could
just mean safe_get refuses every redirect."""
start = f"http://{PUBLIC_A}/start"
target = f"http://{PUBLIC_B}/final"
stub_http["queue"].append(_FakeResponse(302, url=start, location=target))
stub_http["queue"].append(_FakeResponse(200, url=target, body=b"final"))
response = safe_requests.safe_get(start)
assert response.status_code == 200
assert response.content == b"final"
assert [url for url, _kw in stub_http["calls"]] == [start, target]
def test_redirect_to_metadata_is_blocked_even_under_allow_private_ips(
stub_http,
):
"""Callers that legitimately need LAN targets (the Ollama and SearXNG
probes pass ``allow_private_ips=True``) must not thereby inherit a
route to cloud-credential endpoints via a redirect."""
start = f"http://{RFC1918_HOST}:11434/api/tags"
target = f"http://{IMDS_HOST}/latest/meta-data/iam/"
stub_http["queue"].append(_FakeResponse(302, url=start, location=target))
with pytest.raises(ValueError) as excinfo:
safe_requests.safe_get(
start, allow_localhost=True, allow_private_ips=True
)
assert "Redirect target failed SSRF validation" in str(excinfo.value)
assert [url for url, _kw in stub_http["calls"]] == [start]
def test_allow_private_ips_still_permits_a_lan_to_lan_redirect(stub_http):
"""Positive control for the test above: the private-IP opt-in is not
globally disabled by the metadata carve-out."""
start = f"http://{RFC1918_HOST}:11434/api/tags"
target = f"http://{RFC1918_HOST}:11434/api/tags/v2"
stub_http["queue"].append(_FakeResponse(302, url=start, location=target))
stub_http["queue"].append(_FakeResponse(200, url=target, body=b"[]"))
response = safe_requests.safe_get(
start, allow_localhost=True, allow_private_ips=True
)
assert response.status_code == 200
assert [url for url, _kw in stub_http["calls"]] == [start, target]
# ---------------------------------------------------------------------
# 3b. Credentials across a re-validated redirect
# ---------------------------------------------------------------------
def test_safesession_strips_authorization_on_cross_host_redirect():
"""``SafeSession`` delegates redirects to ``requests.resolve_redirects``
and therefore inherits ``Session.rebuild_auth``, which deletes the
Authorization header when the hop changes host. This is the reference
behaviour the standalone helpers are compared against below."""
session = safe_requests.SafeSession()
original = requests.Request(
"GET",
f"http://{PUBLIC_A}/start",
headers={"Authorization": "token SECRET"},
).prepare()
hop = requests.Request(
"GET",
f"http://{PUBLIC_B}/next",
headers={"Authorization": "token SECRET"},
).prepare()
response = requests.Response()
response.request = original
session.rebuild_auth(hop, response)
assert "Authorization" not in hop.headers
# Same-host hop must keep it, or the check above proves nothing.
same_host = requests.Request(
"GET",
f"http://{PUBLIC_A}/next",
headers={"Authorization": "token SECRET"},
).prepare()
session.rebuild_auth(same_host, response)
assert same_host.headers["Authorization"] == "token SECRET"
_CREDS = {
"Authorization": "token SECRET",
"X-Api-Key": "SECRET-VENDOR-KEY",
"X-Goog-Api-Key": "SECRET-GOOGLE-KEY",
}
def _hop_headers(stub_http, index=1):
"""Headers actually dialled on hop ``index`` of the recorded call log."""
_url, kwargs = stub_http["calls"][index]
return {k.lower(): v for k, v in (kwargs.get("headers") or {}).items()}
def test_safe_get_strips_credentials_on_cross_host_redirect(stub_http):
"""The standalone helpers hand-roll their redirect loop, so they do not
inherit the ``rebuild_auth`` behaviour pinned above. Authorization is the
header requests knows; the vendor keys are the ones the keyed search
engines actually send."""
start = f"http://{PUBLIC_A}/start"
target = f"http://{PUBLIC_B}/final"
stub_http["queue"].append(_FakeResponse(302, url=start, location=target))
stub_http["queue"].append(_FakeResponse(200, url=target))
safe_requests.safe_get(start, headers=dict(_CREDS))
hop_url, _kwargs = stub_http["calls"][1]
assert hop_url == target
headers = _hop_headers(stub_http)
assert "authorization" not in headers
assert "x-api-key" not in headers
assert "x-goog-api-key" not in headers
# The hop still happens, and still carries the non-credential headers.
assert headers["user-agent"] == safe_requests.USER_AGENT
def test_safe_get_keeps_credentials_on_same_host_redirect(stub_http):
"""Positive control. Without it, the test above is satisfied by a helper
that strips credentials from every hop, which would break every keyed
engine whose endpoint redirects within its own host."""
start = f"http://{PUBLIC_A}/start"
target = f"http://{PUBLIC_A}/final"
stub_http["queue"].append(_FakeResponse(302, url=start, location=target))
stub_http["queue"].append(_FakeResponse(200, url=target))
safe_requests.safe_get(start, headers=dict(_CREDS))
headers = _hop_headers(stub_http)
assert headers["authorization"] == "token SECRET"
assert headers["x-api-key"] == "SECRET-VENDOR-KEY"
def test_safe_get_does_not_mutate_the_caller_headers(stub_http):
"""Engines keep one headers dict on ``self`` and reuse it for every
search, so stripping must not reach back into the caller's object: an
in-place drop would disarm the key for all later calls, not just this
hop. The caller supplies its own User-Agent here because that is the
real shape (NASA ADS and Paperless both do) and the only one where
safe_get forwards the caller's dict rather than a copy of it.
"""
start = f"http://{PUBLIC_A}/start"
target = f"http://{PUBLIC_B}/final"
stub_http["queue"].append(_FakeResponse(302, url=start, location=target))
stub_http["queue"].append(_FakeResponse(200, url=target))
caller_headers = dict(_CREDS, **{"User-Agent": "engine/1.0"})
expected = dict(caller_headers)
safe_requests.safe_get(start, headers=caller_headers)
assert caller_headers == expected
# ...and the hop itself still dropped them.
assert "x-api-key" not in _hop_headers(stub_http)
def test_safe_post_strips_credentials_on_downgraded_redirect(stub_http):
"""302 converts POST to GET; the credential must not ride the new verb."""
start = f"http://{PUBLIC_A}/start"
target = f"http://{PUBLIC_B}/final"
stub_http["queue"].append(_FakeResponse(302, url=start, location=target))
stub_http["queue"].append(_FakeResponse(200, url=target))
safe_requests.safe_post(start, json={"q": 1}, headers=dict(_CREDS))
headers = _hop_headers(stub_http)
assert "authorization" not in headers
assert "x-api-key" not in headers
def test_safe_post_strips_credentials_on_method_preserving_redirect(stub_http):
"""307 keeps the method, so it takes the other branch of the redirect
loop and needs its own case. Bodiless, because a body on this branch
stops the cross-host hop outright (#6265,
``test_ssrf_redirect_bypass.TestSafePostBodyScope``)."""
start = f"http://{PUBLIC_A}/start"
target = f"http://{PUBLIC_B}/final"
stub_http["queue"].append(_FakeResponse(307, url=start, location=target))
stub_http["queue"].append(_FakeResponse(200, url=target))
safe_requests.safe_post(start, headers=dict(_CREDS))
headers = _hop_headers(stub_http)
assert "authorization" not in headers
assert "x-api-key" not in headers
def test_safesession_strips_vendor_api_key_on_cross_host_redirect():
"""``Session.rebuild_auth`` deletes Authorization and nothing else, so a
session-default vendor key (the Semantic Scholar engine sets ``x-api-key``
on the session) would otherwise outlive the hop."""
session = safe_requests.SafeSession()
original = requests.Request(
"GET", f"http://{PUBLIC_A}/start", headers=dict(_CREDS)
).prepare()
hop = requests.Request(
"GET", f"http://{PUBLIC_B}/next", headers=dict(_CREDS)
).prepare()
response = requests.Response()
response.request = original
session.rebuild_auth(hop, response)
assert "X-Api-Key" not in hop.headers
assert "X-Goog-Api-Key" not in hop.headers
# Same-host hop keeps it, or the check above proves nothing.
same_host = requests.Request(
"GET", f"http://{PUBLIC_A}/next", headers=dict(_CREDS)
).prepare()
session.rebuild_auth(same_host, response)
assert same_host.headers["X-Api-Key"] == "SECRET-VENDOR-KEY"
def test_safesession_keeps_netrc_credentials_for_the_new_host(monkeypatch):
"""``Session.rebuild_auth`` reapplies netrc credentials for the redirect
target after dropping the old ones. The override strips first and calls it
second, so the credential requests installs for the new host stands."""
monkeypatch.setattr(
requests.sessions,
"get_netrc_auth",
lambda url: ("netrc-user", "netrc-pass"),
)
session = safe_requests.SafeSession()
original = requests.Request(
"GET", f"http://{PUBLIC_A}/start", headers=dict(_CREDS)
).prepare()
hop = requests.Request(
"GET", f"http://{PUBLIC_B}/next", headers=dict(_CREDS)
).prepare()
response = requests.Response()
response.request = original
session.rebuild_auth(hop, response)
assert hop.headers["Authorization"].startswith("Basic ")
assert "X-Api-Key" not in hop.headers
def test_safe_get_strips_cookie_on_cross_host_redirect(stub_http):
"""A caller-supplied literal Cookie is re-sent verbatim by the hand-rolled
loop. ``SafeSession`` rebuilds Cookie from the domain-scoped jar on every
hop and needs no help; these helpers have no jar to rescope."""
start = f"http://{PUBLIC_A}/start"
target = f"http://{PUBLIC_B}/final"
stub_http["queue"].append(_FakeResponse(302, url=start, location=target))
stub_http["queue"].append(_FakeResponse(200, url=target))
safe_requests.safe_get(start, headers={"Cookie": "session=SECRET"})
assert "cookie" not in _hop_headers(stub_http)
# ---------------------------------------------------------------------
# 4. Classic bypass corpus against the most permissive user-facing gate
# ---------------------------------------------------------------------
@pytest.fixture
def numeric_only_resolver(monkeypatch):
"""Pin name resolution to ``AI_NUMERICHOST``.
The validator ends in ``socket.getaddrinfo``. Forcing AI_NUMERICHOST
means the platform resolver still canonicalises alternate IPv4
literals (decimal / octal / hex / short form, via ``inet_aton``) but
provably issues no DNS query: a non-numeric host raises
``gaierror`` instead of going to the network. ``localhost`` is served
from a static map so the loopback positive control still works.
Returns the call log, so a test can prove *why* a URL was rejected —
"blocked because the resolver reported a metadata address" is a very
different result from "blocked because resolution failed".
"""
real_getaddrinfo = socket.getaddrinfo
log: list[tuple[str, object]] = []
def resolver(host, port, family=0, type=0, proto=0, flags=0):
if host == "localhost":
result = [
(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("127.0.0.1", 0))
]
log.append((host, "127.0.0.1"))
return result
try:
result = real_getaddrinfo(
host,
port,
family,
type,
proto,
flags | socket.AI_NUMERICHOST,
)
except socket.gaierror as exc:
log.append((host, exc))
raise
log.append((host, result[0][4][0]))
return result
monkeypatch.setattr(socket, "getaddrinfo", resolver)
return log
_BLOCKED_ENDPOINTS = {
"metadata_literal": f"http://{IMDS_HOST}/latest/meta-data/",
# IPv4-mapped IPv6 wrapper around the same target.
"ipv4_mapped_ipv6": f"http://[::ffff:{IMDS_HOST}]/",
# Userinfo host confusion: a public-looking authority whose real host
# is the part after '@'.
"userinfo_confusion": f"http://{PUBLIC_A}@{IMDS_HOST}/",
# Parser differential: urlparse reads the backslash as a literal and
# reports the trailing host, urllib3 (what requests uses) reads it as
# a path delimiter and connects to the leading one.
"backslash_differential": f"http://{IMDS_HOST}\\@{PUBLIC_A}/",
}
_BLOCKED_ENCODINGS = {
# 169.254.169.254 written three other ways inet_aton accepts.
"decimal": "2852039166",
"octal": "0251.0376.0251.0376",
"hex": "0xa9.0xfe.0xa9.0xfe",
}
@pytest.mark.parametrize(
"endpoint",
list(_BLOCKED_ENDPOINTS.values()),
ids=list(_BLOCKED_ENDPOINTS),
)
def test_custom_llm_endpoint_gate_rejects_metadata_targets(
endpoint, numeric_only_resolver
):
"""``is_safe_custom_llm_endpoint`` is the widest user-facing gate:
it runs with ``allow_private_ips=True`` (local LLMs live on loopback
and RFC1918), so the metadata carve-out is the only thing standing
between an authenticated user and instance credentials."""
assert is_safe_custom_llm_endpoint(endpoint) is False
# Two layers now stand between these encodings and the metadata service, and
# each is asserted on its own below so that a regression in either is visible
# by itself:
#
# (a) the pre-DNS guard from #5086 (``security/legacy_ipv4.py``), which
# recognises the historical inet_aton grammar and refuses it outright;
# (b) the resolver check, which canonicalises whatever survives (a) and
# compares the answer against the blocklist.
#
# This file originally asserted (b) alone, because (b) was all there was when
# it was written (2026-08-26); #5086 merged in two days later and (a) now
# short-circuits ahead of it. The contract the original test name stated —
# blocked *deliberately*, never merely because a lookup blew up — is what
# both tests below preserve.
@pytest.mark.parametrize(
"host",
list(_BLOCKED_ENCODINGS.values()),
ids=list(_BLOCKED_ENCODINGS),
)
def test_alternate_ip_encodings_are_blocked_before_any_resolution(
host, numeric_only_resolver
):
"""Layer (a). Alternate IPv4 literals are not IP-parseable by
``ipaddress``, so they survive the literal check; the pre-DNS guard then
recognises the encoding for what it is and refuses it without a lookup.
Asserting the *recognition* (not just the ``False``) is what keeps this
from being satisfied by the far weaker "resolution blew up": a host that
the guard does not classify would fall through to the resolver, and the
empty call log below would then be a failure rather than a pass.
"""
assert is_ambiguous_numeric_ipv4_host(host) is True
assert is_safe_custom_llm_endpoint(host) is False
assert numeric_only_resolver == []
@pytest.mark.parametrize(
"host",
list(_BLOCKED_ENCODINGS.values()),
ids=list(_BLOCKED_ENCODINGS),
)
def test_alternate_ip_encodings_are_also_blocked_by_resolution(
host, numeric_only_resolver, monkeypatch
):
"""Layer (b), reached by neutralising layer (a).
With the pre-DNS guard stubbed out, the encoding has to be caught the way
it was before #5086: the resolver canonicalises it and the answer is
matched against the blocklist. Assert that is what happened — the
resolver was consulted, it returned the metadata address, and *that* is
why the URL was refused — as opposed to the far weaker "resolution blew
up", which would leave the host absent from the answered set below.
"""
monkeypatch.setattr(
ssrf_validator, "is_ambiguous_numeric_ipv4_host", lambda _host: False
)
monkeypatch.setattr(
ssrf_validator,
"is_percent_encoded_numeric_ipv4_host",
lambda _host: False,
)
assert is_safe_custom_llm_endpoint(host) is False
resolved = {
queried: answer
for queried, answer in numeric_only_resolver
if not isinstance(answer, Exception)
}
assert host in resolved, (
f"{host} was refused without the resolver ever answering — the "
f"block is not attributable to the encoding being unwrapped"
)
assert resolved[host] == IMDS_HOST
@pytest.mark.parametrize(
"scheme", ["file", "gopher", "ftp", "dict", "jar", "netdoc"]
)
def test_non_http_schemes_are_rejected_before_any_resolution(
scheme, numeric_only_resolver
):
"""Scheme enforcement must fire ahead of the resolver, or a hostile
scheme still costs a lookup and widens the reachable surface."""
assert validate_url(f"{scheme}://{PUBLIC_A}/x") is False
assert numeric_only_resolver == []
_ALLOWED_ENDPOINTS = {
# Public documentation-range host: the ordinary hosted-provider case.
"public_host": f"https://{PUBLIC_A}/v1",
# Scheme-less loopback: Ollama / LM Studio as users actually type it.
"schemeless_loopback": "localhost:11434",
# Scheme-less RFC1918: vLLM on the LAN.
"schemeless_rfc1918": f"{RFC1918_HOST}:8000",
# Empty / unset endpoint: nothing to send to.
"unset": "",
}
@pytest.mark.parametrize(
"endpoint",
list(_ALLOWED_ENDPOINTS.values()),
ids=list(_ALLOWED_ENDPOINTS),
)
def test_custom_llm_endpoint_gate_admits_legitimate_backends(
endpoint, numeric_only_resolver
):
"""Positive control for the whole corpus above. Without it, every
"blocked" assertion is satisfied by a gate that rejects everything."""
assert is_safe_custom_llm_endpoint(endpoint) is True
def test_dns_name_resolving_into_a_private_range_is_blocked(monkeypatch):
"""Name-based rebinding shape: the host is not an IP literal, so the
decision rests entirely on what the resolver returns. Every answer in
the record set has to clear the gate, not just the first."""
real_getaddrinfo = socket.getaddrinfo
answers: dict[str, list[str]] = {}
queried: list[str] = []
def resolver(host, port, family=0, type=0, proto=0, flags=0):
queried.append(host)
if host in answers:
return [
(socket.AF_INET, socket.SOCK_STREAM, 6, "", (ip, 0))
for ip in answers[host]
]
return real_getaddrinfo(
host, port, family, type, proto, flags | socket.AI_NUMERICHOST
)
monkeypatch.setattr(socket, "getaddrinfo", resolver)
single = "single.invalid"
mixed = "mixed.invalid"
public = "public.invalid"
answers[single] = [RFC1918_HOST]
answers[mixed] = [PUBLIC_A, IMDS_HOST]
answers[public] = [PUBLIC_A, PUBLIC_B]
assert validate_url(f"https://{single}/x") is False
# Mixed record set: a public first answer must not shadow a private
# one later in the list.
assert validate_url(f"https://{mixed}/x") is False
# Positive control: an all-public record set passes.
assert validate_url(f"https://{public}/x") is True
assert queried.count(single) == 1
assert queried.count(mixed) == 1
assert queried.count(public) == 1