Skip to content

~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.

7.7 KB
Raw Blame
"""Error shaping for ``POST /settings/api/notifications/test-url``.

The endpoint tests either the URL the caller sent or, for a blank/sentinel
body, the caller's STORED ``notifications.service_url``. Those two paths
must not report failures the same way:

* caller-supplied -- the caller just typed the URL, so the validator's
  reason (which can name the host) is theirs already and is echoed;
* stored-URL -- the setting is redacted on every read path, so an error
  naming its host would hand back through this endpoint exactly what the
  redaction withholds. Those go to the server log and the caller gets a
  generic failure.

The narrowing that keeps the generic message from swallowing everything:
a ``test_service`` failure that names no destination (the operator
instruction for ``LDR_NOTIFICATIONS_ALLOW_OUTBOUND``, the target-count cap,
the unambiguous-parse refusal, ...) is actionable and host-free, so it is
still returned inline -- ``docs/NOTIFICATIONS.md`` promises exactly that,
and a user who cannot read the server log has nothing else to go on.

``NotificationService.test_service`` is patched throughout: these tests are
about the response the router builds from its result, and each patched
return value is a verbatim string from ``notifications/service.py`` /
``security/notification_validator.py``. No network, no Apprise.
"""

import os

# Rate limiting is read once at import time in
# ``web/dependencies/rate_limit.py``; disable it before the app is imported
# so a direct run of this file cannot flake on the shared settings bucket
# (the PUT + POST pairs below).
os.environ.setdefault("LDR_DISABLE_RATE_LIMITING", "true")

import pytest  # noqa: E402

from local_deep_research.security.notification_validator import (  # noqa: E402
    NotificationURLValidator,
)

WEBHOOK_KEY = "notifications.service_url"
STORED_URL = "discord://HOOKID_XYZ/TOKEN_SECRET_abcdefghijklmnop"
TEST_URL_ROUTE = "/settings/api/notifications/test-url"

BLOCKED_HOST = "internal.corp.example"
# Verbatim from NotificationURLValidator._validate_url_security: the
# hostname is interpolated into the message the caller would receive.
PRIVATE_IP_ERROR = (
    f"{NotificationURLValidator.PRIVATE_IP_REJECTION_PREFIX} {BLOCKED_HOST}"
)
METADATA_ERROR = (
    f"Blocked cloud-metadata / link-local IP address: {BLOCKED_HOST}"
)
# Verbatim from NotificationService.test_service: a static operator
# instruction. It names no destination and only an operator can act on it.
OUTBOUND_DISABLED_ERROR = (
    "Outbound notifications are disabled. The server administrator must "
    "set LDR_NOTIFICATIONS_ALLOW_OUTBOUND=true to enable notification "
    "webhooks. See SECURITY.md 'Notification Webhook SSRF' for details."
)
GENERIC_ERROR = (
    "Failed to test notification service. Check server logs for details."
)


@pytest.fixture
def configured_client(authenticated_client):
    """A logged-in client whose stored notification URL is set.

    Stored through the real settings API so the endpoint's fallback
    resolves it the way it does in production (the tests below assert
    ``test_service`` was handed exactly this URL, which is what proves the
    stored path -- not the caller-supplied one -- was taken).
    """
    response = authenticated_client.put(
        f"/settings/api/{WEBHOOK_KEY}", json={"value": STORED_URL}
    )
    assert response.status_code == 200, response.text
    return authenticated_client


@pytest.fixture
def failing_test_service(monkeypatch):
    """Patch ``test_service`` to fail with a chosen error, recording the
    URL it was asked to test."""
    from local_deep_research.notifications.service import NotificationService

    tested_urls = []

    def install(error):
        def fake_test_service(_service, url):
            tested_urls.append(url)
            return {"success": False, "error": error}

        monkeypatch.setattr(
            NotificationService, "test_service", fake_test_service
        )
        return tested_urls

    return install


@pytest.mark.parametrize(
    "host_bearing_error", [PRIVATE_IP_ERROR, METADATA_ERROR]
)
def test_stored_url_failure_never_names_the_stored_host(
    configured_client, failing_test_service, host_bearing_error
):
    """The two validator messages that interpolate a hostname must not come
    back to a caller who never named the destination."""
    tested_urls = failing_test_service(host_bearing_error)

    response = configured_client.post(TEST_URL_ROUTE, json={"service_url": ""})

    assert response.status_code == 200, response.text
    # The blank body really did take the stored-URL fallback.
    assert tested_urls == [STORED_URL]
    body = response.json()
    assert body["success"] is False
    assert body["error"] == GENERIC_ERROR
    # Nothing host-derived anywhere in the response, not just in "error".
    assert BLOCKED_HOST not in response.text
    assert "Blocked" not in response.text


def test_caller_supplied_failure_still_returns_the_validator_detail(
    configured_client, failing_test_service
):
    """Positive control for the suppression above: a caller who names the
    destination is told why it was refused. Without this the endpoint could
    "pass" the test above by blanking every error it ever returns."""
    tested_urls = failing_test_service(PRIVATE_IP_ERROR)
    caller_url = f"http://{BLOCKED_HOST}/hook"

    response = configured_client.post(
        TEST_URL_ROUTE, json={"service_url": caller_url}
    )

    assert response.status_code == 200, response.text
    assert tested_urls == [caller_url]
    body = response.json()
    assert body["success"] is False
    assert body["error"] == PRIVATE_IP_ERROR
    assert BLOCKED_HOST in body["error"]


def test_stored_url_static_operator_instruction_is_returned_inline(
    configured_client, failing_test_service
):
    """The stored path must not be a blanket "check the logs": a failure
    that names no destination is the user's only actionable feedback, and
    docs/NOTIFICATIONS.md promises this one inline."""
    tested_urls = failing_test_service(OUTBOUND_DISABLED_ERROR)

    response = configured_client.post(TEST_URL_ROUTE, json={"service_url": ""})

    assert response.status_code == 200, response.text
    assert tested_urls == [STORED_URL]
    body = response.json()
    assert body["success"] is False
    assert body["error"] == OUTBOUND_DISABLED_ERROR


def test_stored_url_unknown_failure_is_suppressed(
    configured_client, failing_test_service
):
    """The allowlist fails closed: an error string this router does not
    recognise is treated as possibly host-bearing. A message added to
    ``test_service`` later costs a user some detail; it does not leak by
    default."""
    tested_urls = failing_test_service(
        f"Some future refusal mentioning {BLOCKED_HOST}"
    )

    response = configured_client.post(TEST_URL_ROUTE, json={"service_url": ""})

    assert response.status_code == 200, response.text
    assert tested_urls == [STORED_URL]
    assert response.json()["error"] == GENERIC_ERROR
    assert BLOCKED_HOST not in response.text


def test_stored_url_success_still_reports_the_success_message(
    configured_client, monkeypatch
):
    """Only failures are reshaped: a successful stored-URL test keeps its
    message (and carries no error)."""
    from local_deep_research.notifications.service import NotificationService

    monkeypatch.setattr(
        NotificationService,
        "test_service",
        lambda _service, url: {
            "success": True,
            "message": "Test notification sent successfully",
        },
    )

    response = configured_client.post(TEST_URL_ROUTE, json={"service_url": ""})

    assert response.status_code == 200, response.text
    assert response.json() == {
        "success": True,
        "message": "Test notification sent successfully",
        "error": "",
    }