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.

14.8 KB
Raw Blame
"""
CSRF (Cross-Site Request Forgery) Protection Tests

Re-ported from the pre-FastAPI-migration Flask module, which built the app
with ``create_app()`` and flipped ``WTF_CSRF_ENABLED``. Flask-WTF's
``CSRFProtect`` is gone; enforcement is now the always-on ASGI
``CSRFMiddleware`` (``web/dependencies/csrf.py``), whose own docstring names
this file as coverage it must keep preserving. There is no "disable CSRF"
config switch any more, so ``client_no_csrf`` has no successor and the
rejection status changed from Flask-WTF's **400** to the middleware's
**403**.

SURVEY — covered elsewhere on this branch, deliberately NOT duplicated
---------------------------------------------------------------------
* ``test_csrf_protection_on_state_changing_operations`` — "GET is never
  blocked". ``tests/web/test_csrf_middleware_edges.py``
  ::test_safe_methods_never_blocked / ::test_get_not_blocked_even_with_
  bogus_token_header pin exactly this against the real middleware class.
  The original also ended in a bare ``assert True``.
* ``test_double_submit_cookie_pattern`` — asserted only that the token is a
  non-empty string, i.e. a duplicate of ``test_csrf_token_endpoint_exists``
  below (Flask-WTF used session tokens, not double-submit cookies, and so
  does the ASGI middleware — the pattern named in the title was never
  implemented).
* ``test_csrf_protection_exempt_endpoints`` — ``GET /api/v1/health`` is
  reachable without a token. Covered by
  ``tests/web/routers/test_fastapi_migration.py::test_health_endpoint`` and
  ``tests/security/test_api_v1_auth.py::test_health_endpoint_no_auth_required``;
  the exemption LIST itself is fenced by
  ``tests/security/test_csrf_hardening.py``.

The four ``@pytest.mark.skip(reason="documentation/placeholder ...")``
bodies are kept verbatim — they never ran on main either.
"""

import itertools

import pytest
from fastapi.testclient import TestClient
from tests.test_utils import add_src_to_path

add_src_to_path()

# Monotonic, never random: a random per-client address collides across a
# long session and produces 429s unrelated to the guard under test.
_IP_COUNTER = itertools.count(1)


def _client(app) -> TestClient:
    n = next(_IP_COUNTER)
    client = TestClient(app, raise_server_exceptions=False)
    client.headers.update(
        {"X-Forwarded-For": f"10.78.{n // 250 % 250}.{n % 250 + 1}"}
    )
    return client


def _stamped_client(app) -> tuple[TestClient, str]:
    """A client whose session carries a CSRF token, plus the token."""
    client = _client(app)
    client.get("/auth/login")
    resp = client.get("/auth/csrf-token")
    assert resp.status_code == 200
    return client, resp.json()["csrf_token"]


def test_csrf_middleware_is_installed_on_the_app():
    """The only structural link between this module and the product.

    Every test below asserts a 403 arrives for a request without a valid
    token. That shape passes for the wrong reason if the middleware is gone
    and something else refuses the request — so pin that the real
    ``CSRFMiddleware`` is actually in the app's middleware stack. It also
    gives the module a genuine import of the code under test rather than
    reaching it only through a fixture.
    """
    from local_deep_research.web.dependencies.csrf import CSRFMiddleware
    from local_deep_research.web.fastapi_app import app

    installed = [m.cls for m in app.user_middleware]
    assert CSRFMiddleware in installed, (
        "CSRFMiddleware is not in the app's middleware stack — the 403s the "
        f"tests below assert would be coming from something else. Found: "
        f"{[getattr(c, '__name__', c) for c in installed]}"
    )


class TestCSRFProtection:
    """Test CSRF protection in web forms and API endpoints."""

    def test_csrf_token_endpoint_exists(self, app):
        """The token-mint endpoint answers, and mints something unguessable.

        Length is the load-bearing part beyond
        ``test_fastapi_migration.py::test_csrf_token_endpoint`` (which only
        checks the key is present): a token short enough to brute-force is
        indistinguishable from no CSRF protection at all.
        ``generate_csrf_token`` uses ``secrets.token_hex(32)`` == 64 hex
        characters == 256 bits.
        """
        client = _client(app)
        response = client.get("/auth/csrf-token")
        assert response.status_code == 200
        data = response.json()
        assert "csrf_token" in data
        token = data["csrf_token"]
        assert len(token) >= 32, (
            f"CSRF token is only {len(token)} characters — too short to "
            f"resist guessing"
        )
        assert all(c in "0123456789abcdef" for c in token), (
            f"expected a hex token from secrets.token_hex, got {token!r}"
        )

    def test_csrf_token_is_unique_per_session(self, app):
        """Two sessions must never be handed the same token.

        A process-wide constant would validate an attacker's forged POST
        against the victim's session, which is the whole attack this
        middleware exists to stop.
        """
        _, token1 = _stamped_client(app)
        _, token2 = _stamped_client(app)

        assert token1 and token2
        assert token1 != token2, (
            "two independent sessions were issued the same CSRF token"
        )

    def test_csrf_token_is_stable_within_a_session(self, app):
        """...and the same session must keep getting the SAME token.

        ``generate_csrf_token`` returns the value already stored in the
        session. If it minted a fresh one per call, the token rendered into
        a form would never match the one in the session by the time the form
        was submitted, and every no-JS POST would 403. Paired with the
        uniqueness test above so neither "always the same" nor "always
        different" can pass both.
        """
        client, token1 = _stamped_client(app)
        token2 = client.get("/auth/csrf-token").json()["csrf_token"]
        assert token1 == token2, (
            "the CSRF token was regenerated within a single session; every "
            "rendered form would immediately be stale"
        )

    def test_post_request_without_session_token_rejected(self, app):
        """A POST from a client with no session at all is refused.

        The middleware fails closed: no ``_csrf_token`` in the session means
        403 before the handler runs, whether or not the caller is
        authenticated. ``/auth/login`` is deliberately NOT in
        ``_SKIP_EXACT_PATHS`` — exempting it would re-open login-CSRF
        (OWASP A07), where an attacker's form silently signs the victim into
        the attacker's account.
        """
        response = _client(app).post(
            "/auth/login",
            data={"username": "testuser", "password": "testpass"},
            follow_redirects=False,
        )
        assert response.status_code == 403, (
            f"expected 403 from the CSRF middleware, got "
            f"{response.status_code}: {response.text[:300]}"
        )

    def test_post_request_without_csrf_token_rejected(self, app):
        """A POST that has a session but omits the token is refused."""
        client, _token = _stamped_client(app)
        response = client.post(
            "/auth/login",
            data={"username": "testuser", "password": "testpass"},
            follow_redirects=False,
        )
        assert response.status_code == 403, (
            f"expected 403 for a token-less POST, got "
            f"{response.status_code}: {response.text[:300]}"
        )

    def test_post_request_with_invalid_csrf_token_rejected(self, app):
        """A POST carrying a forged token is refused."""
        client, _token = _stamped_client(app)
        response = client.post(
            "/auth/login",
            data={
                "username": "testuser",
                "password": "testpass",
                "csrf_token": "invalid-fake-token-12345",
            },
            follow_redirects=False,
        )
        assert response.status_code == 403, (
            f"expected 403 for a forged token, got {response.status_code}: "
            f"{response.text[:300]}"
        )

    def test_post_request_with_valid_csrf_token_accepted(self, app):
        """Positive control for the three rejections above.

        A middleware that 403'd everything would satisfy all of them. With a
        real token the request must reach the login handler — which answers
        401 for these bogus credentials. Anything 403 here means CSRF ate a
        legitimate request.
        """
        client, token = _stamped_client(app)
        response = client.post(
            "/auth/login",
            data={
                "username": "testuser",
                "password": "testpass",
                "csrf_token": token,
            },
            follow_redirects=False,
        )
        assert response.status_code != 403, (
            f"CSRF rejected a request carrying a valid token: "
            f"{response.text[:300]}"
        )
        assert response.status_code == 401, (
            f"expected the login handler's 401 for unknown credentials, got "
            f"{response.status_code}: {response.text[:300]}"
        )

    def test_csrf_token_in_json_requests(self, app):
        """A JSON API POST is not exempt, and is refused BEFORE the auth gate.

        The Flask original made both calls and asserted nothing at all. The
        property worth pinning is the middleware's fail-closed rule: an
        UNAUTHENTICATED JSON POST still needs a session-bound token, so the
        answer is 403 (CSRF), not 401 (auth). Without that rule every future
        public mutator endpoint would be forgeable.

        ``tests/security/test_csrf_e2e_flow.py`` covers the authenticated
        direction on ``/api/start_research``; this covers the anonymous one,
        and shows the two rejections are distinguishable.
        """
        client, token = _stamped_client(app)

        without_token = client.post(
            "/api/v1/quick_summary", json={"query": "test query"}
        )
        assert without_token.status_code == 403, (
            f"an unauthenticated JSON POST without a CSRF token returned "
            f"{without_token.status_code}; the middleware must fail closed"
        )

        with_token = client.post(
            "/api/v1/quick_summary",
            json={"query": "test query"},
            headers={"X-CSRFToken": token},
        )
        assert with_token.status_code == 401, (
            f"with a valid CSRF token the request must get past CSRF and be "
            f"stopped by the auth gate instead; got "
            f"{with_token.status_code}: {with_token.text[:300]}"
        )

    @pytest.mark.skip(reason="documentation/placeholder test - not implemented")
    def test_csrf_token_not_leaked_in_logs_or_urls(self):
        """Test that CSRF tokens are not leaked in logs or URLs."""
        # CSRF tokens should:
        # 1. Not appear in URL query parameters (use POST body or headers)
        # 2. Not be logged to console or log files
        # 3. Not be exposed in error messages
        # 4. Be transmitted over HTTPS only in production

        # This is a security best practice documentation test

        # CSRF tokens should be in:
        # - Hidden form fields
        # - Request headers (X-CSRFToken)
        # - Request body (for form submissions)

        # CSRF tokens should NOT be in:
        # - URL query parameters (e.g. ?csrf=token)
        # - Referer headers
        # - Log files

        assert True  # Documentation test


class TestCSRFProtectionDocumentation:
    """Documentation tests for CSRF protection strategy."""

    @pytest.mark.skip(reason="documentation/placeholder test - not implemented")
    def test_csrf_protection_strategy_documentation(self):
        """
        Document CSRF protection strategy for LDR.

        CSRF Protection Mechanisms:
        1. Session-bound CSRF tokens enforced by ASGI CSRFMiddleware
        2. Token validation on all state-changing operations (POST/PUT/DELETE)
        3. CSRF token available via /auth/csrf-token endpoint for API clients
        4. Tokens tied to user session

        How CSRF Works:
        1. Attacker tricks victim into visiting malicious site
        2. Malicious site sends forged request to legitimate site
        3. Request uses victim's cookies (auto-sent by browser)
        4. Without CSRF protection, legitimate site executes unwanted action

        CSRF Protection:
        - Require CSRF token with each state-changing request
        - Token is tied to user's session
        - Attacker cannot obtain victim's token (same-origin policy)
        - Forged requests without valid token are rejected

        LDR-Specific Considerations:
        - Local/self-hosted tool: CSRF risk is lower than multi-user SaaS
        - Still important for web interface security
        - API clients must obtain CSRF token before making requests

        Protected Operations:
        - User authentication (login/logout)
        - Research creation/deletion
        - Settings updates
        - Any database modifications

        Exempt Operations:
        - Read-only GET requests
        - The token-mint endpoint itself
        - The Socket.IO mount (own handshake auth)
        """
        assert True  # Documentation test

    @pytest.mark.skip(reason="documentation/placeholder test - not implemented")
    def test_csrf_vs_cors_clarification(self):
        """
        Clarify difference between CSRF and CORS.

        CSRF (Cross-Site Request Forgery):
        - Attack: Malicious site makes unauthorized requests on behalf of user
        - Protection: CSRF tokens, SameSite cookies
        - Scope: Protects against forged state-changing requests

        CORS (Cross-Origin Resource Sharing):
        - Feature: Allows controlled access to resources from different origins
        - Protection: Controls which external sites can make requests
        - Scope: Browser security policy for cross-origin requests

        Both are needed for comprehensive web security.
        """
        assert True  # Documentation test


@pytest.mark.skip(reason="documentation/placeholder test - not implemented")
def test_csrf_integration_with_authentication():
    """
    Test that CSRF protection works correctly with authentication.

    CSRF and Authentication:
    - CSRF protects authenticated users from forged requests
    - Attacker cannot forge requests even with victim's session cookie
    - CSRF token is separate from authentication token/session
    - Both are required for state-changing authenticated operations

    Authentication Flow with CSRF:
    1. User authenticates (gets session cookie)
    2. User obtains CSRF token
    3. User makes authenticated request with both session and CSRF token
    4. Server validates both authentication and CSRF token
    5. Only then is request processed

    This provides defense-in-depth security.
    """
    assert True  # Documentation test