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.

26.7 KB
Raw Blame
"""Guards: exception internals must not leak into client-visible
error responses.

Two channels are pinned, both AST-based and name-scoped (only a name
bound by an ``except ... as NAME`` counts as an exception, so
``str(user_id)`` or ``f"{folder}"`` cannot false-positive):

1. ``HTTPException(detail=...)`` (and Starlette's) -- the error
   envelope FastAPI serialises verbatim. A ``detail=str(e)`` on a
   handler's except path ships the raw exception message to the
   browser: file paths, SQL fragments, dependency versions, or
   internal hostnames that the credential-focused sanitizers
   (``WebAPIException.to_dict`` -> ``sanitize_error_for_client``)
   are not designed to strip, because raw HTTPException does not pass
   through them at all. The custom handler in fastapi_app.py
   re-serialises ``exc.detail`` verbatim for BOTH envelopes (regular
   ``{"detail": ...}`` and the /api/v1 ``{"error": ..., "detail": ...}``
   compatibility shape), so nothing downstream scrubs it either.

2. broad-except handler bodies -- a ``JSONResponse({"error":
   str(e)}, ...)`` or ``return {"error": f"{e}"}`` inside ``except
   Exception``/``except OSError``/bare ``except`` serialises the same
   internals through a different envelope. The branch's established
   discipline (verified: zero violations at write time) is that only
   *validation* exception messages are client-facing (notes.py's
   ``except ValueError`` 400s, with an explicit comment, surface the
   message so the client can fix and retry); every broad failure
   routes through ``handle_api_error`` or an equivalent that logs
   internally and answers generically.

No occurrence of either leak exists on the branch today (verified
before these guards were written), so both are preventive: the
patterns are the standard ways a migration-era ``except Exception:``
fallback turns a crash into an information disclosure.

Safe idioms, all of which pass:
- static text, or interpolation of non-exception values (the caller's
  own input echoed back is not a disclosure),
- ``f"{type(e).__name__}"`` -- the exception *class* name only, the
  established pattern in notifications/manager.py,
- logging the full exception (``logger.exception``/``logger.debug``)
  and answering generically,
- for channel 2: any exception type whose message is *deliberately*
  client-facing (ValueError/TypeError/KeyError/pydantic
  ValidationError/HTTPException/WebAPIException/
  PolicyDeniedError) is exempt; narrow domain exceptions with by-design
  client messages that fall outside that set belong in
  BROAD_BODY_LEAK_ALLOWLIST with a justification, or, when routed
  through a safe-by-contract sanitizer, are detected by the safe-sink
  exemption below.

Known limitation: a dict built into a local variable inside the
handler and returned indirectly (``body = {"error": str(e)}; ...
return JSONResponse(body)``) is not tracked; only direct dict returns
and response-constructor contents are scanned.

The scan covers the same request/response surface as
test_migration_antipattern_guards.py: web/*.py, web/routers/*.py, and
the top level of auth/, dependencies/, queue/, routes/, services/.
"""

import ast
from pathlib import Path

import local_deep_research.web as web_pkg
import local_deep_research.web.routers as routers_pkg

ROUTERS_DIR = Path(routers_pkg.__file__).resolve().parent
WEB_DIR = Path(web_pkg.__file__).resolve().parent

_SCANNED_SUBPKGS = ("auth", "dependencies", "queue", "routes", "services")
SCANNED_FILES = sorted(
    {
        *ROUTERS_DIR.glob("*.py"),
        *WEB_DIR.glob("*.py"),
        *(
            path
            for subpkg in _SCANNED_SUBPKGS
            for path in (WEB_DIR / subpkg).glob("*.py")
        ),
    }
)


def _rel(path: Path) -> str:
    return str(path.relative_to(ROUTERS_DIR.parents[3]))


def _parse(path: Path) -> ast.Module:
    return ast.parse(path.read_text(encoding="utf-8"), filename=str(path))


def _annotate_parents(tree: ast.AST) -> ast.AST:
    for node in ast.walk(tree):
        for child in ast.iter_child_nodes(node):
            child.parent = node
    return tree


# ===========================================================================
# 1. HTTPException detail
# ===========================================================================


_HTTP_EXCEPTION_CALL_NAMES = {"HTTPException", "StarletteHTTPException"}


def _is_httpexception_call(node: ast.AST) -> bool:
    if not isinstance(node, ast.Call):
        return False
    func = node.func
    if isinstance(func, ast.Name):
        return func.id in _HTTP_EXCEPTION_CALL_NAMES
    if isinstance(func, ast.Attribute):
        return func.attr in _HTTP_EXCEPTION_CALL_NAMES
    return False


def _detail_expression(node: ast.Call) -> ast.AST | None:
    """The detail= argument, whether passed by keyword or 2nd position
    (HTTPException's signature is (status_code, detail, ...))."""
    for kw in node.keywords:
        if kw.arg == "detail":
            return kw.value
    if len(node.args) >= 2:
        return node.args[1]
    return None


def _except_bound_names(fn: ast.AST) -> set[str]:
    """Every name bound by ``except ... as NAME`` anywhere in *fn*,
    nested defs included: a closure using the outer handler's name is
    exactly the leak this guard exists for."""
    return {
        handler.name
        for handler in ast.walk(fn)
        if isinstance(handler, ast.ExceptHandler) and handler.name
    }


def _is_safe_class_name_access(node: ast.Name) -> bool:
    """True for the ``e`` inside ``type(e).__name__`` (or any attribute
    of ``type(e)``): the exception's class, not its contents."""
    parent = getattr(node, "parent", None)
    if not (
        isinstance(parent, ast.Call)
        and isinstance(parent.func, ast.Name)
        and parent.func.id == "type"
    ):
        return False
    grandparent = getattr(parent, "parent", None)
    return isinstance(grandparent, ast.Attribute)


def find_exception_detail_leaks(tree: ast.AST):
    """-> [(lineno, func_name, message)] for every HTTPException whose
    detail interpolates or stringifies an enclosing except-bound name."""
    violations = []
    for node in ast.walk(tree):
        if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
            continue
        bound = _except_bound_names(node)
        if not bound:
            continue
        for call in ast.walk(node):
            if not _is_httpexception_call(call):
                continue
            detail = _detail_expression(call)
            if detail is None:
                continue
            for name_node in ast.walk(detail):
                if (
                    isinstance(name_node, ast.Name)
                    and name_node.id in bound
                    and not _is_safe_class_name_access(name_node)
                ):
                    violations.append(
                        (
                            call.lineno,
                            node.name,
                            f"detail uses except-bound '{name_node.id}'",
                        )
                    )
                    break  # one report per call site is enough
    return violations


# file::func -> justification. Seeded ONLY with verified-safe cases.
# (Currently empty: no HTTPException on the branch interpolates an
# except-bound exception into its client-visible detail.)
DETAIL_LEAK_ALLOWLIST: dict[str, str] = {}


def test_no_exception_internals_in_httpexception_detail():
    violations = []
    for path in SCANNED_FILES:
        tree = _annotate_parents(_parse(path))
        for lineno, func_name, message in find_exception_detail_leaks(tree):
            key = f"{_rel(path)}::{func_name}"
            if key in DETAIL_LEAK_ALLOWLIST:
                continue
            violations.append(
                f"  {_rel(path)}:{lineno}: {func_name}() -- {message}"
            )

    assert not violations, (
        "HTTPException(detail=...) interpolates an exception object on "
        "an error path. `detail` is serialised verbatim into the "
        "client-visible body, so str(e)/f'{e}' ships internals (paths, "
        "SQL, hostnames) that the credential sanitizers never see -- "
        "raw HTTPException bypasses them. Log the exception instead "
        "(logger.exception / logger.debug) and answer with a static "
        "detail, or at most the exception class: "
        'f"{type(e).__name__}".\n' + "\n".join(violations)
    )


class TestDetailLeakScannerSelfTest:
    """Proves the scanner distinguishes the leak from the safe idioms,
    so a silent breakage in the AST walk cannot make the guard above
    pass vacuously."""

    def _scan(self, source):
        return find_exception_detail_leaks(_annotate_parents(ast.parse(source)))

    def test_flags_str_of_exception(self):
        source = (
            "def h():\n"
            "    try:\n"
            "        risky()\n"
            "    except Exception as e:\n"
            "        raise HTTPException(500, detail=str(e))\n"
        )
        assert self._scan(source) == [(5, "h", "detail uses except-bound 'e'")]

    def test_flags_fstring_interpolation(self):
        source = (
            "def h():\n"
            "    try:\n"
            "        risky()\n"
            "    except Exception as exc:\n"
            '        raise HTTPException(status_code=500, detail=f"boom: {exc}")\n'
        )
        assert self._scan(source) == [
            (5, "h", "detail uses except-bound 'exc'")
        ]

    def test_flags_positional_detail(self):
        source = (
            "def h():\n"
            "    try:\n"
            "        risky()\n"
            "    except Exception as e:\n"
            "        raise HTTPException(500, str(e))\n"
        )
        assert len(self._scan(source)) == 1

    def test_flags_exception_attributes(self):
        source = (
            "def h():\n"
            "    try:\n"
            "        risky()\n"
            "    except ValueError as e:\n"
            '        raise HTTPException(500, detail=f"{e.args}")\n'
        )
        assert len(self._scan(source)) == 1

    def test_flags_starlette_httpexception(self):
        source = (
            "def h():\n"
            "    try:\n"
            "        risky()\n"
            "    except Exception as e:\n"
            "        raise StarletteHTTPException(500, detail=str(e))\n"
        )
        assert len(self._scan(source)) == 1

    def test_flags_closure_using_outer_exception(self):
        source = (
            "def h():\n"
            "    try:\n"
            "        risky()\n"
            "    except Exception as e:\n"
            "        def _fail():\n"
            '            raise HTTPException(500, detail=f"failed: {e}")\n'
            "        return _fail\n"
        )
        assert len(self._scan(source)) == 1

    def test_ignores_non_exception_names(self):
        source = (
            "def h(user_id):\n"
            "    raise HTTPException(404, detail=str(user_id))\n"
        )
        assert self._scan(source) == []

    def test_ignores_static_and_unrelated_interpolation(self):
        source = (
            "def h(folder):\n"
            '    raise HTTPException(404, detail=f"folder {folder} not found")\n'
        )
        assert self._scan(source) == []

    def test_ignores_exception_class_name_idiom(self):
        source = (
            "def h():\n"
            "    try:\n"
            "        risky()\n"
            "    except Exception as e:\n"
            "        raise HTTPException(500, detail=type(e).__name__)\n"
        )
        assert self._scan(source) == []

    def test_ignores_exception_used_outside_detail(self):
        """Logging the exception is the fix, not the leak."""
        source = (
            "def h():\n"
            "    try:\n"
            "        risky()\n"
            "    except Exception as e:\n"
            '        logger.exception(f"failed: {e}")\n'
            '        raise HTTPException(500, detail="internal error")\n'
        )
        assert self._scan(source) == []


# ===========================================================================
# 2. Broad-except handler body leakage
# ===========================================================================


# Exception types whose messages are deliberately client-facing in this
# codebase: validation messages (notes.py's ``except ValueError as e:
# return JSONResponse({"error": str(e)}, status_code=400)`` exists for
# exactly that contract, with an explicit comment); the framework's own
# error channel (HTTPException -> handler -> detail -> JSON body); and the
# project-specific sanitized envelope (WebAPIException carries a message
# that runs through ``sanitize_error_for_client``). PolicyDeniedError
# carries a curated decision.reason designed for the client (research.py
# surfaces ``exc.decision.reason`` deliberately). Subclasses are not
# resolvable statically, so any other by-design-client-message exception
# must go in BROAD_BODY_LEAK_ALLOWLIST with a justification.
_CLIENT_FACING_EXCEPTION_NAMES = frozenset(
    {
        "ValueError",
        "TypeError",
        "KeyError",
        "ValidationError",
        "HTTPException",
        "StarletteHTTPException",
        "WebAPIException",
        "PolicyDeniedError",
    }
)


# Response constructors whose content/positional arg is a body that gets
# serialised to the client. RedirectResponse excluded (first arg is a URL);
# StreamingResponse excluded (content is a generator, covered by the BytesIO
# antipattern guard).
_BODY_RESPONSE_FUNCS = frozenset(
    {
        "JSONResponse",
        "Response",
        "PlainTextResponse",
        "HTMLResponse",
        "ORJSONResponse",
        "UJSONResponse",
    }
)


# Functions whose contract is to take a bound exception and return a
# sanitized string/dict for the client, or to introspect the exception
# without serialising it. When ``e`` appears ONLY as an argument to one of
# these inside the response expression, the reference is safe and is not
# flagged. Direct interpolation (str(e), f"{e}", e.args, ...) stays a
# violation. Any new sanitizer must be added here with a matching contract
# documented at its definition site; the set is intentionally small.
_SAFE_EXCEPTION_SINKS = frozenset(
    {
        "safe_error_message",
        "handle_api_error",
        "sanitize_error_for_client",
        "sanitize_error_details",
        "isinstance",
        "issubclass",
        "hasattr",
    }
)


def _except_handler_is_broad(handler: ast.ExceptHandler) -> bool:
    """Bare except, ``except Exception``, or any type not in the
    client-facing set. A Tuple clause with one non-client-facing type
    makes the whole handler broad (conservative)."""
    t = handler.type
    if t is None:
        return True  # bare except
    names: list[str] = []
    if isinstance(t, ast.Name):
        names = [t.id]
    elif isinstance(t, ast.Tuple):
        names = [e.id for e in t.elts if isinstance(e, ast.Name)]
    if not names:
        return True  # complex attr/subscript -> unknown, be conservative
    return any(n not in _CLIENT_FACING_EXCEPTION_NAMES for n in names)


def _is_body_response_call(node: ast.AST) -> bool:
    if not isinstance(node, ast.Call):
        return False
    func = node.func
    if isinstance(func, ast.Name):
        return func.id in _BODY_RESPONSE_FUNCS
    if isinstance(func, ast.Attribute):
        return func.attr in _BODY_RESPONSE_FUNCS
    return False


def _response_content_expr(call: ast.Call) -> ast.AST | None:
    """``content=...`` keyword, else the first positional arg."""
    for kw in call.keywords:
        if kw.arg == "content":
            return kw.value
    if call.args:
        return call.args[0]
    return None


def _handler_body_exprs(handler: ast.ExceptHandler):
    """Every expression in the handler that could become a body: response
    constructor contents and bare dict returns."""
    for node in ast.walk(handler):
        if _is_body_response_call(node):
            expr = _response_content_expr(node)
            if expr is not None:
                yield expr
        elif isinstance(node, ast.Return) and isinstance(node.value, ast.Dict):
            yield node.value


def _is_safe_sink_arg(node: ast.Name) -> bool:
    """True if *node* is a Name passed as a direct argument to a
    safe-exception-sink call."""
    parent = getattr(node, "parent", None)
    if not isinstance(parent, ast.Call):
        return False
    func = parent.func
    if isinstance(func, ast.Name):
        name = func.id
    elif isinstance(func, ast.Attribute):
        name = func.attr
    else:
        return False
    return name in _SAFE_EXCEPTION_SINKS


def _uses_unsafe_name(tree: ast.AST, name: str) -> bool:
    """True if *tree* references *name* anywhere that is not (a) inside
    the safe class-name idiom ``type(name).__name__`` and not (b) a direct
    argument to a safe-exception-sink function call."""
    for node in ast.walk(tree):
        if isinstance(node, ast.Name) and node.id == name:
            if _is_safe_class_name_access(node):
                continue
            if _is_safe_sink_arg(node):
                continue
            return True
    return False


def find_broad_handler_body_leaks(tree: ast.AST):
    """-> [(lineno, func_name, exc_name, message)] for every broad-except
    handler that ships its bound exception into a response body or
    returned dict."""
    violations_list = []
    for fn in ast.walk(tree):
        if not isinstance(fn, (ast.FunctionDef, ast.AsyncFunctionDef)):
            continue
        for handler in ast.walk(fn):
            if not isinstance(handler, ast.ExceptHandler) or not handler.name:
                continue
            if not _except_handler_is_broad(handler):
                continue
            for expr in _handler_body_exprs(handler):
                if _uses_unsafe_name(expr, handler.name):
                    violations_list.append(
                        (
                            handler.lineno,
                            fn.name,
                            handler.name,
                            f"handler for '{handler.name}' ships exception"
                            " into client body",
                        )
                    )
                    break  # one report per handler
    return violations_list


# file::func -> justification. Seeded ONLY with verified-safe cases:
# validation exceptions whose messages are intentionally client-facing
# (see notes.py's 400 ``except ValueError`` contract) belong in their own
# scanner's allowlist; this one covers broad handlers that must not leak
# at all. (Currently empty: zero violations on the branch at write time.)
BROAD_BODY_LEAK_ALLOWLIST: dict[str, str] = {
    # rag.py's test_embedding() handles ``except Exception as e`` broadly
    # to absorb everything upstream, but only returns ``e.decision.reason``
    # inside an ``if isinstance(e, PolicyDeniedError):`` branch -- the leak
    # is gated by a runtime isinstance check that the static scanner
    # cannot see across. The justification here mirrors the comment on
    # the call site: ``e.decision.reason`` is curated client-facing text
    # produced by the egress policy module (not raw exception data).
    # Every other exception goes to ``_format_test_embedding_error``, which
    # does NOT scrub-and-echo: it reads the exception's module and class to
    # SELECT one of a fixed set of literals and interpolates neither, so no
    # exception-derived string reaches the body on that path. (It used to be
    # described as going through ``sanitize_error_for_client``; that is no
    # longer true, and the function's own docstring now explains why echoing
    # scrubbed text would be wrong here -- the reachable exceptions carry SQL
    # text and absolute server paths, which that scrubber does not remove.)
    "src/local_deep_research/web/routers/rag.py::test_embedding": (
        "broad except Exception gates the e.decision.reason response on "
        "isinstance(e, PolicyDeniedError); every other path calls "
        "_format_test_embedding_error, which selects a fixed message from "
        "the exception's type and never interpolates its text"
    ),
}


def test_no_broad_except_body_leakage():
    """A broad ``except`` handler (``except Exception``, ``except OSError``,
    bare ``except``) must route through ``handle_api_error`` or log + answer
    generically -- never interpolate ``str(e)`` / ``f"{e}"`` into a
    JSONResponse body or a returned dict. Narrow validation exceptions are
    exempt: they exist for the client-retry contract (notes.py)."""
    violations = []
    for path in SCANNED_FILES:
        tree = _annotate_parents(_parse(path))
        for (
            lineno,
            func_name,
            exc_name,
            message,
        ) in find_broad_handler_body_leaks(tree):
            key = f"{_rel(path)}::{func_name}"
            if key in BROAD_BODY_LEAK_ALLOWLIST:
                continue
            violations.append(
                f"  {_rel(path)}:{lineno}: {func_name}() -- {message}"
            )

    assert not violations, (
        "Broad-except handler ships a bound exception into a response body. "
        "Route through ``handle_api_error`` (logs internally, answers "
        "generically) or use ``logger.exception`` + a static body. "
        "Validation exceptions (ValueError/TypeError/KeyError/ValidationError/"
        "HTTPException/WebAPIException/PolicyDeniedError) are exempt: their "
        "messages are deliberately client-facing so the client can fix and "
        "retry (see notes.py for ValueError, research.py for "
        "PolicyDeniedError.decision.reason). For any other exception whose "
        "message is by design client-facing, add BROAD_BODY_LEAK_ALLOWLIST "
        "with a justification.\n" + "\n".join(violations)
    )


class TestBroadHandlerBodyLeakSelfTest:
    def _scan(self, source):
        return find_broad_handler_body_leaks(
            _annotate_parents(ast.parse(source))
        )

    def test_flags_broad_exception_into_jsonresponse(self):
        source = (
            "def h():\n"
            "    try:\n"
            "        risky()\n"
            "    except Exception as e:\n"
            '        return JSONResponse({"error": str(e)}, status_code=500)\n'
        )
        assert len(self._scan(source)) == 1

    def test_flags_broad_oserror_into_returned_dict(self):
        source = (
            "def h():\n"
            "    try:\n"
            "        risky()\n"
            "    except OSError as exc:\n"
            '        return {"error": f"disk blew: {exc}"}\n'
        )
        assert len(self._scan(source)) == 1

    def test_flags_bare_except(self):
        source = (
            "def h():\n"
            "    try:\n"
            "        risky()\n"
            "    except Exception as e:\n"
            "        return JSONResponse(content=str(e))\n"
        )
        assert len(self._scan(source)) == 1

    def test_flags_tuple_clause_with_one_broad_type(self):
        source = (
            "def h():\n"
            "    try:\n"
            "        risky()\n"
            "    except (ValueError, OSError) as e:\n"
            '        return JSONResponse({"error": str(e)}, status_code=500)\n'
        )
        assert len(self._scan(source)) == 1

    def test_ignores_pure_validation_exceptions(self):
        source = (
            "def h():\n"
            "    try:\n"
            "        validate()\n"
            "    except ValueError as e:\n"
            "        # Notes.py's established 400 contract -- validation\n"
            "        # message is deliberately surfaced so the client can\n"
            "        # fix and retry.\n"
            '        return JSONResponse({"success": False, "error": str(e)},'
            " status_code=400)\n"
        )
        assert self._scan(source) == []

    def test_ignores_logging_only_path(self):
        source = (
            "def h():\n"
            "    try:\n"
            "        risky()\n"
            "    except Exception as e:\n"
            '        logger.exception("failed: %s", e)\n'
            '        return JSONResponse({"error": "internal error"})\n'
        )
        assert self._scan(source) == []

    def test_ignores_handle_api_error_route(self):
        source = (
            "def h():\n"
            "    try:\n"
            "        risky()\n"
            "    except Exception as e:\n"
            '        return handle_api_error("doing thing", e)\n'
        )
        assert self._scan(source) == []

    def test_ignores_class_only_idiom(self):
        source = (
            "def h():\n"
            "    try:\n"
            "        risky()\n"
            "    except Exception as e:\n"
            '        logger.exception("failed")\n'
            '        return JSONResponse({"error": type(e).__name__})\n'
        )
        assert self._scan(source) == []

    def test_ignores_safe_sink_argument(self):
        source = (
            "def h():\n"
            "    try:\n"
            "        risky()\n"
            "    except Exception as e:\n"
            "        return JSONResponse(\n"
            '            {"error": safe_error_message(e, "doing thing")},\n'
            "            status_code=500,\n"
            "        )\n"
        )
        assert self._scan(source) == []

    def test_still_flags_e_outside_safe_sink(self):
        source = (
            "def h():\n"
            "    try:\n"
            "        risky()\n"
            "    except Exception as e:\n"
            "        return JSONResponse(\n"
            '            {"error": safe_error_message(e) + " " + str(e)},\n'
            "            status_code=500,\n"
            "        )\n"
        )
        assert len(self._scan(source)) == 1

    def test_ignores_isinstance_introspection(self):
        source = (
            "def h():\n"
            "    try:\n"
            "        risky()\n"
            "    except Exception as e:\n"
            "        from ..security.egress.policy import PolicyDeniedError\n"
            "        if isinstance(e, PolicyDeniedError):\n"
            "            return JSONResponse(\n"
            '                {"error": "policy denied"},\n'
            "                status_code=400,\n"
            "            )\n"
            "        return JSONResponse(\n"
            '            {"error": "internal"},\n'
            "            status_code=500,\n"
            "        )\n"
        )
        assert self._scan(source) == []


def test_scan_covers_the_request_response_surface():
    """If the web layout moves, SCANNED_FILES silently shrinking would
    make the guard pass vacuously. Same floor discipline as
    test_migration_antipattern_guards.py."""
    names = {p.name for p in SCANNED_FILES}
    assert len(SCANNED_FILES) >= 50, (
        f"Only {len(SCANNED_FILES)} files scanned ({sorted(names)}) -- "
        "the web/ layout moved; update this test's scope."
    )
    for expected in ("fastapi_app.py", "csrf.py", "research_service.py"):
        assert expected in names, (
            f"{expected} is no longer in the scanned scope -- update "
            "SCANNED_FILES / _SCANNED_SUBPKGS."
        )