~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
"""Hostile-input 500s in settings.py and news_flask_api.py (BC-1e).
Shape A -- ``POST /settings/api/notifications/test-url`` and
``POST /settings/api/rate-limiting/cleanup`` wrapped ``await request.json()``
in a broad ``except Exception``. That intercepted ``json.JSONDecodeError``
before the app's registered handler (``fastapi_app.py::handle_json_decode_error``)
could turn it into a clean 400 -- a malformed body came back as a hardcoded
500 instead.
Shape B -- ``/rate-limiting/cleanup`` also guarded its body with
``data.get("days", 30) if data is not None else 30``, which is *stricter*
than the usual ``or {}`` / ``isinstance`` idiom used elsewhere in this file:
it substitutes the default only when the body is exactly ``None``, so a
truthy non-dict body (``[]``, a bare string, a bare int) reaches
``data.get(...)`` and raises ``AttributeError``.
``POST /news/api/search-history`` (news_flask_api.py) has the same Shape B
problem from a different angle: ``list(data.keys()) if data else 'None'``
in a log line, followed by ``if not data or not data.get("query")`` -- both
assume dict-shaped data whenever it is truthy, so a truthy non-dict body
raises ``AttributeError`` on ``.keys()``/``.get()``.
Fix: each site validates the JSON body's shape before calling dict-only
methods. The cleanup route also maps malformed bytes to its own non-object
400 response while preserving its optional ``null`` and absent-body defaults;
other sites can still use the app's JSON decode handler.
test-url gets extra coverage: the fix only changes how the *body's shape* is
validated. The ``service_url`` *value* inside a well-formed dict is passed
through unchanged to ``NotificationService.test_service`` ->
``NotificationURLValidator``, so a wrong-typed, empty, non-URL, or
internal/loopback ``service_url`` must still be rejected cleanly by that
(unmodified) validator -- never by a crash, and never by silently skipping
the check.
"""
import pytest
from local_deep_research.security.notification_validator import (
NotificationURLValidator,
)
from tests.test_utils import nested_json_past_the_scanner_limit
CLEANUP_ROUTE = "/settings/api/rate-limiting/cleanup"
TEST_URL_ROUTE = "/settings/api/notifications/test-url"
SEARCH_HISTORY_ROUTE = "/news/api/search-history"
# Valid JSON, but not an object -- the shape Shape B lets through unguarded.
TRUTHY_NON_DICT_BODIES = [
pytest.param([1, 2], id="json-array"),
pytest.param("a string", id="json-string"),
pytest.param(3, id="json-number"),
pytest.param(True, id="json-bool"),
]
# ---------------------------------------------------------------------------
# POST /settings/api/rate-limiting/cleanup
# ---------------------------------------------------------------------------
@pytest.mark.parametrize(
"body", [b"{not valid json", b'{"days":"\xff"}'], ids=["syntax", "utf8"]
)
def test_cleanup_malformed_json_bytes_is_400(authenticated_client, body):
"""Invalid JSON bytes share the optional route's non-object envelope."""
resp = authenticated_client.post(
CLEANUP_ROUTE,
content=body,
headers={"Content-Type": "application/json"},
)
assert resp.status_code == 400, (
f"expected 400, got {resp.status_code}: {resp.text[:300]}"
)
assert resp.json() == {"error": "Request body must be a JSON object"}
def test_cleanup_too_deeply_nested_json_is_400_not_500(authenticated_client):
"""Nesting past the parser's limit is a malformed body like any other.
``json.loads`` raises ``RecursionError`` for it, which is not a
``ValueError``, so a guard that only caught ``ValueError`` let it fall
through to the catch-all 500. The body is a few hundred KB, far under the
16 MB JSON cap, so nothing upstream turns it away first.
"""
resp = authenticated_client.post(
CLEANUP_ROUTE,
content=nested_json_past_the_scanner_limit(),
headers={"Content-Type": "application/json"},
)
assert resp.status_code == 400, (
f"expected 400, got {resp.status_code}: {resp.text[:300]}"
)
assert resp.json() == {"error": "Request body must be a JSON object"}
@pytest.mark.parametrize("body", TRUTHY_NON_DICT_BODIES)
def test_cleanup_truthy_non_dict_body_is_400_not_500(
authenticated_client, body
):
"""A truthy non-dict JSON body must be a clean 400 (json_body_error's
'simple' shape, matching this route's other {"error": ...} returns),
never a 500 from an unguarded data.get(...)."""
resp = authenticated_client.post(CLEANUP_ROUTE, json=body)
assert resp.status_code < 500, (
f"{body!r} -> {resp.status_code}: {resp.text[:300]} -- the "
"isinstance(data, dict) guard is missing or bypassed"
)
assert resp.status_code == 400, (
f"{body!r} -> {resp.status_code}, expected 400: {resp.text[:300]}"
)
assert resp.json() == {"error": "Request body must be a JSON object"}
def test_cleanup_literal_null_body_still_defaults(authenticated_client):
"""A genuine JSON `null` body (content-type: application/json, bytes
b"null") is NOT part of the Shape B bug: `data is not None` is already
False for it, so it already fell through to the 30-day default before
this fix and must keep doing so -- this is the route's own documented
"optional body" contract, not an error case."""
resp = authenticated_client.post(
CLEANUP_ROUTE,
content=b"null",
headers={"Content-Type": "application/json"},
)
assert resp.status_code == 200, resp.text
assert resp.json() == {
"message": "Cleaned up rate limiting data older than 30 days"
}
def test_cleanup_no_body_unaffected(authenticated_client):
"""No body / no content-type at all -- is_json is False, data stays
None, defaults to 30 days. Always worked; pinned as a baseline."""
resp = authenticated_client.post(CLEANUP_ROUTE)
assert resp.status_code == 200, resp.text
assert resp.json() == {
"message": "Cleaned up rate limiting data older than 30 days"
}
def test_cleanup_well_formed_body_unaffected(authenticated_client):
"""A proper {"days": N} body must behave exactly as before the fix."""
resp = authenticated_client.post(CLEANUP_ROUTE, json={"days": 10})
assert resp.status_code == 200, resp.text
assert resp.json() == {
"message": "Cleaned up rate limiting data older than 10 days"
}
def test_cleanup_wrong_typed_days_value_still_400(authenticated_client):
"""Shape D sibling, already guarded (int() + try/except) before this
fix -- pinned so a future edit doesn't silently drop it."""
resp = authenticated_client.post(CLEANUP_ROUTE, json={"days": "abc"})
assert resp.status_code == 400
assert resp.json() == {"error": "'days' must be an integer"}
# ---------------------------------------------------------------------------
# POST /settings/api/notifications/test-url
# ---------------------------------------------------------------------------
def test_test_url_malformed_json_bytes_is_400(authenticated_client):
resp = authenticated_client.post(
TEST_URL_ROUTE,
content=b"{not valid json",
headers={"Content-Type": "application/json"},
)
assert resp.status_code == 400, (
f"expected 400, got {resp.status_code}: {resp.text[:300]}"
)
assert resp.json() == {"error": "Invalid JSON body"}
def test_test_url_no_body_is_400_not_500(authenticated_client):
"""This route always parses the body unconditionally (no is_json
gate), so an empty body hits json.JSONDecodeError exactly like garbled
bytes do."""
resp = authenticated_client.post(TEST_URL_ROUTE)
assert resp.status_code == 400, (
f"expected 400, got {resp.status_code}: {resp.text[:300]}"
)
assert resp.json() == {"error": "Invalid JSON body"}
# A body that names no destination -- absent, null, non-dict, or blank
# ``service_url`` -- resolves the caller's STORED notifications.service_url
# instead (#5958, matching main). The test user has none configured, so the
# outcome is this 400 rather than the pre-fallback "service_url is required".
# The load-bearing part is unchanged: a hostile body is a clean 400 with
# ``success: False``, never a 500 and never a call into Apprise.
NO_URL_CONFIGURED = {
"success": False,
"error": "No notification URL configured",
}
def test_test_url_literal_null_body_is_400(authenticated_client):
resp = authenticated_client.post(
TEST_URL_ROUTE,
content=b"null",
headers={"Content-Type": "application/json"},
)
assert resp.status_code == 400, resp.text
assert resp.json() == NO_URL_CONFIGURED
@pytest.mark.parametrize("body", TRUTHY_NON_DICT_BODIES)
def test_test_url_truthy_non_dict_body_is_400_not_500(
authenticated_client, body
):
resp = authenticated_client.post(TEST_URL_ROUTE, json=body)
assert resp.status_code < 500, (
f"{body!r} -> {resp.status_code}: {resp.text[:300]} -- the "
"isinstance(data, dict) guard is missing or bypassed"
)
assert resp.status_code == 400, (
f"{body!r} -> {resp.status_code}, expected 400: {resp.text[:300]}"
)
assert resp.json() == NO_URL_CONFIGURED
def test_test_url_empty_dict_body_unaffected(authenticated_client):
"""An empty dict names no destination, so it takes the stored-URL
fallback and reports the unconfigured 400 -- pinned."""
resp = authenticated_client.post(TEST_URL_ROUTE, json={})
assert resp.status_code == 400, resp.text
assert resp.json() == NO_URL_CONFIGURED
def test_test_url_well_formed_body_reaches_test_service_unchanged(
authenticated_client, monkeypatch
):
"""A well-formed dict body must still reach NotificationService.test_service
with the exact service_url the caller sent -- proves the new body-shape
guard doesn't interfere with routing/parsing for valid input. The real
network/validation boundary is stubbed out here on purpose (this test is
about the JSON-body plumbing, not the validator -- that is covered by
the SSRF-specific tests below, which use the REAL validator)."""
import local_deep_research.notifications.service as service_module
captured = {}
def _fake_test_service(self, url):
captured["url"] = url
return {"success": True, "message": "ok"}
monkeypatch.setattr(
service_module.NotificationService, "test_service", _fake_test_service
)
resp = authenticated_client.post(
TEST_URL_ROUTE, json={"service_url": "discord://webhook_id/token"}
)
assert resp.status_code == 200, resp.text
assert resp.json() == {"success": True, "message": "ok", "error": ""}
assert captured["url"] == "discord://webhook_id/token"
# --- SSRF validation must still run for a wrong-typed/hostile URL value ---
#
# The route's own "outbound disabled" gate (default False) would otherwise
# short-circuit *every* case below with a clean-but-uninformative "outbound
# notifications are disabled" response before ever reaching the validator,
# which would prove nothing about the validator itself. Flip ONLY the
# outbound gate via the real env var NotificationService reads
# (LDR_NOTIFICATIONS_ALLOW_OUTBOUND) so validation actually executes; leave
# LDR_NOTIFICATIONS_ALLOW_PRIVATE_IPS unset (default False) so the
# private-IP check stays strict. None of the URLs below pass structural/
# private-IP validation, so none of them ever reaches apprise's add()/
# notify() -- no network call is made by any test in this section.
@pytest.fixture
def outbound_enabled(monkeypatch):
monkeypatch.setenv("LDR_NOTIFICATIONS_ALLOW_OUTBOUND", "true")
monkeypatch.delenv("LDR_NOTIFICATIONS_ALLOW_PRIVATE_IPS", raising=False)
def test_test_url_wrong_typed_service_url_value_rejected_cleanly(
authenticated_client, outbound_enabled
):
"""{"service_url": 12345} is a well-formed dict (passes the new
isinstance guard) but the *value* is the wrong type. Must not 500.
NOTE: this does NOT assert the specific validator message
("Service URL must be a non-empty string"). That message is computed
correctly by NotificationURLValidator.validate_service_url_with_hint,
but a separate, PRE-EXISTING bug outside this slice's files then
swallows it: notifications/service.py's test_service logs the
rejection via security.url_builder.mask_sensitive_url(url), whose own
``except Exception`` fallback does ``url.split(':')[0]`` -- which
raises AttributeError for a non-string url (confirmed directly:
``mask_sensitive_url(12345)`` -> "'int' object has no attribute
'split'"). That second exception is caught by test_service's own
outer except, so the client still gets a clean 200 with a generic
"Failed to test notification service." message rather than a 500 --
the SSRF/type gate itself is not weakened or bypassed -- but the
specific message is lost. Out of scope for this slice (url_builder.py
is not one of this slice's exclusive files); reported separately, not
fixed here.
"""
resp = authenticated_client.post(
TEST_URL_ROUTE, json={"service_url": 12345}
)
assert resp.status_code < 500, resp.text
assert resp.status_code == 200
body = resp.json()
assert body["success"] is False
assert body["error"], "must still return SOME clean error, not blow up"
def test_test_url_empty_string_service_url_value_rejected_cleanly(
authenticated_client, outbound_enabled
):
"""An empty ``service_url`` no longer reaches the validator: it counts as
"no destination named", so the endpoint resolves the caller's stored URL
(#5958). With none configured that is a 400, not the validator's
"Service URL must be a non-empty string" 200. The validator itself is
still exercised by the wrong-typed / non-URL / loopback rows below, which
all name a destination and therefore skip the fallback."""
resp = authenticated_client.post(TEST_URL_ROUTE, json={"service_url": ""})
assert resp.status_code == 400, resp.text
assert resp.json() == NO_URL_CONFIGURED
def test_test_url_non_url_string_rejected_cleanly(
authenticated_client, outbound_enabled
):
"""A schemeless string never reaches the per-URL validator: the
scheme-boundary parse yields no entries and the whole input as a
scheme-less fragment, so ``NotificationService.test_service`` refuses
the input as a whole. That refusal must be a clean ``success: False``
(not a 500) and its message must tell the operator that every entry
needs a protocol such as ``discord://``."""
resp = authenticated_client.post(
TEST_URL_ROUTE, json={"service_url": "not-a-url-at-all"}
)
assert resp.status_code == 200, resp.text
body = resp.json()
assert body["success"] is False
assert "protocol" in body["error"]
def test_test_url_loopback_ip_still_blocked_by_ssrf_validation(
authenticated_client, outbound_enabled
):
"""This is the load-bearing SSRF check: an internal/loopback URL must
still be rejected by NotificationURLValidator, proving the body-shape
fix did not weaken or bypass it. 127.0.0.1 is an IP literal, so this
is rejected in Phase 1 (structural validation) without any DNS lookup
or network I/O -- see validate_service_url_with_hint's docstring."""
resp = authenticated_client.post(
TEST_URL_ROUTE,
json={"service_url": "http://127.0.0.1:9999/hook"},
)
assert resp.status_code == 200, resp.text
body = resp.json()
assert body["success"] is False
assert body["error"].startswith(
NotificationURLValidator.PRIVATE_IP_REJECTION_PREFIX
), body["error"]
assert "127.0.0.1" in body["error"]
def test_test_url_outbound_disabled_by_default_is_not_a_crash(
authenticated_client,
):
"""Baseline (no outbound_enabled fixture): default server-level gate
refuses cleanly, never a 500, for an otherwise well-formed request."""
resp = authenticated_client.post(
TEST_URL_ROUTE, json={"service_url": "discord://webhook_id/token"}
)
assert resp.status_code == 200, resp.text
body = resp.json()
assert body["success"] is False
assert "disabled" in body["error"].lower()
# ---------------------------------------------------------------------------
# POST /news/api/search-history
# ---------------------------------------------------------------------------
def test_search_history_malformed_json_bytes_is_400(authenticated_client):
"""This route never wrapped ``await request.json()`` in a local
except, so this was never broken by the Shape B bug fixed here --
pinned anyway per the acceptance criteria (all inputs verified per
route)."""
resp = authenticated_client.post(
SEARCH_HISTORY_ROUTE,
content=b"{not valid json",
headers={"Content-Type": "application/json"},
)
assert resp.status_code == 400, (
f"expected 400, got {resp.status_code}: {resp.text[:300]}"
)
def test_search_history_literal_null_body_unaffected(authenticated_client):
resp = authenticated_client.post(
SEARCH_HISTORY_ROUTE,
content=b"null",
headers={"Content-Type": "application/json"},
)
assert resp.status_code == 400, resp.text
assert resp.json() == {"error": "query is required"}
@pytest.mark.parametrize("body", TRUTHY_NON_DICT_BODIES)
def test_search_history_truthy_non_dict_body_is_400_not_500(
authenticated_client, body
):
resp = authenticated_client.post(SEARCH_HISTORY_ROUTE, json=body)
assert resp.status_code < 500, (
f"{body!r} -> {resp.status_code}: {resp.text[:300]} -- data.keys()/"
"data.get() reached a truthy non-dict body unguarded"
)
assert resp.status_code == 400, (
f"{body!r} -> {resp.status_code}, expected 400: {resp.text[:300]}"
)
assert resp.json() == {"error": "query is required"}
def test_search_history_well_formed_body_unaffected(authenticated_client):
resp = authenticated_client.post(
SEARCH_HISTORY_ROUTE,
json={"query": "python", "type": "filter", "resultCount": 5},
)
assert resp.status_code == 200, resp.text
body = resp.json()
assert body["status"] == "success"
assert isinstance(body["id"], int)
# ---------------------------------------------------------------------------
# Regression proof without touching src/ or reverting anything: reproduce
# the exact vulnerable expressions the old code ran, verbatim, against the
# exact hostile inputs used above. Each of these passes today and would
# have failed to even compile as a proof before the fix landed only in the
# sense that the *route* would 500 -- these standalone assertions instead
# show precisely why, independent of whichever version of the route is
# currently checked out.
# ---------------------------------------------------------------------------
def test_regression_reasoning_cleanup_is_not_none_guard_lets_truthy_list_through():
"""settings.py's api_cleanup_rate_limiting used to read
``data.get("days", 30) if data is not None else 30``. This substitutes
the default ONLY when data is exactly None -- a truthy non-dict body
like [] sails through unchanged and breaks on .get()."""
data = []
assert data is not None, "sanity: [] must take the `is not None` branch"
with pytest.raises(AttributeError):
data.get("days", 30) if data is not None else 30 # noqa: B018
def test_regression_reasoning_test_url_falsy_guard_lets_truthy_int_through():
"""settings.py's api_test_notification_url used to read
``if not data or "service_url" not in data:``. For a truthy non-dict
body like a bare int, `not data` is False, so Python must evaluate
`"service_url" not in data` -- which raises TypeError because an int
is not a container."""
data = 42
assert not (not data), "sanity: 42 is truthy, so `not data` is False"
with pytest.raises(TypeError):
"service_url" not in data # noqa: B018
def test_regression_reasoning_search_history_falsy_guard_lets_truthy_list_through():
"""news_flask_api.py's add_search_history used to read
``list(data.keys()) if data else 'None'`` unconditionally in a log
line. `if data` selects the dict-shaped branch for ANY truthy value,
including a bare list, which has no .keys()."""
data = [1, 2]
assert data, "sanity: [1, 2] is truthy, so the dict-shaped branch runs"
with pytest.raises(AttributeError):
list(data.keys()) if data else "None" # noqa: B018