~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 guard for operator-configured LLM provider base_url.
Even though the SSRF validator (``ssrf_validator.validate_url``) gates
outbound HTTP through ``safe_requests``, LangChain provider SDKs
(``ChatOpenAI``, ``ChatOllama``) use their own internal ``httpx`` client
that bypasses ``safe_requests`` entirely. An authenticated user with
permission to edit ``llm.<provider>.url`` could otherwise route every
inference call at internal services / cloud-credential endpoints.
The fix gates ``base_url`` through ``assert_base_url_safe`` immediately
after ``normalize_url`` and before the SDK constructor. These tests lock
in:
1. Every IP in ``ALWAYS_BLOCKED_METADATA_IPS`` raises ValueError.
2. Legitimate localhost / RFC1918 destinations (Ollama / LM Studio /
llama.cpp on private networks) keep working.
3. Validation runs BEFORE the SDK constructor (a future refactor that
moved it after would silently regress).
4. Edge cases like empty / None base_url are rejected.
"""
from unittest.mock import MagicMock, Mock
import pytest
@pytest.fixture(autouse=True)
def _supply_base_provider_key(monkeypatch):
"""Supply a provider_key on the abstract base for these direct tests.
OpenAICompatibleProvider intentionally declares no ``provider_key``
(issue #4546) so concrete subclasses that forget one fail loudly instead
of silently getting cloud context-window resolution. Some tests here
drive throwaway subclasses of it directly, so supply the key the way
every registered provider does. ``"openai_endpoint"`` keeps the pre-fix
cloud routing and does not affect the SSRF guard under test.
"""
monkeypatch.setattr(
"local_deep_research.llm.providers.openai_base."
"OpenAICompatibleProvider.provider_key",
"openai_endpoint",
raising=False,
)
@pytest.mark.parametrize(
"ip",
[
"169.254.169.254", # AWS IMDS / Azure / OCI / DigitalOcean
"169.254.170.2", # AWS ECS task metadata v3
"169.254.170.23", # AWS EKS Pod Identity credentials
"169.254.0.23", # Tencent Cloud
"100.100.100.200", # AlibabaCloud
],
)
def test_assert_base_url_safe_rejects_all_metadata_ips(ip):
"""Every cloud-metadata IP in ALWAYS_BLOCKED_METADATA_IPS must
raise — this is the load-bearing security property."""
from local_deep_research.security.ssrf_validator import (
assert_base_url_safe,
)
with pytest.raises(ValueError, match="failed SSRF validation"):
assert_base_url_safe(f"http://{ip}/", setting_key="llm.test.url")
def test_assert_base_url_safe_accepts_localhost():
"""Localhost is the typical Ollama / LM Studio / llama.cpp host."""
from local_deep_research.security.ssrf_validator import (
assert_base_url_safe,
)
assert (
assert_base_url_safe(
"http://localhost:11434/", setting_key="llm.ollama.url"
)
== "http://localhost:11434/"
)
def test_assert_base_url_safe_accepts_rfc1918():
"""Docker / private-network deployments need RFC1918."""
from local_deep_research.security.ssrf_validator import (
assert_base_url_safe,
)
assert (
assert_base_url_safe(
"http://192.168.1.10:11434/", setting_key="llm.ollama.url"
)
== "http://192.168.1.10:11434/"
)
def test_assert_base_url_safe_rejects_empty_string():
from local_deep_research.security.ssrf_validator import (
assert_base_url_safe,
)
with pytest.raises(ValueError, match="failed SSRF validation"):
assert_base_url_safe("", setting_key="llm.x.url")
def test_assert_base_url_safe_rejects_none():
"""isinstance check in validate_url means None returns False, which
means assert_base_url_safe raises rather than crashing on .strip()."""
from local_deep_research.security.ssrf_validator import (
assert_base_url_safe,
)
with pytest.raises(ValueError, match="failed SSRF validation"):
assert_base_url_safe(None, setting_key="llm.x.url")
def test_assert_base_url_safe_error_message_includes_setting_key():
"""Operators see the error and need to know which setting to fix."""
from local_deep_research.security.ssrf_validator import (
assert_base_url_safe,
)
with pytest.raises(ValueError, match=r"llm\.lmstudio\.url"):
assert_base_url_safe(
"http://169.254.169.254/", setting_key="llm.lmstudio.url"
)
_GETADDRINFO = "local_deep_research.security.ssrf_validator.socket.getaddrinfo"
def _raise_gaierror(*args, **kwargs):
import socket
raise socket.gaierror(-2, "Name or service not known")
def test_assert_base_url_safe_unresolvable_host_says_could_not_resolve(
monkeypatch,
):
"""A host the LOCAL resolver cannot resolve (e.g. one only a proxy can
resolve) is still refused, but with a message that says so rather than
claiming the destination is blocked."""
from local_deep_research.security.ssrf_validator import (
assert_base_url_safe,
)
monkeypatch.setattr(_GETADDRINFO, _raise_gaierror)
with pytest.raises(ValueError) as excinfo:
assert_base_url_safe(
"http://embeddings.corp.internal:8080/v1",
setting_key="embeddings.openai.base_url",
)
msg = str(excinfo.value)
assert "failed SSRF validation" in msg
assert "could not resolve host" in msg
assert "refusing to send" not in msg
assert "embeddings.openai.base_url" in msg
@pytest.mark.parametrize(
"url",
[
# Literal metadata IP: blocked without any DNS lookup.
"http://169.254.169.254/",
# Non-http(s) scheme: refused before resolution matters.
"ftp://unresolvable.example/",
# Percent-encoded numeric IPv4 host (169.254.169.254): refused by
# the encoded-numeric check before any DNS lookup.
"http://%31%36%39.%32%35%34.%31%36%39.%32%35%34/",
# Percent-escaped dots in a numeric host: same pre-DNS check.
"http://169%2e254%2e169%2e254/",
# Ambiguous (octal / hex) numeric IPv4 host: refused pre-DNS.
"http://0251.0376.0251.0376/",
"http://0xa9.254.169.254/",
# RFC-forbidden characters (backslash parser differential, space):
# refused by the forbidden-character check before any DNS lookup.
"http://evil.invalid\\@169.254.169.254/",
"http://ho st.invalid/",
],
)
def test_assert_base_url_safe_blocked_host_keeps_generic_message(
monkeypatch, url
):
"""Refusals that are not a local resolution failure keep the generic
'refusing to send' message, even when DNS would also fail."""
from local_deep_research.security.ssrf_validator import (
assert_base_url_safe,
)
monkeypatch.setattr(_GETADDRINFO, _raise_gaierror)
with pytest.raises(ValueError) as excinfo:
assert_base_url_safe(url, setting_key="llm.x.url")
assert "refusing to send" in str(excinfo.value)
assert "could not resolve host" not in str(excinfo.value)
def test_assert_base_url_safe_name_resolving_to_metadata_is_blocked_not_unresolved(
monkeypatch,
):
"""A hostname that resolves to a metadata IP is a blocked host, not an
unresolvable one."""
import socket
from local_deep_research.security.ssrf_validator import (
assert_base_url_safe,
)
monkeypatch.setattr(
_GETADDRINFO,
lambda *a, **k: [
(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("169.254.169.254", 0))
],
)
with pytest.raises(ValueError) as excinfo:
assert_base_url_safe(
"http://imds.attacker.test/", setting_key="llm.x.url"
)
assert "refusing to send" in str(excinfo.value)
assert "could not resolve host" not in str(excinfo.value)
def test_assert_base_url_safe_unresolvable_host_does_one_lookup(monkeypatch):
"""The "could not resolve host" message comes from validate_url's own
failed lookup; no second DNS query is made to pick the message."""
from local_deep_research.security.ssrf_validator import (
assert_base_url_safe,
)
calls = []
def _counting_gaierror(*args, **kwargs):
calls.append(args[0])
_raise_gaierror()
monkeypatch.setattr(_GETADDRINFO, _counting_gaierror)
with pytest.raises(ValueError, match="could not resolve host"):
assert_base_url_safe("http://nx.invalid/", setting_key="llm.x.url")
assert calls == ["nx.invalid"]
def test_assert_base_url_safe_metadata_then_nxdomain_keeps_generic_message(
monkeypatch,
):
"""If the validating lookup resolved to a metadata IP, a later NXDOMAIN
(e.g. DNS rebinding) cannot relabel the refusal as a DNS problem."""
import socket
from local_deep_research.security.ssrf_validator import (
assert_base_url_safe,
)
answers = iter(
[
[
(
socket.AF_INET,
socket.SOCK_STREAM,
6,
"",
("169.254.169.254", 0),
)
]
]
)
def _metadata_then_gaierror(*args, **kwargs):
try:
return next(answers)
except StopIteration:
_raise_gaierror()
monkeypatch.setattr(_GETADDRINFO, _metadata_then_gaierror)
with pytest.raises(ValueError) as excinfo:
assert_base_url_safe("http://rebind.test/", setting_key="llm.x.url")
assert "refusing to send" in str(excinfo.value)
assert "could not resolve host" not in str(excinfo.value)
def test_assert_base_url_safe_unresolved_flag_does_not_leak(monkeypatch):
"""A gaierror refusal from an earlier, unrelated validate_url call in the
same context must not relabel a later blocked-host refusal."""
from local_deep_research.security.ssrf_validator import (
assert_base_url_safe,
validate_url,
)
monkeypatch.setattr(_GETADDRINFO, _raise_gaierror)
assert validate_url("http://nx.invalid/") is False
with pytest.raises(ValueError) as excinfo:
assert_base_url_safe("http://169.254.169.254/", setting_key="llm.x.url")
assert "refusing to send" in str(excinfo.value)
assert "could not resolve host" not in str(excinfo.value)
# And the reverse: an unresolvable host after a blocked one.
with pytest.raises(ValueError, match="could not resolve host"):
assert_base_url_safe("http://nx.invalid/", setting_key="llm.x.url")
def test_assert_base_url_safe_patched_validate_url_keeps_generic_message(
monkeypatch,
):
"""Callers that stub validate_url to refuse get the generic message
(the label requires validate_url's own resolution failure)."""
from local_deep_research.security import ssrf_validator
monkeypatch.setattr(_GETADDRINFO, _raise_gaierror)
monkeypatch.setattr(ssrf_validator, "validate_url", lambda *a, **k: False)
with pytest.raises(ValueError) as excinfo:
ssrf_validator.assert_base_url_safe(
"http://nx.invalid/", setting_key="llm.x.url"
)
assert "refusing to send" in str(excinfo.value)
assert "could not resolve host" not in str(excinfo.value)
# ---------------------------------------------------------------------
# Per-provider integration tests
# ---------------------------------------------------------------------
class TestOllamaBaseUrlSsrf:
"""OllamaProvider gates base_url at three sites: list_models_for_api,
create_llm, is_available. Each behaves differently on rejection."""
def test_create_llm_rejects_aws_metadata(self):
"""create_llm propagates ValueError so the research-query caller
sees a clear config error instead of silent SSRF."""
from local_deep_research.llm.providers.implementations.ollama import (
OllamaProvider,
)
snapshot = {"llm.ollama.url": "http://169.254.169.254/"}
with pytest.raises(ValueError, match="failed SSRF validation"):
OllamaProvider.create_llm(
model_name="llama3", settings_snapshot=snapshot
)
def test_create_llm_validate_runs_before_chatollama_constructor(
self, monkeypatch
):
"""A future refactor that moved the validate call below the SDK
constructor would silently regress (constructor is mocked in
tests). Make assert_base_url_safe raise; assert ChatOllama mock
has call_count == 0.
Patch the SYMBOL inside ollama.py — ``from langchain_ollama
import ChatOllama`` binds the name locally at import time;
patching ``langchain_ollama.ChatOllama`` would not affect that
local binding.
"""
from local_deep_research.llm.providers.implementations.ollama import (
OllamaProvider,
)
mock_ctor = MagicMock(return_value=Mock())
monkeypatch.setattr(
"local_deep_research.llm.providers.implementations.ollama.ChatOllama",
mock_ctor,
)
monkeypatch.setattr(
"local_deep_research.llm.providers.implementations.ollama.assert_base_url_safe",
Mock(side_effect=ValueError("simulated SSRF rejection")),
)
with pytest.raises(ValueError):
OllamaProvider.create_llm(
model_name="llama3",
settings_snapshot={"llm.ollama.url": "http://example.com/"},
)
assert mock_ctor.call_count == 0, (
"ChatOllama was instantiated despite SSRF guard raising — "
"ordering regression"
)
def test_is_available_returns_false_on_bad_base_url(self):
"""is_available swallows ValueError and returns False so the
model-list UI degrades gracefully."""
from local_deep_research.llm.providers.implementations.ollama import (
OllamaProvider,
)
snapshot = {"llm.ollama.url": "http://169.254.169.254/"}
assert OllamaProvider.is_available(settings_snapshot=snapshot) is False
def test_list_models_for_api_returns_empty_on_bad_base_url(self):
"""list_models_for_api swallows ValueError and returns []."""
from local_deep_research.llm.providers.implementations.ollama import (
OllamaProvider,
)
result = OllamaProvider.list_models_for_api(
api_key="test",
base_url="http://169.254.169.254/",
)
assert result == []
class TestOpenAICompatibleBaseUrlSsrf:
"""OpenAICompatibleProvider gates base_url in create_llm and
_create_llm_instance, but only when ``cls.url_setting`` is set
(subclasses with hardcoded default_base_url skip validation)."""
def test_subclass_with_url_setting_rejects_metadata(self):
"""A subclass with cls.url_setting set (e.g. CustomOpenAIEndpoint
which uses llm.openai_endpoint.url) must reject metadata IPs."""
from local_deep_research.llm.providers.openai_base import (
OpenAICompatibleProvider,
)
# Create a temporary subclass to exercise the cls.url_setting gate
class _TestProvider(OpenAICompatibleProvider):
provider_name = "TestProvider"
url_setting = "llm.test.url"
api_key_setting = None
default_base_url = "http://169.254.169.254/"
with pytest.raises(ValueError, match="failed SSRF validation"):
_TestProvider.create_llm(model_name="test")
def test_pure_provider_skips_validation(self, monkeypatch):
"""Providers with ``url_setting = None`` (Anthropic, OpenAI,
OpenRouter, xAI, IONOS) must NOT invoke assert_base_url_safe.
Their default_base_url is hardcoded; there's no operator URL to
attack, so validation would be wasted work."""
from local_deep_research.llm.providers.openai_base import (
OpenAICompatibleProvider,
)
mock_guard = Mock(return_value="passthrough")
monkeypatch.setattr(
"local_deep_research.llm.providers.openai_base.assert_base_url_safe",
mock_guard,
)
monkeypatch.setattr(
"local_deep_research.llm.providers.openai_base.ChatOpenAI",
Mock(),
)
class _PureProvider(OpenAICompatibleProvider):
provider_name = "PureProvider"
url_setting = None
api_key_setting = None
default_base_url = "https://api.example.com/v1"
_PureProvider.create_llm(model_name="test-model")
assert mock_guard.call_count == 0, (
"Pure-default provider invoked SSRF guard unexpectedly"
)
def test_validate_runs_before_chatopenai_constructor(self, monkeypatch):
"""Same ordering invariant as the Ollama test, for the OpenAI
compat path. Patch the local binding of ChatOpenAI inside
openai_base.py."""
from local_deep_research.llm.providers.openai_base import (
OpenAICompatibleProvider,
)
mock_ctor = MagicMock(return_value=Mock())
monkeypatch.setattr(
"local_deep_research.llm.providers.openai_base.ChatOpenAI",
mock_ctor,
)
monkeypatch.setattr(
"local_deep_research.llm.providers.openai_base.assert_base_url_safe",
Mock(side_effect=ValueError("simulated SSRF rejection")),
)
class _GuardedProvider(OpenAICompatibleProvider):
provider_name = "GuardedProvider"
url_setting = "llm.guarded.url"
api_key_setting = None
default_base_url = "https://api.example.com/v1"
with pytest.raises(ValueError):
_GuardedProvider.create_llm(model_name="test-model")
assert mock_ctor.call_count == 0, (
"ChatOpenAI was instantiated despite SSRF guard raising — "
"ordering regression"
)
class TestOpenAICompatibleListModelsSsrf:
"""OpenAICompatibleProvider.list_models_for_api (used by
CustomOpenAIEndpoint / LMStudio / LlamaCpp) gates base_url through the
SSRF guard when ``cls.url_setting`` is set. On rejection it must
degrade gracefully and return [] (model-listing should NOT 500)."""
def test_list_models_for_api_returns_empty_on_metadata_url(
self, monkeypatch, loguru_caplog
):
"""A subclass with cls.url_setting set must return [] (and log a
warning) when base_url is a blocked metadata endpoint, without ever
instantiating the OpenAI SDK client."""
from local_deep_research.llm.providers.openai_base import (
OpenAICompatibleProvider,
)
# If the guard failed to short-circuit, the SDK client would be
# constructed; make that an explicit failure so the test can't pass
# for the wrong reason.
import openai
def _boom(*args, **kwargs):
raise AssertionError(
"OpenAI client constructed despite blocked base_url"
)
monkeypatch.setattr(openai, "OpenAI", _boom)
class _TestProvider(OpenAICompatibleProvider):
provider_name = "TestListProvider"
url_setting = "llm.test.url"
api_key_setting = None
default_base_url = "https://api.example.com/v1"
@classmethod
def requires_auth_for_models(cls):
return False
with loguru_caplog.at_level("WARNING"):
result = _TestProvider.list_models_for_api(
api_key="test-key",
base_url="http://169.254.169.254/",
)
assert result == []
assert "SSRF validation" in loguru_caplog.text, (
"expected a warning naming SSRF validation"
)
def test_list_models_for_api_allows_localhost(self, monkeypatch):
"""Legitimate local endpoints (LM Studio / llama.cpp on localhost)
must pass the guard and reach the SDK client.
Premise note: production now passes an explicit
``http_client=DefaultHttpxClient(follow_redirects=False)`` to the
OpenAI SDK constructor (the redirect-suppression fix), so this fake
must accept that kwarg; without it a TypeError is swallowed by the
discovery path's broad ``except Exception`` and the capture below
stays empty — the call shape changed, the pinned behaviour did not.
"""
from local_deep_research.llm.providers.openai_base import (
OpenAICompatibleProvider,
)
captured = {}
class _FakeModels:
def list(self):
return Mock(data=[])
class _FakeClient:
def __init__(
self,
api_key=None,
base_url=None,
timeout=None,
max_retries=None,
http_client=None,
):
captured["base_url"] = base_url
self.models = _FakeModels()
import openai
monkeypatch.setattr(openai, "OpenAI", _FakeClient)
class _TestProvider(OpenAICompatibleProvider):
provider_name = "TestListProvider"
url_setting = "llm.test.url"
api_key_setting = None
default_base_url = "https://api.example.com/v1"
@classmethod
def requires_auth_for_models(cls):
return False
result = _TestProvider.list_models_for_api(
api_key="test-key",
base_url="http://localhost:1234/v1",
)
assert result == []
assert captured["base_url"] == "http://localhost:1234/v1", (
"localhost base_url should reach the SDK client unchanged"
)
class TestOpenAIProviderApiBaseSsrf:
"""OpenAIProvider overrides create_llm without calling super and has
url_setting = None, so the base-class guard never runs. It gates
llm.openai.api_base directly and lets ValueError propagate (fail-fast,
matching ollama.create_llm)."""
def test_create_llm_rejects_metadata_api_base(self, monkeypatch):
"""A blocked llm.openai.api_base must raise ValueError before the
ChatOpenAI constructor runs."""
from local_deep_research.llm.providers.implementations.openai import (
OpenAIProvider,
)
mock_ctor = MagicMock(return_value=Mock())
monkeypatch.setattr(
"local_deep_research.llm.providers.implementations.openai.ChatOpenAI",
mock_ctor,
)
snapshot = {
"llm.openai.api_key": "sk-test-key",
"llm.openai.api_base": "http://169.254.169.254/v1",
}
with pytest.raises(ValueError, match="failed SSRF validation"):
OpenAIProvider.create_llm(
model_name="gpt-4o-mini", settings_snapshot=snapshot
)
assert mock_ctor.call_count == 0, (
"ChatOpenAI was instantiated despite SSRF guard raising"
)
def test_create_llm_allows_localhost_api_base(self, monkeypatch):
"""A legitimate local api_base (proxy / gateway on localhost) must
pass the guard and reach the ChatOpenAI constructor."""
from local_deep_research.llm.providers.implementations.openai import (
OpenAIProvider,
)
captured = {}
def _fake_ctor(**kwargs):
captured.update(kwargs)
return Mock()
monkeypatch.setattr(
"local_deep_research.llm.providers.implementations.openai.ChatOpenAI",
_fake_ctor,
)
snapshot = {
"llm.openai.api_key": "sk-test-key",
"llm.openai.api_base": "http://localhost:8080/v1",
}
OpenAIProvider.create_llm(
model_name="gpt-4o-mini", settings_snapshot=snapshot
)
assert captured["openai_api_base"] == "http://localhost:8080/v1", (
"localhost api_base should pass the guard and reach ChatOpenAI"
)
class TestPublicApi:
"""assert_base_url_safe and redact_url_for_log are exported from
security/__init__.py for downstream importers."""
def test_assert_base_url_safe_re_exported(self):
from local_deep_research.security import assert_base_url_safe
assert callable(assert_base_url_safe)
def test_redact_url_for_log_re_exported(self):
from local_deep_research.security import redact_url_for_log
assert callable(redact_url_for_log)
class TestCustomAnthropicEndpointSsrf:
"""CustomAnthropicEndpointProvider gates its operator-configured
base_url (``llm.anthropic_endpoint.url``) through the SSRF guard in the
inherited AnthropicProvider.create_llm, and in list_models_for_api. The
SDK is ChatAnthropic / the anthropic SDK, both of which use their own
httpx client that bypasses safe_requests — so the guard is load-bearing."""
def test_create_llm_rejects_aws_metadata(self):
from local_deep_research.llm.providers.implementations.custom_anthropic_endpoint import (
CustomAnthropicEndpointProvider,
)
snapshot = {"llm.anthropic_endpoint.url": "http://169.254.169.254/"}
with pytest.raises(ValueError, match="failed SSRF validation"):
CustomAnthropicEndpointProvider.create_llm(
model_name="claude-3-5-sonnet", settings_snapshot=snapshot
)
def test_create_llm_validate_runs_before_chatanthropic_constructor(
self, monkeypatch
):
"""Make assert_base_url_safe raise; ChatAnthropic must never be
instantiated. Both names are bound in anthropic.py (the parent's
create_llm), so patch them there."""
from local_deep_research.llm.providers.implementations.custom_anthropic_endpoint import (
CustomAnthropicEndpointProvider,
)
mock_ctor = MagicMock(return_value=Mock())
monkeypatch.setattr(
"local_deep_research.llm.providers.implementations.anthropic.ChatAnthropic",
mock_ctor,
)
monkeypatch.setattr(
"local_deep_research.llm.providers.implementations.anthropic.assert_base_url_safe",
Mock(side_effect=ValueError("simulated SSRF rejection")),
)
with pytest.raises(ValueError):
CustomAnthropicEndpointProvider.create_llm(
model_name="claude-3-5-sonnet",
settings_snapshot={
"llm.anthropic_endpoint.url": "http://example.com/"
},
)
assert mock_ctor.call_count == 0, (
"ChatAnthropic was instantiated despite SSRF guard raising — "
"ordering regression"
)
def test_list_models_for_api_returns_empty_on_metadata_url(self):
from local_deep_research.llm.providers.implementations.custom_anthropic_endpoint import (
CustomAnthropicEndpointProvider,
)
result = CustomAnthropicEndpointProvider.list_models_for_api(
api_key="test", base_url="http://169.254.169.254/"
)
assert result == []