~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
"""ADR-0010 rows 32-33 — scheduler-control authorisation, and the news
rate-limit values / hostile-body coverage the sibling security files do not
reach.
READ FIRST, not duplicated here:
- ``tests/security/test_news_scheduler_isolation_fastapi.py`` already covers
per-user scheduler isolation (``_is_job_owned_by_user``), ``safe_error_
message``, the custom-endpoint SSRF reject path, ``_is_valid_uuid`` on
``subscription_id``, and unauthenticated 401 on ``GET
/news/api/scheduler/{status,stats,users}``. None of that is repeated.
- ``tests/news/test_news_input_validation.py`` already drives 8 of the news
router's ``read_json_dict``-guarded routes (create_subscription,
update_subscription, submit_feedback, save_preferences, vote_on_news,
get_batch_feedback, create_folder, add_search_history) plus malformed-JSON
on research_news_item, and the ``NEWS_FEED_MAX_LIMIT`` clamp. Only the two
routes absent from its ``BODY_ROUTES`` table are added here.
- ``tests/web/test_rate_limit_coverage.py`` pins that create_subscription /
submit_feedback / research_news_item / save_preferences ARE registered
with slowapi. It never asserts the rate VALUE — that gap is closed here.
- ``tests/web/routers/test_news_scheduler_check_now.py`` already covers the
403 gate + a successful run for ``POST /news/api/scheduler/check-now``.
That route is the *model* this file follows for ``start``/``stop``/
``cleanup-now``, not duplicated.
ROW 32 — scheduler-control authorisation
-----------------------------------------
``require_scheduler_control`` (news_flask_api.py:39) gates
``POST /news/api/scheduler/{start,stop,check-now,cleanup-now}`` behind the
``news.scheduler.allow_api_control`` setting (env
``LDR_NEWS_SCHEDULER_ALLOW_API_CONTROL``) because the scheduler these routes
control is a PROCESS-WIDE singleton: an unauthorised caller starting or
stopping it affects every user on the server, not just themselves. Before
this file, only ``check-now`` had a test of that gate; ``start``, ``stop``
and ``cleanup-now`` were reachable by anyone authenticated. Each route below
gets the SAME positive/negative pair check-now already has: gate-off -> 403
without ever resolving the scheduler singleton, and gate-on -> the route
actually succeeds. The positive control matters because "every one of these
routes returns 403" would also be true if a route had been renamed to 404,
or if CSRF rejected the request before ``require_scheduler_control`` ever
ran — the gate-on test proves the request shape and route path are correct
independent of the gate.
Also: ``GET /api/scheduler/status`` — the DOCUMENT scheduler in
``web/routers/scheduler.py``, an entirely different router and a different
process-wide singleton from the news scheduler above (same "/api/scheduler"
URL shape is a coincidence, not a shared prefix). It is absent from
``tests/web/routers/test_fastapi_migration.py::PROTECTED_GET_ENDPOINTS``,
and the only file that exercises it
(``tests/research_scheduler/test_scheduler_edge_cases.py``) always uses
``authenticated_client``, so the unauthenticated case was never asserted.
ROW 33 — news rate-limit VALUES + the two missed hostile-body routes
----------------------------------------------------------------------
The four news POST routes' slowapi ``Limit`` objects are read directly off
``news_flask_api.limiter._route_limits`` (the same technique
``test_notes_rate_limit_keys.py::test_bucket_rate_is_pinned`` uses for
notes), so a silent "10 per minute" -> "10 per hour" loosening of
``_news_create_limit`` fails a test instead of shipping unnoticed.
Two more routes call ``read_json_dict``
(``web/dependencies/json_body.py``) exactly like the 8 covered by
``test_news_input_validation.py``, but are not in that file's table:
- ``PUT /news/api/subscription/folders/{folder_id}`` (``update_folder``)
- ``PUT /news/api/subscription/subscriptions/{subscription_id}``
(``update_subscription_folder``)
Both get the same non-dict / malformed-JSON / well-formed-dict-passes triad.
Verified directly against the live routes before writing the assertions
below: a non-dict or malformed body 400s with ``{"error": "Request body
must be valid JSON"}``; a well-formed dict against a nonexistent id 404s
("Folder not found" / "Subscription not found") — never 400. That 404 (not
a coincidental 200) is the shape that proves the guard is the non-dict
check and not an unconditional rejection.
"""
from unittest.mock import MagicMock, patch
import pytest
from local_deep_research.web.routers import news_flask_api
NEWS_MODULE = "local_deep_research.web.routers.news_flask_api"
SCHEDULER_TARGET = (
"local_deep_research.scheduler.background.get_background_job_scheduler"
)
DOC_SCHEDULER_TARGET = (
"local_deep_research.web.routers.scheduler.get_background_job_scheduler"
)
GATE_ENV = "LDR_NEWS_SCHEDULER_ALLOW_API_CONTROL"
START = "/news/api/scheduler/start"
STOP = "/news/api/scheduler/stop"
CLEANUP = "/news/api/scheduler/cleanup-now"
DOC_SCHEDULER_STATUS = "/api/scheduler/status"
# ===========================================================================
# ROW 32 — scheduler-control authorisation
# ===========================================================================
def _stopped_scheduler():
"""For /start: not running, so the handler calls scheduler.start()."""
scheduler = MagicMock(name="background_job_scheduler")
scheduler.is_running = False
scheduler.user_sessions = {}
return scheduler
def _running_scheduler():
"""For /stop and /cleanup-now: both require is_running True to do
anything other than a no-op message."""
scheduler = MagicMock(name="background_job_scheduler")
scheduler.is_running = True
scheduler.user_sessions = {}
return scheduler
_GATED_ROUTES = [
(START, _stopped_scheduler),
(STOP, _running_scheduler),
(CLEANUP, _running_scheduler),
]
_GATED_ROUTE_IDS = ["start", "stop", "cleanup-now"]
class TestSchedulerControlGate:
"""403 gate on the three global-scheduler mutating routes that, before
this file, only ``check-now`` had asserted (see module docstring)."""
@pytest.mark.parametrize(
"path,scheduler_factory", _GATED_ROUTES, ids=_GATED_ROUTE_IDS
)
def test_gate_disabled_returns_403_without_touching_scheduler(
self, authenticated_client, monkeypatch, path, scheduler_factory
):
monkeypatch.delenv(GATE_ENV, raising=False)
with patch(SCHEDULER_TARGET) as get_sched:
resp = authenticated_client.post(path)
assert resp.status_code == 403, (
f"POST {path} must 403 while the scheduler-control gate is "
f"disabled, got {resp.status_code}: {resp.text[:300]}"
)
assert "disabled" in resp.json()["detail"].lower()
assert not get_sched.called, (
f"POST {path} resolved the scheduler singleton before the "
"403 gate ran"
)
@pytest.mark.parametrize(
"path,scheduler_factory", _GATED_ROUTES, ids=_GATED_ROUTE_IDS
)
def test_gate_enabled_authorised_caller_succeeds(
self, authenticated_client, monkeypatch, path, scheduler_factory
):
"""Positive control: an authorised caller must still reach the
route and get a 200. Without this, a route that had been silently
renamed, 404'd, or was rejected by CSRF ahead of the gate would
make the 403 test above pass for the wrong reason — 'everything is
403' is indistinguishable from 'authorisation works' unless a
successful call is also proven possible."""
monkeypatch.setenv(GATE_ENV, "true")
scheduler = scheduler_factory()
with patch(SCHEDULER_TARGET, return_value=scheduler):
resp = authenticated_client.post(path)
assert resp.status_code == 200, (
f"POST {path} must succeed for an authorised caller once the "
f"gate is enabled, got {resp.status_code}: {resp.text[:300]}"
)
assert resp.json().get("status") in ("success", "triggered"), (
f"POST {path} returned 200 but not a recognised success body: "
f"{resp.text[:300]}"
)
class TestDocumentSchedulerStatusRequiresAuth:
"""``GET /api/scheduler/status`` — the DOCUMENT scheduler in
``web/routers/scheduler.py``. See module docstring for why this is a
different singleton from the news scheduler above."""
def test_unauthenticated_caller_gets_401(self, client):
resp = client.get(DOC_SCHEDULER_STATUS, follow_redirects=False)
assert resp.status_code == 401, (
f"GET {DOC_SCHEDULER_STATUS} answered an unauthenticated "
f"caller with {resp.status_code}: {resp.text[:300]}"
)
def test_authenticated_caller_is_not_blocked(self, authenticated_client):
"""Positive control: a real session must reach the handler (a 401
above must mean 'unauthenticated', not 'this route is broken for
everyone')."""
scheduler = MagicMock()
scheduler.get_document_scheduler_status.return_value = {"enabled": True}
with patch(DOC_SCHEDULER_TARGET, return_value=scheduler):
resp = authenticated_client.get(DOC_SCHEDULER_STATUS)
assert resp.status_code == 200, resp.text[:300]
scheduler.get_document_scheduler_status.assert_called_once()
# ===========================================================================
# ROW 33a — news POST rate-limit VALUES
# ===========================================================================
# endpoint: (shared-bucket scope, amount, granularity)
EXPECTED_NEWS_RATE_LIMITS = {
"create_subscription": ("news_create", 10, "minute"),
"submit_feedback": ("news_feedback", 30, "minute"),
"research_news_item": ("news_research", 5, "minute"),
"save_preferences": ("news_preferences", 10, "minute"),
}
def _limits_for(endpoint_name):
"""The slowapi Limit objects registered for a news endpoint."""
qualified = f"{NEWS_MODULE}.{endpoint_name}"
limits = news_flask_api.limiter._route_limits.get(qualified, [])
assert limits, (
f"{qualified} has no registered rate limit — the decorator was "
"removed or renamed"
)
return limits
class TestNewsRateLimitValuesArePinned:
"""``test_rate_limit_coverage.py`` pins that these four routes ARE rate
limited; it never asserts the VALUE, so ``"10 per minute"`` silently
loosening to ``"10 per hour"`` (or being widened to 10000) passes every
existing test. Read the actual amount/granularity/scope straight off
the registered slowapi ``Limit`` objects instead."""
@pytest.mark.parametrize(
"endpoint,expected", sorted(EXPECTED_NEWS_RATE_LIMITS.items())
)
def test_rate_limit_value_is_pinned(self, endpoint, expected):
scope, amount, granularity = expected
seen = {
(lim.scope, lim.limit.amount, lim.limit.GRANULARITY.name)
for lim in _limits_for(endpoint)
}
assert seen == {(scope, amount, granularity)}, (
f"{endpoint} must be {amount} per {granularity} in shared "
f"bucket {scope!r}, got {seen}"
)
def test_research_is_the_most_restrictive_of_the_four(self):
"""Direct successor of the deleted
``test_research_limit_is_most_restrictive``: the route that kicks
off a full research run must stay the tightest budget."""
rates = {
endpoint: _limits_for(endpoint)[0].limit.amount
for endpoint in EXPECTED_NEWS_RATE_LIMITS
}
assert rates["research_news_item"] == min(rates.values()), rates
def test_feedback_is_the_most_permissive_of_the_four(self):
"""Direct successor of the deleted
``test_feedback_limit_is_most_permissive``."""
rates = {
endpoint: _limits_for(endpoint)[0].limit.amount
for endpoint in EXPECTED_NEWS_RATE_LIMITS
}
assert rates["submit_feedback"] == max(rates.values()), rates
# ===========================================================================
# ROW 33b — the two read_json_dict routes test_news_input_validation misses
# ===========================================================================
UPDATE_FOLDER = "/news/api/subscription/folders/999999"
UPDATE_SUBSCRIPTION_FOLDER = (
"/news/api/subscription/subscriptions/00000000-0000-0000-0000-000000000000"
)
# Both routes 404 against these ids (folder/subscription not found) once
# past the body guard — verified directly, see module docstring.
_MISSED_BODY_ROUTES = [
("put", UPDATE_FOLDER, {"name": "renamed"}, "Folder not found"),
(
"put",
UPDATE_SUBSCRIPTION_FOLDER,
{"is_active": True},
"Subscription not found",
),
]
_MISSED_BODY_ROUTE_IDS = ["update_folder", "update_subscription_folder"]
class TestMissedHostileBodyRoutes:
"""``update_folder`` and ``update_subscription_folder`` both call
``read_json_dict`` exactly like the 8 routes
``test_news_input_validation.py``'s ``BODY_ROUTES`` table already
covers — neither of these two is in that table."""
@pytest.mark.parametrize(
"method,path,_body,_not_found",
_MISSED_BODY_ROUTES,
ids=_MISSED_BODY_ROUTE_IDS,
)
@pytest.mark.parametrize(
"bad_body", ["null", "[]", '"a string"', "123", "true"]
)
def test_non_dict_json_body_is_rejected(
self, authenticated_client, method, path, _body, _not_found, bad_body
):
resp = getattr(authenticated_client, method)(
path,
content=bad_body.encode(),
headers={"Content-Type": "application/json"},
)
assert resp.status_code == 400, (
f"{method.upper()} {path} accepted non-dict body {bad_body!r} "
f"with {resp.status_code} — the isinstance(data, dict) guard "
f"is missing or bypassed. Body: {resp.text[:200]}"
)
assert resp.json().get("error") == "Request body must be valid JSON"
@pytest.mark.parametrize(
"method,path,_body,_not_found",
_MISSED_BODY_ROUTES,
ids=_MISSED_BODY_ROUTE_IDS,
)
def test_malformed_json_is_rejected(
self, authenticated_client, method, path, _body, _not_found
):
resp = getattr(authenticated_client, method)(
path,
content=b"{not valid json",
headers={"Content-Type": "application/json"},
)
assert resp.status_code == 400, (
f"{method.upper()} {path} returned {resp.status_code} for "
f"malformed JSON, expected 400: {resp.text[:200]}"
)
assert resp.json().get("error") == "Request body must be valid JSON"
@pytest.mark.parametrize(
"method,path,body,not_found",
_MISSED_BODY_ROUTES,
ids=_MISSED_BODY_ROUTE_IDS,
)
def test_a_dict_body_is_not_rejected_by_the_guard(
self, authenticated_client, method, path, body, not_found
):
"""Positive control: the guard must reject non-dicts, not
everything. Without this, a route that 400'd unconditionally would
satisfy every assertion above. The well-formed body against a
nonexistent id must reach the NOT-FOUND branch, never the body
guard's 400."""
resp = getattr(authenticated_client, method)(path, json=body)
assert resp.status_code != 400, (
f"{method.upper()} {path}: a well-formed dict body was "
f"rejected by the JSON guard: {resp.text[:300]}"
)
assert resp.json().get("error") != "Request body must be valid JSON"
assert resp.status_code == 404, (
f"{method.upper()} {path} was expected to 404 "
f"({not_found!r}) for a nonexistent id, got "
f"{resp.status_code}: {resp.text[:300]}"
)
assert resp.json().get("error") == not_found