~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
"""Security coverage for the news router's per-user isolation, error
scrubbing and request-boundary input guards (FastAPI port).
The Flask→FastAPI migration deleted ~670 news/notes tests. Four security
guards in ``web/routers/news_flask_api.py`` survived the port *verbatim*
but lost every one of their tests. This file re-establishes them against
the live FastAPI app.
1. ``_is_job_owned_by_user`` (news_flask_api.py:99) — the ONLY thing
scoping APScheduler jobs in ``GET /news/api/scheduler/status`` and
``GET /news/api/scheduler/stats``. At the ADR-0010 snapshot, ``grep -rn
_is_job_owned_by_user tests/`` returned no hits; the helper and route tests
below now pin it. A regression here exposes
other users' job ids, ``last_activity`` timestamps and
credential-presence (``has_password``) — see the ``stats`` handler.
Tested directly *and* through both routes, so the helper and its
wiring cannot regress independently.
2. ``safe_error_message`` (news_flask_api.py:74) — the sole error
scrubber for 38 call sites in this router (CWE-209). It had no direct tests
at the snapshot; this file now supplies them.
3. The SSRF guard ``_reject_custom_endpoint`` (routes: create at :401,
update at :710) and ``_is_valid_uuid`` on ``subscription_id`` (routes:
feed at :292, history at :922). Both are pinned HERE AT THE ROUTE, not
only at the helper — helper-only coverage is exactly what let the
original news SSRF through (the guards lived in a sibling blueprint
nothing routed to).
POLICY NOTE, do not invert it: private IPs and localhost are
DELIBERATELY ALLOWED for ``custom_endpoint`` — that is how users point
LDR at Ollama / LM Studio / vLLM (see
``utilities/url_utils.is_safe_custom_llm_endpoint``). The guard targets
cloud-metadata / link-local addresses and non-HTTP schemes. Every
rejection test below is paired with an acceptance test so a future
"hardening" that blocks 127.0.0.1 fails loudly instead of silently
breaking local LLMs.
VACUITY: the isolation tests never rely on "the list came back empty".
Every one asserts the *positive* control first (the caller CAN see their
own job / session) and only then the negative (they cannot see the other
user's). An empty news DB would pass the negative half by itself.
"""
import ast
import inspect
import pathlib
import uuid
from datetime import datetime, timezone
from unittest.mock import MagicMock
import pytest
from local_deep_research.web.routers import news_flask_api
from local_deep_research.web.routers.news_flask_api import (
_is_job_owned_by_user,
_is_valid_uuid,
safe_error_message,
)
SCHEDULER_TARGET = (
"local_deep_research.scheduler.background.get_background_job_scheduler"
)
GATE_ENV = "LDR_NEWS_SCHEDULER_ALLOW_API_CONTROL"
STATUS = "/news/api/scheduler/status"
STATS = "/news/api/scheduler/stats"
USERS = "/news/api/scheduler/users"
FEED = "/news/api/feed"
SUBSCRIBE = "/news/api/subscribe"
def _history(subscription_id):
return f"/news/api/subscriptions/{subscription_id}/history"
def _subscription(subscription_id):
return f"/news/api/subscriptions/{subscription_id}"
# ---------------------------------------------------------------------------
# Two-real-user harness
# ---------------------------------------------------------------------------
def _fresh_client(app):
"""A TestClient with its own X-Forwarded-For.
Rate limiting is keyed on the client IP (``rate_limit._get_client_ip``,
which trusts X-Forwarded-For from the TestClient sentinel peer). Two
clients sharing an IP would share ``/auth/register``'s per-IP
bucket and the second registration would 429.
"""
from fastapi.testclient import TestClient
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}.11"})
return client
def _csrf(client):
"""CSRF is enforced by ASGI middleware — fetch a real token."""
client.get("/auth/login")
return client.get("/auth/csrf-token").json()["csrf_token"]
def _register_and_login(app, username):
password = "SchedIso!Pass123" # noqa: S105 — test-only credential
client = _fresh_client(app)
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 in (200, 302), (
f"registration of {username!r} failed: "
f"{resp.status_code} / {resp.text[:400]}"
)
resp = client.post(
"/auth/login",
data={
"username": username,
"password": password,
"csrf_token": _csrf(client),
},
follow_redirects=False,
)
assert resp.status_code in (200, 302), (
f"login of {username!r} failed: {resp.status_code} / {resp.text[:400]}"
)
token = client.get("/auth/csrf-token").json()["csrf_token"]
client.headers.update({"X-CSRFToken": token})
whoami = client.get("/auth/check")
assert whoami.status_code == 200 and whoami.json().get("username") == (
username
), f"session did not bind to {username!r}: {whoami.text[:300]}"
return client
@pytest.fixture
def prefix_users(app):
"""Two real, logged-in users whose names collide by PREFIX.
``alice`` is a strict string prefix of ``alice_bob``. This is the
shape ``test_prefix_collision_prevented`` guarded on main: any
ownership check that degrades from ``==`` to ``startswith`` /
``in`` hands every one of ``alice_bob``'s jobs to ``alice``.
"""
stem = uuid.uuid4().hex[:8]
alice = f"alice{stem}"
alice_bob = f"alice{stem}_bob"
assert alice_bob.startswith(alice), "the fixture must set up a prefix pair"
return {
"alice": alice,
"alice_bob": alice_bob,
"alice_client": _register_and_login(app, alice),
"alice_bob_client": _register_and_login(app, alice_bob),
}
# ---------------------------------------------------------------------------
# Scheduler mock
# ---------------------------------------------------------------------------
ALICE_JOB = "job_alice_sub_1"
ALICE_JOB_2 = "job_alice_sub_2"
OTHER_JOB = "job_other_sub_1"
SYSTEM_JOB = "cleanup_inactive_users"
# Distinct, greppable timestamps so "did user B's last_activity leak into
# user A's response?" can be asserted against the raw response text.
ALICE_SEEN = datetime(2024, 3, 1, 10, 15, 0, tzinfo=timezone.utc)
OTHER_SEEN = datetime(2019, 7, 4, 23, 59, 58, tzinfo=timezone.utc)
def _job(job_id, args):
job = MagicMock()
job.id = job_id
job.name = f"name-of-{job_id}"
job.args = args
job.next_run_time = None
job.trigger = f"interval[trigger-of-{job_id}]"
return job
def _scheduler(owner, other):
"""A running scheduler holding jobs + sessions for two users.
``owner`` and ``other`` are usernames. Every user-scoped field is
populated for BOTH so the isolation assertions have something real to
filter: the negative half of each test would pass trivially against
an empty scheduler.
"""
scheduler = MagicMock()
scheduler.is_running = True
scheduler.config = {"check_interval": 300}
scheduler.user_sessions = {
owner: {
"scheduled_jobs": {ALICE_JOB, ALICE_JOB_2},
"last_activity": ALICE_SEEN,
},
other: {
"scheduled_jobs": {OTHER_JOB},
"last_activity": OTHER_SEEN,
},
}
scheduler._credential_store.retrieve.side_effect = lambda user: (
"stored-password" if user == other else None
)
scheduler.get_user_sessions_summary.return_value = [
{"user_id": owner, "scheduled_jobs": 2},
{"user_id": other, "scheduled_jobs": 1},
]
scheduler.scheduler.get_jobs.return_value = [
_job(ALICE_JOB, (owner, 1)),
_job(ALICE_JOB_2, (owner, 2)),
_job(OTHER_JOB, (other, 1)),
_job(SYSTEM_JOB, ()),
]
return scheduler
def _patch_scheduler(monkeypatch, scheduler):
monkeypatch.setattr(
SCHEDULER_TARGET, lambda *a, **kw: scheduler, raising=True
)
def _spy_on_ownership(monkeypatch):
"""Wrap the real ``_is_job_owned_by_user`` so route wiring is provable.
Returns the list of ``(job_id, username)`` pairs the route asks about.
Without this, a route that dropped the filter entirely but happened to
return the right jobs (e.g. because the mock only held one user) would
still pass.
"""
calls = []
real = _is_job_owned_by_user
def _spy(job, username, scheduler):
calls.append((job.id, username))
return real(job, username, scheduler)
monkeypatch.setattr(news_flask_api, "_is_job_owned_by_user", _spy)
return calls
# ===========================================================================
# COVERAGE AREA 1a — _is_job_owned_by_user, direct
# ===========================================================================
class TestIsJobOwnedByUserHelper:
"""Direct unit coverage of the ownership predicate.
All news scheduler jobs pass the username as ``args[0]``; the
``user_sessions[username]["scheduled_jobs"]`` set is the fallback for
jobs enqueued without it.
"""
@staticmethod
def _no_sessions_scheduler():
# spec=[] => hasattr(scheduler, "user_sessions") is False, forcing
# the args-only path. A bare MagicMock would auto-create the
# attribute and silently exercise the fallback instead.
return MagicMock(spec=[])
def test_owner_matches_via_job_args(self):
job = _job("alice_sub_1", ("alice", 1))
assert (
_is_job_owned_by_user(job, "alice", self._no_sessions_scheduler())
is True
)
def test_other_users_job_does_not_match(self):
job = _job("bob_sub_1", ("bob", 1))
assert (
_is_job_owned_by_user(job, "alice", self._no_sessions_scheduler())
is False
)
def test_prefix_collision_prevented(self):
"""``alice`` must NOT match ``alice_bob``'s job.
The subtle one. Ownership is exact equality on both paths; a
regression to ``startswith`` / ``in`` / a ``job.id`` prefix test
hands every ``alice_*`` job to ``alice``. Both the args path and
the scheduled_jobs fallback are pinned, because the fallback keys
by ``job.id`` — which really does start with the other username.
"""
job = _job("alice_bob_sub_42", ("alice_bob", 42))
assert (
_is_job_owned_by_user(job, "alice", self._no_sessions_scheduler())
is False
), "'alice' must not own a job whose args name 'alice_bob'"
scheduler = MagicMock()
scheduler.user_sessions = {
"alice": {"scheduled_jobs": {"alice_sub_1"}},
"alice_bob": {"scheduled_jobs": {"alice_bob_sub_42"}},
}
assert _is_job_owned_by_user(job, "alice", scheduler) is False, (
"the scheduled_jobs fallback must key on exact set membership "
"for THIS user, not on the job id sharing alice's prefix"
)
# Positive control: the same scheduler DOES grant alice her own job,
# so the assertion above is not passing because the lookup is broken.
own = _job("alice_sub_1", ())
assert _is_job_owned_by_user(own, "alice", scheduler) is True
def test_reverse_prefix_collision_prevented(self):
"""And the other direction: ``alice_bob`` must not own ``alice``'s."""
job = _job("alice_sub_1", ("alice", 1))
assert (
_is_job_owned_by_user(
job, "alice_bob", self._no_sessions_scheduler()
)
is False
)
def test_match_via_scheduled_jobs_fallback(self):
job = _job("some_job_id", ())
scheduler = MagicMock()
scheduler.user_sessions = {"alice": {"scheduled_jobs": {"some_job_id"}}}
assert _is_job_owned_by_user(job, "alice", scheduler) is True
def test_system_job_is_owned_by_nobody(self):
job = _job(SYSTEM_JOB, ())
scheduler = MagicMock()
scheduler.user_sessions = {"alice": {"scheduled_jobs": set()}}
assert _is_job_owned_by_user(job, "alice", scheduler) is False
def test_job_without_args_attribute_is_not_owned(self):
job = MagicMock(spec=["id"])
job.id = "no_args_job"
scheduler = MagicMock(spec=[])
assert _is_job_owned_by_user(job, "alice", scheduler) is False
def test_empty_args_falls_through_to_fallback(self):
job = _job("empty_args_job", ())
scheduler = MagicMock()
scheduler.user_sessions = {"alice": {}}
assert _is_job_owned_by_user(job, "alice", scheduler) is False
def test_unknown_user_owns_nothing(self):
job = _job(ALICE_JOB, ("alice", 1))
scheduler = MagicMock()
scheduler.user_sessions = {
"alice": {"scheduled_jobs": {ALICE_JOB}},
}
assert _is_job_owned_by_user(job, "charlie", scheduler) is False
# ===========================================================================
# COVERAGE AREA 1b — the two route call sites
# ===========================================================================
class TestSchedulerStatusIsolation:
"""``GET /news/api/scheduler/status`` — scoped by default."""
def test_caller_sees_own_jobs_and_not_the_other_users(
self, prefix_users, monkeypatch
):
"""Positive control FIRST, then the isolation assertion.
Without the positive half this test would pass against an empty
scheduler, proving nothing.
"""
monkeypatch.delenv(GATE_ENV, raising=False)
alice, other = prefix_users["alice"], prefix_users["alice_bob"]
_patch_scheduler(monkeypatch, _scheduler(alice, other))
resp = prefix_users["alice_client"].get(STATUS)
assert resp.status_code == 200, resp.text
body = resp.json()
# --- positive control: alice's own state IS reported ---
assert body["active_users"] == 1
assert body["total_scheduled_jobs"] == 2
assert body["scheduled_jobs"] == 2
job_ids = [j["id"] for j in body.get("apscheduler_jobs", [])]
assert set(job_ids) == {ALICE_JOB, ALICE_JOB_2}, (
f"alice must see exactly her own two jobs, got {job_ids}"
)
assert body["apscheduler_job_count"] == 2
# --- isolation: nothing belonging to the other user, and no
# system job either ---
assert OTHER_JOB not in job_ids
assert SYSTEM_JOB not in job_ids
assert other not in resp.text, (
"the other user's username must not appear anywhere in the "
f"scoped status body: {resp.text[:500]}"
)
def test_isolation_holds_symmetrically_for_the_other_user(
self, prefix_users, monkeypatch
):
"""The prefix pair, driven from the *other* side, at the route.
``alice_bob`` is the scheduler's "other" user here, so this also
pins that a caller whose name merely *contains* another user's
name gets only their own row.
"""
monkeypatch.delenv(GATE_ENV, raising=False)
alice, other = prefix_users["alice"], prefix_users["alice_bob"]
_patch_scheduler(monkeypatch, _scheduler(alice, other))
resp = prefix_users["alice_bob_client"].get(STATUS)
assert resp.status_code == 200, resp.text
body = resp.json()
assert body["active_users"] == 1
assert body["total_scheduled_jobs"] == 1
job_ids = [j["id"] for j in body.get("apscheduler_jobs", [])]
assert job_ids == [OTHER_JOB], (
f"alice_bob must see exactly his own job, got {job_ids}"
)
assert ALICE_JOB not in job_ids
assert ALICE_JOB_2 not in job_ids
def test_route_actually_calls_the_ownership_filter(
self, prefix_users, monkeypatch
):
"""Wiring pin: the helper is consulted for every candidate job,
always with the *authenticated* username."""
monkeypatch.delenv(GATE_ENV, raising=False)
alice, other = prefix_users["alice"], prefix_users["alice_bob"]
_patch_scheduler(monkeypatch, _scheduler(alice, other))
calls = _spy_on_ownership(monkeypatch)
resp = prefix_users["alice_client"].get(STATUS)
assert resp.status_code == 200, resp.text
assert calls, (
"GET /news/api/scheduler/status returned jobs without ever "
"calling _is_job_owned_by_user — the per-user filter is unwired"
)
assert {job_id for job_id, _ in calls} == {
ALICE_JOB,
ALICE_JOB_2,
OTHER_JOB,
SYSTEM_JOB,
}
assert {user for _, user in calls} == {alice}, (
"the filter must be applied with the session username only"
)
def test_show_all_reveals_every_user_when_api_control_enabled(
self, prefix_users, monkeypatch
):
"""The opt-in ``show_all`` branch. Pinned so the scoped default
above cannot be 'fixed' by hard-coding the filter on, and so the
gate stays the only thing that widens visibility."""
monkeypatch.setenv(GATE_ENV, "true")
alice, other = prefix_users["alice"], prefix_users["alice_bob"]
_patch_scheduler(monkeypatch, _scheduler(alice, other))
resp = prefix_users["alice_client"].get(STATUS)
assert resp.status_code == 200, resp.text
body = resp.json()
assert body["active_users"] == 2
assert body["total_scheduled_jobs"] == 3
job_ids = {j["id"] for j in body.get("apscheduler_jobs", [])}
assert job_ids == {ALICE_JOB, ALICE_JOB_2, OTHER_JOB, SYSTEM_JOB}
def test_gate_defaults_to_scoped_when_env_is_unset(
self, prefix_users, monkeypatch
):
"""Fail-closed default: no env var means scoped, not show_all."""
monkeypatch.delenv(GATE_ENV, raising=False)
alice, other = prefix_users["alice"], prefix_users["alice_bob"]
_patch_scheduler(monkeypatch, _scheduler(alice, other))
body = prefix_users["alice_client"].get(STATUS).json()
assert body["active_users"] == 1, (
"with the scheduler-control gate unset the status endpoint must "
"scope to the caller"
)
def test_user_with_no_session_sees_zeros(self, prefix_users, monkeypatch):
"""A caller absent from ``user_sessions`` gets zeros, not everyone."""
monkeypatch.delenv(GATE_ENV, raising=False)
# Neither key is the caller: alice is authenticated but the
# scheduler only knows two strangers.
_patch_scheduler(monkeypatch, _scheduler("stranger_a", "stranger_b"))
resp = prefix_users["alice_client"].get(STATUS)
assert resp.status_code == 200, resp.text
body = resp.json()
assert body["active_users"] == 0
assert body["total_scheduled_jobs"] == 0
assert body["scheduled_jobs"] == 0
assert body.get("apscheduler_jobs", []) == []
class TestSchedulerStatsIsolation:
"""``GET /news/api/scheduler/stats`` — the higher-value leak.
This handler exposes, per user, ``has_password`` (whether a scheduler
credential is stored for them), ``last_activity`` and a job count.
"""
def test_stats_scoped_to_caller_and_leaks_no_credential_presence(
self, prefix_users, monkeypatch
):
monkeypatch.delenv(GATE_ENV, raising=False)
alice, other = prefix_users["alice"], prefix_users["alice_bob"]
_patch_scheduler(monkeypatch, _scheduler(alice, other))
resp = prefix_users["alice_client"].get(STATS)
assert resp.status_code == 200, resp.text
body = resp.json()
# --- positive control ---
assert body["current_user"] == alice
assert alice in body["user_sessions"], (
"alice must see her own session row — otherwise the negative "
"assertions below are vacuous"
)
assert body["user_sessions"][alice]["scheduled_jobs_count"] == 2
assert body["user_sessions"][alice]["last_activity"] == (
ALICE_SEEN.isoformat()
)
job_ids = [j["id"] for j in body["apscheduler_jobs"]]
assert set(job_ids) == {ALICE_JOB, ALICE_JOB_2}
# --- isolation ---
assert list(body["user_sessions"]) == [alice], (
f"only the caller's session may be reported, got "
f"{list(body['user_sessions'])}"
)
assert other not in body["user_sessions"]
assert OTHER_JOB not in job_ids
assert SYSTEM_JOB not in job_ids
# The three fields a leak would expose, checked against raw text so
# a rename of the containing key cannot hide them.
assert OTHER_SEEN.isoformat() not in resp.text, (
"another user's last_activity timestamp leaked"
)
assert other not in resp.text, (
"another user's username leaked into the scoped stats body"
)
assert "stored-password" not in resp.text
def test_stats_route_calls_the_ownership_filter(
self, prefix_users, monkeypatch
):
"""Wiring pin for the second call site, independent of the first."""
monkeypatch.delenv(GATE_ENV, raising=False)
alice, other = prefix_users["alice"], prefix_users["alice_bob"]
_patch_scheduler(monkeypatch, _scheduler(alice, other))
calls = _spy_on_ownership(monkeypatch)
resp = prefix_users["alice_client"].get(STATS)
assert resp.status_code == 200, resp.text
assert calls, (
"GET /news/api/scheduler/stats returned jobs without ever "
"calling _is_job_owned_by_user — the per-user filter is unwired"
)
assert {job_id for job_id, _ in calls} == {
ALICE_JOB,
ALICE_JOB_2,
OTHER_JOB,
SYSTEM_JOB,
}
assert {user for _, user in calls} == {alice}
def test_stats_show_all_reveals_every_user_when_enabled(
self, prefix_users, monkeypatch
):
monkeypatch.setenv(GATE_ENV, "true")
alice, other = prefix_users["alice"], prefix_users["alice_bob"]
_patch_scheduler(monkeypatch, _scheduler(alice, other))
resp = prefix_users["alice_client"].get(STATS)
assert resp.status_code == 200, resp.text
body = resp.json()
assert set(body["user_sessions"]) == {alice, other}
# has_password is real per-user credential-presence data — exactly
# what the scoped default must withhold.
assert body["user_sessions"][other]["has_password"] is True
assert body["user_sessions"][alice]["has_password"] is False
assert {j["id"] for j in body["apscheduler_jobs"]} == {
ALICE_JOB,
ALICE_JOB_2,
OTHER_JOB,
SYSTEM_JOB,
}
class TestSchedulerActiveUsersIsolation:
"""``GET /news/api/scheduler/users`` — the third ``show_all`` filter."""
def test_users_summary_scoped_to_caller(self, prefix_users, monkeypatch):
monkeypatch.delenv(GATE_ENV, raising=False)
alice, other = prefix_users["alice"], prefix_users["alice_bob"]
_patch_scheduler(monkeypatch, _scheduler(alice, other))
resp = prefix_users["alice_client"].get(USERS)
assert resp.status_code == 200, resp.text
body = resp.json()
# --- positive control: alice's own row survives the filter ---
assert body["active_users"] == 1
assert [u["user_id"] for u in body["users"]] == [alice]
# --- isolation ---
assert other not in resp.text
def test_users_summary_scoped_symmetrically(
self, prefix_users, monkeypatch
):
"""The prefix-pair direction: ``alice_bob`` gets only his row even
though ``alice`` is a substring of his name."""
monkeypatch.delenv(GATE_ENV, raising=False)
alice, other = prefix_users["alice"], prefix_users["alice_bob"]
_patch_scheduler(monkeypatch, _scheduler(alice, other))
body = prefix_users["alice_bob_client"].get(USERS).json()
assert body["active_users"] == 1
assert [u["user_id"] for u in body["users"]] == [other]
def test_users_summary_shows_all_when_enabled(
self, prefix_users, monkeypatch
):
monkeypatch.setenv(GATE_ENV, "true")
alice, other = prefix_users["alice"], prefix_users["alice_bob"]
_patch_scheduler(monkeypatch, _scheduler(alice, other))
body = prefix_users["alice_client"].get(USERS).json()
assert body["active_users"] == 2
assert {u["user_id"] for u in body["users"]} == {alice, other}
# ===========================================================================
# COVERAGE AREA 2 — safe_error_message (CWE-209)
# ===========================================================================
# Each secret is something a real exception in these handlers can carry.
SECRETS = {
"filesystem_path": "/home/victim/.config/ldr/encrypted_databases/victim.db",
"credential": "sk-proj-AAAABBBBCCCCDDDDEEEEFFFF",
"connection_string": "postgresql://ldr_admin:[email protected]:5432/ldr",
"sql_fragment": (
"SELECT password_hash FROM users WHERE username = 'victim'"
),
}
class _WrappedValueError(ValueError):
"""A domain exception subclassing ValueError, as news.api raises."""
class TestSafeErrorMessageRedaction:
"""``safe_error_message`` must never echo the exception.
Policy is derived from the implementation (news_flask_api.py:74), not
invented: the exception's own text is logged and then *discarded*;
the returned string is one of four fixed forms, the only variable
part being the caller-supplied ``context``.
"""
@pytest.mark.parametrize("label,secret", sorted(SECRETS.items()))
@pytest.mark.parametrize(
"exc_type",
[ValueError, KeyError, TypeError, RuntimeError, Exception],
ids=["value", "key", "type", "runtime", "generic"],
)
def test_exception_payload_never_reaches_the_client(
self, exc_type, label, secret
):
message = safe_error_message(exc_type(secret), "creating subscription")
assert secret not in message, (
f"{exc_type.__name__} carrying a {label} leaked it into the "
f"client-facing message: {message!r}"
)
# Substring check too: a partial echo is still a leak.
for fragment in ("victim", "S3cr3tP4ss", "password_hash", "sk-proj"):
assert fragment not in message, (
f"fragment {fragment!r} from the {label} survived into "
f"{message!r}"
)
def test_exception_class_name_is_not_disclosed(self):
class SqlalchemyOperationalError(Exception):
pass
message = safe_error_message(
SqlalchemyOperationalError(SECRETS["connection_string"]),
"getting subscription history",
)
assert "SqlalchemyOperationalError" not in message
assert "Traceback" not in message
assert 'File "' not in message
def test_stack_trace_is_not_disclosed(self):
"""A raised-and-caught exception must contribute no frame info."""
try:
raise RuntimeError(SECRETS["filesystem_path"])
except RuntimeError as exc:
message = safe_error_message(exc, "running subscription")
assert SECRETS["filesystem_path"] not in message
assert "Traceback" not in message
assert __file__ not in message
assert "test_stack_trace_is_not_disclosed" not in message
def test_messages_are_still_useful(self):
"""Redaction must not degrade into an empty or uniform string —
the caller still learns the class of failure and what failed."""
assert (
safe_error_message(ValueError("x"), "creating subscription")
== "Invalid input provided"
)
assert (
safe_error_message(KeyError("x"), "creating subscription")
== "Required data missing"
)
assert (
safe_error_message(TypeError("x"), "creating subscription")
== "Invalid data format"
)
assert (
safe_error_message(RuntimeError("x"), "creating subscription")
== "An error occurred while creating subscription"
)
assert safe_error_message(RuntimeError("x")) == "An error occurred"
assert safe_error_message(RuntimeError("x"), "") == "An error occurred"
def test_value_error_subclasses_take_the_scrubbed_branch(self):
"""news.api raises ValueError subclasses; isinstance, not type()."""
message = safe_error_message(
_WrappedValueError(SECRETS["credential"]), "updating subscription"
)
assert message == "Invalid input provided"
assert SECRETS["credential"] not in message
def test_returns_a_plain_nonempty_string(self):
for exc in (
ValueError("v"),
KeyError("k"),
TypeError("t"),
Exception("e"),
):
message = safe_error_message(exc, "getting scheduler stats")
assert isinstance(message, str) and message.strip()
def test_only_the_developer_supplied_context_is_interpolated(self):
"""The ONE variable channel in the output is ``context``.
That is safe only for as long as every call site passes a string
literal. Pinned statically so a future refactor that threads a
request value (a query, a subscription id, a username) through
``context`` re-opens the leak this function exists to close.
"""
source = pathlib.Path(inspect.getsourcefile(news_flask_api)).read_text()
tree = ast.parse(source)
calls = []
for node in ast.walk(tree):
if (
isinstance(node, ast.Call)
and isinstance(node.func, ast.Name)
and node.func.id == "safe_error_message"
):
calls.append(node)
assert len(calls) >= 20, (
f"expected safe_error_message to still be the router's shared "
f"scrubber, found only {len(calls)} call sites"
)
offenders = []
for node in calls:
context = None
if len(node.args) > 1:
context = node.args[1]
for keyword in node.keywords:
if keyword.arg == "context":
context = keyword.value
if context is not None and not isinstance(context, ast.Constant):
offenders.append(node.lineno)
assert offenders == [], (
"safe_error_message(context=...) must be a string literal at "
"every call site; non-literal context at line(s) "
f"{offenders} can interpolate request data into a message that "
"exists precisely to withhold it"
)
# ===========================================================================
# COVERAGE AREA 3 — endpoint and subscription validation at the route
# ===========================================================================
# Rejected: cloud-metadata / link-local targets and non-HTTP schemes.
BLOCKED_ENDPOINTS = [
"http://169.254.169.254/latest/meta-data/",
"http://169.254.169.254:80/latest/meta-data/iam/security-credentials/",
"http://[fd00:ec2::254]/latest/meta-data/",
"file:///etc/passwd",
"gopher://127.0.0.1:11211/_stats",
"ftp://169.254.169.254/",
]
# ALLOWED ON PURPOSE — this is how users reach Ollama / LM Studio / vLLM.
# A change that starts rejecting these does not harden the feature, it
# removes it. Keep these in lockstep with
# utilities/url_utils.is_safe_custom_llm_endpoint's docstring.
ALLOWED_ENDPOINTS = [
"http://127.0.0.1:11434",
"http://localhost:11434/v1",
"http://192.168.1.10:8000",
"http://10.0.0.5:1234/v1",
"localhost:11434",
"https://api.openai.com/v1",
]
SSRF_ERROR = "Invalid custom endpoint URL"
class TestCustomEndpointSsrfGuardAtCreateRoute:
"""``POST /news/api/subscribe`` → ``_reject_custom_endpoint_async``."""
@pytest.mark.parametrize("endpoint", BLOCKED_ENDPOINTS)
def test_metadata_and_non_http_endpoints_are_rejected(
self, authenticated_client, endpoint
):
resp = authenticated_client.post(
SUBSCRIBE,
json={"query": "ssrf guard probe", "custom_endpoint": endpoint},
)
assert resp.status_code == 400, (
f"{endpoint!r} must be rejected at the request boundary, got "
f"{resp.status_code}: {resp.text[:300]}"
)
body = resp.json()
assert body.get("success") is False
assert body.get("error") == SSRF_ERROR
@pytest.mark.parametrize("endpoint", ALLOWED_ENDPOINTS)
def test_local_llm_endpoints_are_still_accepted(
self, authenticated_client, endpoint
):
"""Private IPs and localhost MUST keep working — see module
docstring. This is the guard-rail against over-blocking."""
resp = authenticated_client.post(
SUBSCRIBE,
json={
"query": f"local llm probe {uuid.uuid4().hex[:6]}",
"custom_endpoint": endpoint,
},
)
assert resp.status_code == 200, (
f"{endpoint!r} is a legitimate local-LLM endpoint and must not "
f"be blocked, got {resp.status_code}: {resp.text[:300]}"
)
assert resp.json().get("error") != SSRF_ERROR
def test_absent_custom_endpoint_is_not_treated_as_hostile(
self, authenticated_client
):
"""Positive control for the common case: no endpoint at all."""
resp = authenticated_client.post(
SUBSCRIBE, json={"query": "no endpoint at all"}
)
assert resp.status_code == 200, resp.text
assert resp.json()["status"] == "success"
def test_rejection_happens_before_any_subscription_is_created(
self, authenticated_client, monkeypatch
):
"""Fail-fast: no DB row, no research thread. Pinned by asserting
``api.create_subscription`` is never reached."""
called = []
monkeypatch.setattr(
"local_deep_research.news.api.create_subscription",
lambda *a, **kw: called.append((a, kw)),
)
resp = authenticated_client.post(
SUBSCRIBE,
json={
"query": "must not persist",
"custom_endpoint": "http://169.254.169.254/latest/meta-data/",
},
)
assert resp.status_code == 400
assert called == [], (
"the SSRF guard must run before api.create_subscription — a "
"hostile endpoint reached persistence"
)
class TestCustomEndpointSsrfGuardAtUpdateRoute:
"""``PUT /news/api/subscriptions/{id}`` → same guard, second call site.
The guard runs before the subscription is looked up, so a nonexistent
id still yields the SSRF 400 for a hostile endpoint — and a *different*
outcome for a benign one, which is what makes the pairing meaningful.
"""
@pytest.mark.parametrize("endpoint", BLOCKED_ENDPOINTS)
def test_metadata_and_non_http_endpoints_are_rejected(
self, authenticated_client, endpoint
):
resp = authenticated_client.put(
_subscription(str(uuid.uuid4())),
json={"custom_endpoint": endpoint},
)
assert resp.status_code == 400, (
f"{endpoint!r} must be rejected on update too, got "
f"{resp.status_code}: {resp.text[:300]}"
)
assert resp.json().get("error") == SSRF_ERROR
@pytest.mark.parametrize("endpoint", ALLOWED_ENDPOINTS)
def test_local_llm_endpoints_pass_the_guard_on_update(
self, authenticated_client, endpoint
):
"""A benign endpoint must get PAST the guard. Proven by the
response being anything other than the SSRF 400 body — here the
unknown-subscription failure from further down the handler."""
resp = authenticated_client.put(
_subscription(str(uuid.uuid4())),
json={"custom_endpoint": endpoint},
)
body = (
resp.json()
if resp.headers.get("content-type", "").startswith(
"application/json"
)
else {}
)
assert body.get("error") != SSRF_ERROR, (
f"{endpoint!r} is a legitimate local-LLM endpoint and must not "
f"be blocked on update: {resp.text[:300]}"
)
def test_hostile_endpoint_never_reaches_the_update_call(
self, authenticated_client, monkeypatch
):
called = []
monkeypatch.setattr(
"local_deep_research.news.api.update_subscription",
lambda *a, **kw: called.append((a, kw)),
)
resp = authenticated_client.put(
_subscription(str(uuid.uuid4())),
json={"custom_endpoint": "file:///etc/passwd"},
)
assert resp.status_code == 400
assert called == [], (
"the SSRF guard must run before api.update_subscription"
)
# ``_is_valid_uuid`` exists to stop these reaching the LIKE-pattern
# queries in news/api.py: ``%`` and ``_`` are LIKE wildcards, so an
# unvalidated id is a subscription-enumeration vector.
INVALID_SUBSCRIPTION_IDS = [
"%",
"%%",
"_",
"sub123",
"null",
"undefined",
"' OR '1'='1",
"'; DROP TABLE news_subscriptions; --",
"../../etc/passwd",
"00000000-0000-0000-0000-00000000000", # one char short
]
class TestSubscriptionIdValidationAtRoutes:
"""``_is_valid_uuid`` at both wired call sites."""
@pytest.mark.parametrize("bad_id", INVALID_SUBSCRIPTION_IDS)
def test_feed_rejects_non_uuid_subscription_id(
self, authenticated_client, bad_id
):
resp = authenticated_client.get(
FEED, params={"subscription_id": bad_id}
)
assert resp.status_code == 400, (
f"GET /news/api/feed?subscription_id={bad_id!r} must be a 400, "
f"got {resp.status_code}: {resp.text[:300]}"
)
body = resp.json()
assert body.get("success") is False
assert body.get("error") == "Invalid subscription_id"
def test_feed_accepts_a_well_formed_uuid(self, authenticated_client):
"""Positive control — the guard must not reject valid ids, or the
parametrized rejections above prove nothing."""
resp = authenticated_client.get(
FEED, params={"subscription_id": str(uuid.uuid4())}
)
assert resp.status_code == 200, resp.text
assert resp.json().get("error") != "Invalid subscription_id"
def test_feed_without_subscription_id_is_unaffected(
self, authenticated_client
):
resp = authenticated_client.get(FEED)
assert resp.status_code == 200, resp.text
assert resp.json().get("error") != "Invalid subscription_id"
@pytest.mark.parametrize("bad_id", INVALID_SUBSCRIPTION_IDS)
def test_history_rejects_non_uuid_subscription_id(
self, authenticated_client, bad_id
):
# Path segments containing "/" are normalised by the HTTP client
# before they ever reach Starlette's router; those ids are covered
# by the query-param variant above.
if "/" in bad_id:
pytest.skip("multi-segment id cannot reach this path route")
resp = authenticated_client.get(_history(bad_id))
assert resp.status_code == 400, (
f"GET {_history(bad_id)} must be a 400, got {resp.status_code}: "
f"{resp.text[:300]}"
)
body = resp.json()
assert body.get("success") is False
assert body.get("error") == "Invalid subscription_id"
def test_history_rejection_precedes_the_api_call(
self, authenticated_client, monkeypatch
):
called = []
monkeypatch.setattr(
"local_deep_research.news.api.get_subscription_history",
lambda *a, **kw: called.append((a, kw)),
)
resp = authenticated_client.get(_history("%"))
assert resp.status_code == 400
assert called == [], (
"a LIKE wildcard reached api.get_subscription_history — the "
"_is_valid_uuid guard is not wired ahead of it"
)
def test_history_accepts_a_well_formed_uuid(self, authenticated_client):
"""Positive control: a valid (but unknown) uuid must fail as
NOT-FOUND, never as invalid-input."""
resp = authenticated_client.get(_history(str(uuid.uuid4())))
assert resp.status_code != 400, (
f"a well-formed uuid must clear the validator: {resp.text[:300]}"
)
if resp.headers.get("content-type", "").startswith("application/json"):
assert resp.json().get("error") != "Invalid subscription_id"
class TestIsValidUuidHelper:
"""Direct coverage, so a route-level pass cannot be produced by an
unrelated 400 further up the handler."""
@pytest.mark.parametrize("bad_id", INVALID_SUBSCRIPTION_IDS)
def test_rejects_hostile_ids(self, bad_id):
assert _is_valid_uuid(bad_id) is False
@pytest.mark.parametrize("bad_id", [None, "", [], {}, 0])
def test_rejects_non_string_and_empty(self, bad_id):
assert _is_valid_uuid(bad_id) is False
def test_accepts_canonical_and_hyphenless_uuids(self):
value = uuid.uuid4()
assert _is_valid_uuid(str(value)) is True
assert _is_valid_uuid(value.hex) is True
# ===========================================================================
# Shared: none of the above endpoints may be reached anonymously
# ===========================================================================
class TestScopedEndpointsRequireAuth:
"""Isolation is worthless if the endpoint answers anonymous callers."""
@pytest.mark.parametrize("path", [STATUS, STATS, USERS])
def test_scheduler_read_endpoints_require_auth(
self, client, path, monkeypatch
):
monkeypatch.setenv(GATE_ENV, "true")
touched = []
monkeypatch.setattr(
SCHEDULER_TARGET, lambda *a, **kw: touched.append(1)
)
resp = client.get(path, follow_redirects=False)
assert resp.status_code in (401, 302), (
f"{path} answered an unauthenticated caller with {resp.status_code}"
)
assert touched == [], (
f"{path} resolved the scheduler singleton before authenticating"
)