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.

5.1 KB
Raw Blame
"""Tests for configurable API CORS (security.cors.allowed_origins).

Main applied CORS headers to API routes from this setting; the FastAPI
migration dropped it, leaving LDR_SECURITY_CORS_ALLOWED_ORIGINS as dead
config. _configure_cors restores it via Starlette's CORSMiddleware,
fail-closed (no middleware when unset). Because the app reads the setting
once at import, these tests build throwaway apps through _configure_cors
rather than re-importing the real app.
"""

from unittest.mock import patch

from fastapi import FastAPI
from fastapi.testclient import TestClient
from starlette.middleware.cors import CORSMiddleware

from local_deep_research.web.fastapi_app import _configure_cors

ENV = "local_deep_research.settings.env_registry.get_env_setting"


def _app_with_cors(configured):
    app = FastAPI()

    @app.get("/api/v1/health")
    def health():
        return {"status": "ok"}

    with patch(ENV, return_value=configured):
        _configure_cors(app)
    return app


def _has_cors(app):
    """True when CORS is registered on `app`, wrapper or not.

    This used to test `m.cls is CORSMiddleware` exactly. That stopped being
    the registered class when CORS was re-scoped to API prefixes: the app now
    registers `_PathScopedCORSMiddleware`, which builds a real
    `CORSMiddleware` internally and delegates to it only for matching paths
    (Starlette's CORSMiddleware has no path predicate of its own).

    Accepting either is right for what these tests assert — "is CORS wired up
    at all", used in both the positive and the fail-closed direction. The
    behavioural checks in this file (does an allowed Origin actually get the
    header, does an unconfigured app stay silent) are what pin the semantics;
    this helper only answers the registration question.
    """
    from local_deep_research.web.fastapi_app import _PathScopedCORSMiddleware

    return any(
        m.cls is CORSMiddleware or m.cls is _PathScopedCORSMiddleware
        for m in app.user_middleware
    )


class TestFailClosed:
    def test_unset_registers_no_cors(self):
        assert _has_cors(_app_with_cors(None)) is False

    def test_empty_registers_no_cors(self):
        assert _has_cors(_app_with_cors("")) is False

    def test_separator_only_registers_no_cors(self):
        """A separator-only value parses to zero origins -- the same intent
        as unset, not an allowlist that can never match any Origin."""
        assert _has_cors(_app_with_cors(",")) is False
        assert _has_cors(_app_with_cors(" , , ")) is False

    def test_no_cors_headers_when_unconfigured(self):
        app = _app_with_cors(None)
        client = TestClient(app)
        resp = client.get(
            "/api/v1/health", headers={"Origin": "https://evil.example"}
        )
        assert "access-control-allow-origin" not in resp.headers


class TestConfiguredOrigins:
    def test_single_origin_allows_that_origin(self):
        app = _app_with_cors("https://app.example.com")
        assert _has_cors(app) is True

        client = TestClient(app)
        resp = client.get(
            "/api/v1/health",
            headers={"Origin": "https://app.example.com"},
        )
        assert (
            resp.headers["access-control-allow-origin"]
            == "https://app.example.com"
        )

    def test_non_whitelisted_origin_gets_no_allow_header(self):
        app = _app_with_cors("https://app.example.com")
        client = TestClient(app)
        resp = client.get(
            "/api/v1/health",
            headers={"Origin": "https://evil.example"},
        )
        assert "access-control-allow-origin" not in resp.headers

    def test_multiple_origins_reflects_each_match(self):
        app = _app_with_cors("https://a.example.com, https://b.example.com")
        client = TestClient(app)
        for origin in ("https://a.example.com", "https://b.example.com"):
            resp = client.get("/api/v1/health", headers={"Origin": origin})
            assert resp.headers["access-control-allow-origin"] == origin

    def test_preflight_options_is_answered(self):
        app = _app_with_cors("https://app.example.com")
        client = TestClient(app)
        resp = client.options(
            "/api/v1/health",
            headers={
                "Origin": "https://app.example.com",
                "Access-Control-Request-Method": "POST",
                "Access-Control-Request-Headers": "X-CSRFToken",
            },
        )
        assert resp.status_code == 200
        assert (
            resp.headers["access-control-allow-origin"]
            == "https://app.example.com"
        )
        assert "POST" in resp.headers["access-control-allow-methods"]


class TestWildcard:
    def test_wildcard_allows_any_origin_without_credentials(self):
        app = _app_with_cors("*")
        client = TestClient(app)
        resp = client.get(
            "/api/v1/health", headers={"Origin": "https://anything.example"}
        )
        assert resp.headers["access-control-allow-origin"] == "*"
        # Credentials must never be combined with the wildcard origin.
        assert "access-control-allow-credentials" not in resp.headers