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.7 KB
Raw Blame
"""
API Security Tests

Re-ported from the pre-FastAPI-migration Flask module, which built the app
with ``create_app()`` + ``app.test_client()`` and therefore skipped itself
whole after the migration.

THE ONE THING THAT CHANGED, AND WHY EVERY TEST HERE SENDS A CSRF TOKEN
----------------------------------------------------------------------
Under Flask these tests POSTed with ``WTF_CSRF_ENABLED = False`` and
asserted **401** — i.e. "the auth gate refuses an anonymous caller". Under
FastAPI the always-on ``CSRFMiddleware`` sits OUTSIDE the auth dependency,
so the same bare POST now returns **403** and never reaches the auth gate at
all. Asserting 403 would look like a passing port while silently deleting
the OWASP API2 coverage: an attacker can mint a CSRF token from the public
``GET /auth/csrf-token`` in one request, so CSRF is not what protects these
endpoints.

So every POST below carries a REAL token. That keeps the assertions on the
same guard the originals were about (authentication), and additionally pins
that the two rejections stay distinguishable — 403 for a forged request,
401 for an unauthenticated one.

DROPPED
-------
* ``/api/v1/settings`` (from ``test_api2``) and ``/api/v1/research`` — no
  such routes on this branch, so asserting "not 200" against them passed
  against a 404 and would have passed with authentication removed entirely.
  Replaced with the ``/api/v1`` routes this app actually serves.
* ``test_api4``'s second half (100 ``GET /api/v1/health`` calls with the
  comment "should eventually rate limit") contained no assertion at all.
  Rate limiting has real coverage in
  ``tests/security/test_rate_limiter_fastapi.py`` and
  ``tests/web/routers/test_auth_rate_limits.py``.
* ``TestAPIRateLimiting`` — a class containing a fixture and zero tests.

The five ``@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: random per-client addresses collide across a long
# session and produce 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.79.{n // 250 % 250}.{n % 250 + 1}"}
    )
    return client


@pytest.fixture
def anon(app):
    """An unauthenticated client that nonetheless holds a valid CSRF token.

    This is the realistic attacker: ``GET /auth/csrf-token`` is public, so
    obtaining a token costs one request and proves nothing about identity.
    """
    client = _client(app)
    client.get("/auth/login")
    token = client.get("/auth/csrf-token").json()["csrf_token"]
    client.headers.update({"X-CSRFToken": token})
    return client


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

    Several assertions below are "this path does not serve an admin surface",
    which a 404 satisfies just as well when the whole router is absent. Pinning
    that ``/api/v1`` is really mounted makes those 404s mean "no such route"
    rather than "no such API", and gives the module a real import of the code
    under test.
    """
    from local_deep_research.web.fastapi_app import app

    api_v1_paths = [
        r.path
        for r in app.routes
        if getattr(r, "path", "").startswith("/api/v1")
    ]
    assert api_v1_paths, "no /api/v1 routes are mounted on the app"


class TestAPISecurityOWASPTop10:
    """Test API security based on OWASP API Security Top 10 2023."""

    # API1:2023 - Broken Object Level Authorization (BOLA)
    @pytest.mark.skip(reason="documentation/placeholder test - not implemented")
    def test_api1_broken_object_level_authorization(self):
        """
        Test that users can only access their own objects.

        BOLA/IDOR (Insecure Direct Object Reference):
        - User A tries to access User B's research by changing research_id
        - API should verify that user owns the requested object
        """
        # Example vulnerable endpoint:
        # GET /api/v1/quick_summary/{research_id}
        # Without checking if current user owns research_id

        # Test accessing research with different IDs
        # Should return 403 Forbidden if not owned by user
        # Should return 404 Not Found to avoid info leakage

        # For LDR with per-user databases, this is mitigated by architecture
        assert True  # Architecture-level protection

    # API2:2023 - Broken Authentication
    def test_api2_broken_authentication(self, anon):
        """Protected API endpoints refuse a caller who is merely CSRF-valid.

        The client here has a real session and a real CSRF token — it just
        has not authenticated. Both rejections must be the auth gate's 401,
        which is what makes this an authentication test rather than a CSRF
        test.
        """
        get_resp = anon.get("/api/v1/")
        assert get_resp.status_code == 401, (
            f"GET /api/v1/ returned {get_resp.status_code} without auth: "
            f"{get_resp.text[:200]}"
        )

        post_resp = anon.post("/api/v1/quick_summary", json={"query": "x"})
        assert post_resp.status_code == 401, (
            f"POST /api/v1/quick_summary returned {post_resp.status_code} "
            f"for a CSRF-valid but unauthenticated caller — if this is 403 "
            f"then CSRF is the only thing guarding it, and an attacker mints "
            f"tokens freely: {post_resp.text[:200]}"
        )

    def test_public_health_endpoint_still_answers_this_client(self, anon):
        """Positive control for every 401/404 assertion in this module.

        All of them use this same client. If the app were rejecting it
        wholesale — misconfigured middleware, a dead router mount — every
        one of those tests would pass while proving nothing. A 200 from the
        public health route shows the client is fine and the /api/v1 mount
        is live, so the rejections elsewhere are endpoint-specific.
        """
        resp = anon.get("/api/v1/health")
        assert resp.status_code == 200, (
            f"the public health endpoint answered {resp.status_code}; every "
            f"other assertion in this module is vacuous until this passes"
        )

    # API4:2023 - Unrestricted Resource Consumption
    def test_api4_unrestricted_resource_consumption(self, anon):
        """A huge body from an anonymous caller is refused, not parsed.

        1.1 MB of JSON. The auth gate runs before the handler deserializes
        anything, so an unauthenticated client cannot make the server spend
        memory or CPU on a payload of its choosing.
        """
        large_query = "test query " * 100000
        response = anon.post(
            "/api/v1/quick_summary", json={"query": large_query}
        )
        assert response.status_code == 401, (
            f"a 1.1MB unauthenticated payload returned "
            f"{response.status_code}; expected the auth gate's 401: "
            f"{response.text[:200]}"
        )

    # API5:2023 - Broken Function Level Authorization
    @pytest.mark.parametrize(
        "endpoint",
        [
            "/api/v1/admin/users",
            "/api/v1/admin/settings",
            "/api/v1/admin/logs",
        ],
    )
    def test_api5_broken_function_level_authorization(self, anon, endpoint):
        """There is no administrative surface under /api/v1 at all.

        LDR has no privilege tiers — every user owns exactly their own
        encrypted database — so the correct answer is "no such route". This
        fails the day someone mounts an admin router without an
        authorization story. (Paired with the health control above, which
        proves /api/v1 itself is mounted, so these 404s mean "absent route",
        not "absent router".)
        """
        response = anon.get(endpoint)
        assert response.status_code == 404, (
            f"{endpoint} returned {response.status_code}; an admin endpoint "
            f"exists that this test has no authorization coverage for"
        )

    # API6:2023 - Unrestricted Access to Sensitive Business Flows
    @pytest.mark.skip(reason="documentation/placeholder test - not implemented")
    def test_api6_unrestricted_sensitive_flows(self):
        """
        Test protection of sensitive business logic flows.

        Examples:
        - Account deletion without verification
        - Mass data export without limits
        - Automated scraping/abuse
        """
        # For LDR, sensitive flows might include:
        # - Deleting all research history
        # - Exporting all data
        # - Automated research generation (resource intensive)

        # These should have:
        # - Confirmation required
        # - Rate limiting
        # - CAPTCHA for automated abuse prevention

        pass  # Implementation-specific

    # API8:2023 - Security Misconfiguration
    def test_api8_security_misconfiguration(self, anon):
        """Health must not leak debug state, and a 404 must not leak internals.

        The Flask original checked ``"debug" not in str(data)`` on the health
        payload and that an unknown path was a 404. The 404 half is
        strengthened here: a generic error, with no traceback and no
        filesystem path, is the actual property "should return generic
        error, not stack trace" was reaching for.
        """
        response = anon.get("/api/v1/health")
        assert response.status_code == 200
        data = response.json()
        assert "debug" not in str(data).lower() or not data.get("debug"), (
            f"the health payload exposes debug state: {data}"
        )

        missing = anon.get("/api/v1/nonexistent")
        assert missing.status_code == 404, (
            f"an unknown /api/v1 path returned {missing.status_code}: "
            f"{missing.text[:200]}"
        )
        body = missing.text.lower()
        for leak in ("traceback", 'file "/', "site-packages"):
            assert leak not in body, (
                f"the 404 body leaks internals ({leak!r}): {missing.text[:400]}"
            )

    # API9:2023 - Improper Inventory Management
    @pytest.mark.skip(reason="documentation/placeholder test - not implemented")
    def test_api9_improper_inventory_management(self):
        """
        Test API documentation and version management.

        Issues:
        - Undocumented API endpoints
        - Old API versions still accessible
        - Deprecated endpoints without sunset dates
        - Shadow APIs (forgotten endpoints)
        """
        # This is primarily a documentation/process issue
        # Verify:
        # - API endpoints are documented
        # - Old versions are deprecated properly
        # - API versioning is clear (/api/v1/, /api/v2/)

        assert True  # Documentation/process test

    # API10:2023 - Unsafe Consumption of APIs
    @pytest.mark.skip(reason="documentation/placeholder test - not implemented")
    def test_api10_unsafe_consumption_of_apis(self):
        """
        Test secure consumption of external APIs.

        LDR consumes external APIs:
        - Search engines
        - Wikipedia
        - Web scraping targets

        Risks:
        - Malicious responses from external APIs
        - Injection attacks via external data
        - Excessive trust in external data
        """
        # External API responses should be:
        # - Validated (schema/type checking)
        # - Sanitized (remove dangerous content)
        # - Size-limited (prevent memory exhaustion)
        # - Timeout-protected (prevent hanging)

        assert True  # Implementation-specific


class TestAPIInputValidation:
    """Input handling on an endpoint the caller is not entitled to.

    Every case here is the same invariant seen from a different angle: NO
    body shape gets an anonymous caller past the auth gate. That is stronger
    than it sounds — it means malformed, missing, mistyped and boundary
    input are all rejected BEFORE any deserialization or validation code
    runs, so none of it is reachable pre-auth.
    """

    def test_json_parsing_errors_handled(self, anon):
        """Malformed JSON is rejected by the auth gate, never parsed."""
        response = anon.post(
            "/api/v1/quick_summary",
            content="{ invalid json }",
            headers={"Content-Type": "application/json"},
        )
        assert response.status_code == 401, response.status_code

    def test_missing_required_fields_rejected(self, anon):
        """A body with no ``query`` field still stops at the auth gate."""
        response = anon.post("/api/v1/quick_summary", json={})
        assert response.status_code == 401, response.status_code

    def test_invalid_data_types_rejected(self, anon):
        """A non-string ``query`` still stops at the auth gate."""
        response = anon.post("/api/v1/quick_summary", json={"query": 12345})
        assert response.status_code == 401, response.status_code

    @pytest.mark.parametrize(
        "payload",
        [
            pytest.param({"query": ""}, id="empty-string"),
            pytest.param({"query": "a" * 10000}, id="very-long-string"),
            pytest.param({"query": None}, id="null"),
        ],
    )
    def test_boundary_value_validation(self, anon, payload):
        """Boundary values stop at the auth gate too."""
        response = anon.post("/api/v1/quick_summary", json=payload)
        assert response.status_code == 401, response.status_code


@pytest.mark.skip(reason="documentation/placeholder test - not implemented")
def test_api_security_documentation():
    """
    Documentation test for API security best practices.

    OWASP API Security Top 10 2023:
    1. Broken Object Level Authorization (BOLA)
    2. Broken Authentication
    3. Broken Object Property Level Authorization
    4. Unrestricted Resource Consumption
    5. Broken Function Level Authorization
    6. Unrestricted Access to Sensitive Business Flows
    7. Server Side Request Forgery (SSRF)
    8. Security Misconfiguration
    9. Improper Inventory Management
    10. Unsafe Consumption of APIs

    LDR-Specific API Security Considerations:
    - Research API endpoints handle user queries
    - External data fetching (SSRF risk)
    - Resource-intensive operations (DoS risk)
    - Per-user database isolation (BOLA mitigation)

    Recommended Security Controls:
    - Input validation on all API endpoints
    - Rate limiting on expensive operations
    - URL whitelist for external fetching
    - Request size limits
    - Proper error handling (no info leakage)
    - API versioning and documentation
    - Authentication on protected endpoints
    - Authorization checks on object access
    """
    assert True  # Documentation test