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.9 KB
Raw Blame
"""Test CSRF with header."""

import pytest

# test-time bypass and uses Flask app.config[]. After Wave 9, CSRF is
# fail-closed even for tests; the new pattern is to fetch a real token
# (see tests/web/routers/test_state_changing_flows.py).


import time  # noqa: E402


class TestCSRFHeader:
    """Test CSRF protection with headers."""

    def test_csrf_with_header(self, client):
        """Login flow with the always-on FastAPI CSRF middleware.

        Mutating POSTs must carry the session CSRF token; verifies a
        token-bearing login succeeds.
        """
        from tests.api_tests.conftest import get_csrf_token

        # Generate unique username to avoid conflicts
        test_username = f"testuser_csrf_{int(time.time() * 1000)}"

        # Get login page
        response = client.get("/auth/login")
        assert response.status_code == 200

        # Register the user (with a real CSRF token)
        register_response = client.post(
            "/auth/register",
            data={
                "username": test_username,
                "password": "TestPass123",
                "confirm_password": "TestPass123",
                "acknowledge": "true",
                "csrf_token": get_csrf_token(client),
            },
        )
        assert register_response.status_code in [200, 302]

        # Now test login (with a real CSRF token)
        response = client.post(
            "/auth/login",
            data={
                "username": test_username,
                "password": "TestPass123",
                "csrf_token": get_csrf_token(client),
            },
            follow_redirects=False,
        )

        assert response.status_code in [200, 302]

        if response.status_code == 302:
            # Login successful, redirecting
            loc = response.headers.get("location", "")
            assert "/" in loc or "/research" in loc

    def test_api_csrf_exemption(self, authenticated_client):
        """Test that API routes are exempt from CSRF."""
        # API routes should work without CSRF token
        response = authenticated_client.get("/api/v1/health")
        assert response.status_code == 200

        # Research API should also work
        response = authenticated_client.get("/research/api/history")
        assert response.status_code in [200, 404]  # 404 if no history yet

    @pytest.mark.skip(
        reason="Inspects Flask-WTF's app.extensions['csrf']._exempt_blueprints "
        "registry, which doesn't exist under FastAPI (CSRF is always-on "
        "middleware that exempts by path, not Blueprint object). Exemption "
        "behaviour is covered by test_api_csrf_exemption + "
        "tests/security/test_csrf_*."
    )
    def test_csrf_blueprint_exemptions_configured(self, app):
        """Test that only api_v1 blueprint is CSRF-exempt.

        Flask-WTF 1.2.2 requires Blueprint objects (not strings) in
        _exempt_blueprints.  Only api_v1 should be exempt — the browser-facing
        blueprints (api, benchmark, research) should require CSRF tokens since
        the frontend already sends them.
        """
        csrf = app.extensions.get("csrf")
        assert csrf is not None, "CSRFProtect extension not initialized"

        exempt_names = {bp.name for bp in csrf._exempt_blueprints}

        # api_v1 should be exempt (programmatic REST API)
        assert "api_v1" in exempt_names, (
            "Blueprint 'api_v1' missing from _exempt_blueprints. "
            "Ensure csrf.exempt() receives the Blueprint object, not a string."
        )

        # Browser-facing blueprints should NOT be exempt
        for should_not_be_exempt in ("api", "benchmark", "research"):
            assert should_not_be_exempt not in exempt_names, (
                f"Blueprint '{should_not_be_exempt}' should not be in "
                f"_exempt_blueprints — it is browser-facing and the frontend "
                f"already sends CSRF tokens."
            )