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.

23.2 KB
Raw Blame
"""Egress-policy warning checks.

Check functions: take primitive values, return a warning dict or ``None``.
These surface on the research-form banner so users can see at a glance
when their current policy lets data leave the machine — or silently
blocks something they configured. Most checks are pure; the engine-URL
checks additionally read operator environment state (never the DB) and
never resolve DNS, so all of them stay cheap enough for the
page-render hot path.

The banners, each with its own dismiss flag:

1. **Public search egress enabled** — the active scope permits any public
   engine to fire. The default scope ``adaptive`` can resolve to a
   public-allowing posture, so this fires on a fresh install too; suppressed
   via the dismiss flag so first-launch isn't a wall of red.
2. **Cloud LLM enabled** — the user hasn't opted into
   ``llm.require_local_endpoint`` and the configured provider is one of
   the unambiguously-cloud providers. Critical-severity because the
   user's full prompt content leaks on every research run.
3. **Cloud embeddings enabled** — even worse: indexing a corpus with
   OpenAI embeddings POSTs every chunk to OpenAI. Critical-severity.
4. **Unprotected egress / effective scope / trusted destinations** —
   posture-transparency banners for the egress-scope machinery.
5. **SearXNG private URL not approved** — the selected public engine's
   private instance URL lacks operator approval, so the engine will
   silently self-disable at run time.
6. **Local engine public URL** — the mirror image: a LOCAL document
   engine (Paperless, Elasticsearch) points at a host that does not look
   private, so document queries would leave the box and a private-only
   scope excludes the engine.
"""

from typing import Optional

# Single source of truth — these warning checks now live in the same
# egress package as the PDP, so import the cloud-provider set directly
# instead of maintaining a hand-synced copy (which previously risked
# drift: a provider added to one list but not the other).
from .policy import _CLOUD_LLM_PROVIDERS


def check_public_egress_enabled(
    egress_scope: str,
    acknowledged: bool,
) -> Optional[dict]:
    """Banner when the active scope permits public-internet search engines.

    Fires for ``adaptive`` (which can resolve to a public posture), ``both``,
    and ``public_only`` — the scopes that can allow public engines. Suppressed
    when the user has acknowledged the
    egress-policy warnings via the fresh-install flag — otherwise every
    new install would face three loud banners before doing anything.
    """
    if acknowledged:
        return None
    # "adaptive" can resolve to a public-allowing scope (public or
    # unclassifiable primary), so warn conservatively — better a dismissible
    # banner than a silent public-egress path the user didn't expect.
    if egress_scope not in ("both", "public_only", "adaptive"):
        return None

    return {
        "type": "public_egress_enabled",
        "icon": "🌐",
        "title": "Public search egress enabled",
        "message": (
            "This run can reach external search engines. Set the Egress "
            "Scope to 'Private only' below if you want to keep all "
            "research traffic on-machine."
        ),
        "dismissKey": "app.warnings.dismiss_egress_policy",
        "actionUrl": "#policy_egress_scope",
        "actionLabel": "Adjust scope",
    }


def check_unprotected_egress(egress_scope: str) -> Optional[dict]:
    """Loud, non-dismissible banner when egress protection is turned off.

    The ``unprotected`` escape hatch disables all egress-scope restrictions
    for the run — any engine / URL / LLM / embeddings provider is permitted
    (only the hard SSRF and cloud-metadata blocks remain). Deliberately not
    dismissible: the user should never forget protection is off.
    """
    if (egress_scope or "").lower() != "unprotected":
        return None

    return {
        "type": "egress_unprotected",
        "icon": "⚠️",
        "title": "Egress protection is disabled",
        "message": (
            "Egress Scope is set to 'Unprotected' — this run may reach any "
            "search engine, URL, and LLM/embeddings provider. Only hard SSRF "
            "and cloud-metadata blocking still applies. Pick another scope "
            "below to re-enable protection."
        ),
        "dismissKey": None,
        "actionUrl": "#policy_egress_scope",
        "actionLabel": "Adjust scope",
    }


def check_trusted_destinations(
    trusted_inference, trusted_engines
) -> Optional[dict]:
    """Banner when the user has vouched for off-machine destinations.

    Trusting an inference provider or search engine relaxes the two-axis
    classification (treats an off-machine sink as contained), so sensitive
    sources may flow to it. Surfaced so a stale trust entry can't silently keep
    sending data off-box.
    """
    from .policy import coerce_str_list

    names = sorted(
        {
            n
            for n in coerce_str_list(trusted_inference)[1]
            + coerce_str_list(trusted_engines)[1]
            if n.strip()
        }
    )
    if not names:
        return None

    return {
        "type": "egress_trusted_destinations",
        "icon": "🤝",
        "title": "Trusted destinations configured",
        "message": (
            "These off-machine destinations are marked trusted, so sensitive "
            "sources may be combined with them: "
            + ", ".join(names)
            + ". Remove any you did not intend."
        ),
        "dismissKey": None,
        "actionUrl": "#policy_egress_scope",
        "actionLabel": "Review",
    }


def check_effective_scope(
    egress_scope: str,
    effective_scope: str,
    primary_engine: str,
    acknowledged: bool,
) -> Optional[dict]:
    """Informational banner stating what the ADAPTIVE scope actually resolves
    to for the current primary engine.

    Adaptive is opaque on its own ("follows your primary"); this makes the
    effective posture explicit so the user knows whether THIS config means
    public searches, private/local-only, or both. Only fires for ``adaptive``
    (the explicit scopes are self-describing in the dropdown). Has its own
    dismiss flag so dismissing it doesn't hide the risk banners.
    """
    if acknowledged:
        return None
    if (egress_scope or "").lower() != "adaptive":
        return None
    eff = (effective_scope or "").lower()
    primary = primary_engine or "your primary engine"
    base = {
        "type": "egress_effective_scope",
        "dismissKey": "app.warnings.dismiss_adaptive_scope_info",
        "actionUrl": "#policy_egress_scope",
        "actionLabel": "Change mode",
    }
    if eff == "private_only":
        return {
            **base,
            "icon": "🔒",
            "title": "Adaptive → Private only (stays local)",
            "message": (
                f"Your primary engine ('{primary}') is private, so this run "
                "stays on your machine: only local engines run, and LLM + "
                "embeddings are forced local — nothing leaves the box."
            ),
        }
    if eff == "public_only":
        return {
            **base,
            "icon": "🌐",
            "title": "Adaptive → Public searches enabled",
            "message": (
                f"Your primary engine ('{primary}') is public, so this run "
                "uses public web/academic engines. Your local collections are "
                "not queried."
            ),
        }
    # both (unclassifiable primary)
    return {
        **base,
        "icon": "🔀",
        "title": "Adaptive → Public + private searches enabled",
        "message": (
            f"Your primary ('{primary}') could not be classified as public "
            "or private, so this run can use both public engines and your "
            "local collections."
        ),
    }


def check_private_engine_url_blocked(
    display_name: str,
    engine_name: str,
    url_setting: str,
    instance_url: str,
    active: bool,
    acknowledged: bool,
) -> Optional[dict]:
    """Banner when an active PUBLIC engine's instance URL is a
    private/loopback address the egress guard will refuse to fetch.

    Generic over any engine in ``guarded_engine_url_descriptors()`` with
    ``is_public=True`` — the banner type, dismiss key, env-lock variable,
    and settings anchor are all derived from ``engine_name`` /
    ``url_setting``, so a future public engine with a configurable URL is
    covered by declaration, not by new code.

    Without this, the failure mode is quiet: the engine disables itself at
    init with only a log-panel ERROR, and research runs simply return no
    results from it. This banner surfaces the misconfiguration on the
    research form with the exact operator remedies.

    Cheap by design (no DNS on the page-render hot path): a literal-IP host
    is classified with the SSRF validator's own ``is_ip_blocked`` (so CGNAT
    and every other range the gate blocks fire the banner, and metadata /
    always-blocked addresses — which the env remedies would not help — stay
    silent); a hostname is classified with the ``is_private_ip`` known-name
    check. A private-DNS hostname (``searx.internal`` → 10.x) and a
    short-form literal (``127.1``) are therefore missed here — the runtime
    gate still blocks them and logs the remediation; this advisory covers
    the dominant localhost / LAN-literal case, mirroring the
    cloud-embeddings banner's no-DNS tradeoff. Approval state is read via
    the operator-approval resolver (blanket gate / origin allowlist /
    env-locked URL), which reads only environment state. The displayed
    remedy is derived from the SAME parser the allowlist uses
    (``_engine_url_origin``), so the printed line round-trips by
    construction; a URL that parser refuses (malformed port, forbidden
    characters) yields no banner rather than an unusable remedy. Fails
    toward NO banner on any error — advisory, never load-bearing.
    """
    if acknowledged or not active:
        return None
    if not instance_url or not isinstance(instance_url, str):
        return None
    try:
        import ipaddress

        from ..network_utils import is_private_ip
        from ..ssrf_validator import is_ip_blocked
        from .validators import (
            _engine_url_origin,
            engine_url_origin_text,
            resolve_engine_allow_private_ips,
        )

        parsed_origin = _engine_url_origin(instance_url)
        if parsed_origin is None:
            return None
        _scheme, host, _port = parsed_origin

        try:
            ipaddress.ip_address(host)
            is_literal = True
        except ValueError:
            is_literal = False
        if is_literal:
            # Fire only for addresses the gate blocks SOLELY as private —
            # metadata/always-blocked fall out of the loose call.
            strict = is_ip_blocked(
                host, allow_localhost=False, allow_private_ips=False
            )
            loose = is_ip_blocked(
                host, allow_localhost=True, allow_private_ips=True
            )
            if not strict or loose:
                return None
        elif not is_private_ip(host):
            return None

        if resolve_engine_allow_private_ips(instance_url, url_setting):
            return None

        # Origin for the copy-paste remedy — rendered by the allowlist's own
        # inverse formatter, so feeding it back into the allowlist is
        # guaranteed to match; never the raw URL, so userinfo/path/query
        # never appear.
        origin = engine_url_origin_text(parsed_origin)
        env_lock_var = "LDR_" + url_setting.upper().replace(".", "_")
        anchor = "setting-" + url_setting.replace(".", "-")
    except Exception:  # noqa: silent-exception - advisory only
        return None

    return {
        "type": f"{engine_name}_private_url_blocked",
        "icon": "🚫",
        "title": f"{display_name} is disabled: private URL not approved",
        "message": (
            f"{display_name} is enabled as a search engine, but its "
            f"instance URL ({origin}) is a private/localhost address, "
            "which is not fetched by default — research runs will return "
            f"no {display_name} results. The server operator must approve "
            "it in the server environment and restart LDR: set "
            f"LDR_SEARCH_PRIVATE_ENGINE_URL_ALLOWLIST={origin} "
            f"(recommended), or env-lock the URL via {env_lock_var}, or "
            "LDR_SEARCH_ALLOW_PRIVATE_ENGINE_URLS=true to allow all "
            "private URLs. Only one of these is needed."
        ),
        "dismissKey": f"app.warnings.dismiss_{engine_name}_private_url",
        "actionUrl": f"/settings#{anchor}",
        "actionLabel": f"Open {display_name} settings",
    }


def check_searxng_private_url_blocked(
    primary_engine: str,
    instance_url: str,
    acknowledged: bool,
) -> Optional[dict]:
    """SearXNG-keyed convenience wrapper over
    ``check_private_engine_url_blocked``. Test-facing only — production
    emission goes through the orchestrator's descriptor loop; this wrapper
    exists so focused tests can exercise the SearXNG parameterization
    (active = SearXNG is the selected primary engine) without duplicating
    the declaration constants."""
    return check_private_engine_url_blocked(
        "SearXNG",
        "searxng",
        "search.engine.web.searxng.default_params.instance_url",
        instance_url,
        (primary_engine or "").lower() == "searxng",
        acknowledged,
    )


def _extract_engine_hosts(urls) -> list:
    """Best-effort host extraction from an engine URL setting value.

    Accepts a single URL string, a list of URL strings, or a JSON/comma
    string of them (settings saved via the web UI may arrive as raw JSON
    text — same class of input ``SearXNGSearchEngine._normalize_list``
    handles). Entries that cannot be parsed are skipped: this feeds an
    advisory banner, so failure means silence, never breakage.
    """
    import json as _json
    from urllib.parse import urlsplit

    if urls is None:
        return []
    if isinstance(urls, str):
        stripped = urls.strip()
        if not stripped:
            return []
        try:
            parsed = _json.loads(stripped)
            urls = parsed if isinstance(parsed, list) else [stripped]
        except (ValueError, RecursionError):
            urls = [u.strip() for u in stripped.split(",") if u.strip()]
    if not isinstance(urls, list):
        return []
    hosts = []
    for entry in urls:
        if not isinstance(entry, str) or not entry.strip():
            continue
        text = entry.strip()
        try:
            host = urlsplit(text).hostname
            if not host and "://" not in text:
                # Bare "host:9200" / "host" forms (Elasticsearch accepts
                # them): urlsplit reads the host as the scheme, so re-parse
                # as a network location.
                host = urlsplit("//" + text).hostname
            if host:
                hosts.append(host)
        except Exception:  # noqa: silent-exception - advisory only
            continue
    return hosts


def check_local_engine_public_url(
    display_name: str,
    engine_name: str,
    urls,
    active: bool,
    acknowledged: bool,
    extra: str = "",
    url_setting: str = "",
) -> Optional[dict]:
    """Banner when a LOCAL document engine (Paperless, Elasticsearch) points
    at a host that does not look private.

    The mirror image of the SearXNG private-URL banner: for a public engine
    a private URL is the anomaly, for a local document store a PUBLIC one
    is — search queries about the user's private documents are sent to that
    host, and the PDP's fail-up reclassifies the engine as exposing, so a
    Private-only egress scope silently excludes it from runs. Both
    consequences are surprising, so surface them.

    Advisory only, never a block: a genuinely-public endpoint can be a
    deliberate choice (e.g. Elastic Cloud). Classification is cheap and
    DNS-free: a host counts as local when the ``is_private_ip`` known-name
    check says so, when it is a dotless single label (a Docker/compose
    service name like ``paperless`` can never be public DNS), or when it
    is an IP literal in a range the egress gate itself treats as private
    (CGNAT and friends, via ``is_ip_blocked``). A multi-label private-DNS
    hostname (``paperless.internal`` → 10.x) is still flagged even though
    nothing would leave the box — the banner over-warns in the safe
    direction and is dismissible. Fails toward silence on any error.
    """
    if acknowledged or not active:
        return None
    try:
        import ipaddress

        from ..network_utils import is_private_ip
        from ..ssrf_validator import is_ip_blocked

        def _looks_local(host: str) -> bool:
            # IP literals FIRST: an IPv6 literal contains no dot, so the
            # dotless-service-name shortcut below would otherwise swallow
            # every IPv6 address, public ones included.
            try:
                ipaddress.ip_address(host)
            except ValueError:
                # Not an IP literal: known-private/localhost names, or a
                # dotless single label (a Docker/compose service name can
                # never be public DNS). Multi-label hostnames can't be
                # verified without DNS and stay flagged.
                return is_private_ip(host) or "." not in host
            # IP literal: defer to the gate's own private classification
            # (covers CGNAT etc. that is_private_ip doesn't know about).
            return is_ip_blocked(
                host, allow_localhost=False, allow_private_ips=False
            ) and not is_ip_blocked(
                host, allow_localhost=True, allow_private_ips=True
            )

        flagged = sorted(
            {h for h in _extract_engine_hosts(urls) if not _looks_local(h)}
        )
    except Exception:  # noqa: silent-exception - advisory only
        return None
    labels = flagged + ([extra] if extra else [])
    if not labels:
        return None

    subject = ", ".join(labels)
    action_url = (
        f"/settings#setting-{url_setting.replace('.', '-')}"
        if url_setting
        else "/settings"
    )
    return {
        "type": f"{engine_name}_public_url",
        "icon": "📤",
        "title": f"{display_name} points at a non-private host",
        "message": (
            f"Your {display_name} engine is configured for {subject}, "
            "which does not look like a private/local address. Searches "
            "send queries about your private documents to that host, and "
            "under a Private-only egress scope the engine is excluded from "
            "runs entirely. If this is a deliberate, trusted hosted "
            "endpoint, dismiss this warning. (Multi-label private-DNS "
            "hostnames cannot be verified without a lookup and may be "
            "flagged incorrectly.)"
        ),
        "dismissKey": f"app.warnings.dismiss_{engine_name}_public_url",
        "actionUrl": action_url,
        "actionLabel": f"Review {display_name} settings",
    }


def check_cloud_llm_enabled(
    provider: str,
    require_local_endpoint: bool,
    acknowledged: bool,
) -> Optional[dict]:
    """Banner when the configured LLM provider is cloud-only and the
    require-local-endpoint toggle is off.

    Critical-severity: the user's full prompt content (including the
    research query and all retrieved context) is sent to the provider on
    every call.
    """
    if acknowledged:
        return None
    if require_local_endpoint:
        return None
    if not provider or provider.lower() not in _CLOUD_LLM_PROVIDERS:
        return None

    return {
        "type": "cloud_llm_enabled",
        "icon": "☁️",
        "title": "LLM provider is cloud-hosted",
        "message": (
            f"Your LLM provider ({provider}) is cloud-hosted. Query "
            "content will be sent off-machine on every research run, "
            "independent of the Egress Scope setting. Tick 'Require local "
            "LLM endpoint' below if you want fully local inference."
        ),
        "dismissKey": "app.warnings.dismiss_cloud_llm",
        "actionUrl": "#llm_require_local_endpoint",
        "actionLabel": "Require local LLM",
    }


def check_cloud_embeddings_enabled(
    embeddings_provider: str,
    embeddings_base_url: str,
    require_local_embeddings: bool,
    acknowledged: bool,
) -> Optional[dict]:
    """Banner when the embeddings provider sends data off-machine on
    indexing.

    Highest-severity of the three: indexing a private corpus with OpenAI
    embeddings POSTs every chunk to OpenAI's API. A user who is unaware
    of this loses their corpus.

    Suppressed when ``base_url`` is set to a local URL (LM Studio,
    vLLM, llama.cpp), since the OpenAI provider type is then pointed at
    a local endpoint.
    """
    if acknowledged:
        return None
    if require_local_embeddings:
        return None
    if (embeddings_provider or "").lower() != "openai":
        return None

    # If base_url is set and points to a local hostname, suppress the
    # warning — the user has configured OpenAI-compatible-but-local
    # (LM Studio, vLLM, etc.). Uses is_private_ip so RFC1918 ranges
    # (10.x, 172.16-31.x, 192.168.x), CGNAT, link-local, IPv6 private,
    # and .local mDNS hosts are all recognised. Substring-matching
    # against a small list missed legitimate private-network endpoints.
    #
    # NOTE: this is a LITERAL-IP / .local check only — it does NOT resolve
    # DNS, unlike the enforcing evaluate_embeddings (which DNS-resolves via
    # _classify_host). So a private-DNS base_url (e.g. my-llm.internal → 10.x)
    # is treated as LOCAL by enforcement (nothing leaves) yet still shows this
    # banner. That divergence is intentional: this advisory runs on every
    # settings-page render and must stay synchronous/cheap, so it deliberately
    # over-warns (the safe direction) rather than add a blocking DNS lookup to
    # page load. The banner never blocks anything; the PDP is the source of
    # truth for what actually egresses.
    if embeddings_base_url:
        try:
            from urllib.parse import urlsplit

            from ..network_utils import is_private_ip

            parsed = urlsplit(embeddings_base_url)
            hostname = parsed.hostname
            if hostname and is_private_ip(hostname):
                return None
        except Exception:  # noqa: silent-exception
            # Defensive: if URL parsing fails, fall through to issuing
            # the banner — the failure itself is a sign of misconfig
            # the user should see, not a reason to mask the warning.
            pass

    return {
        "type": "cloud_embeddings_enabled",
        "icon": "📤",
        "title": "Document chunks will be sent to OpenAI",
        "message": (
            "Your embeddings provider is OpenAI. Indexing a collection "
            "will POST every chunk to OpenAI's API — the entire corpus "
            "leaves the machine. Tick 'Require local embeddings' below "
            "to switch to sentence-transformers or a local OpenAI-"
            "compatible endpoint."
        ),
        "dismissKey": "app.warnings.dismiss_cloud_embeddings",
        "actionUrl": "#embeddings_require_local",
        "actionLabel": "Require local embeddings",
    }