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.0 KB
Raw Blame
"""Test authentication without CSRF to verify CSRF protection."""

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).


from loguru import logger  # noqa: E402


class TestWithoutCSRF:
    """Test that authentication properly requires CSRF tokens."""

    @pytest.mark.skip(
        reason="Toggled Flask's app.config['WTF_CSRF_ENABLED'] = False to test "
        "the CSRF-disabled path. FastAPI's CSRFMiddleware is always active with "
        "no per-test toggle, so 'login works with CSRF disabled' is not a "
        "reachable state. Token-required behaviour is covered by "
        "test_login_requires_csrf_by_default below + tests/security/test_csrf_*."
    )
    def test_login_without_csrf_protection(self, app):
        """(Obsolete) login behavior when CSRF protection is disabled."""

    def test_login_requires_csrf_by_default(self, client):
        """Login requires a CSRF token (always-on FastAPI middleware)."""
        response = client.post(
            "/auth/login",
            data={"username": "testuser", "password": "TestPass123"},
            follow_redirects=False,
        )
        # Should fail because the CSRF token is missing (403, not a 302)
        assert response.status_code != 302

    def test_api_endpoints_without_csrf(self, authenticated_client):
        """Test that API endpoints work without CSRF for authenticated users."""
        # API endpoints typically don't require CSRF for GET requests
        endpoints = [
            "/settings/api",
            "/history/api",
            "/metrics/api/cost-analytics",
        ]

        for endpoint in endpoints:
            response = authenticated_client.get(endpoint)
            assert response.status_code == 200
            logger.info(f"✅ GET {endpoint} works without CSRF token")

    def test_post_api_without_csrf(self, authenticated_client):
        """Test that POST API endpoints properly handle CSRF."""
        # Try to start research without CSRF token
        # audit: PUNCHLIST reviewed 2026-05 — issue resolved by prior PR (recommendation: FIX_ASSERTION).
        research_data = {"query": "test query", "mode": "quick"}

        # POST without CSRF should fail unless endpoint is exempt
        response = authenticated_client.post(
            "/api/start_research",
            json=research_data,  # JSON requests typically bypass CSRF
            content_type="application/json",
        )

        # JSON API endpoints often bypass CSRF protection
        logger.info(
            f"POST /api/start_research (JSON) -> {response.status_code}"
        )

        # Form POST without CSRF should fail
        response = authenticated_client.post(
            "/api/start_research",
            data=research_data,  # Form data should require CSRF
            follow_redirects=False,
        )

        logger.info(
            f"POST /api/start_research (form data, no CSRF) -> {response.status_code}"
        )