~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
"""
Cookie Security Tests
Security model (unchanged by the migration):
- The Secure flag is added iff the request is genuinely HTTPS.
- HTTP requests never get Secure regardless of source IP. Setting Secure on
an HTTP response makes the browser drop the cookie entirely, so doing it
on an IP heuristic broke legitimate Docker/LAN access while providing no
cryptographic protection whatsoever (issue #3849).
- Explicit LDR_TEST_MODE: never Secure (for CI/development).
WHAT CHANGED, MECHANICALLY
--------------------------
Flask decided on ``wsgi.url_scheme``, which ``ProxyFix`` (a WSGI middleware
*inside* the app) rewrote from ``X-Forwarded-Proto``. The ASGI port decides
on ``scope["scheme"]``, and the proxy-header translation moved OUT of the
app object entirely: it is now uvicorn's ``ProxyHeadersMiddleware``, wired
in ``web/app.py`` from ``TRUST_PROXY_HEADERS`` at server start. Nothing
reachable through ``TestClient(app)`` can turn a header into an https
scheme — which is why ``test_https_via_x_forwarded_proto_gets_secure`` is
re-ported below as its inverse: the app must NOT trust an unverified
forwarded header on its own.
SURVEY — covered elsewhere, deliberately NOT duplicated
-------------------------------------------------------
* ``test_localhost_http_no_secure_flag`` / ``test_localhost_session_cookie_
works`` / ``test_localhost_http_no_secure_flag_in_production`` — that
``GET /auth/login`` sets a session cookie and that plain HTTP never carries
Secure: ``tests/web/test_session_cookie_behavior.py``
::test_unauthenticated_session_cookie_is_lax_and_httponly and
::test_http_request_does_not_get_secure_session_cookie (same full stack,
same middleware taken out of testing mode).
* ``test_testing_mode_no_secure_flag`` —
``tests/web/test_secure_cookie_middleware.py::test_testing_mode_never_adds_secure``.
* ``test_cookie_security_summary`` — a placeholder that never ran on main.
Kept verbatim below.
WHAT IS RESTORED HERE
---------------------
The per-source-IP sweep. The unit tests exercise two addresses (8.8.8.8 and
127.0.0.1); this drives the whole #3849 table — Docker bridge, Docker
Desktop NAT (172.67.130.145, the address that actually triggered the bug),
LAN, loopback, public — through the real middleware stack, so a
reintroduced "public IP gets Secure" heuristic fails here rather than in
production.
"""
import pytest
from fastapi.testclient import TestClient
from tests.test_utils import add_src_to_path
add_src_to_path()
def _built_secure_cookie_middleware(app):
"""Walk the built middleware stack to the live SecureCookieMiddleware.
Explicit ``LDR_TEST_MODE`` can disable the Secure flag. These checks
force production mode and restore the original value afterwards, so
they verify the scheme policy regardless of that startup setting.
"""
from local_deep_research.web.fastapi_app import SecureCookieMiddleware
node = app.middleware_stack
seen = set()
while node is not None and id(node) not in seen:
seen.add(id(node))
if isinstance(node, SecureCookieMiddleware):
return node
node = getattr(node, "app", None)
return None
@pytest.fixture
def production_cookie_mode(app, monkeypatch):
"""Force one request so the middleware stack is built, then take the
live SecureCookieMiddleware out of testing mode.
``monkeypatch`` restores both attributes afterwards, so the shared app
object is left exactly as found — the HTTP cases below trip the one-shot
"serving HTTP to a public client" warning latch.
"""
TestClient(app, raise_server_exceptions=False).get("/auth/login")
mw = _built_secure_cookie_middleware(app)
assert mw is not None, (
"SecureCookieMiddleware is not in the built middleware stack; the "
"cookie-security behaviour under test is not installed at all"
)
monkeypatch.setattr(mw, "testing", False)
monkeypatch.setattr(
mw, "_warned_insecure_public", mw._warned_insecure_public
)
return app
def _session_cookies(response) -> list[str]:
return [
c
for c in response.headers.get_list("set-cookie")
if c.startswith("session=")
]
class TestHttpCookieSecurity:
"""HTTP requests never get the Secure flag, regardless of source IP.
This is the core fix for #3849: setting Secure on an HTTP response makes
the browser drop the cookie, breaking sessions without adding any real
security (the underlying transport is still plaintext).
"""
@pytest.mark.parametrize(
"remote_addr",
[
"127.0.0.1",
"192.168.1.100",
"10.0.0.50",
"172.16.0.1",
"172.17.0.2", # Default Docker bridge
"172.67.130.145", # Docker Desktop NAT (the #3849 trigger)
"8.8.8.8",
"104.16.0.1",
],
)
def test_http_no_secure_flag(self, production_cookie_mode, remote_addr):
app = production_cookie_mode
client = TestClient(
app, raise_server_exceptions=False, client=(remote_addr, 45678)
)
response = client.get("/auth/login")
assert response.status_code == 200
cookies = _session_cookies(response)
assert cookies, (
f"GET /auth/login from {remote_addr} set no session cookie — the "
f"Secure assertion below would be vacuous"
)
for cookie in cookies:
assert "; secure" not in cookie.lower(), (
f"HTTP from {remote_addr} must NOT get the Secure flag "
f"(#3849: the browser then drops the cookie). Got: {cookie}"
)
class TestHttpsCookieSecurity:
"""HTTPS requests always get the Secure flag."""
def test_https_scheme_gets_secure(self, production_cookie_mode):
"""Positive control for the whole IP sweep above.
Without this, a middleware that had stopped adding Secure entirely —
or one still pinned to testing mode — would satisfy all eight
no-Secure cases while providing no protection over real HTTPS.
"""
app = production_cookie_mode
client = TestClient(
app,
raise_server_exceptions=False,
base_url="https://testserver",
client=("10.0.0.1", 45678),
)
response = client.get("/auth/login")
assert response.status_code == 200
cookies = _session_cookies(response)
assert cookies, "GET /auth/login over https set no session cookie"
assert all("; secure" in c.lower() for c in cookies), (
f"an https session cookie is missing the Secure flag: {cookies}"
)
def test_forwarded_proto_alone_does_not_add_secure(
self, production_cookie_mode
):
"""An unverified ``X-Forwarded-Proto`` must not flip the decision.
Flask's in-app ``ProxyFix`` translated this header unconditionally.
Under ASGI the translation lives in uvicorn's
``ProxyHeadersMiddleware``, enabled only when the operator sets
``TRUST_PROXY_HEADERS`` (``web/app.py``) — so the app object itself
must ignore it. Honouring an attacker-settable header here would let
any client mark its own cookie Secure over plaintext and lose the
cookie, which is the #3849 failure mode dressed up as a proxy
feature.
"""
app = production_cookie_mode
client = TestClient(
app, raise_server_exceptions=False, client=("10.0.0.1", 45678)
)
response = client.get(
"/auth/login", headers={"X-Forwarded-Proto": "https"}
)
assert response.status_code == 200
cookies = _session_cookies(response)
assert cookies, "GET /auth/login set no session cookie"
for cookie in cookies:
assert "; secure" not in cookie.lower(), (
f"the app trusted a raw X-Forwarded-Proto header over a "
f"plain-HTTP connection: {cookie}"
)
@pytest.mark.skip(reason="documentation/placeholder test - not implemented")
def test_cookie_security_summary():
"""
Summary of cookie security behavior for CI validation.
Expected behavior:
| Scenario | request scheme | Secure Flag |
|-------------------------------|-----------------|-------------|
| HTTP from localhost | http | No |
| HTTP from LAN client | http | No |
| HTTP from Docker NAT gateway | http | No |
| HTTP from public IP | http | No |
| HTTPS via reverse proxy | https | Yes |
| Direct HTTPS | https | Yes |
| TESTING=1 mode | any | No |
The decision is based purely on the protocol, not the source IP.
Setting Secure on HTTP responses doesn't add security and breaks the
browser's ability to store the cookie. "HTTPS via reverse proxy" means
uvicorn was started with proxy headers trusted, so scope["scheme"] is
already https by the time the app sees the request.
"""
assert True