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.

3.8 KB
Raw Blame
"""CORS must reach API paths only, not every HTML page.

On `main`, `SecurityHeaders._is_api_route` (security/security_headers.py,
deleted by the FastAPI migration) gated CORS headers to `/api/`,
`/research/api/` and `/history/api`. The migration registered Starlette's
`CORSMiddleware` app-wide instead, and that middleware has no path predicate —
so an operator who whitelisted an origin for API access also granted it
cross-origin reads of every HTML page. `_PathScopedCORSMiddleware` restores the
scoping.

Bounded either way: `_configure_cors` is fail-closed (no
`security.cors.allowed_origins` -> no middleware at all, the default), and
`allow_credentials` is hardcoded False, so a cross-origin caller never carries
the session cookie. This is about not widening a boundary that was deliberate.
"""

from starlette.middleware.cors import CORSMiddleware
from starlette.responses import PlainTextResponse
from starlette.testclient import TestClient

from local_deep_research.web.fastapi_app import (
    _CORS_API_PREFIXES,
    _PathScopedCORSMiddleware,
)

ORIGIN = "https://allowed.example"


def _build(path_returns="ok"):
    """Minimal ASGI app behind the same wrapper the real app uses."""

    async def inner(scope, receive, send):
        await PlainTextResponse(path_returns)(scope, receive, send)

    return _PathScopedCORSMiddleware(
        inner,
        prefixes=_CORS_API_PREFIXES,
        cors_factory=lambda app: CORSMiddleware(
            app,
            allow_origins=[ORIGIN],
            allow_credentials=False,
            allow_methods=["GET", "POST", "OPTIONS"],
            allow_headers=["Content-Type"],
        ),
    )


def test_api_paths_receive_cors_headers():
    """Positive control: without this, a wrapper that dropped CORS entirely
    would satisfy the negative assertions below."""
    client = TestClient(_build())
    for prefix in _CORS_API_PREFIXES:
        resp = client.get(prefix + "thing", headers={"Origin": ORIGIN})
        assert resp.headers.get("access-control-allow-origin") == ORIGIN, (
            f"{prefix} lost its CORS header; API clients depend on it"
        )


def test_html_pages_do_not_receive_cors_headers():
    """The actual regression: a whitelisted origin must not gain cross-origin
    read access to ordinary pages."""
    client = TestClient(_build())
    for path in ("/", "/settings", "/history", "/research/index"):
        resp = client.get(path, headers={"Origin": ORIGIN})
        assert "access-control-allow-origin" not in resp.headers, (
            f"{path} carried CORS headers; whitelisting an origin for the API "
            f"must not also expose HTML pages cross-origin"
        )


def test_preflight_is_answered_on_api_paths_only():
    """OPTIONS preflight is what actually gates a cross-origin write."""
    client = TestClient(_build())
    api = client.options(
        "/api/thing",
        headers={
            "Origin": ORIGIN,
            "Access-Control-Request-Method": "POST",
        },
    )
    assert api.headers.get("access-control-allow-origin") == ORIGIN

    page = client.options(
        "/settings",
        headers={
            "Origin": ORIGIN,
            "Access-Control-Request-Method": "POST",
        },
    )
    assert "access-control-allow-origin" not in page.headers


def test_non_http_scopes_bypass_cors():
    """CORS is HTTP-only; a websocket scope must pass straight through rather
    than being handed to CORSMiddleware."""
    seen = {}

    async def inner(scope, receive, send):
        seen["type"] = scope["type"]

    wrapper = _PathScopedCORSMiddleware(
        inner, prefixes=_CORS_API_PREFIXES, cors_factory=lambda app: app
    )

    import asyncio

    async def _noop():
        return {"type": "websocket.connect"}

    asyncio.run(wrapper({"type": "websocket", "path": "/api/x"}, _noop, _noop))
    assert seen["type"] == "websocket"