~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Sweep for credential / sensitive-data leakage across the FastAPI port.
Two questions, asked systematically:
1. Can a secret reach an HTTP response body or header?
2. Can a secret reach a log line?
The repo already answers most of (1) — ``tests/web/test_exception_handler_
contract.py`` pins the response shape of every registered handler, and
``tests/security/test_api_key_leakage.py`` pins the provider error paths.
Neither asserts anything about the **log** side, and the CWE-209 fix that
prompted this sweep (``json.JSONDecodeError`` carries the entire offending
document on ``.doc``) lives entirely on the log side. Section 3 below
closes that.
The bulk of the file is (2), and it is aimed at the gaps in the
``check-sensitive-logging`` pre-commit hook rather than at the hook's
own behavior (which ``test_sensitive_logging_hook.py`` already covers).
The hook's ``_is_logger_call()`` accepts a receiver that is ``Name(
'logger')`` or any ``Attribute(attr='logger')`` — nothing else. Three
consequences, each demonstrated in Section 1 by running the real hook:
* **Chained loguru calls are invisible.** ``logger.bind(...).info(...)``
has a ``Call`` receiver, so ALL THREE of the hook's checks are skipped:
the secret-name check (``_check_sensitive_logging`` — the hook's whole
reason to exist), the traceback check, and the exception-variable
check. Inside ``SECURE_LOGGING_DIRS`` the hook *does* look at ``bind``
chains (``_check_secure_dir_call``) but re-runs only ONE of the three,
so even there ``logger.bind(k=1).info(f"{user_password}")`` passes
clean. Section 2 measures the blast radius by de-chaining the AST and
re-running the hook's own checker.
* **FastAPI request bodies are invisible.** The hook knows Flask's
``request.form`` / ``request.json`` / ``request.headers`` as
*attributes*. In FastAPI they are *coroutine calls* — ``await
request.json()``, ``await request.body()``. The visitor has no
``ast.Await`` branch, so the port silently removed the hook's
request-body protection.
* **Exception-handler parameters are invisible.** The exception-variable
check tracks only names bound by ``except ... as e``. A FastAPI
exception handler receives the exception as a *parameter*. This one is
already called out in a comment in ``fastapi_app.py``'s JSON-decode
handler; nothing pinned it.
Method labels used in the class docstrings below:
* **static** — AST analysis; no application code is executed.
* **executed** — real ASGI request/response cycles through the real
registered handlers, with a loguru sink attached.
Every non-trivial assertion is paired with a positive control that proves
the code path under test actually ran, because ``assert SENTINEL not in
body`` is vacuously true whenever the request never reached the code that
would have leaked.
"""
import ast
import json
from collections import Counter
from pathlib import Path
import pytest
from fastapi import FastAPI, Request
from fastapi.testclient import TestClient
from loguru import logger
from local_deep_research.security.data_sanitizer import DataSanitizer
from local_deep_research.web.exceptions import WebAPIException
from local_deep_research.web.fastapi_app import _register_exception_handlers
# Assembled from fragments so no scanner (gitleaks in CI) sees a
# credential-shaped literal, and deliberately given NO recognizable shape
# (no ``sk-`` prefix, no ``?api_key=`` context, not a JWT) so that
# ``log_sanitizer._CREDENTIAL_PATTERNS`` cannot rescue a leak by accident.
# A SQLCipher passphrase is exactly this shape: arbitrary user text.
SENTINEL = "NOT" + "AREALSECRET" + "leaksweep" + "0042"
REPO_ROOT = Path(__file__).resolve().parents[2]
SRC_ROOT = REPO_ROOT / "src" / "local_deep_research"
HOOKS_DIR = REPO_ROOT / ".pre-commit-hooks"
# Loguru level names that emit a record.
LOG_LEVELS = frozenset(
{
"trace",
"debug",
"info",
"success",
"warning",
"error",
"critical",
"exception",
"log",
}
)
# Response constructors: anything here puts its arguments on the wire.
RESPONSE_CTORS = frozenset(
{
"JSONResponse",
"HTMLResponse",
"PlainTextResponse",
"Response",
"StreamingResponse",
"WorkerCleanupStreamingResponse",
"HTTPException",
}
)
# Exception attributes that carry the raw error payload. ``doc`` is
# ``json.JSONDecodeError.doc`` — the ENTIRE offending document, which is
# the request body on the FastAPI JSON-decode path.
UNBOUNDED_EXC_ATTRS = frozenset(
{"args", "doc", "message", "__cause__", "__context__", "__traceback__"}
)
# FastAPI request accessors that return the raw client payload. Unlike
# Flask's attributes of the same name these are coroutine *calls*.
REQUEST_BODY_METHODS = frozenset({"json", "body", "form", "stream"})
def _load_hook_checker():
"""Import the real pre-commit hook module (hyphenated filename)."""
import importlib.util
path = HOOKS_DIR / "check-sensitive-logging.py"
spec = importlib.util.spec_from_file_location("_cs_logging_hook", path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module.SensitiveLoggingChecker
HookChecker = _load_hook_checker()
# A path inside SECURE_LOGGING_DIRS and one outside it. The hook's
# behavior differs between the two, and the interesting gap is the one
# that survives in the *secure* directory.
SECURE_PATH = (
"src/local_deep_research/web_search_engines/engines/search_engine_x.py"
)
PLAIN_PATH = "src/local_deep_research/web/routers/x.py"
def _hook_errors(code, filename=PLAIN_PATH):
"""Run the real hook's checker over *code* and return its findings."""
checker = HookChecker(filename)
checker.visit(ast.parse(code))
return checker.errors
def _iter_src_modules():
"""Yield (relative_path, parsed_tree) for every module under src/."""
for path in sorted(SRC_ROOT.rglob("*.py")):
try:
tree = ast.parse(path.read_text(encoding="utf-8"))
except (SyntaxError, UnicodeDecodeError): # pragma: no cover
continue
yield path.relative_to(SRC_ROOT).as_posix(), tree
# ---------------------------------------------------------------------------
# Section 1 — the hook's blind spots, demonstrated against the real hook
# ---------------------------------------------------------------------------
class TestSensitiveLoggingHookBlindSpots:
"""static. Establishes WHICH log call sites the hook cannot see.
These tests assert the hook's *observed* behavior. They are the
justification for the independent sweeps in Section 2: a sweep that
merely re-ran the hook would inherit its blind spots and prove
nothing. Each gap test is preceded by a positive control showing the
hook fires on the un-obscured spelling, so a "not flagged" result
means "the hook is blind here", not "the harness is broken".
"""
def test_direct_secret_name_is_flagged_positive_control(self):
errors = _hook_errors('logger.info(f"pw={user_password}")')
assert any("user_password" in e for e in errors), errors
def test_one_bind_disables_the_secret_name_check(self):
"""A single ``.bind()`` defeats the hook's primary check."""
chained = 'logger.bind(k=1).info(f"pw={user_password}")'
assert _hook_errors(chained) == []
# ...and it is the chaining, not the message, that hides it:
assert _hook_errors(chained.replace("bind(k=1).", "")) != []
def test_bind_chain_unchecked_for_secrets_even_in_secure_dirs(self):
"""``_check_secure_dir_call`` re-runs only 1 of the hook's 3 checks.
In SECURE_LOGGING_DIRS the hook explicitly inspects ``bind``
chains — but only through ``_check_exception_var_in_log``. The
secret-name check and the traceback check are still skipped, so
the strictest directory in the tree is no better protected
against a named secret than any other.
"""
# Positive control: the check the secure-dir path DOES re-run.
exc_var = (
"try:\n"
" pass\n"
"except Exception as e:\n"
' logger.bind(k=1).warning(f"{e}")\n'
)
assert _hook_errors(exc_var, SECURE_PATH) != []
# The two checks it does not re-run:
secret = 'logger.bind(k=1).info(f"key={api_key}")'
traceback = 'logger.bind(k=1).warning("x", exc_info=True)'
assert _hook_errors(secret, SECURE_PATH) == []
assert _hook_errors(traceback, SECURE_PATH) == []
# Both fire without the chain, in the same file.
assert _hook_errors(secret.replace("bind(k=1).", ""), SECURE_PATH)
assert _hook_errors(traceback.replace("bind(k=1).", ""), SECURE_PATH)
def test_fastapi_request_body_call_is_invisible(self):
"""The Flask->FastAPI spelling change disarmed the hook."""
# Positive control: Flask's attribute spelling is caught.
flask_style = (
'async def r(request):\n logger.info(f"h={request.headers}")\n'
)
assert any("request.headers" in e for e in _hook_errors(flask_style))
# FastAPI's spelling of the same leak is not.
for method in ("json", "body", "form"):
fastapi_style = (
"async def r(request):\n"
f' logger.info(f"b={{await request.{method}()}}")\n'
)
assert _hook_errors(fastapi_style) == [], method
def test_exception_handler_parameter_is_invisible(self):
"""Only ``except ... as e`` is tracked, never a handler param."""
# Positive control: the except-bound spelling is caught.
except_bound = (
"try:\n"
" pass\n"
"except Exception as exc:\n"
' logger.error(f"boom {exc}")\n'
)
assert _hook_errors(except_bound) != []
# The FastAPI handler signature carries the same exception.
for expr in ("{exc}", "{exc.args}", "{exc.doc}", "{exc.__cause__}"):
handler = (
"async def h(request, exc):\n"
f' logger.error(f"boom {expr}")\n'
)
assert _hook_errors(handler) == [], expr
# ---------------------------------------------------------------------------
# Section 2 — independent sweeps over src/, one per blind spot
# ---------------------------------------------------------------------------
class _Dechainer(ast.NodeTransformer):
"""Rewrite ``logger.<chain>(...).level(...)`` as ``logger.level(...)``.
Feeding the result back to the hook's own checker makes it evaluate
chained call sites with the rules it already applies to unchained
ones. Only chains rooted at the name ``logger`` are rewritten, so
unrelated ``x.foo().info()`` calls are left alone.
"""
def visit_Call(self, node):
self.generic_visit(node)
func = node.func
if not (isinstance(func, ast.Attribute) and func.attr in LOG_LEVELS):
return node
if not isinstance(func.value, ast.Call):
return node
root = func.value
while isinstance(root, ast.Call) and isinstance(
root.func, ast.Attribute
):
root = root.func.value
if not (isinstance(root, ast.Name) and root.id == "logger"):
return node
node.func = ast.copy_location(
ast.Attribute(
value=ast.copy_location(
ast.Name(id="logger", ctx=ast.Load()), node
),
attr=func.attr,
ctx=ast.Load(),
),
node,
)
return node
def _violations_hidden_by_chaining(relpath, tree_a, tree_b):
"""Hook findings that appear only after the logger chains are removed."""
before = HookChecker(relpath)
before.visit(tree_a)
after = HookChecker(relpath)
after.visit(ast.fix_missing_locations(_Dechainer().visit(tree_b)))
seen = set(before.errors)
return [e for e in after.errors if e not in seen]
# Every call site in src/ that violates a rule the hook enforces but
# cannot see, because a ``logger.bind(...)`` chain hides it. Counted per
# file so the baseline survives unrelated line-number churn.
#
# All eleven are latent rather than live TODAY, for two reasons
# established by execution (see TestLoguruKwargSemantics):
# * ``exc_info=True`` is not a loguru kwarg at all — loguru drops it
# into ``record["extra"]`` and renders no traceback (this is a
# repo-wide idiom: 79 sites, only these 4 hidden by a chain).
# * the ``reason=str(exc)`` / ``target=e.target`` sites put the
# exception text in ``record["extra"]`` too, and no configured sink
# renders extras (log_utils.config_logger uses loguru's default
# format; the DB and frontend sinks build their own payloads).
# They are still rule violations, and the failure mode is a one-line
# change away: any sink gaining ``serialize=True`` or an ``{extra}``
# format publishes all seven exception messages at once.
KNOWN_CHAINED_VIOLATIONS = {
"config/llm_config.py": 1,
"news/api.py": 1,
"notifications/manager.py": 1,
"research_library/services/download_service.py": 1,
"security/egress/run_classification.py": 2,
"web/routers/rag.py": 2,
"web/routers/research.py": 1,
"web/services/research_service.py": 1,
"web_search_engines/engines/search_engine_elasticsearch.py": 1,
}
class TestViolationsHiddenBehindLoggerChains:
"""static. Re-runs the hook's rules on de-chained logger call sites."""
def test_dechaining_unmasks_hook_errors_positive_control(self):
"""The transform must actually change what the hook reports."""
code = (
"try:\n"
" pass\n"
"except Exception as e:\n"
' logger.bind(k=1).warning("boom", reason=str(e))\n'
' logger.bind(k=1).info(f"key={api_key}")\n'
)
found = _violations_hidden_by_chaining(
PLAIN_PATH, ast.parse(code), ast.parse(code)
)
assert len(found) == 2, found
assert any("Exception variable" in e for e in found), found
assert any("api_key" in e for e in found), found
def test_dechaining_is_a_no_op_on_unchained_code_negative_control(self):
"""No chain, no new findings — the transform invents nothing."""
code = (
"try:\n"
" pass\n"
"except Exception as e:\n"
' logger.warning("boom", reason=str(e))\n'
"obj.helper().info('unrelated chain')\n"
)
assert (
_violations_hidden_by_chaining(
PLAIN_PATH, ast.parse(code), ast.parse(code)
)
== []
)
def test_chained_violation_baseline_has_not_grown(self):
"""No NEW rule violation may hide behind a ``logger.bind()`` chain.
The baseline above is the finding, not a blessing: every entry is
a call site the hook would reject if it were spelled without the
chain. A new entry here means a reviewer's only remaining defense
(the pre-commit hook) was bypassed by a one-token spelling change.
"""
found = Counter()
detail = {}
for relpath, tree in _iter_src_modules():
source = (SRC_ROOT / relpath).read_text(encoding="utf-8")
if ".bind(" not in source and ".opt(" not in source:
continue
hidden = _violations_hidden_by_chaining(
relpath, tree, ast.parse(source)
)
if hidden:
found[relpath] = len(hidden)
detail[relpath] = hidden
new = {
path: count
for path, count in found.items()
if count > KNOWN_CHAINED_VIOLATIONS.get(path, 0)
}
assert not new, (
"New hook-invisible logging violations behind a logger chain:\n"
+ "\n".join(f" {path}: {detail[path]}" for path in sorted(new))
)
def _log_calls(tree):
"""Yield every ``<something>.<level>(...)`` call in *tree*."""
for node in ast.walk(tree):
if (
isinstance(node, ast.Call)
and isinstance(node.func, ast.Attribute)
and node.func.attr in LOG_LEVELS
):
yield node
def _request_payload_reads(call):
"""Names of request-payload accessors interpolated into *call*.
``ast.walk`` visits both an ``Await`` and the ``Call`` it wraps, so
hits are keyed by node identity to report each accessor once.
"""
hits = {}
for node in ast.walk(call):
target = node.value if isinstance(node, ast.Await) else node
if not (
isinstance(target, ast.Call)
and isinstance(target.func, ast.Attribute)
and target.func.attr in REQUEST_BODY_METHODS
and isinstance(target.func.value, ast.Name)
and target.func.value.id in {"request", "req"}
):
continue
hits[id(target)] = f"request.{target.func.attr}()"
return list(hits.values())
class TestRequestPayloadNeverReachesALogCall:
"""static. Covers the blind spot the Flask->FastAPI rename created."""
PLANTED = (
"async def handler(request):\n"
' logger.info(f"body={await request.json()}")\n'
' logger.warning("form %s", await request.form())\n'
)
def test_detector_finds_planted_reads_positive_control(self):
tree = ast.parse(self.PLANTED)
found = [
h for call in _log_calls(tree) for h in _request_payload_reads(call)
]
assert sorted(found) == ["request.form()", "request.json()"], found
def test_detector_ignores_non_request_receivers_negative_control(self):
tree = ast.parse(
"async def handler(response):\n"
' logger.info(f"{response.json()}")\n'
)
assert [
h for call in _log_calls(tree) for h in _request_payload_reads(call)
] == []
def test_no_log_call_in_src_interpolates_a_request_payload(self):
offenders = []
for relpath, tree in _iter_src_modules():
for call in _log_calls(tree):
for hit in _request_payload_reads(call):
offenders.append(f"{relpath}:{call.lineno} {hit}")
assert not offenders, (
"Log calls interpolating a raw request payload (invisible to "
"check-sensitive-logging, which only knows Flask's attribute "
"spelling):\n " + "\n ".join(offenders)
)
def _handler_exception_params(tree):
"""Yield (function_node, exc_param_name) for FastAPI exception handlers."""
for node in ast.walk(tree):
if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
continue
decorated = any(
isinstance(dec, ast.Call)
and isinstance(dec.func, ast.Attribute)
and dec.func.attr == "exception_handler"
for dec in node.decorator_list
)
if not decorated:
continue
args = node.args.posonlyargs + node.args.args
if len(args) >= 2:
yield node, args[-1].arg
def _getattr_field(node, exc_name):
"""The literal field name in ``getattr(exc, "field", ...)``, or None."""
if not (
isinstance(node, ast.Call)
and isinstance(node.func, ast.Name)
and node.func.id == "getattr"
and len(node.args) >= 2
and isinstance(node.args[0], ast.Name)
and node.args[0].id == exc_name
and isinstance(node.args[1], ast.Constant)
and isinstance(node.args[1].value, str)
):
return None
return node.args[1].value
def _bounded_getattr(node, exc_name):
"""``getattr(exc, "msg"/"lineno"/"colno", ...)`` — a bounded field."""
field = _getattr_field(node, exc_name)
return field is not None and field not in UNBOUNDED_EXC_ATTRS
def _unbounded_getattr(node, exc_name):
"""``getattr(exc, "doc"/"args"/...)`` — the raw payload by another name."""
field = _getattr_field(node, exc_name)
return field is not None and field in UNBOUNDED_EXC_ATTRS
def _unbounded_exception_uses(call, exc_name):
"""Ways *call* exposes the whole exception rather than a bounded field.
Bounded and therefore allowed: ``exc.status_code``, ``exc.error_code``,
``exc.detail`` (already the public HTTP envelope), ``exc.to_dict()``
(sanitized at the boundary by ``WebAPIException.to_dict``), and
``getattr(exc, "msg"/"lineno"/"colno", ...)`` — the bounded
``JSONDecodeError`` fields the CWE-209 fix settled on. ``isinstance``
and ``type()`` are bounded too: they read the class, not the payload.
"""
arguments = list(call.args) + [kw.value for kw in call.keywords]
# A bare ``exc`` Name is only a leak when the exception object itself
# is rendered. ``exc.status_code`` and ``isinstance(exc, X)`` also
# walk over that Name, so collect the ones that are already covered
# by a bounded enclosing expression and exclude them below.
bounded = set()
for arg in arguments:
for node in ast.walk(arg):
if (
isinstance(node, ast.Attribute)
and node.attr not in UNBOUNDED_EXC_ATTRS
and isinstance(node.value, ast.Name)
and node.value.id == exc_name
):
bounded.add(id(node.value))
elif (
isinstance(node, ast.Call)
and isinstance(node.func, ast.Name)
and node.func.id in {"isinstance", "type"}
):
for sub in node.args:
if isinstance(sub, ast.Name) and sub.id == exc_name:
bounded.add(id(sub))
elif _bounded_getattr(node, exc_name):
bounded.add(id(node.args[0]))
found = []
for arg in arguments:
for node in ast.walk(arg):
if (
isinstance(node, ast.Name)
and node.id == exc_name
and id(node) not in bounded
):
found.append("bare exception object")
elif (
isinstance(node, ast.Attribute)
and node.attr in UNBOUNDED_EXC_ATTRS
and isinstance(node.value, ast.Name)
and node.value.id == exc_name
):
found.append(f"exc.{node.attr}")
elif _unbounded_getattr(node, exc_name):
found.append(f'getattr(exc, "{node.args[1].value}")')
return found
def _sinks_in_handler(func):
"""Yield calls in *func* whose arguments are logged or sent to a client."""
for node in ast.walk(func):
if not isinstance(node, ast.Call):
continue
if (
isinstance(node.func, ast.Attribute)
and node.func.attr in LOG_LEVELS
):
yield "log", node
elif isinstance(node.func, ast.Name) and node.func.id in RESPONSE_CTORS:
yield "response", node
class TestExceptionHandlersKeepTheExceptionBounded:
"""static. Guards the blind spot the JSON-decode fix documented."""
PLANTED = (
"@app.exception_handler(ValueError)\n"
"async def h(request, exc):\n"
' logger.error(f"failed: {exc}")\n'
' logger.info("doc %s", getattr(exc, "doc", ""))\n'
' return JSONResponse({"error": str(exc.args)})\n'
)
# The shape the CWE-209 fix settled on: bounded scalar fields only,
# plus the boundary-sanitized ``to_dict()`` envelope.
SAFE = (
"@app.exception_handler(ValueError)\n"
"async def h(request, exc):\n"
' logger.warning("failed {} {}", exc.error_code, exc.status_code)\n'
" logger.warning(\n"
' "{} line {}",\n'
' getattr(exc, "msg", "invalid"),\n'
' getattr(exc, "lineno", "?"),\n'
" )\n"
" if isinstance(exc, ValueError):\n"
" raise exc from None\n"
" return JSONResponse(exc.to_dict(), status_code=exc.status_code)\n"
)
def test_detector_flags_planted_handler_positive_control(self):
tree = ast.parse(self.PLANTED)
handlers = list(_handler_exception_params(tree))
assert len(handlers) == 1
func, exc_name = handlers[0]
found = [
(kind, use)
for kind, call in _sinks_in_handler(func)
for use in _unbounded_exception_uses(call, exc_name)
]
assert ("log", "bare exception object") in found, found
assert ("log", 'getattr(exc, "doc")') in found, found
assert ("response", "exc.args") in found, found
def test_detector_accepts_bounded_fields_negative_control(self):
tree = ast.parse(self.SAFE)
func, exc_name = next(_handler_exception_params(tree))
assert [
use
for _, call in _sinks_in_handler(func)
for use in _unbounded_exception_uses(call, exc_name)
] == []
def test_registered_handlers_expose_only_bounded_fields(self):
"""Every ``@app.exception_handler`` in the web layer, swept."""
checked = 0
offenders = []
for relpath, tree in _iter_src_modules():
if not relpath.startswith("web/"):
continue
for func, exc_name in _handler_exception_params(tree):
checked += 1
for kind, call in _sinks_in_handler(func):
for use in _unbounded_exception_uses(call, exc_name):
offenders.append(
f"{relpath}:{call.lineno} ({kind}) {use}"
)
# Positive control: the sweep must have found real handlers, or
# the emptiness below means nothing.
assert checked >= 5, f"only {checked} exception handlers discovered"
assert not offenders, (
"Exception handlers exposing the raw exception (the "
"check-sensitive-logging exception-variable check cannot see a "
"handler PARAMETER, only `except ... as e`):\n "
+ "\n ".join(offenders)
)
# ---------------------------------------------------------------------------
# Section 3 — executed: real handlers, real ASGI cycle, planted sentinel
# ---------------------------------------------------------------------------
@pytest.fixture(scope="module")
def handler_client():
"""A bare app carrying only the real registered exception handlers."""
app = FastAPI()
@app.post("/reads-json")
async def reads_json(request: Request):
await request.json()
return {"ok": True}
@app.get("/web-api-error")
async def web_api_error():
raise WebAPIException(
message=f"upstream refused: {SENTINEL}",
status_code=502,
error_code="UPSTREAM_FAILED",
)
@app.get("/boom")
async def boom():
try:
raise ValueError(f"inner cause holding {SENTINEL}")
except ValueError as cause:
raise RuntimeError(f"outer failure {SENTINEL}") from cause
_register_exception_handlers(app)
with TestClient(app, raise_server_exceptions=False) as client:
yield client
@pytest.fixture
def captured_logs():
"""Collect every loguru record emitted during the test."""
records = []
def sink(message):
records.append((message.record["message"], str(message)))
logger.enable("local_deep_research")
sink_id = logger.add(
sink,
level="TRACE",
diagnose=False,
backtrace=False,
format="{level}|{name}|{message}",
)
try:
yield records
finally:
logger.remove(sink_id)
class TestExceptionHandlerLogsAndResponses:
"""executed. Plants a sentinel, proves the handler ran, then asserts.
``tests/web/test_exception_handler_contract.py`` already pins the
response bodies; what is new here is the LOG side, and the headers.
"""
def test_malformed_json_body_reaches_neither_response_nor_log(
self, handler_client, captured_logs
):
"""``JSONDecodeError.doc`` is the whole request body (CWE-209).
This is the regression guard for the fix the sweep was told
about: the handler must log ``.msg``/``.lineno``/``.colno`` and
never the exception, whose ``__str__`` is one library release
away from including ``.doc``.
"""
body = json.dumps({"passphrase": SENTINEL})[:-1] # truncated -> 400
response = handler_client.post(
"/reads-json",
content=body,
headers={"Content-Type": "application/json"},
)
# --- positive controls: the handler under test actually ran ---
assert response.status_code == 400
assert response.json() == {"error": "Invalid JSON body"}
handler_lines = [
raw
for message, raw in captured_logs
if message.startswith("JSON decode error handling")
]
assert handler_lines, (
"handler emitted no log line; the no-leak assertion below "
f"would be vacuous. captured: {captured_logs}"
)
# The body really did carry the sentinel to the parser.
assert SENTINEL in body
# --- the actual assertions ---
assert SENTINEL not in response.text
for _, raw in captured_logs:
assert SENTINEL not in raw, raw
def test_web_api_exception_log_line_omits_the_message(
self, handler_client, captured_logs
):
"""The handler logs ``error_code``/``status_code``, nothing else.
``to_dict()`` deliberately ships ``message`` to the client, so
the response is the positive control that the exception really
propagated; the log line must still stay bounded, because a log
line is retained and shared far more freely than a 502 body.
"""
response = handler_client.get("/web-api-error")
# --- positive controls ---
assert response.status_code == 502
assert response.json()["error_code"] == "UPSTREAM_FAILED"
# The exception carried the sentinel all the way to the client,
# which proves the handler ran on a sentinel-bearing exception.
assert SENTINEL in response.text
handler_lines = [
raw
for message, raw in captured_logs
if message.startswith("Web API error")
]
assert handler_lines, f"no handler log line: {captured_logs}"
assert any("UPSTREAM_FAILED" in raw for raw in handler_lines)
# --- the actual assertion ---
for _, raw in captured_logs:
assert SENTINEL not in raw, raw
def test_catch_all_500_omits_the_exception_from_body_and_headers(
self, handler_client
):
"""The catch-all must not echo ``str(exc)`` or ``__cause__``.
Headers matter as much as the body here: the catch-all is
registered for bare ``Exception``, so Starlette wires it into
``ServerErrorMiddleware`` — outside every ``add_middleware``
layer — and it stamps its own header set by hand. A future
diagnostic header (``X-Error-Detail``) would bypass every
response-body assertion in the suite.
"""
response = handler_client.get("/boom")
# --- positive controls: this handler, not Starlette's default ---
assert response.status_code == 500
assert response.json() == {"error": "Server error"}
# Only the custom handler stamps security headers on this path.
assert response.headers.get("X-Content-Type-Options") == "nosniff"
# --- the actual assertions ---
assert SENTINEL not in response.text
for name, value in response.headers.items():
assert SENTINEL not in value, name
class TestLoguruKwargSemantics:
"""executed. Pins the loguru behavior the baseline above relies on.
``check-sensitive-logging`` rejects ``exc_info=True`` on
warning/error/critical because it "exposes tracebacks in
production". That is a stdlib-``logging`` kwarg; loguru has no such
parameter and files it under ``record["extra"]`` instead, so the 79
``exc_info=True`` sites in src/ emit no traceback at all — the
diagnostic they were written for is silently dropped. The loguru
spelling that DOES attach a traceback is ``.opt(exception=...)``,
and the hook permits it outside SECURE_LOGGING_DIRS by design.
Both facts matter for reading the baseline in
``KNOWN_CHAINED_VIOLATIONS``, so they are asserted rather than
assumed.
"""
@staticmethod
def _emit(call):
captured = []
sink_id = logger.add(
lambda m: captured.append((str(m), m.record)),
level="TRACE",
diagnose=False,
backtrace=False,
format="{message}",
)
try:
logger.enable("local_deep_research")
try:
raise RuntimeError(f"failure carrying {SENTINEL}")
except RuntimeError:
call()
finally:
logger.remove(sink_id)
return captured
def test_exc_info_kwarg_attaches_no_exception(self):
# Spelled through ``**`` so this file does not itself trip
# ``check-sensitive-logging``'s exc_info rule while demonstrating
# that the rule guards a kwarg loguru does not implement. The
# call loguru receives is identical either way.
exc_info_kwarg = {"exc_info": True}
captured = self._emit(
lambda: logger.warning("chained", **exc_info_kwarg)
)
assert captured, "positive control: nothing was logged"
text, record = captured[0]
assert record["exception"] is None
assert record["extra"]["exc_info"] is True
assert SENTINEL not in text
def test_opt_exception_does_attach_the_message(self):
captured = self._emit(
lambda: logger.opt(exception=True).warning("chained")
)
assert captured, "positive control: nothing was logged"
text, record = captured[0]
assert record["exception"] is not None
assert SENTINEL in text
def test_extras_are_not_rendered_by_the_default_format(self):
"""Why the ``reason=str(exc)`` sites are latent, not live."""
captured = self._emit(
lambda: logger.bind(k=1).warning("chained", reason=SENTINEL)
)
assert captured, "positive control: nothing was logged"
text, record = captured[0]
assert record["extra"]["reason"] == SENTINEL
assert SENTINEL not in text
# ---------------------------------------------------------------------------
# Section 4 — settings read paths: are ``*.api_key`` values masked on read?
# ---------------------------------------------------------------------------
def _shipped_settings():
"""Every setting definition shipped in defaults/, with its source file."""
defaults = SRC_ROOT / "defaults"
for path in sorted(defaults.rglob("*.json")):
try:
payload = json.loads(path.read_text(encoding="utf-8"))
except (json.JSONDecodeError, UnicodeDecodeError): # pragma: no cover
continue
if not isinstance(payload, dict):
continue
for key, spec in payload.items():
if isinstance(spec, dict) and "ui_element" in spec:
yield path.relative_to(SRC_ROOT).as_posix(), key, spec
class TestShippedSecretsRedactOnKeyNameAlone:
"""static. The bulk settings GET has no ``ui_element`` to lean on.
``GET /settings/api/bulk`` and ``GET /settings/api/{key}`` call
``DataSanitizer.redact_value(key, None, value)`` — ``ui_element`` is
``None``, so a secret is masked only if the last dotted segment of
its key is an exact match in ``DEFAULT_SENSITIVE_KEYS``. Any shipped
setting marked ``ui_element == "password"`` whose leaf name is not in
that set ships in the clear on those two read paths.
``test_bulk_secret_name_coverage.py`` pins the same predicate against
synthetic settings; this sweeps the ~20 real defaults files, which is
where a new provider's key actually gets added.
"""
def test_predicate_discriminates_negative_control(self):
assert DataSanitizer.is_sensitive_setting("llm.temperature", None) is (
False
)
assert DataSanitizer.is_sensitive_setting("llm.openai.api_key", None)
def test_every_password_typed_default_is_masked_without_ui_element(self):
secrets = [
(source, key)
for source, key, spec in _shipped_settings()
if spec.get("ui_element") == "password"
]
# Positive control: the corpus was actually loaded and does
# contain password-typed settings, or the loop below is vacuous.
assert len(secrets) >= 20, f"only {len(secrets)} secrets found"
unmasked = [
f"{source}::{key}"
for source, key in secrets
if not DataSanitizer.is_sensitive_setting(key, None)
]
assert not unmasked, (
"Shipped password-typed settings whose leaf name is not in "
"DataSanitizer.DEFAULT_SENSITIVE_KEYS — GET /settings/api/bulk "
"and GET /settings/api/{key} pass ui_element=None and would "
"return these in the clear:\n " + "\n ".join(unmasked)
)
def test_redact_value_masks_a_planted_secret_end_to_end(self):
"""The predicate is only useful if ``redact_value`` acts on it."""
for _, key, spec in _shipped_settings():
if spec.get("ui_element") == "password":
sample_key = key
break
else: # pragma: no cover
pytest.fail("no password-typed setting found in defaults/")
masked = DataSanitizer.redact_value(sample_key, None, SENTINEL)
assert masked == DataSanitizer.REDACTION_TEXT
assert SENTINEL not in str(masked)
# ...and an empty value stays readable, so the UI can still tell
# "configured" from "not configured".
assert DataSanitizer.redact_value(sample_key, None, "") == ""