~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
test(security): pin cross-user news and settings isolation (#5631)
LearningCircuitd53a13077907
diff --git a/.github/scripts/file-whitelist-check.sh b/.github/scripts/file-whitelist-check.shindex 518674a63a..507663e735 100755--- a/.github/scripts/file-whitelist-check.sh+++ b/.github/scripts/file-whitelist-check.sh@@ -407,11 +407,26 @@ RESEARCH_DATA_VIOLATIONS+=("$file")fifi-# Check for Flask secret keys-if grep -E "SECRET_KEY.*=.*[\"'][^\"']{16,}[\"']" "$file" >/dev/null 2>&1; then+# Check for Flask secret keys. Exclude NON_/NOT_ at each occurrence,+# never by discarding an entire matching line: a real assignment can share+# a line with a decoy comment. GNU grep on the CI runner supports PCRE.+# Keep the original broad equals-assignment branch, including indexed and+# attribute targets after SECRET_KEY. The colon branch also recognizes+# mapping entries and typed assignments without treating arbitrary prose+# after SECRET_KEY as an assignment target.+# Identifiers ending in NON_SECRET_KEY/NOT_SECRET_KEY remain exempt.+# Consume all matches: no early-closing pipe or -q can turn a bulk match+# into SIGPIPE. Exit 1 means no match; other scanner errors fail the gate.+if grep -P "(?<!NON_)(?<!NOT_)SECRET_KEY(?:.*=|[A-Za-z0-9_]*[^A-Za-z0-9_]*:).*[\"'][^\"']{16,}[\"']" "$file" >/dev/null; thenif ! grep -iE "(os\.environ|getenv|config\[|example|placeholder)" "$file" >/dev/null 2>&1; thenFLASK_SECRET_VIOLATIONS+=("$file")fi+else+GREP_STATUS=$?+if [ "$GREP_STATUS" -ne 1 ]; then+printf 'ERROR: SECRET_KEY scan failed for %s (grep exit %s)\n' "$file" "$GREP_STATUS" >&2+exit "$GREP_STATUS"+fifi# Check for environment files@@ -690,11 +705,17 @@ echo ""for violation in "${RESEARCH_DATA_VIOLATIONS[@]}"; doecho " 📊 $violation"-# Show the specific lines with research data+# Show the specific lines with research data. Captured into a variable+# first (with `|| true`) rather than piped straight into `head -3`: on a+# bulk match that pipe's early close could SIGPIPE the grep feeding it, and+# this loop is a bare statement (not an `if` condition) under+# `set -euo pipefail`, so an uncaught SIGPIPE here would abort the whole+# script mid-report instead of just this violation's detail lines.echo " → Found hardcoded research data:"-grep -n -E "(research_id|session_id|query_id).*=.*[\"'][0-9a-f]{8,}[\"']" "$violation" 2>/dev/null | head -3 | while read -r line; do+RESEARCH_DATA_HITS=$(grep -n -E "(research_id|session_id|query_id).*=.*[\"'][0-9a-f]{8,}[\"']" "$violation" 2>/dev/null || true)+echo "$RESEARCH_DATA_HITS" | head -3 | while read -r line; doecho " $line"-done+done || trueecho " → Issue: Hardcoded research/session IDs in non-test file"echo " → Fix: Use environment variables or configuration files"@@ -713,9 +734,23 @@ echo " 🔐 $violation"# Show the specific lines with secret keysecho " → Found hardcoded Flask secret key:"-grep -n -E "SECRET_KEY.*=.*[\"'][^\"']{16,}[\"']" "$violation" 2>/dev/null | head -3 | while read -r line; do+# Purely cosmetic (the file is already recorded as a violation by this+# point), so the simple `... || true` capture is enough here. The+# detection site above rejects scanner errors before any reporting begins.+# Use the same pattern for detection and reporting (kept byte-identical --+# see the comment there), with no second `grep -v`.+# Still avoids piping straight into `head -3`: on a bulk match that pipe's+# early close could SIGPIPE the grep feeding it, and outside an `if`+# condition that failure is not suppressed by `set -e`.+FLASK_KEY_REPORT_HITS=$(grep -n -P "(?<!NON_)(?<!NOT_)SECRET_KEY(?:.*=|[A-Za-z0-9_]*[^A-Za-z0-9_]*:).*[\"'][^\"']{16,}[\"']" "$violation" 2>/dev/null || true)+# Same reasoning applies to displaying the result: on a bulk match, `head -3`+# closing early can SIGPIPE the `echo` feeding it, and this loop is a bare+# statement (not an `if` condition), so `set -e` would otherwise treat that+# as a fatal error and abort the whole script mid-report. `|| true` on the+# full display pipeline keeps a large match count from crashing the script.+echo "$FLASK_KEY_REPORT_HITS" | head -3 | while read -r line; doecho " $line"-done+done || trueecho " → Issue: Hardcoded Flask SECRET_KEY"echo " → Fix: Use os.environ or load from secure config file"@@ -746,12 +781,17 @@ echo ""for violation in "${HIGH_ENTROPY_VIOLATIONS[@]}"; doecho " 🎲 $violation"-# Show sample of high entropy strings+# Show sample of high entropy strings. Captured first (with `|| true`)+# rather than piped straight into `head -3` -- same SIGPIPE-under-pipefail+# shape as the research-data block above: a bulk match's early `head` close+# can SIGPIPE the grep feeding it, and this is a bare statement, not an+# `if` condition, under `set -euo pipefail`.echo " → Found high-entropy strings:"-grep -n -E "[a-zA-Z0-9+/]{40,}={0,2}|[a-f0-9]{40,}" "$violation" 2>/dev/null | head -3 | while read -r line; do+HIGH_ENTROPY_HITS=$(grep -n -E "[a-zA-Z0-9+/]{40,}={0,2}|[a-f0-9]{40,}" "$violation" 2>/dev/null || true)+echo "$HIGH_ENTROPY_HITS" | head -3 | while read -r line; do# Truncate long lines for readabilityecho " ${line:0:120}..."-done+done || trueecho " → Issue: High-entropy strings that could be secrets"echo " → Fix: Review and move to environment variables if sensitive"@@ -768,11 +808,16 @@ echo ""for violation in "${HARDCODED_PATH_VIOLATIONS[@]}"; doecho " 📁 $violation"-# Show the specific hardcoded paths+# Show the specific hardcoded paths. Captured first (with `|| true`)+# rather than piped straight into `head -5` -- same SIGPIPE-under-pipefail+# shape as the research-data block above: a bulk match's early `head` close+# can SIGPIPE the grep feeding it, and this is a bare statement, not an+# `if` condition, under `set -euo pipefail`.echo " → Found hardcoded paths:"-grep -n -E "(/home/[a-zA-Z0-9_-]+|/Users/[a-zA-Z0-9_-]+|C:\\\\Users\\\\[a-zA-Z0-9_-]+|/opt/|/var/|/etc/|/usr/local/)" "$violation" 2>/dev/null | head -5 | while read -r line; do+HARDCODED_PATH_HITS=$(grep -n -E "(/home/[a-zA-Z0-9_-]+|/Users/[a-zA-Z0-9_-]+|C:\\\\Users\\\\[a-zA-Z0-9_-]+|/opt/|/var/|/etc/|/usr/local/)" "$violation" 2>/dev/null || true)+echo "$HARDCODED_PATH_HITS" | head -5 | while read -r line; doecho " $line"-done+done || trueecho " → Issue: Hardcoded absolute paths reduce portability"echo " → Fix: Use relative paths, environment variables, or config files"@@ -789,11 +834,22 @@ echo ""for violation in "${HARDCODED_IP_VIOLATIONS[@]}"; doecho " 🌐 $violation"-# Show the specific IP addresses+# Show the specific IP addresses. Captured first (with `|| true`) rather+# than piped straight into `head -5`: the same SIGPIPE-under-pipefail shape+# as the FLASK SECRET KEY reporting block above -- a bulk match's early+# `head` close can SIGPIPE the greps feeding it, and this is a bare+# statement, not an `if` condition, under `set -euo pipefail`, so an+# uncaught SIGPIPE here would abort the whole script mid-report. (The+# second `grep -v` here is purely cosmetic display filtering of+# already-recorded violations -- unlike the FLASK SECRET KEY detection+# pattern, it drops whole lines on purpose, since there is no per-line+# secret being discarded, just already-excluded private/localhost IPs kept+# out of the printed sample.)+HARDCODED_IP_HITS=$(grep -n -E "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" "$violation" 2>/dev/null | grep -v -E "(127\.0\.0\.1|0\.0\.0\.0|localhost|192\.168\.|10\.|172\.(1[6-9]|2[0-9]|3[0-1])\.|255\.255\.255\.|192\.0\.2\.|198\.51\.100\.|203\.0\.113\.)" || true)echo " → Found hardcoded IP addresses:"-grep -n -E "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" "$violation" 2>/dev/null | grep -v -E "(127\.0\.0\.1|0\.0\.0\.0|localhost|192\.168\.|10\.|172\.(1[6-9]|2[0-9]|3[0-1])\.|255\.255\.255\.|192\.0\.2\.|198\.51\.100\.|203\.0\.113\.)" | head -5 | while read -r line; do+echo "$HARDCODED_IP_HITS" | head -5 | while read -r line; doecho " $line"-done+done || trueecho " → Issue: Hardcoded IP addresses (non-private/localhost)"echo " → Fix: Use DNS names, environment variables, or config files"diff --git a/tests/security/auth_fixtures.py b/tests/security/auth_fixtures.pynew file mode 100644index 0000000000..df3d6523b4--- /dev/null+++ b/tests/security/auth_fixtures.py@@ -0,0 +1,12 @@+from __future__ import annotations++from dataclasses import dataclass++from fastapi.testclient import TestClient+++@dataclass(frozen=True, slots=True)+class AuthenticatedUser:+ username: str+ password: str+ client: TestClientdiff --git a/tests/security/conftest.py b/tests/security/conftest.pyindex 8619b84847..4e16da8cd6 100644--- a/tests/security/conftest.py+++ b/tests/security/conftest.py@@ -2,7 +2,14 @@Fixtures for security module tests."""+from __future__ import annotations++import uuid+import pytest+from fastapi.testclient import TestClient++from .auth_fixtures import AuthenticatedUser@pytest.fixture@@ -67,3 +74,122 @@ def sample_urls():"empty": "","none": None,}+++# =============================================================================+# Cross-user (two-principal) fixtures+# =============================================================================+#+# FastAPI-native replacement for the Flask-era fixture of the same name (the+# production code it originally targeted, news/flask_api.py, was removed by+# the FastAPI migration, #3299). These pin BOLA/per-user routing end-to-end:+# require_auth -> session cookie -> the caller's own per-user encrypted+# database (get_user_db_session(<authenticated username>)), not a shared+# table filtered by a foreign key. The warm-cache wrong-password regression+# from #5596 has separate focused tests.+#+# Modeled on the ``_new_client``/``_register_and_login`` helpers in+# ``test_two_user_attack_simulation.py`` -- kept local (not imported) so this+# fixture has no import-order dependency on that test module.+++def _new_test_client(app) -> TestClient:+ """A client with its own cookie jar and its own rate-limit bucket.++ ``/auth/register`` is capped per-IP; the TestClient peer address is+ treated as private so ``X-Forwarded-For`` is honoured for the slowapi+ key. Without a unique one, user B's registration lands in user A's+ bucket.+ """+ client = TestClient(app, raise_server_exceptions=False)+ octet_a = uuid.uuid4().int % 254 + 1+ octet_b = uuid.uuid4().int % 254 + 1+ client.headers.update({"X-Forwarded-For": f"10.{octet_a}.{octet_b}.1"})+ return client+++def _csrf(client: TestClient) -> str:+ client.get("/auth/login")+ resp = client.get("/auth/csrf-token")+ return resp.json().get("csrf_token", "") if resp.status_code == 200 else ""+++def _drop_stale_csrf_header(client: TestClient) -> None:+ """CSRFMiddleware prefers the header over the form field.++ ``_register_and_login`` attaches a persistent ``X-CSRFToken`` default+ header; once the session rotates its token that stale header shadows the+ correct ``csrf_token`` form field and every form POST 403s.+ """+ for name in ("X-CSRFToken", "X-CSRF-Token"):+ client.headers.pop(name, None)+++def _register_and_login(+ client: TestClient, username: str, password: str+) -> None:+ _drop_stale_csrf_header(client)+ resp = client.post(+ "/auth/register",+ data={+ "username": username,+ "password": password,+ "confirm_password": password,+ "acknowledge": "true",+ "csrf_token": _csrf(client),+ },+ follow_redirects=False,+ )+ assert resp.status_code == 302, (+ f"Registration failed for {username}: "+ f"{resp.status_code} {resp.text[:300]}"+ )++ _drop_stale_csrf_header(client)+ resp = client.post(+ "/auth/login",+ data={+ "username": username,+ "password": password,+ "csrf_token": _csrf(client),+ },+ follow_redirects=False,+ )+ assert resp.status_code == 302, (+ f"Login failed for {username}: {resp.status_code} {resp.text[:300]}"+ )++ # Attach the fresh (post-login) CSRF token as a default header so later+ # PUT/POST/DELETE calls through this client pass CSRFMiddleware without+ # each test having to fetch and pass it explicitly.+ token = client.get("/auth/csrf-token")+ if token.status_code == 200:+ client.headers.update({"X-CSRFToken": token.json()["csrf_token"]})+++def two_authenticated_users(+ app,+) -> tuple[AuthenticatedUser, AuthenticatedUser]:+ """Register and log in two independent users against one FastAPI app.++ Returns ``(user_a, user_b)`` as :class:`AuthenticatedUser` tuples. The+ ``app`` fixture (tests/conftest.py) points ``LDR_DATA_DIR`` at a fresh+ per-test temp directory before the app is created, so each test gets its+ own encrypted-database directory -- no manual cleanup needed between+ user A and user B.+ """+ username_a = f"sec2u_{uuid.uuid4().hex[:12]}"+ username_b = f"sec2u_{uuid.uuid4().hex[:12]}"+ password_a = "TestPass123!" # noqa: S105+ password_b = "OtherPass456!" # noqa: S105++ client_a = _new_test_client(app)+ client_b = _new_test_client(app)+ _register_and_login(client_a, username_a, password_a)+ _register_and_login(client_b, username_b, password_b)++ return (+ AuthenticatedUser(username_a, password_a, client_a),+ AuthenticatedUser(username_b, password_b, client_b),+ )diff --git a/tests/security/test_cross_user_news_subscriptions.py b/tests/security/test_cross_user_news_subscriptions.pynew file mode 100644index 0000000000..f8e0926f3f--- /dev/null+++ b/tests/security/test_cross_user_news_subscriptions.py@@ -0,0 +1,209 @@+"""Cross-user subscription isolation on the live FastAPI news router.++Each handler opens the authenticated user's encrypted database, so foreign+and absent subscription IDs share the same not-found response. GET, PUT,+DELETE and history return the complete curated NewsAPIException envelope,+including the caller-supplied ID. The run route returns a flat not-found+body. Owner-success and survival controls distinguish isolation from a+broken route; research startup is stubbed to avoid external work.+"""++# allow: no-sut-import — requests exercise the live FastAPI app through+# authenticated clients. Only research startup is replaced with a local stub.+from typing import Final+from uuid import UUID, uuid4++import pytest++from .auth_fixtures import AuthenticatedUser++pytestmark = pytest.mark.real_session_check+++QUERY_SENTINEL: Final = "cross-user-news-7c315fc0-9ef0-4d77-b48c-f4cc07fe8955"+++def _create_subscription(user: AuthenticatedUser) -> str:+ response = user.client.post(+ "/news/api/subscribe",+ json={+ "name": "Cross-user security subscription",+ "query": QUERY_SENTINEL,+ "subscription_type": "search",+ "refresh_minutes": 240,+ },+ )+ assert response.status_code == 200, response.text[:300]+ subscription_id = response.json()["subscription_id"]+ _ = UUID(subscription_id)+ return subscription_id+++def _assert_subscription_not_found(response, subscription_id: str) -> None:+ """Only the caller-supplied ID may vary in the curated error envelope."""+ assert response.status_code == 404, response.text[:300]+ assert response.json() == {+ "error": f"Subscription not found: {subscription_id}",+ "error_code": "SUBSCRIPTION_NOT_FOUND",+ "status_code": 404,+ "details": {"subscription_id": subscription_id},+ }+++ ("method", "json_body"),+ [+ pytest.param("GET", None, id="get"),+ pytest.param(+ "PUT",+ {"name": "attacker overwrite"},+ id="update",+ ),+ pytest.param("DELETE", None, id="delete"),+ ],+)+def test_foreign_and_absent_subscription_actions_are_indistinguishable(+ two_authenticated_users: tuple[AuthenticatedUser, AuthenticatedUser],+ method: str,+ json_body: dict[str, str] | None,+) -> None:+ # Given+ user_a, user_b = two_authenticated_users+ subscription_id = _create_subscription(user_a)+ absent_subscription_id = str(uuid4())++ # When+ foreign = user_b.client.request(+ method,+ f"/news/api/subscriptions/{subscription_id}",+ json=json_body,+ )+ absent = user_b.client.request(+ method,+ f"/news/api/subscriptions/{absent_subscription_id}",+ json=json_body,+ )++ _assert_subscription_not_found(foreign, subscription_id)+ _assert_subscription_not_found(absent, absent_subscription_id)++ survivor = user_a.client.get(f"/news/api/subscriptions/{subscription_id}")+ assert survivor.status_code == 200+ survivor_data = survivor.json()+ assert survivor_data["id"] == subscription_id+ assert survivor_data["query_or_topic"] == QUERY_SENTINEL+ assert survivor_data["name"] == "Cross-user security subscription"+++def test_foreign_subscription_is_absent_from_list(+ two_authenticated_users: tuple[AuthenticatedUser, AuthenticatedUser],+) -> None:+ # Given+ user_a, user_b = two_authenticated_users+ subscription_id = _create_subscription(user_a)++ # When+ response = user_b.client.get("/news/api/subscriptions/current")++ # Then+ assert response.status_code == 200+ subscriptions = response.json()["subscriptions"]+ assert all(item["id"] != subscription_id for item in subscriptions)+ assert all(item["query"] != QUERY_SENTINEL for item in subscriptions)++ # Then+ # Owner-positive control: without it the two ``all(...)`` assertions above+ # hold vacuously over user B's empty list, so a listing endpoint that+ # returned no subscriptions to anyone would still pass.+ survivor = user_a.client.get("/news/api/subscriptions/current")+ assert survivor.status_code == 200+ survivor_subscriptions = survivor.json()["subscriptions"]+ assert any(item["id"] == subscription_id for item in survivor_subscriptions)+ assert any(+ item["query"] == QUERY_SENTINEL for item in survivor_subscriptions+ )+++def test_foreign_subscription_cannot_be_run_and_survives(+ two_authenticated_users: tuple[AuthenticatedUser, AuthenticatedUser],+ monkeypatch,+) -> None:+ """A foreign call starts nothing; the owner can start the same subscription."""+ starts = []+ research_id = str(uuid4())++ def start_research(_request, data, username):+ starts.append((username, data))+ return {"status": "success", "research_id": research_id}++ monkeypatch.setattr(+ "local_deep_research.web.routers.news_flask_api._start_research_in_process",+ start_research,+ )++ # Given+ user_a, user_b = two_authenticated_users+ subscription_id = _create_subscription(user_a)++ # When+ denied = user_b.client.post(+ f"/news/api/subscriptions/{subscription_id}/run"+ )++ # Then+ # The body matters as much as the status: an unregistered or misspelled+ # route also answers 404, so status alone would pass against a URL that+ # reaches no handler at all. The JSON payload only comes from the route's+ # own not-found branch, which -- unlike GET/PUT/DELETE/history -- does not+ # go through the NewsAPIException machinery and so does not echo the id.+ assert denied.status_code == 404+ assert denied.json() == {"error": "Subscription not found"}+ assert starts == []++ survivor = user_a.client.get(f"/news/api/subscriptions/{subscription_id}")+ assert survivor.status_code == 200+ assert survivor.json()["query_or_topic"] == QUERY_SENTINEL++ owner_run = user_a.client.post(+ f"/news/api/subscriptions/{subscription_id}/run"+ )+ assert owner_run.status_code == 200, owner_run.text[:300]+ assert owner_run.json()["status"] == "success"+ assert owner_run.json()["research_id"] == research_id+ assert len(starts) == 1+ username, request_data = starts[0]+ assert username == user_a.username+ assert request_data["query"] == QUERY_SENTINEL+ assert request_data["metadata"]["subscription_id"] == subscription_id+++def test_foreign_and_absent_subscription_history_are_indistinguishable(+ two_authenticated_users: tuple[AuthenticatedUser, AuthenticatedUser],+) -> None:+ # Given+ user_a, user_b = two_authenticated_users+ subscription_id = _create_subscription(user_a)+ absent_subscription_id = str(uuid4())++ # When+ foreign = user_b.client.get(+ f"/news/api/subscriptions/{subscription_id}/history"+ )+ absent = user_b.client.get(+ f"/news/api/subscriptions/{absent_subscription_id}/history"+ )++ _assert_subscription_not_found(foreign, subscription_id)+ _assert_subscription_not_found(absent, absent_subscription_id)++ # Then+ # Owner-positive control: without it the equality above holds just as well+ # when ``/history`` 404s for everyone, so it could not tell "foreigners are+ # refused" apart from "the route is broken".+ survivor = user_a.client.get(+ f"/news/api/subscriptions/{subscription_id}/history"+ )+ assert survivor.status_code == 200+ survivor_data = survivor.json()+ assert survivor_data["subscription"]["id"] == subscription_id+ assert survivor_data["subscription"]["query_or_topic"] == QUERY_SENTINELdiff --git a/tests/security/test_cross_user_settings.py b/tests/security/test_cross_user_settings.pynew file mode 100644index 0000000000..47bd3a4eb4--- /dev/null+++ b/tests/security/test_cross_user_settings.py@@ -0,0 +1,172 @@+"""Cross-user settings isolation tests.++Covers the single-setting ``PUT``/``GET /settings/api/{key}`` routes and the+full-snapshot ``GET /settings/api`` route on the live FastAPI settings router+(``src/local_deep_research/web/routers/settings.py``). Every one of them opens+``get_user_db_session(<authenticated username>)`` -- the caller's own per-user+encrypted SQLCipher database -- so isolation here is structural rather than a+per-route ownership check.++Note on what the provider-secret tests can and cannot prove. ``llm.openai.api_key``+is redacted by key name, not by value: ``DataSanitizer.redact_value`` returns+``"[REDACTED]"`` for that key whichever row it was handed. So on those tests the+response-body assertions pin the *redaction* behaviour only -- a read that served+the wrong user's row would still render ``"[REDACTED]"`` and still omit+``USER_A_SECRET``. What pins the write path there is ``_stored_provider_secret``,+which bypasses HTTP and reads each user's encrypted database directly with that+user's own credentials.++The read path is pinned by+``test_non_secret_setting_read_uses_authenticated_user_database`` (single-key+route) and by the ``UNREDACTED_SETTING_KEY`` assertions inside+``test_provider_secret_absent_from_foreign_full_snapshot`` (full-snapshot+route), both of which use ``search.fetch.mode`` -- an unredacted key whose+value differs per user, so the response reveals which database the GET+actually read from.++This supersedes the PR's original Flask-era version (``tests/security/+test_cross_user_settings.py`` against ``web/routes/settings_routes.py``, a+module the FastAPI migration, #3299, removed) -- ported onto the live FastAPI+route paths (``/settings/api``, ``/settings/api/{key}``) and TestClient.+"""++from typing import Final++import pytest++from local_deep_research.database.models import Setting+from local_deep_research.database.session_context import get_user_db_session+from .auth_fixtures import AuthenticatedUser++pytestmark = pytest.mark.real_session_check+++SETTING_KEY: Final = "llm.openai.api_key"+USER_A_SECRET: Final = "sk-a-6ff08c43-1607-43e8-b6f8-21ad5729738e"+USER_B_SECRET: Final = "sk-b-2f3405da-f3e7-42c7-9056-a7559d913841"+UNREDACTED_SETTING_KEY: Final = "search.fetch.mode"+USER_A_MODE: Final = "summary_focus"+USER_B_MODE: Final = "disabled"+++def _set_provider_secret(user: AuthenticatedUser, secret: str) -> None:+ response = user.client.put(+ f"/settings/api/{SETTING_KEY}", json={"value": secret}+ )+ assert response.status_code == 200, response.text[:300]+++def _stored_provider_secret(user: AuthenticatedUser) -> str:+ with get_user_db_session(user.username, user.password) as db_session:+ setting = db_session.query(Setting).filter_by(key=SETTING_KEY).one()+ return str(setting.value)+++def test_provider_secret_isolated_on_single_setting_read(+ two_authenticated_users: tuple[AuthenticatedUser, AuthenticatedUser],+) -> None:+ # Given+ user_a, user_b = two_authenticated_users+ _set_provider_secret(user_a, USER_A_SECRET)+ _set_provider_secret(user_b, USER_B_SECRET)++ # When+ response = user_b.client.get(f"/settings/api/{SETTING_KEY}")++ # Then+ assert response.status_code == 200+ data = response.json()+ assert data["key"] == SETTING_KEY+ assert data["value"] == "[REDACTED]"+ assert USER_A_SECRET not in response.text++ # Then+ survivor = user_a.client.get(f"/settings/api/{SETTING_KEY}")+ assert survivor.status_code == 200+ survivor_data = survivor.json()+ assert survivor_data["key"] == SETTING_KEY+ assert survivor_data["value"] == "[REDACTED]"+ assert _stored_provider_secret(user_a) == USER_A_SECRET+ assert _stored_provider_secret(user_b) == USER_B_SECRET+++def test_provider_secret_absent_from_foreign_full_snapshot(+ two_authenticated_users: tuple[AuthenticatedUser, AuthenticatedUser],+) -> None:+ """Pin both redaction AND cross-user read isolation on ``GET /settings/api``.++ The redaction assertions below (``SETTING_KEY`` reads ``"[REDACTED]"``,+ ``USER_A_SECRET`` absent from the raw response body) hold for ANY user's+ row, because ``DataSanitizer.redact_settings_snapshot`` redacts by+ setting NAME, not by row ownership -- they would still pass even if this+ route served user A's entire settings dict to user B. They pin the+ redaction behaviour only, same caveat as+ ``test_provider_secret_isolated_on_single_setting_read`` above.++ The ``UNREDACTED_SETTING_KEY`` assertions are the actual isolation+ discriminator: ``search.fetch.mode`` is not in+ ``DataSanitizer.DEFAULT_SENSITIVE_KEYS`` (its leaf is ``mode``), so it+ ships unredacted in the snapshot with a value that differs per user. If+ ``GET /settings/api`` leaked user A's snapshot to user B, then+ ``data["settings"][UNREDACTED_SETTING_KEY]["value"]`` would read+ ``USER_A_MODE`` instead of the asserted ``USER_B_MODE`` -- so THIS+ assertion is the one that fails on a leak, not the redaction ones above.+ """+ # Given+ user_a, user_b = two_authenticated_users+ _set_provider_secret(user_a, USER_A_SECRET)+ _set_provider_secret(user_b, USER_B_SECRET)+ put_a = user_a.client.put(+ f"/settings/api/{UNREDACTED_SETTING_KEY}", json={"value": USER_A_MODE}+ )+ put_b = user_b.client.put(+ f"/settings/api/{UNREDACTED_SETTING_KEY}", json={"value": USER_B_MODE}+ )+ assert put_a.status_code == 200, put_a.text[:300]+ assert put_b.status_code == 200, put_b.text[:300]++ # When+ response = user_b.client.get("/settings/api")++ # Then+ assert response.status_code == 200+ data = response.json()+ assert data["status"] == "success"+ assert isinstance(data["settings"], dict)+ assert data["settings"][SETTING_KEY]["value"] == "[REDACTED]"+ assert USER_A_SECRET not in response.text+ assert data["settings"][UNREDACTED_SETTING_KEY]["value"] == USER_B_MODE++ # Then+ survivor = user_a.client.get("/settings/api")+ assert survivor.status_code == 200+ survivor_settings = survivor.json()["settings"]+ assert survivor_settings[SETTING_KEY]["value"] == "[REDACTED]"+ assert survivor_settings[UNREDACTED_SETTING_KEY]["value"] == USER_A_MODE+ assert _stored_provider_secret(user_a) == USER_A_SECRET+ assert _stored_provider_secret(user_b) == USER_B_SECRET+++def test_non_secret_setting_read_uses_authenticated_user_database(+ two_authenticated_users: tuple[AuthenticatedUser, AuthenticatedUser],+) -> None:+ # Given+ user_a, user_b = two_authenticated_users+ response_a = user_a.client.put(+ f"/settings/api/{UNREDACTED_SETTING_KEY}", json={"value": USER_A_MODE}+ )+ response_b = user_b.client.put(+ f"/settings/api/{UNREDACTED_SETTING_KEY}", json={"value": USER_B_MODE}+ )+ assert response_a.status_code == 200, response_a.text[:300]+ assert response_b.status_code == 200, response_b.text[:300]++ # When+ read_b = user_b.client.get(f"/settings/api/{UNREDACTED_SETTING_KEY}")+ read_a = user_a.client.get(f"/settings/api/{UNREDACTED_SETTING_KEY}")++ # Then+ assert read_b.status_code == 200+ assert read_a.status_code == 200+ assert read_b.json()["value"] == USER_B_MODE+ assert read_a.json()["value"] == USER_A_MODEdiff --git a/tests/security/test_flask_secret_key_gate.py b/tests/security/test_flask_secret_key_gate.pynew file mode 100644index 0000000000..2e81e2afbe--- /dev/null+++ b/tests/security/test_flask_secret_key_gate.py@@ -0,0 +1,456 @@+"""Regression coverage for the CI hardcoded SECRET_KEY detector.++The production shell script must recognize direct, prefixed, suffixed,+indexed and attribute assignments while excluding NON_/NOT_ decoys only+at their own occurrence. A decoy elsewhere on a line must not suppress a+real assignment. Bulk matches must finish reporting, and scanner errors+must fail closed.++The fixed input panel invokes the actual script in an isolated repository.+The Python-only repository scan extracts its live detector and suppression+patterns; it supplements, rather than replaces, the fixed input panel.+"""++# allow: no-sut-import — the subject is a CI shell script+# (.github/scripts/file-whitelist-check.sh), not importable Python. The tests+# run the actual script as a subprocess so a defect in its own pipeline+# mechanics (not just its patterns) is caught, and read the pattern out of+# the script rather than restating it so the two cannot drift.++import os+import re+import shlex+import shutil+import subprocess+from pathlib import Path+from typing import Final++import pytest++REPO_ROOT: Final = Path(__file__).resolve().parents[2]+GATE_SCRIPT: Final = REPO_ROOT / ".github/scripts/file-whitelist-check.sh"+++def _gate_pattern() -> str:+ """Extract the SECRET_KEY detection pattern from the gate script.++ This used to be a two-stage pipeline (a broad, prefix-agnostic match+ piped through a second, inverted grep dropping NON_/NOT_ decoy lines).+ It is now a single ``grep -P`` (PCRE) pattern: prefix-agnostic like+ before, but with the decoy exclusion built in as two negative+ lookbehinds instead of a second grep, so it can no longer discard a+ genuine secret that merely shares a line with a decoy.+ """+ source = GATE_SCRIPT.read_text()+ # The shell string contains escaped quotes, so the extraction must consume+ # `\"` pairs rather than stopping at the first quote it meets.+ # Deliberately does NOT require the pattern to begin with SECRET_KEY:+ # a change that prefixes it with a boundary group must still be extracted,+ # so the assertions below fail on the narrowing itself rather than erroring+ # out here and hiding it.+ matches = re.findall(+ r'grep (?:-n )?-P "((?:[^"\\]|\\.)*SECRET_KEY(?:[^"\\]|\\.)*)"',+ source,+ )+ # The script greps twice with the same pattern: once to detect, once to+ # report the offending lines. If they ever diverge, the report would point+ # at different lines than the detection fired on.+ assert matches, "no SECRET_KEY grep -P found in the gate script"+ assert len(set(matches)) == 1, (+ f"detection and reporting patterns have diverged: {set(matches)}"+ )+ # Guard against a future edit accidentally reintroducing a prefix+ # allowlist (a boundary group with hardcoded alternatives like+ # `JWT_|FLASK_`) into the pattern. That kind of narrowing is exactly the+ # regression this file exists to catch, and it would otherwise still+ # pass the extraction above.+ pattern = matches[0]+ assert "JWT_" not in pattern and "FLASK_" not in pattern, (+ "detection pattern hardcodes specific prefixes again: "+ f"{pattern!r} -- it must match SECRET_KEY regardless of prefix, "+ "with decoy names excluded via the lookbehind instead"+ )+ # Guard against a future edit reintroducing the whole-line-discard bug:+ # a second, inverted `grep -v` chained after the detection grep. The+ # fix for that bug was to remove stage 2 entirely (fold the exclusion+ # into the single pattern above), not to patch stage 2's aim. Checked+ # at every occurrence of the pattern (both the detection and reporting+ # call sites), not just the first.+ rest_of_line_after_each_match = [+ source[m.end() :].split("\n", 1)[0]+ for m in re.finditer(re.escape(pattern), source)+ ]+ assert not any(+ "grep -v" in rest for rest in rest_of_line_after_each_match+ ), (+ "a `grep -v` immediately follows the SECRET_KEY detection pattern "+ "again -- this is the whole-line-discard shape: a decoy anywhere "+ "on a line would suppress a genuine secret sharing that line. The "+ "exclusion belongs in the single pattern (lookbehind), not a "+ "second, inverted grep over the whole line."+ )+ # The script embeds the pattern in a double-quoted shell string, so its+ # literal quotes arrive backslash-escaped. That is shell syntax, not regex+ # syntax -- `\"` is a valid escape to bash and an error to `re`.+ return pattern.replace('\\"', '"')+++def _suppression() -> re.Pattern[str]:+ """Extract the gate's suppression clause, compiled with the script's flags.++ Read out of the script for the same reason the detection pattern is: a+ restated copy is exactly the drift this file exists to prevent. The clause+ is anchored to the SECRET_KEY block -- the script has a dozen other+ ``grep -iE`` calls, so a bare search for one would pick up an unrelated+ check.+ """+ source = GATE_SCRIPT.read_text()+ block = re.search(+ r'grep -P "(?:[^"\\]|\\.)*SECRET_KEY(?:[^"\\]|\\.)*"[^\n]*\n'+ r"\s*if ! grep -(?P<flags>[A-Za-z]*)E "+ r'"(?P<pattern>(?:[^"\\]|\\.)*)"',+ source,+ )+ assert block, "no suppression clause found in the SECRET_KEY gate block"+ # The script greps case-insensitively; a case-sensitive copy here would+ # exempt fewer files than CI does and fail where CI passes.+ assert "i" in block.group("flags"), (+ "suppression clause is no longer case-insensitive; "+ f"flags are {block.group('flags')!r}"+ )+ return re.compile(block.group("pattern").replace('\\"', '"'), re.IGNORECASE)+++def _run_gate(content: str, tmp_path: Path) -> subprocess.CompletedProcess[str]:+ """Run the actual gate script against a throwaway single-file repo.++ Builds an isolated git repository containing only a minimal+ ``.file-whitelist.txt`` (so the probe file passes the unrelated+ whitelist check without pulling in the real repo's ~100-pattern list)+ and the probe file itself, then invokes the real+ ``file-whitelist-check.sh`` in ``CHECK_ALL_FILES=true`` mode against it+ -- the same mode the release gate uses, reachable without a GitHub+ Actions ``GITHUB_EVENT_NAME``/``GITHUB_BASE_REF`` environment.++ This runs the gate's own bash pipeline end to end (its own detection+ grep, its own ``if``, its own ``set -euo pipefail``) rather than+ reimplementing it in Python or in a separate subprocess pipeline: a+ helper that models the gate's mechanics cannot catch a defect in the+ gate's own mechanics. SIGPIPE/pipefail and whole-line filtering+ failures are not visible to a helper that only replays the gate's+ *patterns*.+ """+ repo = tmp_path / "repo"+ repo.mkdir()+ subprocess.run(["git", "init", "-q"], cwd=repo, check=True)+ (repo / ".file-whitelist.txt").write_text(r"\.py$" + "\n")+ (repo / "probe.py").write_text(content)+ subprocess.run(["git", "add", "probe.py"], cwd=repo, check=True)+ env = dict(os.environ)+ env["CHECK_ALL_FILES"] = "true"+ return subprocess.run(+ ["bash", str(GATE_SCRIPT)],+ cwd=repo,+ env=env,+ capture_output=True,+ text=True,+ )+++def _flags(content: str, tmp_path: Path) -> bool:+ """True if the real gate script reports a Flask secret key for ``content``."""+ result = _run_gate(content, tmp_path)+ return "❌ FLASK SECRET KEY" in result.stdout+++ "line",+ [+ 'SECRET_KEY = "super-secret-flask-value-1"',+ # Prefixed names are real Flask/third-party config keys, not+ # near-misses. The class matters more than any single example: the+ # gate must catch ANY <PREFIX>_SECRET_KEY, not a hardcoded allowlist+ # of prefixes -- that allowlist is exactly the regression this suite+ # exists to prevent.+ 'JWT_SECRET_KEY = "super-secret-jwt-value-123"',+ 'FLASK_SECRET_KEY = "super-secret-flask-value1"',+ # This project's own session-secret env var names -- the single most+ # likely place a real leak would show up, and the pair that a+ # `JWT_`/`FLASK_`-only allowlist is blind to.+ 'LDR_SECRET_KEY = "super-secret-ldr-value-123"',+ 'LDR_BOOTSTRAP_SECRET_KEY = "super-secret-boot-value-1"',+ 'APP_SECRET_KEY = "super-secret-app-value-123"',+ 'DJANGO_SECRET_KEY = "super-secret-django-value1"',+ 'STRIPE_SECRET_KEY = "super-secret-stripe-value1"',+ 'SESSION_SECRET_KEY = "super-secret-sess-value-1"',+ 'MY_SECRET_KEY = "super-secret-my-value-1234"',+ 'CSRF_SECRET_KEY = "super-secret-csrf-value-1"',+ 'OAUTH_SECRET_KEY = "super-secret-oauth-value1"',+ # Mapping form, which the trailing `[^A-Za-z0-9_]*[:=]` admits.+ '"SECRET_KEY": "super-secret-flask-value-1",',+ "SECRET_KEY: str = 'super-secret-flask-value-1'",+ # Attribute and subscript assignment forms. Deliberately not+ # `config["SECRET_KEY"] = ...`: the gate's suppression clause (see+ # `_suppression`) exempts any file containing the literal substring+ # `config[` -- meant for a `config["SECRET_KEY"]` *read* -- and that+ # substring match fires just as well on this *write*, suppressing+ # it too. That is a real, separate gap in the suppression heuristic,+ # not the pipeline bug this file targets, so it is sidestepped here+ # rather than fixed here.+ 'self.SECRET_KEY = "super-secret-flask-value-1"',+ 'settings["SECRET_KEY"] = "super-secret-flask-value-1"',+ # Suffixed names. The original `SECRET_KEY.*=` caught these; a pattern+ # that jumps straight from SECRET_KEY to a non-identifier character+ # does not, which is the same silent narrowing in the other direction.+ 'SECRET_KEY2 = "super-secret-flask-value-1"',+ 'SECRET_KEY_V2 = "super-secret-flask-value-1"',+ 'SECRET_KEYS = "super-secret-flask-value-1"',+ ],+)+def test_hardcoded_secret_is_flagged(line: str, tmp_path: Path) -> None:+ assert _flags(line, tmp_path), f"gate missed a hardcoded secret: {line}"+++ "target",+ [+ 'SECRET_KEYS["active"]',+ "SECRET_KEYS[0]",+ "SECRET_KEY.value",+ 'SECRET_KEYS[scope["primary"]]',+ ],+)+def test_indexed_and_attribute_assignments_are_flagged(target, tmp_path):+ """Decoy exclusions must retain the original assignment-target coverage."""+ result = _run_gate(f'{target} = "synthetic-private-marker-928"\n', tmp_path)+ assert result.returncode == 1, result.stdout + result.stderr+ assert "❌ FLASK SECRET KEY" in result.stdout+ assert "SECURITY REMINDER" in result.stdout++[email protected]("line_count", [150, 3000])+def test_bulk_secret_dump_is_flagged(line_count: int, tmp_path: Path) -> None:+ """A file of many hardcoded secrets must still be flagged.++ Regression test for a fail-open in the gate's own pipeline mechanics,+ not in its pattern: an earlier stage 2 was ``grep -qv -E "<exclude>"``.+ ``-q`` exits the instant it sees the first surviving line, which --+ once stage 1 had enough matching lines queued up behind it -- killed+ stage 1 with SIGPIPE (exit 141) on its next write. Under this script's+ ``set -euo pipefail``, that 141 became the pipeline's exit status, and+ ``set -e`` is suppressed inside an ``if`` condition, so the ``if`` just+ saw a nonzero status and silently treated it as "no match". A single+ hardcoded secret (one line, well under the pipe-buffer threshold) was+ still caught; a bulk credential dump -- the highest-severity case a+ secret-scanning gate exists for -- passed silently. Kept as a+ regression test through the removal of stage 2: the detection+ pipeline changed shape,+ but must still not silently fail open on a large match. This only+ reproduces by driving the real script (see ``_run_gate``): a Python+ reimplementation of the pipeline has no ``-q``, no SIGPIPE, and no+ ``set -e``/``pipefail`` interaction to trigger it.+ """+ content = "\n".join(+ f'SECRET_KEY_{i} = "super-secret-flask-value-{i:04d}"'+ for i in range(line_count)+ )+ result = _run_gate(content, tmp_path)+ assert "❌ FLASK SECRET KEY" in result.stdout+ assert result.returncode == 1, result.stdout + result.stderr+ # Reaching the final message proves reporting did not abort on SIGPIPE.+ assert "SECURITY REMINDER" in result.stdout++[email protected]("error_status", [2, 137])+def test_secret_scanner_errors_fail_the_gate(+ tmp_path, monkeypatch, error_status+):+ """A broken PCRE scan must not be treated as a clean file."""+ real_grep = shutil.which("grep")+ assert real_grep is not None+ bin_dir = tmp_path / "bin"+ bin_dir.mkdir()+ shim = bin_dir / "grep"+ shim.write_text(+ "#!/usr/bin/env bash\n"+ 'for arg in "$@"; do\n'+ ' if [[ "$arg" == "-P" ]]; then\n'+ ' echo "simulated secret scanner failure" >&2\n'+ f" exit {error_status}\n"+ " fi\n"+ "done\n"+ f'exec {shlex.quote(real_grep)} "$@"\n',+ encoding="utf-8",+ )+ shim.chmod(0o755)+ monkeypatch.setenv("PATH", str(bin_dir) + os.pathsep + os.environ["PATH"])++ result = _run_gate(+ 'LDR_SECRET_KEY = "super-secret-ldr-value-123"\n', tmp_path+ )++ assert result.returncode == error_status, result.stdout + result.stderr+ assert "SECRET_KEY scan failed for probe.py" in result.stderr+ assert "All security checks passed!" not in result.stdout+++def test_no_secret_match_keeps_the_gate_green(tmp_path):+ result = _run_gate("value = 1\n", tmp_path)+ assert result.returncode == 0, result.stdout + result.stderr+ assert "All security checks passed!" in result.stdout+++def test_real_secret_with_trailing_decoy_comment_is_flagged(+ tmp_path: Path,+) -> None:+ """A genuine secret must still be flagged when a decoy trails it on the+ same line.++ Regression test for whole-line filtering: the old stage 2,+ ``grep -v -E "(^|[^A-Za-z0-9_])NO[NT]_SECRET_KEY"``, dropped the entire+ matching LINE whenever a decoy identifier appeared anywhere on it -- so+ a trailing ``# NOT_SECRET_KEY ...`` comment silently suppressed a real,+ hardcoded secret earlier on the same line. The fix folds the exclusion+ into the detection pattern itself (a lookbehind right before+ ``SECRET_KEY``), so it can only ever suppress the decoy occurrence, not+ an unrelated real match sharing its line.++ Deliberately avoids the words "example"/"placeholder" in the comment:+ those (along with "os.environ"/"getenv"/"config[") trip the gate's+ separate, file-scoped suppression clause (see `_suppression`), which+ would mask this file's violation for an unrelated reason and defeat the+ point of this test. That suppression-heuristic gap is real but is not+ the pipeline bug this test targets -- sidestepped here the same way the+ `config["SECRET_KEY"] = ...` case is sidestepped above.+ """+ content = 'LDR_SECRET_KEY = "super-secret-ldr-value-123" # NOT_SECRET_KEY marker\n'+ assert _flags(content, tmp_path), (+ "gate missed a real secret with a same-line trailing decoy comment"+ )+++def test_unlike_decoy_comment_is_flagged(tmp_path: Path) -> None:+ """A second, differently-worded same-line decoy case for the same bug.++ ``# unlike NOT_SECRET_KEY`` is the minimal case: nothing but the decoy+ word itself trails the real assignment on the line.+ """+ content = (+ 'SECRET_KEY = "super-secret-flask-value-1" # unlike NOT_SECRET_KEY\n'+ )+ assert _flags(content, tmp_path), (+ "gate missed a real secret with a same-line 'unlike NOT_SECRET_KEY' comment"+ )+++def test_real_secret_and_decoy_on_separate_lines_is_flagged(+ tmp_path: Path,+) -> None:+ """A decoy on one line must not suppress a real secret on another line.++ Sanity check in the other direction from the same-line tests above: even+ under the old whole-line-discard stage 2, a decoy on a *different* line+ only ever dropped that other line, never the real secret's own line -- so+ this case was never broken. It is kept here as a fixed point: the+ lookbehind fix must not regress it while fixing the same-line case.+ """+ content = (+ 'SECRET_KEY = "super-secret-flask-value-1"\n'+ 'NON_SECRET_KEY: Final = "search.fetch.mode"\n'+ )+ assert _flags(content, tmp_path), (+ "gate missed a real secret sharing a file with a decoy on another line"+ )+++ "line",+ [+ # Too short to be a real key.+ 'SECRET_KEY = "short"',+ # No assignment.+ 'log.info("SECRET_KEY rotated after %s", when)',+ # Names that merely CONTAIN "SECRET_KEY" are not all Flask secret+ # keys -- unlike JWT_/FLASK_/LDR_/etc, "NON_"/"NOT_" are negation+ # prefixes, not real credential namespaces. The lookbehind drops+ # exactly these two decoy names while still catching every genuine+ # prefix (see test_hardcoded_secret_is_flagged above).+ 'NON_SECRET_KEY: Final = "search.fetch.mode"',+ 'NOT_SECRET_KEY: Final = "search.fetch.mode"',+ # A decoy alone, as a comment -- must not flag on its own either.+ "# NOT_SECRET_KEY is not a real setting, see docs",+ # No SECRET_KEY-like token on the line at all.+ "x = 1",+ ],+)+def test_benign_line_is_not_flagged(line: str, tmp_path: Path) -> None:+ assert not _flags(line, tmp_path), f"gate over-matched: {line}"+++def test_decoy_alone_is_not_flagged(tmp_path: Path) -> None:+ """A file containing only a decoy identifier must never be flagged.++ Explicit standalone case (distinct from the parametrized benign-line+ cases above) because it is the direct counterpart to+ ``test_real_secret_with_trailing_decoy_comment_is_flagged``: same decoy+ text, but with no real secret anywhere in the file to (incorrectly)+ suppress.+ """+ content = 'NOT_SECRET_KEY: Final = "search.fetch.mode"\n'+ assert not _flags(content, tmp_path), "gate flagged a decoy-only file"+++def test_environment_read_is_suppressed() -> None:+ """A file that also reads the environment is exempt, as the script says."""+ content = 'SECRET_KEY = os.environ["SECRET_KEY"]\n'+ assert _suppression().search(content), (+ "suppression clause no longer matches an os.environ read"+ )+++def test_repository_is_clean_under_the_gate() -> None:+ """No Python file under src/ or tests/ trips the gate today.++ This is what turns the tests above into a real guard: widening the pattern+ is only safe if the repository still passes, and this fails loudly if a+ genuine secret is ever committed under those trees.++ Scope caveat: this walks ``src/`` and ``tests/`` ``*.py`` only, whereas the+ CI gate (``file-whitelist-check.sh`` over ``git ls-files``) scans every+ tracked file of every type. So this can stay green while CI flags a secret+ in a ``.md``/``.yml``/``.js`` or a root/``scripts/`` file -- the safe+ direction (this passing never means CI is more lenient), but it is not a+ complete pre-CI mirror. Broaden the walk if that gap ever matters.+ """+ # Walk the Python source trees directly; the full-script cases above+ # independently exercise tracked-file discovery through git.+ pattern = re.compile(_gate_pattern())+ suppression = _suppression()+ offenders = []+ scanned = 0+ for root in ("src", "tests"):+ for path in sorted((REPO_ROOT / root).rglob("*.py")):+ if "__pycache__" in path.parts:+ continue+ scanned += 1+ text = path.read_text(errors="replace")+ # Per LINE, because that is what grep does. Searching the whole+ # file lets `[^A-Za-z0-9_]*` span newlines and match a SECRET_KEY+ # mention on one line against an unrelated assignment on another.+ # The decoy exclusion is now built into `pattern` itself (the+ # lookbehinds), so a single `pattern.search(line)` is the whole+ # per-line check -- no second, separately-applied exclusion+ # pattern to combine it with.+ if not any(pattern.search(line) for line in text.splitlines()):+ continue+ # Suppression is file-scoped in the script, so it is file-scoped+ # here too.+ if not suppression.search(text):+ offenders.append(str(path.relative_to(REPO_ROOT)))++ # Guard against a walk that silently finds nothing and passes vacuously.+ # A floor, not a census: the tree holds ~2300 .py files today, so 100 is+ # low enough that ordinary pruning will not false-fail and high enough+ # that a broken root or glob will.+ assert scanned > 100, f"only scanned {scanned} files; the walk is broken"+ assert offenders == [], f"files trip the SECRET_KEY gate: {offenders}"