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.

docs: add explanatory comments for SSRF security patterns (#2607)

LearningCircuit907e2317323f
diff --git a/src/local_deep_research/security/safe_requests.py b/src/local_deep_research/security/safe_requests.py
index f9c12d7e4b..b5ad17937d 100644
--- a/src/local_deep_research/security/safe_requests.py
+++ b/src/local_deep_research/security/safe_requests.py
@@ -31,7 +31,8 @@ def _check_response_size(response: requests.Response) -> None:
Only checks the Content-Length header; responses using chunked transfer
encoding (or otherwise missing Content-Length) are not bounded by this
- check.
+ check. A complete solution would require streaming with iter_content()
+ and counting bytes, which is out of scope here.
Must be called before returning a response to the caller. On rejection
the response is closed so the connection is not leaked.
@@ -120,7 +121,12 @@ def safe_get(
try:
response = requests.get(url, params=params, **kwargs)
- # Follow redirects manually with SSRF validation on each hop
+ # Follow redirects manually with SSRF validation on each hop.
+ # Each hop uses a fresh requests.get() call without a session,
+ # so cookies set by intermediate responses are not carried
+ # forward. This is acceptable for current callers (all stateless).
+ # Callers needing cookie persistence across redirects should use
+ # SafeSession instead, which preserves cookies via its cookie jar.
if caller_wants_redirects:
redirects_followed = 0
while (
@@ -153,6 +159,10 @@ def safe_get(
current_url = redirect_url
response.close()
+ # Note: params are intentionally NOT forwarded to redirect
+ # hops. Per HTTP spec, the server's Location header contains
+ # the complete target URL. Re-appending original query params
+ # would corrupt it.
response = requests.get(redirect_url, **kwargs)
redirects_followed += 1
@@ -161,6 +171,9 @@ def safe_get(
and redirects_followed >= _MAX_REDIRECTS
):
response.close()
+ # Note: raises ValueError here, while SafeSession raises
+ # requests.TooManyRedirects (delegated to the base class).
+ # Callers should catch ValueError for standalone functions.
raise ValueError(
f"Too many redirects ({_MAX_REDIRECTS}) from {url}"
)
@@ -235,7 +248,10 @@ def safe_post(
try:
response = requests.post(url, data=data, json=json, **kwargs)
- # Follow redirects manually with SSRF validation on each hop
+ # Follow redirects manually with SSRF validation on each hop.
+ # Each hop uses a fresh request without a session, so cookies
+ # set by intermediate responses are not carried forward. Callers
+ # needing cookie persistence should use SafeSession instead.
if caller_wants_redirects:
redirect_method = "POST"
redirects_followed = 0
@@ -290,6 +306,9 @@ def safe_post(
and redirects_followed >= _MAX_REDIRECTS
):
response.close()
+ # Note: raises ValueError here, while SafeSession raises
+ # requests.TooManyRedirects (delegated to the base class).
+ # Callers should catch ValueError for standalone functions.
raise ValueError(
f"Too many redirects ({_MAX_REDIRECTS}) from {url}"
)
@@ -397,6 +416,9 @@ class SafeSession(requests.Session):
logger.warning(
f"Request to {request.url} blocked by SSRF validation"
)
+ # Note: This error says "security validation" while safe_get/
+ # safe_post say "SSRF validation". The difference indicates the
+ # source (session vs standalone function) in logs.
raise ValueError(
f"Redirect target failed security validation (possible SSRF): {request.url}"
)
@@ -406,6 +428,12 @@ class SafeSession(requests.Session):
# followed as redirects (have a Location header and allow_redirects
# is True). A 3xx with no Location, or when allow_redirects=False,
# is the final response and must be size-checked.
+ #
+ # Default True matches the requests library's default behavior.
+ # For intermediate redirect hops dispatched by resolve_redirects(),
+ # allow_redirects may not be explicitly in kwargs, so True is the
+ # correct default (we wouldn't be in send() for a redirect hop
+ # if redirects were disabled).
is_intermediate_redirect = (
response.status_code in _REDIRECT_STATUS_CODES
and response.headers.get("Location")
diff --git a/src/local_deep_research/security/ssrf_validator.py b/src/local_deep_research/security/ssrf_validator.py
index 7528be2a70..1b461798cb 100644
--- a/src/local_deep_research/security/ssrf_validator.py
+++ b/src/local_deep_research/security/ssrf_validator.py
@@ -104,7 +104,9 @@ def is_ip_blocked(
def validate_url(
url: str,
- allow_redirects: bool = True,
+ allow_redirects: bool = True, # Unused — kept for API compatibility.
+ # Redirect validation is handled at the caller level (safe_get/safe_post
+ # manual loop and SafeSession.send() override), not in validate_url().
allow_localhost: bool = False,
allow_private_ips: bool = False,
) -> bool:
@@ -118,7 +120,8 @@ def validate_url(
Args:
url: URL to validate
- allow_redirects: Whether to allow redirects (future use)
+ allow_redirects: Unused — kept for API compatibility. Redirect
+ validation is handled by safe_get/safe_post and SafeSession.
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.
@@ -136,9 +139,11 @@ def validate_url(
SSRF validation is automatically bypassed during pytest runs via
the PYTEST_CURRENT_TEST environment variable (set by pytest).
"""
- # Bypass SSRF validation only during pytest runs.
- # PYTEST_CURRENT_TEST is set automatically by pytest and is not
- # user-controllable in production environments.
+ # Security trade-off: This bypasses ALL SSRF validation during pytest
+ # runs. PYTEST_CURRENT_TEST is set automatically by pytest and is not
+ # expected in production. If this bypass is ever removed, tests that
+ # call safe_get/safe_post with real URLs will need to mock
+ # validate_url() or use responses/pytest-httpserver instead.
pytest_current_test = os.environ.get("PYTEST_CURRENT_TEST")
if pytest_current_test:
logger.debug(f"SSRF validation bypassed in pytest for URL: {url}")