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.

5.2 KB
Raw Blame
"""Boundary scrub for exception-derived API fields (CWE-209 / CodeQL #8019).

FastAPI port of the ``_scrub_error_fields`` unit coverage from the Flask
``tests/web/test_api_coverage.py`` (PR #5032). The Flask suite exercised the
helper through ``_serialize_results`` under a Flask app context; here we call
``api_v1._scrub_error_fields`` directly (it mutates its dict argument in place),
which needs no request context.

Endpoint-level wiring — that api_quick_summary / api_generate_report /
api_analyze_documents actually INVOKE it — lives in
``tests/security/test_api_v1_boundary_fastapi.py::TestErrorScrubWiring``.

That sentence used to read "is covered by the existing api_v1 endpoint tests",
which was not true when written: no endpoint test asserted the call, so a
regression that removed `_scrub_error_fields` from the three call sites would
have left this file green while secrets reached the client. A helper with a
docstring claiming wiring coverage that does not exist is worse than one that
claims nothing, because the claim is what stops the next person looking.
"""

from local_deep_research.web.routers.api_v1 import (
    _ERROR_BOUNDARY_MAX_LEN,
    _scrub_error_fields,
)


def test_error_prefixed_current_knowledge_is_scrubbed():
    leaked = (
        "Error: LLM call failed: "
        "https://api.example.com/v1?api_key=sk-SECRETKEY1234567890AB"
    )
    results = {"current_knowledge": leaked}
    _scrub_error_fields(results)
    assert "sk-SECRETKEY1234567890AB" not in results["current_knowledge"]
    assert results["current_knowledge"].startswith("Error:")


def test_error_prefixed_summary_real_payload_shape():
    # quick_summary()/analyze_documents() return the strategy's error text
    # under the key `summary` — regression guard for the field-name mismatch
    # where only `current_knowledge` was scrubbed and the payload leaked.
    leaked = (
        "Error: LLM call failed: "
        "https://api.example.com/v1?api_key=sk-REALSHAPE12345678901"
    )
    results = {
        "research_id": "abc-123",
        "summary": leaked,
        "findings": [],
        "formatted_findings": "",
        "sources": [],
    }
    _scrub_error_fields(results)
    assert "sk-REALSHAPE12345678901" not in results["summary"]
    assert results["summary"].startswith("Error:")


def test_bare_api_key_in_prose_is_scrubbed():
    leaked = (
        "Error: OpenAI authentication failed for key "
        "sk-proj-AbCdEfGhIjKlMnOpQrStUvWx"
    )
    results = {"summary": leaked}
    _scrub_error_fields(results)
    assert "sk-proj-AbCdEfGhIjKlMnOpQrStUvWx" not in results["summary"]
    assert results["summary"].startswith("Error:")


def test_error_prefixed_finding_content_is_scrubbed():
    leaked = (
        "Error: connection to "
        "https://db.example.com:5432/db?password=supersecret123"
    )
    results = {"findings": [{"phase": "Error", "content": leaked}]}
    _scrub_error_fields(results)
    assert "supersecret123" not in results["findings"][0]["content"]
    assert results["findings"][0]["content"].startswith("Error:")


def test_long_non_error_field_passes_through_unchanged():
    # The scrub only fires on fields starting with "Error:". Legitimate,
    # non-exception prose must pass through untouched — no truncation and no
    # credential-regex false positives on "Authorization"/"Bearer" mentions.
    long_summary = (
        "Research summary: Authentication systems often use Bearer tokens in "
        "the Authorization header, but a well-designed API also supports API "
        "keys, OAuth, and session cookies. "
    ) * 20  # ~1800 chars, well over the boundary cap
    assert not long_summary.startswith("Error:")
    results = {"current_knowledge": long_summary}
    _scrub_error_fields(results)
    assert results["current_knowledge"] == long_summary


def test_categorizable_token_survives_boundary_cap():
    # A categorizable signal ("Connection refused") past the 200-char
    # sanitize_error_for_client default must survive: the boundary cap is
    # aligned to the strategy layer (_ERROR_BOUNDARY_MAX_LEN = 500). If it
    # regresses to 200 the token is truncated and error classification breaks.
    assert _ERROR_BOUNDARY_MAX_LEN >= 500
    leading = "x" * 230
    leaked = f"Error: {leading} Connection refused [Errno 111]"
    results = {"current_knowledge": leaked}
    _scrub_error_fields(results)
    assert "Connection refused" in results["current_knowledge"]


def test_helper_covers_all_field_spellings():
    leaked = (
        "Error: connect to "
        "https://api.example.com/v1?api_key=sk-LEAK1234567890abcd"
    )
    results = {
        "current_knowledge": leaked,
        "summary": leaked,
        "formatted_findings": leaked,
        "findings": [{"phase": "Error", "content": leaked}],
    }
    _scrub_error_fields(results)
    for field in ("current_knowledge", "summary", "formatted_findings"):
        assert "sk-LEAK1234567890abcd" not in results[field]
        assert results[field].startswith("Error:")
    assert "sk-LEAK1234567890abcd" not in results["findings"][0]["content"]


def test_non_dict_input_is_noop():
    # generate_report can return a falsy/non-dict result; the guard must not
    # raise. (The FastAPI port added this over the Flask original.)
    _scrub_error_fields(None)
    _scrub_error_fields("not a dict")