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.

2.7 KB
Raw Blame
# allow: no-sut-import — asserts the CSRF exemption policy of the live middleware
"""Test that CSRF exemptions stay narrowly scoped.

Ported from the Flask-WTF era (which inspected
``app.extensions['csrf']._exempt_blueprints``). Under FastAPI the
``CSRFMiddleware`` exempts by PATH, not by Blueprint object — and the policy
is deliberately stricter: only the WebSocket mount and the two
auth-bootstrap endpoints are exempt. Notably ``/api/v1`` is NOT exempt
anymore (it authenticates with session cookies, so CSRF applies), which is
a hardening over the old Flask behaviour where the api_v1 blueprint was
blanket-exempt.
"""

from local_deep_research.web.dependencies.csrf import (
    _SKIP_EXACT_PATHS,
    _SKIP_PATH_PREFIXES,
)


def _is_exempt(path: str) -> bool:
    """Mirror the middleware's exemption test for a given path."""
    return path in _SKIP_EXACT_PATHS or any(
        path.startswith(p) for p in _SKIP_PATH_PREFIXES
    )


class TestCsrfHardening:
    """Verify CSRF exemptions are narrowly scoped (FastAPI path-based policy)."""

    def test_websocket_mount_is_exempt(self):
        """Socket.IO handles its own auth handshake, so /ws is CSRF-exempt."""
        assert _is_exempt("/ws")
        assert _is_exempt("/ws/socket.io/")

    def test_api_v1_is_not_exempt(self):
        """api_v1 uses session-cookie auth → CSRF applies (stricter than Flask).

        Regression guard: re-adding /api/v1 to the skip lists would silently
        drop CSRF protection from the programmatic REST API.
        """
        assert not _is_exempt("/api/v1/")
        assert not _is_exempt("/api/v1/health")

    def test_browser_facing_prefixes_not_exempt(self):
        """api / benchmark / research routes must require CSRF tokens."""
        for path in (
            "/api/check/ollama_model",
            "/benchmark/api/start",
            "/research/api/start",
        ):
            assert not _is_exempt(path), f"{path} should NOT be CSRF-exempt"

    def test_only_token_mint_endpoints_exact_exempt(self):
        """Only the token-mint endpoint is exact-path exempt.

        /auth/validate-password was exempt under Flask but is enforced here:
        the register/change-password forms that call it already render a CSRF
        token, so the exemption only left an unauthenticated password-accepting
        POST outside the middleware.

        Listing /auth/login or /auth/register here would re-open a
        login-CSRF (OWASP A07) vector, so they must stay enforced.
        """
        assert "/auth/csrf-token" in _SKIP_EXACT_PATHS
        assert "/auth/validate-password" not in _SKIP_EXACT_PATHS
        assert "/auth/login" not in _SKIP_EXACT_PATHS
        assert "/auth/register" not in _SKIP_EXACT_PATHS