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): allow SearXNG on private network IPs

LearningCircuit9571db09f71b
diff --git a/src/local_deep_research/security/safe_requests.py b/src/local_deep_research/security/safe_requests.py
index 8b2d0136c4..059a5895c3 100644
--- a/src/local_deep_research/security/safe_requests.py
+++ b/src/local_deep_research/security/safe_requests.py
@@ -23,6 +23,7 @@ def safe_get(
params: Optional[dict] = None,
timeout: int = DEFAULT_TIMEOUT,
allow_localhost: bool = False,
+ allow_private_ips: bool = False,
**kwargs,
) -> requests.Response:
"""
@@ -35,6 +36,10 @@ def safe_get(
allow_localhost: Whether to allow localhost/loopback addresses.
Set to True for trusted internal services like self-hosted
search engines (e.g., searxng). Default False.
+ allow_private_ips: Whether to allow all RFC1918 private IPs (10.x, 172.16-31.x,
+ 192.168.x) plus localhost. Use for trusted self-hosted services like SearXNG
+ that may be running on a different machine on the local network.
+ Note: AWS metadata endpoint (169.254.169.254) is ALWAYS blocked.
**kwargs: Additional arguments to pass to requests.get()
Returns:
@@ -45,7 +50,11 @@ def safe_get(
requests.RequestException: If request fails
"""
# Validate URL to prevent SSRF
- if not validate_url(url, allow_localhost=allow_localhost):
+ if not validate_url(
+ url,
+ allow_localhost=allow_localhost,
+ allow_private_ips=allow_private_ips,
+ ):
raise ValueError(
f"URL failed security validation (possible SSRF): {url}"
)
@@ -92,6 +101,7 @@ def safe_post(
json: Optional[dict] = None,
timeout: int = DEFAULT_TIMEOUT,
allow_localhost: bool = False,
+ allow_private_ips: bool = False,
**kwargs,
) -> requests.Response:
"""
@@ -105,6 +115,10 @@ def safe_post(
allow_localhost: Whether to allow localhost/loopback addresses.
Set to True for trusted internal services like self-hosted
search engines (e.g., searxng). Default False.
+ allow_private_ips: Whether to allow all RFC1918 private IPs (10.x, 172.16-31.x,
+ 192.168.x) plus localhost. Use for trusted self-hosted services like SearXNG
+ that may be running on a different machine on the local network.
+ Note: AWS metadata endpoint (169.254.169.254) is ALWAYS blocked.
**kwargs: Additional arguments to pass to requests.post()
Returns:
@@ -115,7 +129,11 @@ def safe_post(
requests.RequestException: If request fails
"""
# Validate URL to prevent SSRF
- if not validate_url(url, allow_localhost=allow_localhost):
+ if not validate_url(
+ url,
+ allow_localhost=allow_localhost,
+ allow_private_ips=allow_private_ips,
+ ):
raise ValueError(
f"URL failed security validation (possible SSRF): {url}"
)
@@ -165,25 +183,39 @@ class SafeSession(requests.Session):
with SafeSession() as session:
response = session.get(url)
- # For trusted internal services (e.g., searxng):
+ # For trusted internal services (e.g., searxng on localhost):
with SafeSession(allow_localhost=True) as session:
response = session.get(url)
+
+ # For trusted internal services on any private network IP:
+ with SafeSession(allow_private_ips=True) as session:
+ response = session.get(url)
"""
- def __init__(self, allow_localhost: bool = False):
+ def __init__(
+ self, allow_localhost: bool = False, allow_private_ips: bool = False
+ ):
"""
Initialize SafeSession.
Args:
allow_localhost: Whether to allow localhost/loopback addresses.
+ allow_private_ips: Whether to allow all RFC1918 private IPs (10.x, 172.16-31.x,
+ 192.168.x) plus localhost. Use for trusted self-hosted services like SearXNG.
+ Note: AWS metadata endpoint (169.254.169.254) is ALWAYS blocked.
"""
super().__init__()
self.allow_localhost = allow_localhost
+ self.allow_private_ips = allow_private_ips
def request(self, method: str, url: str, **kwargs) -> requests.Response:
"""Override request method to add SSRF validation."""
# Validate URL
- if not validate_url(url, allow_localhost=self.allow_localhost):
+ if not validate_url(
+ url,
+ allow_localhost=self.allow_localhost,
+ allow_private_ips=self.allow_private_ips,
+ ):
raise ValueError(
f"URL failed security validation (possible SSRF): {url}"
)
diff --git a/src/local_deep_research/security/ssrf_validator.py b/src/local_deep_research/security/ssrf_validator.py
index c8364df004..87398c17f9 100644
--- a/src/local_deep_research/security/ssrf_validator.py
+++ b/src/local_deep_research/security/ssrf_validator.py
@@ -38,40 +38,60 @@ AWS_METADATA_IP = "169.254.169.254"
ALLOWED_SCHEMES = {"http", "https"}
-def is_ip_blocked(ip_str: str, allow_localhost: bool = False) -> bool:
+def is_ip_blocked(
+ ip_str: str, allow_localhost: bool = False, allow_private_ips: bool = False
+) -> bool:
"""
Check if an IP address is in a blocked range.
Args:
ip_str: IP address as string
allow_localhost: Whether to allow localhost/loopback addresses
+ allow_private_ips: Whether to allow all RFC1918 private IPs (10.x, 172.16-31.x,
+ 192.168.x) plus localhost. Use for trusted self-hosted services like SearXNG.
+ Note: AWS metadata endpoint (169.254.169.254) is ALWAYS blocked.
Returns:
True if IP is blocked, False otherwise
"""
# Loopback ranges that can be allowed for trusted internal services
+ # nosec B104 - These hardcoded IPs are intentional for SSRF allowlist
LOOPBACK_RANGES = [
ipaddress.ip_network("127.0.0.0/8"), # IPv4 loopback
ipaddress.ip_network("::1/128"), # IPv6 loopback
]
+ # RFC1918 private network ranges - allowed with allow_private_ips=True
+ # nosec B104 - These hardcoded IPs are intentional for SSRF allowlist
+ PRIVATE_RANGES = [
+ ipaddress.ip_network("10.0.0.0/8"), # Class A private
+ ipaddress.ip_network("172.16.0.0/12"), # Class B private
+ ipaddress.ip_network("192.168.0.0/16"), # Class C private
+ ]
+
try:
ip = ipaddress.ip_address(ip_str)
+ # ALWAYS block AWS metadata endpoint - critical SSRF target for credential theft
+ if str(ip) == AWS_METADATA_IP:
+ return True
+
# Check if IP is in any blocked range
for blocked_range in BLOCKED_IP_RANGES:
if ip in blocked_range:
- # If allow_localhost is True, skip blocking for loopback addresses
- if allow_localhost:
+ # If allow_private_ips is True, skip blocking for private + loopback
+ if allow_private_ips:
+ is_loopback = any(ip in lr for lr in LOOPBACK_RANGES)
+ is_private = any(ip in pr for pr in PRIVATE_RANGES)
+ if is_loopback or is_private:
+ continue
+ # If allow_localhost is True, skip blocking for loopback only
+ elif allow_localhost:
is_loopback = any(ip in lr for lr in LOOPBACK_RANGES)
if is_loopback:
continue
return True
- # Specifically block AWS metadata endpoint
- if str(ip) == AWS_METADATA_IP:
- return True
-
return False
except ValueError:
@@ -80,7 +100,10 @@ def is_ip_blocked(ip_str: str, allow_localhost: bool = False) -> bool:
def validate_url(
- url: str, allow_redirects: bool = True, allow_localhost: bool = False
+ url: str,
+ allow_redirects: bool = True,
+ allow_localhost: bool = False,
+ allow_private_ips: bool = False,
) -> bool:
"""
Validate URL to prevent SSRF attacks.
@@ -96,6 +119,10 @@ def validate_url(
allow_localhost: Whether to allow localhost/loopback addresses.
Set to True for trusted internal services like self-hosted
search engines (e.g., searxng). Default False.
+ allow_private_ips: Whether to allow all RFC1918 private IPs (10.x, 172.16-31.x,
+ 192.168.x) plus localhost. Use for trusted self-hosted services like SearXNG
+ that may be running on a different machine on the local network.
+ Note: AWS metadata endpoint (169.254.169.254) is ALWAYS blocked.
Returns:
True if URL is safe, False otherwise
@@ -136,7 +163,11 @@ def validate_url(
# Check if hostname is an IP address
try:
ip = ipaddress.ip_address(hostname)
- if is_ip_blocked(str(ip), allow_localhost=allow_localhost):
+ if is_ip_blocked(
+ str(ip),
+ allow_localhost=allow_localhost,
+ allow_private_ips=allow_private_ips,
+ ):
logger.warning(
f"Blocked URL with internal/private IP: {hostname} - {url}"
)
@@ -156,7 +187,11 @@ def validate_url(
for info in addr_info:
ip_str = info[4][0] # Extract IP address from addr_info tuple
- if is_ip_blocked(ip_str, allow_localhost=allow_localhost):
+ if is_ip_blocked(
+ ip_str,
+ allow_localhost=allow_localhost,
+ allow_private_ips=allow_private_ips,
+ ):
logger.warning(
f"Blocked URL - hostname {hostname} resolves to "
f"internal/private IP: {ip_str} - {url}"
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_searxng.py b/src/local_deep_research/web_search_engines/engines/search_engine_searxng.py
index a1804d6e52..8eb69b6dfc 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_searxng.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_searxng.py
@@ -82,9 +82,9 @@ class SearXNGSearchEngine(BaseSearchEngine):
)
try:
# Make sure it's accessible.
- # allow_localhost=True since searxng is typically self-hosted locally
+ # allow_private_ips=True since SearXNG is typically self-hosted on local network
response = safe_get(
- self.instance_url, timeout=5, allow_localhost=True
+ self.instance_url, timeout=5, allow_private_ips=True
)
if response.status_code == 200:
logger.info("SearXNG instance is accessible.")
@@ -195,7 +195,7 @@ class SearXNGSearchEngine(BaseSearchEngine):
self.instance_url,
headers=initial_headers,
timeout=10,
- allow_localhost=True,
+ allow_private_ips=True,
)
cookies = initial_response.cookies
except Exception:
@@ -237,7 +237,7 @@ class SearXNGSearchEngine(BaseSearchEngine):
headers=headers,
cookies=cookies,
timeout=15,
- allow_localhost=True,
+ allow_private_ips=True,
)
if response.status_code == 200:
diff --git a/tests/security/test_ssrf_validator.py b/tests/security/test_ssrf_validator.py
new file mode 100644
index 0000000000..147179bcf1
--- /dev/null
+++ b/tests/security/test_ssrf_validator.py
@@ -0,0 +1,365 @@
+"""
+SSRF Validator Tests
+
+Tests for the SSRF (Server-Side Request Forgery) protection that validates URLs
+before making outgoing HTTP requests.
+
+Security model:
+- By default, block all private/internal IPs (RFC1918, localhost, link-local)
+- allow_localhost=True: Allow only loopback addresses (127.x.x.x, ::1)
+- allow_private_ips=True: Allow all RFC1918 private IPs + localhost
+- AWS metadata endpoint (169.254.169.254) is ALWAYS blocked
+
+The allow_private_ips parameter is designed for trusted self-hosted services like
+SearXNG that may be running on a different machine on the local network.
+"""
+
+import pytest
+from unittest.mock import patch
+
+from tests.test_utils import add_src_to_path
+
+add_src_to_path()
+
+
[email protected](autouse=True)
+def disable_ssrf_test_bypass():
+ """
+ Disable the test mode bypass for SSRF validation.
+
+ The SSRF validator bypasses validation when PYTEST_CURRENT_TEST is set.
+ We need to mock the os.environ.get calls to test the actual validation.
+ """
+ with (
+ patch(
+ "src.local_deep_research.security.ssrf_validator.os.environ.get"
+ ) as mock_env_get,
+ patch(
+ "src.local_deep_research.security.ssrf_validator.get_env_setting"
+ ) as mock_get_env_setting,
+ ):
+ # Return empty string for test bypass checks (so .lower() works)
+ # Return None for PYTEST_CURRENT_TEST (it's checked for truthiness)
+ def mock_environ_get(key, default=""):
+ if key == "TESTING":
+ return "" # Empty string so .lower() works
+ if key == "PYTEST_CURRENT_TEST":
+ return None # None is falsy, bypasses the check
+ return default
+
+ mock_env_get.side_effect = mock_environ_get
+ # Return False for disable_validation setting
+ mock_get_env_setting.return_value = False
+ yield
+
+
+class TestIsIpBlocked:
+ """Test the is_ip_blocked function."""
+
+ def test_localhost_blocked_by_default(self):
+ """Localhost should be blocked by default."""
+ from src.local_deep_research.security.ssrf_validator import (
+ is_ip_blocked,
+ )
+
+ assert is_ip_blocked("127.0.0.1") is True
+ assert is_ip_blocked("127.0.0.2") is True
+
+ def test_localhost_allowed_with_allow_localhost(self):
+ """Localhost should be allowed with allow_localhost=True."""
+ from src.local_deep_research.security.ssrf_validator import (
+ is_ip_blocked,
+ )
+
+ assert is_ip_blocked("127.0.0.1", allow_localhost=True) is False
+ assert is_ip_blocked("127.0.0.2", allow_localhost=True) is False
+
+ def test_private_ip_blocked_with_allow_localhost(self):
+ """Private IPs should still be blocked with allow_localhost=True."""
+ from src.local_deep_research.security.ssrf_validator import (
+ is_ip_blocked,
+ )
+
+ assert is_ip_blocked("192.168.1.100", allow_localhost=True) is True
+ assert is_ip_blocked("10.0.0.5", allow_localhost=True) is True
+ assert is_ip_blocked("172.16.0.1", allow_localhost=True) is True
+
+ def test_private_ip_allowed_with_allow_private_ips(self):
+ """Private IPs should be allowed with allow_private_ips=True."""
+ from src.local_deep_research.security.ssrf_validator import (
+ is_ip_blocked,
+ )
+
+ # 192.168.x.x range
+ assert is_ip_blocked("192.168.1.100", allow_private_ips=True) is False
+ assert is_ip_blocked("192.168.0.1", allow_private_ips=True) is False
+ assert is_ip_blocked("192.168.255.255", allow_private_ips=True) is False
+
+ # 10.x.x.x range
+ assert is_ip_blocked("10.0.0.1", allow_private_ips=True) is False
+ assert is_ip_blocked("10.255.255.255", allow_private_ips=True) is False
+
+ # 172.16-31.x.x range
+ assert is_ip_blocked("172.16.0.1", allow_private_ips=True) is False
+ assert is_ip_blocked("172.31.255.255", allow_private_ips=True) is False
+
+ def test_localhost_also_allowed_with_allow_private_ips(self):
+ """Localhost should also be allowed with allow_private_ips=True."""
+ from src.local_deep_research.security.ssrf_validator import (
+ is_ip_blocked,
+ )
+
+ assert is_ip_blocked("127.0.0.1", allow_private_ips=True) is False
+ assert is_ip_blocked("127.0.0.2", allow_private_ips=True) is False
+
+ def test_aws_metadata_always_blocked(self):
+ """AWS metadata endpoint should ALWAYS be blocked, even with allow_private_ips."""
+ from src.local_deep_research.security.ssrf_validator import (
+ is_ip_blocked,
+ )
+
+ # Without any allowlist
+ assert is_ip_blocked("169.254.169.254") is True
+
+ # With allow_localhost
+ assert is_ip_blocked("169.254.169.254", allow_localhost=True) is True
+
+ # With allow_private_ips - CRITICAL: Must still be blocked!
+ assert is_ip_blocked("169.254.169.254", allow_private_ips=True) is True
+
+ def test_public_ip_not_blocked(self):
+ """Public IPs should not be blocked."""
+ from src.local_deep_research.security.ssrf_validator import (
+ is_ip_blocked,
+ )
+
+ assert is_ip_blocked("8.8.8.8") is False
+ assert is_ip_blocked("1.1.1.1") is False
+ assert is_ip_blocked("142.250.185.206") is False # google.com
+
+ def test_link_local_blocked(self):
+ """Link-local addresses should be blocked."""
+ from src.local_deep_research.security.ssrf_validator import (
+ is_ip_blocked,
+ )
+
+ assert is_ip_blocked("169.254.1.1") is True
+ assert is_ip_blocked("169.254.100.100") is True
+
+
+class TestValidateUrl:
+ """Test the validate_url function."""
+
+ def test_public_url_allowed(self):
+ """Public URLs should be allowed."""
+ from src.local_deep_research.security.ssrf_validator import validate_url
+
+ with patch("socket.getaddrinfo") as mock_getaddrinfo:
+ # Mock DNS resolution to return a public IP
+ mock_getaddrinfo.return_value = [
+ (2, 1, 6, "", ("142.250.185.206", 0))
+ ]
+ assert validate_url("https://google.com") is True
+
+ def test_localhost_url_blocked_by_default(self):
+ """Localhost URLs should be blocked by default."""
+ from src.local_deep_research.security.ssrf_validator import validate_url
+
+ assert validate_url("http://127.0.0.1:8080") is False
+ assert validate_url("http://localhost:8080") is False
+
+ def test_localhost_url_allowed_with_allow_localhost(self):
+ """Localhost URLs should be allowed with allow_localhost=True."""
+ from src.local_deep_research.security.ssrf_validator import validate_url
+
+ with patch("socket.getaddrinfo") as mock_getaddrinfo:
+ mock_getaddrinfo.return_value = [(2, 1, 6, "", ("127.0.0.1", 0))]
+ assert (
+ validate_url("http://localhost:8080", allow_localhost=True)
+ is True
+ )
+
+ assert (
+ validate_url("http://127.0.0.1:8080", allow_localhost=True) is True
+ )
+
+ def test_private_ip_url_blocked_with_allow_localhost(self):
+ """Private IP URLs should still be blocked with allow_localhost=True."""
+ from src.local_deep_research.security.ssrf_validator import validate_url
+
+ assert (
+ validate_url("http://192.168.1.100:8080", allow_localhost=True)
+ is False
+ )
+ assert (
+ validate_url("http://10.0.0.5:8080", allow_localhost=True) is False
+ )
+
+ def test_private_ip_url_allowed_with_allow_private_ips(self):
+ """Private IP URLs should be allowed with allow_private_ips=True."""
+ from src.local_deep_research.security.ssrf_validator import validate_url
+
+ # 192.168.x.x - typical home network
+ assert (
+ validate_url("http://192.168.1.100:8080", allow_private_ips=True)
+ is True
+ )
+ assert (
+ validate_url("http://192.168.0.1:80", allow_private_ips=True)
+ is True
+ )
+
+ # 10.x.x.x - typical corporate network
+ assert (
+ validate_url("http://10.0.0.5:8080", allow_private_ips=True) is True
+ )
+ assert (
+ validate_url("http://10.10.10.10:3000", allow_private_ips=True)
+ is True
+ )
+
+ # 172.16-31.x.x - Docker default network etc.
+ assert (
+ validate_url("http://172.16.0.1:8080", allow_private_ips=True)
+ is True
+ )
+ assert (
+ validate_url("http://172.20.0.2:5000", allow_private_ips=True)
+ is True
+ )
+
+ def test_aws_metadata_url_always_blocked(self):
+ """AWS metadata URL should ALWAYS be blocked."""
+ from src.local_deep_research.security.ssrf_validator import validate_url
+
+ aws_metadata_url = "http://169.254.169.254/latest/meta-data"
+
+ # Without any allowlist
+ assert validate_url(aws_metadata_url) is False
+
+ # With allow_localhost
+ assert validate_url(aws_metadata_url, allow_localhost=True) is False
+
+ # With allow_private_ips - CRITICAL: Must still be blocked!
+ assert validate_url(aws_metadata_url, allow_private_ips=True) is False
+
+ def test_invalid_scheme_blocked(self):
+ """Invalid schemes should be blocked."""
+ from src.local_deep_research.security.ssrf_validator import validate_url
+
+ assert validate_url("ftp://example.com") is False
+ assert validate_url("file:///etc/passwd") is False
+ assert validate_url("javascript:alert(1)") is False
+
+ def test_hostname_resolving_to_private_ip_blocked(self):
+ """Hostnames that resolve to private IPs should be blocked."""
+ from src.local_deep_research.security.ssrf_validator import validate_url
+
+ with patch("socket.getaddrinfo") as mock_getaddrinfo:
+ # Simulate a hostname resolving to a private IP (DNS rebinding attack)
+ mock_getaddrinfo.return_value = [(2, 1, 6, "", ("192.168.1.1", 0))]
+ assert validate_url("http://evil.com") is False
+
+ def test_hostname_resolving_to_private_ip_allowed_with_allow_private_ips(
+ self,
+ ):
+ """Hostnames resolving to private IPs allowed with allow_private_ips=True."""
+ from src.local_deep_research.security.ssrf_validator import validate_url
+
+ with patch("socket.getaddrinfo") as mock_getaddrinfo:
+ mock_getaddrinfo.return_value = [
+ (2, 1, 6, "", ("192.168.1.100", 0))
+ ]
+ assert (
+ validate_url("http://my-searxng.local", allow_private_ips=True)
+ is True
+ )
+
+
+class TestSearXNGUseCase:
+ """Test the specific SearXNG use case that motivated allow_private_ips."""
+
+ def test_searxng_on_localhost(self):
+ """SearXNG on localhost should work with allow_private_ips."""
+ from src.local_deep_research.security.ssrf_validator import validate_url
+
+ with patch("socket.getaddrinfo") as mock_getaddrinfo:
+ mock_getaddrinfo.return_value = [(2, 1, 6, "", ("127.0.0.1", 0))]
+ assert (
+ validate_url("http://localhost:8080", allow_private_ips=True)
+ is True
+ )
+
+ assert (
+ validate_url("http://127.0.0.1:8080", allow_private_ips=True)
+ is True
+ )
+
+ def test_searxng_on_lan(self):
+ """SearXNG on LAN should work with allow_private_ips."""
+ from src.local_deep_research.security.ssrf_validator import validate_url
+
+ # Home network
+ assert (
+ validate_url("http://192.168.1.100:8080", allow_private_ips=True)
+ is True
+ )
+
+ # NAS or server on network
+ assert (
+ validate_url("http://10.0.0.50:8888", allow_private_ips=True)
+ is True
+ )
+
+ # Docker network
+ assert (
+ validate_url("http://172.17.0.2:8080", allow_private_ips=True)
+ is True
+ )
+
+ def test_searxng_hostname_on_lan(self):
+ """SearXNG with hostname on LAN should work with allow_private_ips."""
+ from src.local_deep_research.security.ssrf_validator import validate_url
+
+ with patch("socket.getaddrinfo") as mock_getaddrinfo:
+ # Simulate local DNS or /etc/hosts entry
+ mock_getaddrinfo.return_value = [(2, 1, 6, "", ("192.168.1.50", 0))]
+ assert (
+ validate_url(
+ "http://searxng.local:8080", allow_private_ips=True
+ )
+ is True
+ )
+
+
+class TestDocumentation:
+ """Documentation tests explaining the security model."""
+
+ def test_security_model_documentation(self):
+ """
+ Document the SSRF protection security model.
+
+ WHY THIS EXISTS:
+ - SSRF attacks can be used to access internal services
+ - Attackers can steal credentials from cloud metadata endpoints
+ - Internal services often have weaker security (no auth required)
+
+ THE allow_private_ips PARAMETER:
+ - Designed for trusted self-hosted services like SearXNG
+ - SearXNG URL is admin-configured, not arbitrary user input
+ - Users intentionally run SearXNG on their local network
+ - This is NOT the classic SSRF vector (user submits URL)
+
+ CRITICAL: AWS METADATA IS ALWAYS BLOCKED:
+ - 169.254.169.254 is the #1 SSRF target for credential theft
+ - Even with allow_private_ips=True, this endpoint is blocked
+ - This protects against credential theft in cloud environments
+
+ SECURITY MODEL:
+ | Parameter | Localhost | Private IPs | AWS Metadata |
+ |---------------------|-----------|-------------|--------------|
+ | (default) | Blocked | Blocked | Blocked |
+ | allow_localhost | Allowed | Blocked | Blocked |
+ | allow_private_ips | Allowed | Allowed | BLOCKED |
+ """
+ assert True # Documentation test