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.

35.2 KB
Raw Blame
"""Integration tests for the ``full_search.py`` egress Policy Enforcement Point:

``web_search_engines/engines/full_search.py`` — per-URL egress gating in the
full-content fetch path: when an ``egress_context`` is present, URLs that
``evaluate_url`` denies for the scope are dropped before any network fetch,
allowed ones are kept, and the denial audit log is URL-redacted.

These drive the REAL call-site code. Only unavoidable heavy deps are mocked
(the network ``batch_fetch_and_extract`` and the SSRF ``validate_url``, so the
SSRF axis is isolated from the orthogonal egress-scope axis under test). The
egress decision itself (``evaluate_url``) is exercised for real against IP
literals, so no DNS / network is required.
"""

from __future__ import annotations

import asyncio
from unittest.mock import AsyncMock, MagicMock

import pytest
import requests
from loguru import logger
from requests.structures import CaseInsensitiveDict

from local_deep_research.research_library.downloaders import (
    playwright_html as pw,
)
from local_deep_research.security.egress.policy import (
    EgressContext,
    EgressScope,
)
from local_deep_research.web_search_engines.engines import full_search as fs


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------


def _make_ctx(scope: EgressScope) -> EgressContext:
    return EgressContext(
        scope=scope,
        primary_engine="library",
        require_local_llm=False,
        require_local_embeddings=False,
    )


class _FakeWebSearch:
    """Minimal stand-in for the inner web search engine (``.invoke``)."""

    def __init__(self, results):
        self._results = results

    def invoke(self, query):  # noqa: D401 - protocol method
        return list(self._results)


# Public + private IP literals resolve locally (no DNS / network) and match
# the literal-classification path the existing edge-case tests use.
_PUBLIC_URL = "http://93.184.216.34/page"
_PRIVATE_URL = "http://10.0.0.5/api"


# ===========================================================================
# full_search per-URL egress gating
# ===========================================================================


@pytest.fixture
def patched_fetch(monkeypatch):
    """Capture the URLs that survive the gate and reach the fetcher; isolate
    the SSRF axis by forcing ``validate_url`` True so only egress scope
    decides which URLs pass."""
    captured = {"urls": None, "calls": 0}

    def fake_batch(urls, **kwargs):
        captured["calls"] += 1
        captured["urls"] = list(urls)
        return {u: "content-for-" + u for u in urls}

    monkeypatch.setattr(fs, "batch_fetch_and_extract", fake_batch)
    monkeypatch.setattr(
        fs,
        "validate_url",
        lambda u, allow_private_ips=False, block_link_local=False: True,
    )
    return captured


def _engine(results, ctx):
    return fs.FullSearchResults(
        llm=None,  # llm None -> check_urls returns results unfiltered
        web_search=_FakeWebSearch(results),
        egress_context=ctx,
    )


def test_run_private_only_drops_public_and_private(patched_fetch):
    """PRIVATE_ONLY: the public URL is denied by ``evaluate_url`` (counted)
    and the private one is refused uncounted before it, because private
    result URLs stay blocked under every scope but ``unprotected`` even
    when the SSRF check admits them (forced True here)."""
    results = [
        {"title": "pub", "link": _PUBLIC_URL},
        {"title": "priv", "link": _PRIVATE_URL},
    ]
    ctx = _make_ctx(EgressScope.PRIVATE_ONLY)
    engine = _engine(results, ctx)
    out = engine.run("q")

    assert patched_fetch["calls"] == 0
    assert ctx._fetch_denial_count["count"] == 1
    assert all(r["full_content"] is None for r in out)


def test_run_public_only_drops_private_keeps_public(patched_fetch):
    results = [
        {"title": "pub", "link": _PUBLIC_URL},
        {"title": "priv", "link": _PRIVATE_URL},
    ]
    engine = _engine(results, _make_ctx(EgressScope.PUBLIC_ONLY))
    out = engine.run("q")

    assert patched_fetch["urls"] == [_PUBLIC_URL]
    by_link = {r["link"]: r for r in out}
    assert by_link[_PUBLIC_URL]["full_content"] == "content-for-" + _PUBLIC_URL
    assert by_link[_PRIVATE_URL]["full_content"] is None


def test_run_all_denied_skips_fetch_entirely(patched_fetch):
    """PRIVATE_ONLY with only public URLs -> nothing passes the gate, the
    network fetcher is never invoked, all results get null content."""
    results = [{"title": "pub", "link": _PUBLIC_URL}]
    engine = _engine(results, _make_ctx(EgressScope.PRIVATE_ONLY))
    out = engine.run("q")

    assert patched_fetch["calls"] == 0
    assert out[0]["full_content"] is None


def test_run_without_egress_context_does_not_gate(patched_fetch):
    """Control: with no egress_context the per-URL scope gate is inactive, so
    both URLs reach the fetcher. This proves the gate above is driven by the
    egress context and not by some unrelated filter."""
    results = [
        {"title": "pub", "link": _PUBLIC_URL},
        {"title": "priv", "link": _PRIVATE_URL},
    ]
    engine = fs.FullSearchResults(
        llm=None, web_search=_FakeWebSearch(results), egress_context=None
    )
    engine.run("q")

    assert patched_fetch["urls"] == [_PUBLIC_URL, _PRIVATE_URL]


def test_get_full_content_gates_per_url(patched_fetch):
    """The secondary ``_get_full_content`` path enforces the same per-URL
    scope gate."""
    items = [
        {"title": "pub", "link": _PUBLIC_URL},
        {"title": "priv", "link": _PRIVATE_URL},
    ]
    engine = _engine(items, _make_ctx(EgressScope.PUBLIC_ONLY))
    out = engine._get_full_content(items)

    assert patched_fetch["urls"] == [_PUBLIC_URL]
    by_link = {r["link"]: r for r in out}
    assert by_link[_PUBLIC_URL]["full_content"] == "content-for-" + _PUBLIC_URL
    assert by_link[_PRIVATE_URL]["full_content"] is None


def test_denied_url_audit_log_is_redacted(patched_fetch):
    """The denial audit record must carry only scheme://host (no path / query /
    token), proving sensitive URL components are not leaked into logs."""
    sensitive_url = "http://93.184.216.34/secret/report?token=SUPERSECRET"
    results = [{"title": "pub", "link": sensitive_url}]

    records = []

    def sink(message):
        records.append(message.record)

    # 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(sink, level="WARNING")
    try:
        engine = _engine(results, _make_ctx(EgressScope.PRIVATE_ONLY))
        engine.run("q")
    finally:
        logger.remove(sink_id)
        logger.disable("local_deep_research")

    # Find the policy-audit denial record for this URL.
    audit = [
        r
        for r in records
        if r["extra"].get("policy_audit") and "url" in r["extra"]
    ]
    assert audit, "expected a policy_audit denial record"
    rec = audit[0]
    logged_url = rec["extra"]["url"]
    assert logged_url == "http://93.184.216.34"
    # The token / path must appear nowhere in the logged URL or message.
    assert "SUPERSECRET" not in logged_url
    assert "SUPERSECRET" not in rec["message"]
    assert "/secret" not in logged_url
    assert rec["extra"]["reason"] == "scope_mismatch_private_only"


# ===========================================================================
# The private-fetch grant on the downloader path under a run policy
# ===========================================================================
#
# The two gates above only ever see the result URL. The download pipeline
# (``batch_fetch_and_extract`` -> ``AutoHTMLDownloader``) applies its address
# flags to every destination it actually reaches, redirect hops and browser
# subresources included, but it never sees the run's egress policy. So
# ``FullSearchResults`` may hand it the private-fetch grant only when that
# policy restricts no host class (no run context, or the ``unprotected``
# scope); under ``public_only`` / ``private_only`` / ``strict`` the pipeline
# runs with the strict default. The tests below drive the REAL pipeline with
# the HTTP transport faked at the ``requests`` adapter (no network, no DNS:
# IP literals only) and assert that a destination the policy denies is never
# fetched.

_PRIVATE_HOP = "http://10.0.0.5/internal"
_PUBLIC_HOP = "http://93.184.216.34/next"
_ARTICLE_HTML = (
    "<html><head><title>Wiki page</title></head><body><article><p>"
    "This page documents the deployment procedure for the internal wiki. "
    "It describes how the service is provisioned, how backups are rotated "
    "every night, how the on-call engineer restores a snapshot, and which "
    "dashboards to watch after a release. Each section lists the commands "
    "to run and the expected output so the procedure can be followed step "
    "by step without prior knowledge of the system."
    "</p></article></body></html>"
).encode("utf-8")
_SPA_SHELL_HTML = (
    '<html><body><div id="root"></div>'
    "<noscript>You need to enable JavaScript to run this app.</noscript>"
    "</body></html>"
).encode("utf-8")


def _html_ok(body: bytes):
    return (200, {"Content-Type": "text/html; charset=utf-8"}, body)


def _redirect_to(location: str):
    return (302, {"Location": location}, b"")


@pytest.fixture
def transport(monkeypatch):
    """Fake the bottom of ``requests``: serve scripted responses per URL and
    record every URL the pipeline actually tried to fetch. Patching the
    adapter (not the downloader) keeps ``SafeSession`` and its per-hop
    redirect validation in the loop, which is what is under test."""
    state = {"responses": {}, "urls": []}

    def fake_send(_adapter, request, **_kwargs):
        state["urls"].append(request.url)
        status, headers, body = state["responses"].get(
            request.url, (404, {}, b"")
        )
        response = requests.Response()
        response.status_code = status
        response.headers = CaseInsensitiveDict(
            {"Content-Length": str(len(body)), **headers}
        )
        response._content = body
        response.encoding = "utf-8"
        response.url = request.url
        response.request = request
        return response

    monkeypatch.setattr(requests.adapters.HTTPAdapter, "send", fake_send)
    return state


def _granted_engine(results, ctx, settings_snapshot=None):
    return fs.FullSearchResults(
        llm=None,
        web_search=_FakeWebSearch(results),
        settings_snapshot=settings_snapshot,
        egress_context=ctx,
        allow_private_ips=True,
    )


@pytest.mark.parametrize(
    "scope,entry,denied_hop",
    [
        (EgressScope.PUBLIC_ONLY, _PUBLIC_URL, _PRIVATE_HOP),
        (EgressScope.PRIVATE_ONLY, _PRIVATE_URL, _PUBLIC_HOP),
        (EgressScope.STRICT, _PRIVATE_URL, _PUBLIC_HOP),
    ],
    ids=["public_only", "private_only", "strict"],
)
def test_redirect_hop_denied_by_scope_is_never_fetched(
    transport, scope, entry, denied_hop
):
    """Grant on, run scope restricting hosts by class. Under public_only the
    public result URL passes both gates, then redirects to a private host
    the scope denies: the pipeline gets the strict default, so the hop is
    refused by its per-hop SSRF check and never reaches the transport (with
    the grant forwarded it would be fetched, since the address flags alone
    admit it). Under private_only / strict the grant is withheld from the
    result URL's own SSRF check too, so the private entry is refused before
    any request and the redirect is never reached; for those scopes the
    pipeline-grant rule is pinned by ``test_pipeline_grant_follows_run_scope``.
    """
    transport["responses"] = {
        entry: _redirect_to(denied_hop),
        denied_hop: _html_ok(_ARTICLE_HTML),
    }
    engine = _granted_engine([{"title": "r", "link": entry}], _make_ctx(scope))

    out = engine.run("q")

    assert denied_hop not in transport["urls"]
    assert out[0]["full_content"] is None
    if scope is EgressScope.PUBLIC_ONLY:
        # The public entry itself was fetched, so the pipeline really ran
        # and the hop, not the initial gate, is what was refused.
        assert entry in transport["urls"]
    else:
        # A private entry under the strict default is refused before the
        # first request goes out: nothing is fetched at all.
        assert transport["urls"] == []


@pytest.mark.parametrize(
    "ctx",
    [None, _make_ctx(EgressScope.UNPROTECTED)],
    ids=["no_context", "unprotected"],
)
def test_permitted_fetch_reaches_private_result_with_grant(transport, ctx):
    """CONTROL: with no run policy, or one that restricts no host class, the
    grant reaches the pipeline and an RFC1918 result page is fetched and
    extracted end to end."""
    transport["responses"] = {_PRIVATE_URL: _html_ok(_ARTICLE_HTML)}
    engine = _granted_engine([{"title": "r", "link": _PRIVATE_URL}], ctx)

    out = engine.run("q")

    assert _PRIVATE_URL in transport["urls"]
    assert out[0]["full_content"]
    assert "deployment procedure" in out[0]["full_content"]


def test_grant_off_blocks_private_result_before_any_fetch(transport):
    """CONTROL: without the grant the strict ``validate_url`` in full_search
    rejects the private result URL even under ``unprotected``, so the fetch
    in the test above is due to the grant and nothing else."""
    transport["responses"] = {_PRIVATE_URL: _html_ok(_ARTICLE_HTML)}
    engine = fs.FullSearchResults(
        llm=None,
        web_search=_FakeWebSearch([{"title": "r", "link": _PRIVATE_URL}]),
        egress_context=_make_ctx(EgressScope.UNPROTECTED),
    )

    out = engine.run("q")

    assert transport["urls"] == []
    assert out[0]["full_content"] is None


def test_link_local_result_stays_blocked_with_grant_and_no_policy(transport):
    """CONTROL: the link-local carve-out survives the grant on the path
    where the grant does reach the pipeline (no run context)."""
    link_local = "http://169.254.10.5/latest/meta-data"
    transport["responses"] = {link_local: _html_ok(_ARTICLE_HTML)}
    engine = _granted_engine([{"title": "r", "link": link_local}], None)

    out = engine.run("q")

    assert transport["urls"] == []
    assert out[0]["full_content"] is None


class _TerminalHop:
    """Stand-in for the Playwright APIResponse of a 200 hop, with the
    attributes the route guards read."""

    status = 200
    headers: dict = {}
    headers_array: list = []


def _recording_auto_downloader(children):
    class _Recording(pw.AutoHTMLDownloader):
        def _get_playwright_downloader(self):
            child = super()._get_playwright_downloader()
            if child not in children:
                children.append(child)
            return child

    return _Recording


@pytest.mark.parametrize("guard", ["playwright", "crawl4ai"])
def test_browser_subresource_denied_by_scope_is_never_fetched(
    transport, monkeypatch, guard
):
    """Restricted full search keeps static fetches but never opens a browser.

    Even a directly called browser route guard refuses transport before its
    own SSRF checks, since native DNS cannot enforce the run scope.
    """
    children = []
    monkeypatch.setattr(
        pw, "AutoHTMLDownloader", _recording_auto_downloader(children)
    )
    monkeypatch.setattr(
        pw.PlaywrightHTMLDownloader, "_fetch_html", lambda self, url: None
    )
    transport["responses"] = {_PUBLIC_URL: _html_ok(_SPA_SHELL_HTML)}
    engine = _granted_engine(
        [{"title": "r", "link": _PUBLIC_URL}],
        _make_ctx(EgressScope.PUBLIC_ONLY),
        settings_snapshot={"web.enable_javascript_rendering": True},
    )

    engine.run("q")

    assert transport["urls"] and set(transport["urls"]) == {_PUBLIC_URL}
    assert children == [], "restricted runs must keep the static path"
    child = pw.PlaywrightHTMLDownloader(block_link_local=True)
    child.session.egress_context = _make_ctx(EgressScope.PUBLIC_ONLY)
    assert child.allow_private_ips is False
    assert child.block_link_local is True

    route = MagicMock()
    route.request.url = _PUBLIC_URL
    route.request.method = "GET"
    route.request.resource_type = "xhr"
    if guard == "playwright":
        route.fetch = MagicMock(return_value=_TerminalHop())
        child._playwright_route_guard(route)
        route.abort.assert_called_once_with("blockedbyclient")
        route.fulfill.assert_not_called()
    else:
        route.fetch = AsyncMock(return_value=_TerminalHop())
        route.fulfill = AsyncMock()
        route.abort = AsyncMock()
        route.fallback = AsyncMock()
        asyncio.run(child._crawl4ai_route_guard(route))
        route.abort.assert_awaited_once_with("blockedbyclient")
        route.fulfill.assert_not_awaited()
    route.fetch.assert_not_called()
    child.close()


@pytest.mark.parametrize(
    "scope,url,expected",
    [
        (None, _PRIVATE_URL, True),
        (EgressScope.UNPROTECTED, _PRIVATE_URL, True),
        (EgressScope.PUBLIC_ONLY, _PUBLIC_URL, False),
        (EgressScope.BOTH, _PUBLIC_URL, False),
    ],
    ids=[
        "no_context",
        "unprotected",
        "public_only",
        "both",
    ],
)
@pytest.mark.parametrize("method", ["run", "_get_full_content"])
def test_pipeline_grant_follows_run_scope(
    monkeypatch, scope, url, expected, method
):
    """Pin the rule at both call sites: the pipeline is handed the grant
    only with no run context or under ``unprotected``; every scope that
    restricts hosts by class, and the internal ``both`` fallback, gets the
    strict default. ``block_link_local`` stays on either way. The result
    URL is one the scope admits, so the real ``evaluate_url`` lets it
    through to the pipeline call. Under ``private_only`` / ``strict`` no
    result URL reaches the pipeline at all (public ones are denied by
    ``evaluate_url``, private ones refused before it), which
    ``test_strict_and_private_only_fetch_no_result_url`` pins."""
    seen = {}

    def fake_batch(urls, **kwargs):
        seen.update(kwargs)
        return {u: "content" for u in urls}

    monkeypatch.setattr(fs, "batch_fetch_and_extract", fake_batch)
    monkeypatch.setattr(
        fs,
        "validate_url",
        lambda u, allow_private_ips=False, block_link_local=False: True,
    )
    ctx = None if scope is None else _make_ctx(scope)
    items = [{"title": "r", "link": url}]
    engine = _granted_engine(items, ctx)

    if method == "run":
        engine.run("q")
    else:
        engine._get_full_content(items)

    assert seen["allow_private_ips"] is expected
    assert seen["block_link_local"] is True


def test_pipeline_grant_off_stays_off_under_unprotected(monkeypatch):
    """CONTROL for the pin above: the permissive scope never manufactures a
    grant the engine does not hold."""
    seen = {}

    def fake_batch(urls, **kwargs):
        seen.update(kwargs)
        return {u: "content" for u in urls}

    monkeypatch.setattr(fs, "batch_fetch_and_extract", fake_batch)
    monkeypatch.setattr(
        fs,
        "validate_url",
        lambda u, allow_private_ips=False, block_link_local=False: True,
    )
    engine = fs.FullSearchResults(
        llm=None,
        web_search=_FakeWebSearch([{"title": "r", "link": _PRIVATE_URL}]),
        egress_context=_make_ctx(EgressScope.UNPROTECTED),
    )

    engine.run("q")

    assert seen["allow_private_ips"] is False
    assert seen["block_link_local"] is True


# ===========================================================================
# Grant withheld under protected scopes: refused before evaluate_url, so it
# never spends the run's denied-fetch quota (#6535 follow-up)
# ===========================================================================


@pytest.mark.parametrize(
    "scope,expected",
    [
        (None, True),
        (EgressScope.UNPROTECTED, True),
        (EgressScope.PUBLIC_ONLY, False),
        (EgressScope.PRIVATE_ONLY, False),
        (EgressScope.STRICT, False),
        (EgressScope.BOTH, False),
    ],
    ids=[
        "no_context",
        "unprotected",
        "public_only",
        "private_only",
        "strict",
        "both",
    ],
)
@pytest.mark.parametrize("method", ["run", "_get_full_content"])
def test_ssrf_validation_grant_follows_run_scope(
    monkeypatch, scope, expected, method
):
    """The SSRF validation of result URLs gets the same effective grant as
    the pipeline: on only with no run context or under ``unprotected``."""
    seen = []

    def fake_validate(u, allow_private_ips=False, block_link_local=False):
        seen.append((allow_private_ips, block_link_local))
        return False

    monkeypatch.setattr(fs, "validate_url", fake_validate)
    monkeypatch.setattr(
        fs, "batch_fetch_and_extract", MagicMock(return_value={})
    )
    ctx = None if scope is None else _make_ctx(scope)
    items = [{"title": "r", "link": _PRIVATE_URL}]
    engine = _granted_engine(items, ctx)

    if method == "run":
        engine.run("q")
    else:
        engine._get_full_content(items)

    assert seen == [(expected, True)]


@pytest.mark.parametrize("method", ["run", "_get_full_content"])
def test_private_results_under_public_only_do_not_spend_denial_quota(
    monkeypatch, method
):
    """Default ADAPTIVE resolves SearXNG to PUBLIC_ONLY. With the grant set,
    private result URLs must be refused by the SSRF check (uncounted), not
    by ``evaluate_url`` (counted): otherwise MAX_DENIED_FETCHES_PER_RUN
    private results exhaust the quota and the public result in the same
    batch is then denied with ``denial_quota_exceeded``. Real
    ``validate_url`` and ``evaluate_url`` on IP literals, no DNS."""
    from local_deep_research.security.egress.policy import (
        MAX_DENIED_FETCHES_PER_RUN,
    )

    fetched = {}

    def fake_batch(urls, **kwargs):
        fetched["urls"] = list(urls)
        fetched["allow_private_ips"] = kwargs["allow_private_ips"]
        return {u: "content" for u in urls}

    monkeypatch.setattr(fs, "batch_fetch_and_extract", fake_batch)
    ctx = _make_ctx(EgressScope.PUBLIC_ONLY)
    items = [
        {"title": f"p{i}", "link": f"http://10.0.{i // 250}.{i % 250 + 1}/"}
        for i in range(MAX_DENIED_FETCHES_PER_RUN + 5)
    ]
    items.append({"title": "pub", "link": _PUBLIC_URL})
    engine = _granted_engine(items, ctx)

    if method == "run":
        out = engine.run("q")
    else:
        out = engine._get_full_content(items)

    assert ctx._fetch_denial_count["count"] == 0
    assert fetched["urls"] == [_PUBLIC_URL]
    assert fetched["allow_private_ips"] is False
    by_link = {r["link"]: r["full_content"] for r in out}
    assert by_link[_PUBLIC_URL] == "content"
    assert all(v is None for k, v in by_link.items() if k != _PUBLIC_URL)


def test_grant_withheld_is_logged_once_per_wrapper(monkeypatch):
    """Operators who set the flag see why it does not apply: one INFO line
    naming the scope per wrapper, not one per URL or per search."""
    monkeypatch.setattr(
        fs, "batch_fetch_and_extract", MagicMock(return_value={})
    )
    messages = []
    logger.enable("local_deep_research")
    sink_id = logger.add(
        lambda m: messages.append(m.record["message"]), level="INFO"
    )
    try:
        engine = _granted_engine(
            [{"title": "r", "link": _PRIVATE_URL}],
            _make_ctx(EgressScope.PUBLIC_ONLY),
        )
        engine.run("q")
        engine.run("q")
    finally:
        logger.remove(sink_id)
        logger.disable("local_deep_research")

    withheld = [m for m in messages if "not applied under egress scope" in m]
    assert len(withheld) == 1
    assert "public_only" in withheld[0]
    assert "LDR_SEARCH_ALLOW_PRIVATE_RESULT_FETCH" in withheld[0]


# ===========================================================================
# Hosts the SSRF validator admits but the policy does not classify public
# are refused uncounted too, under every scope but ``unprotected``: the
# wrapper asks the policy's own classifier (``host_not_classified_public``),
# not a second, narrower list
# ===========================================================================

# Each host passes ``validate_url`` with the grant withheld (the SSRF
# validator's PRIVATE_IP_RANGES does not list it) yet ``_classify_host``
# does not classify it public. The fake-IP range is ambiguous; the other
# ranges are private. The wrapper refuses them before ``evaluate_url`` so
# no scope's denied-fetch quota is spent.
_SSRF_ADMITTED_NONPUBLIC_HOSTS = [
    "198.18.0.7",  # benchmarking range; fake-ip DNS (Clash, sing-box)
    "192.0.2.10",  # TEST-NET-1
    "198.51.100.7",  # TEST-NET-2
    "203.0.113.5",  # TEST-NET-3
    "192.0.0.170",  # IETF protocol assignments
    "240.0.0.1",  # reserved
    "[2001:db8::1]",  # IPv6 documentation
]


def _run_quota_batch(monkeypatch, method, links, scope=EgressScope.PUBLIC_ONLY):
    """Real ``validate_url`` and ``evaluate_url`` under ``scope`` (default
    ``public_only``), grant withheld; returns (ctx, fetched, by_link)."""
    from local_deep_research.security.egress.policy import (
        MAX_DENIED_FETCHES_PER_RUN,
    )

    fetched = {}

    def fake_batch(urls, **kwargs):
        fetched["urls"] = list(urls)
        return {u: "content" for u in urls}

    monkeypatch.setattr(fs, "batch_fetch_and_extract", fake_batch)
    ctx = _make_ctx(scope)
    items = [
        {"title": f"p{i}", "link": links[i % len(links)] + f"p{i}"}
        for i in range(MAX_DENIED_FETCHES_PER_RUN + 5)
    ]
    items.append({"title": "pub", "link": _PUBLIC_URL})
    engine = _granted_engine(items, ctx)
    if method == "run":
        out = engine.run("q")
    else:
        out = engine._get_full_content(items)
    return ctx, fetched, {r["link"]: r["full_content"] for r in out}


@pytest.mark.parametrize("host", _SSRF_ADMITTED_NONPUBLIC_HOSTS)
@pytest.mark.parametrize("method", ["run", "_get_full_content"])
def test_ssrf_admitted_nonpublic_results_do_not_spend_denial_quota(
    monkeypatch, host, method
):
    ctx, fetched, by_link = _run_quota_batch(
        monkeypatch, method, [f"http://{host}/"]
    )

    assert ctx._fetch_denial_count["count"] == 0
    assert fetched["urls"] == [_PUBLIC_URL]
    assert by_link[_PUBLIC_URL] == "content"
    assert all(v is None for k, v in by_link.items() if k != _PUBLIC_URL)


@pytest.mark.parametrize(
    "nat64_host",
    [
        "[64:ff9b::198.18.0.7]",  # fake IP, RFC 6052 well-known /96
        "[64:ff9b:1:c612:0:700::]",  # 198.18.0.7, RFC 8215 local-use /48
        "[64:ff9b::192.0.2.10]",  # TEST-NET-1, /96
    ],
)
@pytest.mark.parametrize("method", ["run", "_get_full_content"])
def test_nat64_wrapped_nonpublic_results_do_not_spend_denial_quota(
    monkeypatch, nat64_host, method
):
    """With ``LDR_SECURITY_ALLOW_NAT64`` set, the SSRF validator admits a
    NAT64 wrap of an address it admits. The policy classifies the embedded
    IPv4, so the wrapped form is refused uncounted like the bare one."""
    monkeypatch.setenv("LDR_SECURITY_ALLOW_NAT64", "true")
    link = f"http://{nat64_host}/"
    assert fs.validate_url(link, allow_private_ips=False, block_link_local=True)

    ctx, fetched, by_link = _run_quota_batch(monkeypatch, method, [link])

    assert ctx._fetch_denial_count["count"] == 0
    assert fetched["urls"] == [_PUBLIC_URL]
    assert all(v is None for k, v in by_link.items() if k != _PUBLIC_URL)


@pytest.mark.parametrize("method", ["run", "_get_full_content"])
def test_nat64_local_use_wrap_of_public_ipv4_is_a_public_result(
    monkeypatch, method
):
    """Current Python releases count 64:ff9b:1::/48 (RFC 8215) as private
    whatever it carries; the policy follows the embedded IPv4, so a public
    payload is fetched under ``public_only`` like the bare address."""
    monkeypatch.setenv("LDR_SECURITY_ALLOW_NAT64", "true")
    # 93.184.216.34 embedded at the RFC 6052 /48 byte positions.
    link = "http://[64:ff9b:1:5db8:d8:2200::]/"

    ctx, fetched, by_link = _run_quota_batch(monkeypatch, method, [link])

    assert ctx._fetch_denial_count["count"] == 0
    assert sorted(fetched["urls"]) == sorted(by_link)
    assert set(by_link.values()) == {"content"}


def _patch_fake_ip_dns(monkeypatch):
    """Answer every ``*.fakeip.test`` name inside 198.18.0.0/15, as a
    fake-IP resolver (Clash, sing-box, Surge TUN) does for public names."""
    import socket

    real_getaddrinfo = socket.getaddrinfo

    def fake_getaddrinfo(host, *args, **kwargs):
        if isinstance(host, str) and host.endswith(".fakeip.test"):
            return [
                (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("198.18.3.4", 0))
            ]
        return real_getaddrinfo(host, *args, **kwargs)

    monkeypatch.setattr(socket, "getaddrinfo", fake_getaddrinfo)


@pytest.mark.parametrize("method", ["run", "_get_full_content"])
def test_fake_ip_dns_results_do_not_spend_denial_quota(monkeypatch, method):
    """Hostnames that resolve into 198.18.0.0/15 (a fake-ip resolver, or a
    hostile A record) pass the SSRF validator's DNS check but remain
    unclassified through the policy's own DNS path: refused, uncounted."""
    _patch_fake_ip_dns(monkeypatch)

    ctx, fetched, _ = _run_quota_batch(
        monkeypatch,
        method,
        [f"http://h{i}.fakeip.test/" for i in range(3)],
    )

    assert ctx._fetch_denial_count["count"] == 0
    assert fetched["urls"] == [_PUBLIC_URL]


def test_fake_ip_refusal_audit_reason_names_a_nonpublic_host(monkeypatch):
    """The uncounted refusal covers ambiguous (fake-IP) hosts as well as
    private ones, so its audit reason says "nonpublic", not "private"."""
    _patch_fake_ip_dns(monkeypatch)
    records = []
    logger.enable("local_deep_research")
    sink_id = logger.add(lambda m: records.append(m.record), level="WARNING")
    try:
        _run_quota_batch(monkeypatch, "run", ["http://h0.fakeip.test/"])
    finally:
        logger.remove(sink_id)
        logger.disable("local_deep_research")

    reasons = {
        r["extra"].get("reason")
        for r in records
        if r["extra"].get("policy_audit") and "url" in r["extra"]
    }
    assert reasons == {"nonpublic_result_host_public_only"}


_SCOPES_WITH_PRIVATE_HOST_FETCHES = [
    EgressScope.STRICT,
    EgressScope.PRIVATE_ONLY,
    EgressScope.BOTH,
]


def _fetch_one(monkeypatch, method, link, ctx, grant=True):
    """Run the wrapper on one result URL with the REAL ``validate_url``
    and ``evaluate_url``; returns the URLs handed to the pipeline (None
    when it was never called)."""
    fetched = {"urls": None}

    def fake_batch(urls, **kwargs):
        fetched["urls"] = list(urls)
        return {u: "content" for u in urls}

    monkeypatch.setattr(fs, "batch_fetch_and_extract", fake_batch)
    items = [{"title": "r", "link": link}]
    engine = fs.FullSearchResults(
        llm=None,
        web_search=_FakeWebSearch(items),
        egress_context=ctx,
        allow_private_ips=grant,
    )
    if method == "run":
        engine.run("q")
    else:
        engine._get_full_content(items)
    return fetched["urls"]


@pytest.mark.parametrize("grant", [True, False], ids=["grant", "no_grant"])
@pytest.mark.parametrize(
    "scope", _SCOPES_WITH_PRIVATE_HOST_FETCHES, ids=lambda s: s.value
)
@pytest.mark.parametrize("host", _SSRF_ADMITTED_NONPUBLIC_HOSTS)
@pytest.mark.parametrize("method", ["run", "_get_full_content"])
def test_ssrf_admitted_nonpublic_results_stay_blocked_under_protected_scopes(
    monkeypatch, host, scope, grant, method
):
    """The SSRF check admits these addresses; evaluate_url admits the
    private ranges under strict/private_only/both but now refuses ambiguous
    fake-IP addresses. The wrapper refuses all non-public result URLs before
    evaluate_url, uncounted, with or without the withheld grant."""
    ctx = _make_ctx(scope)
    link = f"http://{host}/page"
    assert fs.validate_url(link, allow_private_ips=False, block_link_local=True)

    fetched = _fetch_one(monkeypatch, method, link, ctx, grant=grant)

    assert fetched is None
    assert ctx._fetch_denial_count["count"] == 0


@pytest.mark.parametrize("method", ["run", "_get_full_content"])
def test_fake_ip_public_hosts_are_not_fetched_under_strict(monkeypatch, method):
    """Under fake-IP DNS a public name resolves into 198.18.0.0/15 and the
    policy cannot tell whether it is public or local. The wrapper refuses it
    (uncounted) instead of fetching the public page."""
    _patch_fake_ip_dns(monkeypatch)
    ctx = _make_ctx(EgressScope.STRICT)

    fetched = _fetch_one(monkeypatch, method, "http://news.fakeip.test/a", ctx)

    assert fetched is None
    assert ctx._fetch_denial_count["count"] == 0


@pytest.mark.parametrize(
    "scope",
    [EgressScope.STRICT, EgressScope.PRIVATE_ONLY],
    ids=lambda s: s.value,
)
@pytest.mark.parametrize("method", ["run", "_get_full_content"])
def test_strict_and_private_only_fetch_no_result_url(
    monkeypatch, scope, method
):
    """Under ``strict`` and ``private_only`` the wrapper fetches no result
    URL at all, grant or not: public ones are denied by ``evaluate_url``
    (counted), RFC 1918 ones by the SSRF check and the remaining
    policy-private ones by the wrapper's classifier check (uncounted)."""
    ctx = _make_ctx(scope)
    for link in (_PUBLIC_URL, _PRIVATE_URL, "http://198.18.0.7/"):
        assert _fetch_one(monkeypatch, method, link, ctx) is None
    assert ctx._fetch_denial_count["count"] == 1


@pytest.mark.parametrize("method", ["run", "_get_full_content"])
def test_unprotected_without_grant_fetches_ssrf_admitted_ranges(
    monkeypatch, method
):
    """The documented exception: ``unprotected`` lifts the scope's host
    restriction, so without the grant only the SSRF check stands, and it
    admits 198.18.0.0/15 and the other ranges listed above. Pinned so the
    docs that name this exception stay true."""
    link = "http://198.18.0.7/page"

    fetched = _fetch_one(
        monkeypatch,
        method,
        link,
        _make_ctx(EgressScope.UNPROTECTED),
        grant=False,
    )

    assert fetched == [link]


_CLASSIFIER_PARITY_URLS = [
    _PUBLIC_URL,
    _PRIVATE_URL,
    "http://127.0.0.1/",
    "http://169.254.169.254/latest/meta-data/",
    "http://169.254.10.10/",
    "http://0251.0376.0251.0376/",
    "http://198.18.0.7/",
    "http://192.0.2.10/",
    "http://240.0.0.1/",
    "http://[2001:db8::1]/",
    "http://[::1]/",
    "http://[fd00::1]/",
    "http://10%2e0%2e0%2e1/",
    "http://10.0.0.1./",
    "http://localwiki/",
    "ftp://10.0.0.1/",
    "javascript:alert(1)",
    "http:///nohost",
    "",
]


@pytest.mark.parametrize("scope", list(EgressScope))
def test_host_not_classified_public_matches_evaluate_url(scope):
    """The class property: ``host_not_classified_public`` is True exactly
    when ``evaluate_url`` under ``public_only`` denies with
    ``scope_mismatch_public_only``, whatever the probing context's scope,
    and it never records a denial. ``localwiki`` is a declared local
    hostname (no DNS); IP literals need none either."""
    from local_deep_research.security.egress.policy import (
        evaluate_url,
        host_not_classified_public,
    )

    def ctx(s):
        return EgressContext(
            scope=s,
            primary_engine="library",
            require_local_llm=False,
            require_local_embeddings=False,
            local_hostnames=("localwiki",),
        )

    for url in _CLASSIFIER_PARITY_URLS:
        probe = ctx(scope)
        refused = host_not_classified_public(url, probe)
        assert probe._fetch_denial_count["count"] == 0, url
        decision = evaluate_url(url, ctx(EgressScope.PUBLIC_ONLY))
        expected = decision.reason == "scope_mismatch_public_only"
        assert refused is expected, (url, decision.reason)