~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
# allow: no-sut-import — drives the real SecureCookieMiddleware ASGI class
"""SecureCookieMiddleware adds the Secure flag ONLY over real HTTPS (#3849).
Marking a cookie Secure on a response served over plain HTTP makes the browser
drop the cookie entirely (RFC6265bis) — so a deployment reachable over HTTP
from a public IP would hit an unbreakable login loop. Main's Flask middleware
adds Secure iff the request scheme is https; a regression in the ASGI port
also added it for "public IP over HTTP", reintroducing that bug. This pins the
correct behavior on the FastAPI middleware.
"""
import asyncio
from unittest.mock import patch
from local_deep_research.web.fastapi_app import SecureCookieMiddleware
def _cookie_is_secure(scheme, client_ip, testing=False):
async def inner(scope, receive, send):
await send(
{
"type": "http.response.start",
"status": 200,
"headers": [
(b"set-cookie", b"session=abc; Path=/; HttpOnly"),
],
}
)
await send({"type": "http.response.body", "body": b"ok"})
async def run():
mw = SecureCookieMiddleware(inner, testing=testing)
scope = {
"type": "http",
"scheme": scheme,
"client": (client_ip, 12345),
"headers": [],
}
sent = []
async def send(m):
sent.append(m)
async def receive():
return {"type": "http.request", "body": b"", "more_body": False}
await mw(scope, receive, send)
start = next(m for m in sent if m["type"] == "http.response.start")
cookie = next(
v.decode("latin-1")
for k, v in start["headers"]
if k == b"set-cookie"
)
return "Secure" in cookie
return asyncio.run(run())
def _run_warning_request(middleware, *, scheme, client_ip):
"""Drive one request through an existing middleware instance."""
async def run():
scope = {
"type": "http",
"scheme": scheme,
"client": (client_ip, 12345),
"headers": [],
}
async def send(_message):
return None
async def receive():
return {"type": "http.request", "body": b"", "more_body": False}
await middleware(scope, receive, send)
asyncio.run(run())
def _warning_middleware(*, testing=False):
async def inner(_scope, _receive, _send):
return None
return SecureCookieMiddleware(inner, testing=testing)
def test_http_from_public_ip_does_not_get_secure_flag():
# The #3849 regression: this used to be True and broke login over HTTP.
assert _cookie_is_secure("http", "8.8.8.8") is False
def test_http_from_private_ip_does_not_get_secure_flag():
assert _cookie_is_secure("http", "127.0.0.1") is False
def test_https_gets_secure_flag():
assert _cookie_is_secure("https", "8.8.8.8") is True
def test_testclient_host_over_http_no_secure():
assert _cookie_is_secure("http", "testclient") is False
def test_testing_mode_never_adds_secure():
assert _cookie_is_secure("https", "8.8.8.8", testing=True) is False
def test_public_http_warning_is_emitted_once_across_repeated_requests():
middleware = _warning_middleware()
with (
patch.object(
middleware,
"_maybe_warn_insecure_public",
wraps=middleware._maybe_warn_insecure_public,
) as warning_gate,
patch("local_deep_research.web.fastapi_app.logger.warning") as warning,
):
_run_warning_request(middleware, scheme="http", client_ip="8.8.8.8")
_run_warning_request(middleware, scheme="http", client_ip="8.8.8.8")
assert warning_gate.call_count == 2
warning.assert_called_once()
assert warning.call_args.args[1] == "8.8.8.8"
assert middleware._warned_insecure_public is True
def test_public_http_warning_bounds_a_long_scoped_address():
"""#6938: a public IPv6 address with a scope id parses at any length,
and under TRUST_PROXY_HEADERS it is the client's X-Forwarded-For
entry, so the logged form is capped (an ordinary address, as above,
is logged unchanged and unquoted)."""
middleware = _warning_middleware()
with patch("local_deep_research.web.fastapi_app.logger.warning") as warning:
_run_warning_request(
middleware,
scheme="http",
client_ip="2607:f8b0::1%" + "z" * 100_000,
)
warning.assert_called_once()
assert len(warning.call_args.args[1]) <= 64
def test_private_and_loopback_http_stay_silent():
middleware = _warning_middleware()
with (
patch.object(
middleware,
"_maybe_warn_insecure_public",
wraps=middleware._maybe_warn_insecure_public,
) as warning_gate,
patch("local_deep_research.web.fastapi_app.logger.warning") as warning,
):
_run_warning_request(
middleware, scheme="http", client_ip="192.168.1.20"
)
_run_warning_request(middleware, scheme="http", client_ip="127.0.0.1")
assert warning_gate.call_count == 2
warning.assert_not_called()
assert middleware._warned_insecure_public is False
def test_https_does_not_enter_the_insecure_public_warning_gate():
middleware = _warning_middleware()
with (
patch.object(
middleware,
"_maybe_warn_insecure_public",
wraps=middleware._maybe_warn_insecure_public,
) as warning_gate,
patch("local_deep_research.web.fastapi_app.logger.warning") as warning,
):
_run_warning_request(middleware, scheme="https", client_ip="8.8.8.8")
warning_gate.assert_not_called()
warning.assert_not_called()
assert middleware._warned_insecure_public is False
def test_testing_mode_does_not_enter_the_insecure_public_warning_gate():
middleware = _warning_middleware(testing=True)
with (
patch.object(
middleware,
"_maybe_warn_insecure_public",
wraps=middleware._maybe_warn_insecure_public,
) as warning_gate,
patch("local_deep_research.web.fastapi_app.logger.warning") as warning,
):
_run_warning_request(middleware, scheme="http", client_ip="8.8.8.8")
warning_gate.assert_not_called()
warning.assert_not_called()
assert middleware._warned_insecure_public is False