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.

2.5 KB
Raw Blame
"""Regression: an egress-refused notification URL must be logged redacted.

``_filter_urls_by_egress_policy`` audit-logs URLs it refuses. A raw
http/https notification URL can carry a token in its path or query
(Slack/webhook-style), so logging the raw value slips a secret past the
name-based sensitive-logging hook. The refusal log must record only
``scheme://host`` via ``redact_url_for_log``.
"""

from types import SimpleNamespace

from loguru import logger

from local_deep_research.notifications.manager import NotificationManager
from local_deep_research.security.egress import policy as egress_policy


_TOKEN = "S3cretWebhookToken1234567890"
_REFUSED_URL = (
    f"https://hooks.example.com/services/T000/B000/{_TOKEN}?auth={_TOKEN}"
)


def test_refused_url_is_redacted_in_audit_log(mocker):
    """The refusal warning must not contain the token; only scheme+host."""
    snapshot = {"search.tool": "searxng"}
    manager = NotificationManager(
        settings_snapshot=snapshot, user_id="test_user"
    )

    # Force a refusal decision for the http(s) URL and neutralise the
    # snapshot->context construction so the real policy engine is bypassed.
    fake_ctx = SimpleNamespace(scope=SimpleNamespace(value="public_only"))
    mocker.patch.object(
        egress_policy, "context_from_snapshot", return_value=fake_ctx
    )
    mocker.patch.object(
        egress_policy,
        "evaluate_url",
        return_value=SimpleNamespace(allowed=False, reason="blocked host"),
    )

    records = []
    # The package disables its own loguru namespace in __init__; enable it so
    # the in-module audit warnings actually reach our sink. Restore in finally.
    logger.enable("local_deep_research")
    sink_id = logger.add(
        lambda message: records.append(message.record),
        level="WARNING",
        filter=lambda r: r["extra"].get("policy_audit") is True,
    )
    try:
        result = manager._filter_urls_by_egress_policy(_REFUSED_URL)
    finally:
        logger.remove(sink_id)
        logger.disable("local_deep_research")

    # Refused URL is dropped from the dispatch string.
    assert result == ""

    refusal = [
        r
        for r in records
        if r["message"] == "notification URL refused by egress policy"
    ]
    assert refusal, "expected an egress-refusal audit log record"
    logged_url = refusal[0]["extra"]["url"]

    # Only scheme + host survive; no token in path or query.
    assert logged_url == "https://hooks.example.com"
    assert _TOKEN not in logged_url
    assert _TOKEN not in str(refusal[0]["extra"])