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.

59.0 KB
Raw Blame
"""A library/collection engine is only as contained as its VECTOR STORE.

Both twins of the hardcoded ``library`` / ``collection_*`` branch —
``policy.classify_engine`` (the scope PEP) and
``run_classification.engine_label`` (the two-axis resolver) — used to return
"contained / local" unconditionally. That holds only while the RAG index is a
local file. A server-backed store on a public endpoint ships the collection's
embeddings and every query to a third party, so both must fail UP, exactly as
Paperless/Elasticsearch do via ``url_setting``.

The discriminator is ``BaseVectorStore.is_local_file``, so these tests use
synthetic store classes rather than any concrete backend: nothing here depends
on a particular remote implementation, and the local-file case must be a strict
no-op (see ``test_local_file_store_*``).
"""

from __future__ import annotations

from contextlib import ExitStack
from unittest.mock import patch

import pytest

from local_deep_research.security.egress import policy as P
from local_deep_research.security.egress import run_classification as rc
from local_deep_research.security.egress.classification import (
    Exposure,
    Label,
    Sensitivity,
)

POLICY = "local_deep_research.security.egress.policy"

URI_SETTING = "vector_store.test.uri"
# A public IP LITERAL, so the classifier answers "public" from the address
# itself — these tests never touch DNS.
PUBLIC_URI = {URI_SETTING: "https://93.184.216.34:19530"}
LOCAL_URI = {URI_SETTING: "http://127.0.0.1:19530"}
LOCAL_PATH = {URI_SETTING: "./store.db"}


class _LocalFileStore:
    """Stands in for FAISS: the index is a file on this machine."""

    provider_key = "faiss"
    is_local_file = True


class _RemoteStore:
    """Stands in for ANY server-backed store (Milvus/Qdrant/pgvector/...)."""

    provider_key = "remote"
    is_local_file = False
    uri_setting = URI_SETTING


class _RemoteStoreNoUriSetting:
    """A server-backed store that never declared where its endpoint lives."""

    provider_key = "undeclared"
    is_local_file = False


def ctx(scope: str = "both", username=None) -> P.EgressContext:
    return P.EgressContext(
        scope=P.EgressScope(scope),
        primary_engine="library",
        require_local_llm=False,
        require_local_embeddings=False,
        username=username,
    )


def _store(cls, providers=("remote",)):
    """Patch the vector-store layer to report ``cls`` for ``providers``."""
    stack = ExitStack()
    stack.enter_context(
        patch(f"{POLICY}._vector_store_class", lambda provider: cls)
    )
    stack.enter_context(
        patch(
            f"{POLICY}._resolve_vector_store_providers",
            return_value=set(providers),
        )
    )
    stack.enter_context(
        patch(f"{POLICY}._resolve_collection_is_public", return_value=False)
    )
    return stack


# ---------------------------------------------------------------------------
# Local-file store: strict no-op (the FAISS user's classification must not move)
# ---------------------------------------------------------------------------


def test_local_file_store_is_contained():
    with _store(_LocalFileStore, providers=("faiss",)):
        assert P.vector_store_is_contained("library", ctx(), {}) is True


def test_local_file_store_ignores_a_public_uri_setting():
    """A local-file store never consults a URI — no DNS, no fail-up."""
    with _store(_LocalFileStore, providers=("faiss",)):
        with patch(f"{POLICY}._classify_engine_url") as classify_url:
            assert (
                P.vector_store_is_contained("library", ctx(), PUBLIC_URI)
                is True
            )
        classify_url.assert_not_called()


def test_local_file_store_label_unchanged():
    with _store(_LocalFileStore, providers=("faiss",)):
        assert rc.engine_label("library", PUBLIC_URI, ctx()) == Label(
            Sensitivity.SENSITIVE, Exposure.CONTAINED
        )
        assert rc.engine_label("collection_x", PUBLIC_URI, ctx()) == Label(
            Sensitivity.SENSITIVE, Exposure.CONTAINED
        )


def test_local_file_store_classification_unchanged():
    with _store(_LocalFileStore, providers=("faiss",)):
        assert P.classify_engine(
            "collection_x", ctx(), settings_snapshot=PUBLIC_URI
        ) == P.EngineClassification(is_public=False, is_local=True)


def test_local_file_store_still_allowed_under_private_only():
    with _store(_LocalFileStore, providers=("faiss",)):
        assert (
            P.evaluate_engine(
                "collection_x",
                ctx("private_only"),
                settings_snapshot=PUBLIC_URI,
            ).allowed
            is True
        )


def test_default_install_is_contained_without_patching():
    """End to end on the real provider registry: today's default is FAISS."""
    with patch(
        f"{POLICY}._resolve_index_vector_store_providers", return_value=set()
    ):
        assert P.vector_store_is_contained("library", ctx(), {}) is True


# ---------------------------------------------------------------------------
# Non-local-file store on a public endpoint: both twins fail up
# ---------------------------------------------------------------------------


def test_remote_store_on_public_uri_is_not_contained():
    with _store(_RemoteStore):
        assert (
            P.vector_store_is_contained("library", ctx(), PUBLIC_URI) is False
        )


def test_engine_label_fails_up_to_exposing():
    """Twin A: the two-axis resolver."""
    with _store(_RemoteStore):
        assert rc.engine_label("collection_x", PUBLIC_URI, ctx()) == Label(
            Sensitivity.SENSITIVE, Exposure.EXPOSING
        )
        assert rc.engine_label("library", PUBLIC_URI, ctx()) == Label(
            Sensitivity.SENSITIVE, Exposure.EXPOSING
        )


def test_classify_engine_fails_up_preserves_privacy():
    """Twin B: the scope PEP. Losing containment must be a pure TIGHTENING —
    it strips ``is_local`` but must PRESERVE the collection's real (private)
    ``is_public`` flag rather than overwriting it with True. Overwriting it
    would make a private collection newly ELIGIBLE under PUBLIC_ONLY just
    because its store went remote, which is a relaxation, not a
    tightening."""
    with _store(_RemoteStore):
        assert P.classify_engine(
            "collection_x", ctx(), settings_snapshot=PUBLIC_URI
        ) == P.EngineClassification(is_public=False, is_local=False)


def test_private_only_denies_a_remote_vector_store():
    with _store(_RemoteStore):
        decision = P.evaluate_engine(
            "collection_x", ctx("private_only"), settings_snapshot=PUBLIC_URI
        )
    assert decision.allowed is False
    # Neither axis holds (not local: store left the box; not public: the
    # collection is genuinely private) so the fail-closed "unclassified"
    # check fires before the scope-specific PRIVATE_ONLY check does.
    assert decision.reason == "unclassified"


def test_private_only_denies_even_a_public_collection():
    """``is_public`` is about the DATA, not about where the index lives."""
    stack = _store(_RemoteStore)
    with stack:
        stack.enter_context(
            patch(f"{POLICY}._resolve_collection_is_public", return_value=True)
        )
        assert (
            P.evaluate_engine(
                "collection_x",
                ctx("private_only"),
                settings_snapshot=PUBLIC_URI,
            ).allowed
            is False
        )


def test_unrestricted_scope_denies_a_private_remote_store():
    """A PRIVATE collection whose store isn't contained satisfies neither
    axis (not local: the store left the box; not public: the content is
    private) and must be denied even under the permissive default (BOTH)
    scope — losing containment must never grant new eligibility, in any
    scope, to a collection that was never marked public."""
    with _store(_RemoteStore):
        decision = P.evaluate_engine(
            "collection_x", ctx(), settings_snapshot=PUBLIC_URI
        )
    assert decision.allowed is False
    assert decision.reason == "unclassified"


def test_unrestricted_scope_still_allows_a_remote_store_when_public():
    """The fail-up tightens PRIVATE_ONLY; it must not break the default scope
    for a collection that IS actually marked public — ``is_public`` is
    preserved (not overwritten), so a public collection stays eligible under
    BOTH/PUBLIC_ONLY regardless of where its store lives."""
    stack = _store(_RemoteStore)
    with stack:
        stack.enter_context(
            patch(f"{POLICY}._resolve_collection_is_public", return_value=True)
        )
        assert P.classify_engine(
            "collection_x", ctx(), settings_snapshot=PUBLIC_URI
        ) == P.EngineClassification(is_public=True, is_local=False)
        assert (
            P.evaluate_engine(
                "collection_x", ctx(), settings_snapshot=PUBLIC_URI
            ).allowed
            is True
        )


def test_remote_store_with_a_sensitive_peer_source_denies_the_run():
    """Quadrant 4: a sensitive collection whose sink is off-box can't coexist
    with another sensitive source."""
    with _store(_RemoteStore):
        decision = rc.audit_run(
            PUBLIC_URI,
            ctx(),
            engines=["collection_x", "library"],
            llm_provider=None,
        )
    assert decision.allowed is False
    assert decision.reason == "sensitive_to_exposing_search"


# ---------------------------------------------------------------------------
# Non-local-file store that IS on this machine: contained (no false positive)
# ---------------------------------------------------------------------------


def test_remote_store_on_a_local_host_is_contained():
    with _store(_RemoteStore):
        assert P.vector_store_is_contained("library", ctx(), LOCAL_URI) is True
        assert rc.engine_label("library", LOCAL_URI, ctx()) == Label(
            Sensitivity.SENSITIVE, Exposure.CONTAINED
        )


def test_remote_store_on_a_local_file_path_is_contained():
    """An embedded/lite mode pointed at a filesystem path never leaves the box."""
    with _store(_RemoteStore):
        assert P.vector_store_is_contained("library", ctx(), LOCAL_PATH) is True


# ---------------------------------------------------------------------------
# Fail direction when the store cannot be resolved
# ---------------------------------------------------------------------------


def test_unset_uri_fails_up():
    with _store(_RemoteStore):
        assert P.vector_store_is_contained("library", ctx(), {}) is False


def test_undeclared_uri_setting_fails_up():
    with _store(_RemoteStoreNoUriSetting, providers=("undeclared",)):
        assert (
            P.vector_store_is_contained("library", ctx(), PUBLIC_URI) is False
        )


def test_unknown_provider_class_fails_up():
    """A configured provider whose class can't be loaded is assumed remote."""
    with _store(None, providers=("mystery",)):
        assert P.vector_store_is_contained("library", ctx(), {}) is False


def test_unloadable_default_provider_stays_contained():
    """...unless the key itself is a known local-file store: a FAISS install
    with a broken/absent driver must not start denying local runs."""
    with _store(None, providers=("faiss",)):
        assert P.vector_store_is_contained("library", ctx(), {}) is True


def test_unreachable_vector_store_layer_keeps_the_status_quo():
    """No provider resolvable at all -> the run can't build a store either, so
    nothing can leave the box; classification stays contained."""
    with patch(f"{POLICY}._resolve_vector_store_providers", return_value=set()):
        assert P.vector_store_is_contained("library", ctx(), PUBLIC_URI) is True


def test_recorded_index_provider_tightens_the_settings_default():
    """A collection indexed into a remote store counts even when the current
    default setting says local-file."""

    def _cls(provider):
        return _LocalFileStore if provider == "faiss" else _RemoteStore

    with patch(f"{POLICY}._vector_store_class", _cls):
        with patch(
            f"{POLICY}._resolve_index_vector_store_providers",
            return_value={"remote"},
        ):
            assert (
                P.vector_store_is_contained("library", ctx(), PUBLIC_URI)
                is False
            )


def test_index_provider_lookup_failure_is_soft():
    """An unavailable DB must not break classification: the settings-resolved
    provider (FAISS today) remains the primary signal.

    ``RAGIndex.vector_store_provider`` does not exist on the CURRENT schema,
    so the ``column is None`` guard in
    ``_resolve_index_vector_store_providers`` returns ``set()`` before
    ``get_user_db_session`` is ever called — patching only the session opener
    (as this test used to) never reaches the DB call or its ``except``, so it
    passed unconditionally regardless of what was patched. Patch the column
    onto the model too (``create=True`` — the attribute is genuinely absent
    today, this is simulating the future schema) so the test actually drives
    execution into the ``with get_user_db_session(...)`` block and exercises
    the failure path this test is named for.
    """
    from local_deep_research.database.models.library import RAGIndex

    with patch.object(RAGIndex, "vector_store_provider", object(), create=True):
        with patch(
            "local_deep_research.database.session_context.get_user_db_session",
            side_effect=RuntimeError("db unavailable"),
        ):
            assert (
                P._resolve_index_vector_store_providers("library", None)
                == set()
            )
            assert P.vector_store_is_contained("library", ctx(), {}) is True


# ---------------------------------------------------------------------------
# The filesystem-path discriminator
# ---------------------------------------------------------------------------


def test_local_filesystem_uri_recognition():
    for value in ("./store.db", "/var/lib/store.db", "~/store.db"):
        assert P._is_local_filesystem_uri(value) is True
    assert P._is_local_filesystem_uri("C:\\data\\store.db") is True
    assert P._is_local_filesystem_uri("file:///var/lib/store.db") is True


def test_file_uri_with_remote_authority_is_not_a_local_file():
    """A ``file://`` URI is local ONLY for the RFC 8089 local forms — an
    empty authority (``file:///path``) or ``localhost``
    (``file://localhost/path``). Any other authority is a remote-share
    reference that some resolvers treat as a host (e.g.
    ``file://attacker.example.com/share/store.db``); it must NOT be
    classified as on-box, so it falls through to the DNS/remote path
    instead of short-circuiting the containment check."""
    assert P._is_local_filesystem_uri("file:///var/lib/x") is True
    assert P._is_local_filesystem_uri("file://localhost/var/lib/x") is True
    assert P._is_local_filesystem_uri("FILE://LOCALHOST/var/lib/x") is True
    assert (
        P._is_local_filesystem_uri("file://attacker.example.com/share/db")
        is False
    )
    assert P._is_local_filesystem_uri("file://evil-host/share/db") is False


def test_file_uri_with_remote_authority_endpoint_is_not_contained():
    """End-to-end: a store whose ``uri_setting`` is a ``file://`` URI naming
    a remote authority must be classified as REMOTE (not contained), not
    silently treated as on-box just because it uses the ``file`` scheme."""
    remote_file_uri = {URI_SETTING: "file://attacker.example.com/share/db"}
    with _store(_RemoteStore):
        assert (
            P.vector_store_is_contained("library", ctx(), remote_file_uri)
            is False
        )

    # The RFC 8089 local forms remain contained.
    for local_uri in (
        "file:///var/lib/store.db",
        "file://localhost/var/lib/store.db",
    ):
        with _store(_RemoteStore):
            assert (
                P.vector_store_is_contained(
                    "library", ctx(), {URI_SETTING: local_uri}
                )
                is True
            )


def test_hostish_values_are_not_treated_as_paths():
    """Anything that could be a host must go through the DNS classifier."""
    for value in (
        "store.db",
        "example.com:19530",
        "https://example.com",
        "",
        None,
        b"/store.db",
    ):
        assert P._is_local_filesystem_uri(value) is False


def test_unc_path_is_not_treated_as_a_local_file():
    """A Windows UNC network path (``\\\\host\\share``) must NOT be mistaken for
    a local file: it is not a recognised path prefix, so it falls through to the
    DNS classifier and fails up rather than being silently classified on-box."""
    assert P._is_local_filesystem_uri("\\\\server\\share\\store.db") is False


def test_windows_forward_slash_drive_is_a_local_file():
    """``C:/data/store.db`` (forward-slash drive path) is still a local file."""
    assert P._is_local_filesystem_uri("C:/data/store.db") is True


def test_file_uri_four_slash_unc_in_path_is_not_a_local_file():
    """``file:////host/share`` (4+ slashes) parses with an EMPTY netloc —
    ``urlsplit`` only ever gives the authority meaning to the first two
    slashes after the scheme, so the 3rd/4th slash just start the PATH —
    but that path then begins with ``//host/share``, a UNC-style
    remote-share reference smuggled past the empty-netloc check. This must
    NOT be classified as on-box."""
    assert (
        P._is_local_filesystem_uri("file:////attacker.example.com/share/db")
        is False
    )
    assert P._is_local_filesystem_uri("file:////host/share/db") is False


def test_file_uri_windows_drive_authority_is_a_local_file():
    """``file://C:/data/store.db`` — the Windows drive-letter form some
    resolvers emit — parses with ``netloc == "C:"`` (a bare drive letter and
    colon, not a real authority). This RFC 8089 local form must be
    recognised as on-box, not misclassified as a remote host named "C"."""
    assert P._is_local_filesystem_uri("file://C:/data/store.db") is True
    assert P._is_local_filesystem_uri("FILE://c:/data/store.db") is True


def test_malformed_file_uri_authority_does_not_raise():
    """A ``file://`` URI whose authority is an unbalanced IPv6-literal
    bracket makes ``urlsplit`` raise ``ValueError: Invalid IPv6 URL`` instead
    of returning a parse. Unlike its sibling in ``_classify_engine_url``
    (which already wraps this identical call), the ``urlsplit`` call here
    used to be bare, so the exception propagated out of
    ``_is_local_filesystem_uri`` and up through
    ``_vector_store_endpoint_is_local`` -> ``vector_store_is_contained`` ->
    ``classify_engine`` into ``_resolve_adaptive_scope``'s catch-all, which
    resolves the PERMISSIVE ``EgressScope.BOTH`` — exactly the cloud-capable
    scope this module's fail-up rules exist to avoid admitting a remote
    endpoint into. Must instead fail toward not-local, like every other
    unrecognised form in this function."""
    for value in (
        "file://[attacker.example.com/share/db",
        "file://[::1/db",
    ):
        assert P._is_local_filesystem_uri(value) is False


def test_malformed_file_uri_does_not_widen_adaptive_scope_to_both():
    """End-to-end: a private, non-contained collection whose store endpoint
    is a malformed ``file://[...`` URI must resolve ADAPTIVE to the
    restrictive PRIVATE_ONLY, not the permissive BOTH the pre-fix
    ``ValueError`` used to force via the catch-all in
    ``_resolve_adaptive_scope``.

    What this pins is the END-TO-END outcome, not any one guard: today the
    ``ValueError`` is absorbed by ``_file_uri_names_a_remote_share`` in
    ``_vector_store_endpoint_is_local``, one frame BEFORE
    ``_is_local_filesystem_uri`` is reached, so reverting the ``try``/
    ``except`` inside ``_is_local_filesystem_uri`` would NOT flip this
    assertion. That revert is caught by
    ``test_malformed_file_uri_authority_does_not_raise`` (the unit test
    above); this test catches the loss of the fail-closed *result* through
    whichever frame absorbs it."""
    malformed_uri = {
        URI_SETTING: "file://[attacker.example.com/share/db",
    }
    with _store(_RemoteStore):
        resolved = P._resolve_adaptive_scope(
            "collection_x",
            malformed_uri,
            username=None,
            local_hostnames=(),
        )
    assert resolved == P.EgressScope.PRIVATE_ONLY
    assert resolved != P.EgressScope.BOTH


def test_single_letter_scheme_authority_is_not_a_drive_path():
    """``x://host:port`` must NOT be classified as a Windows drive-absolute
    path just because it matches ``<letter>:<sep>``: what follows the
    "drive letter" is a DOUBLE separator ("//"), which is a URL authority
    marker, not a path — so a vector-store endpoint written this way must
    fall through to the DNS classifier instead of short-circuiting local.
    ``str.isalpha()`` is Unicode-wide, so a Cyrillic or fullwidth lookalike
    scheme letter must be rejected the same way."""
    for value in (
        "x://attacker.example.com:19530",
        "с://attacker.example.com:19530",  # Cyrillic "es", not ASCII "c"
        "\uff23://attacker.example.com:19530",  # fullwidth "C"
    ):
        assert P._is_local_filesystem_uri(value) is False
    # Real Windows drive-absolute paths (single separator after the drive
    # letter, no authority) are unaffected.
    assert P._is_local_filesystem_uri("C:\\data\\store.db") is True
    assert P._is_local_filesystem_uri("C:/data/store.db") is True
    assert P._is_local_filesystem_uri("file://C:/data/store.db") is True


def test_single_letter_scheme_endpoint_is_not_contained():
    """End-to-end: a store whose ``uri_setting`` uses a single-letter-scheme
    authority (rejected as a local FILE path by the test above) must be
    classified REMOTE by the DNS classifier it falls through to, not
    silently treated as an on-box drive path."""
    drive_lookalike = {URI_SETTING: "x://93.184.216.34:19530"}
    with _store(_RemoteStore):
        assert (
            P.vector_store_is_contained("library", ctx(), drive_lookalike)
            is False
        )


def test_unicode_drive_letter_path_is_not_a_local_file():
    """``<letter>:/path`` is a Windows drive-absolute path ONLY for an ASCII
    drive letter. ``str.isalpha()`` is Unicode-wide, so "\u0441:/data/store.db"
    (Cyrillic "es"), "\uff23:/data/store.db" (fullwidth "C") and
    "\u00e9:/data/store.db" matched the drive-path branch and short-circuited
    LOCAL — skipping host classification entirely on a value no resolver
    reads as a drive path. This is the bare-path twin of the ``file://``
    netloc check pinned by
    ``test_unicode_drive_letter_authority_is_not_a_local_file``.

    WHICH REVERT EACH CASE CATCHES: dropping the ``isascii()`` guard from the
    drive-letter branch of ``_is_local_filesystem_uri`` flips every attack row
    here to True (and the end-to-end row below to contained). The literal-"//"
    authority check does NOT catch them — these have a SINGLE separator after
    the colon, so that guard never fires."""
    for value in (
        "\u0441:/data/store.db",  # Cyrillic "es", not ASCII "c"
        "\uff23:/data/store.db",  # fullwidth "C"
        "\u00e9:/data/store.db",  # accented "e"
        "\u0441:\\data\\store.db",  # backslash separator twin
    ):
        assert P._is_local_filesystem_uri(value) is False
    # Real ASCII drive-absolute paths (either case, either separator) are
    # unaffected.
    for value in ("C:/data/store.db", "c:\\data\\store.db", "Z:/x"):
        assert P._is_local_filesystem_uri(value) is True


def test_unicode_drive_letter_path_endpoint_is_not_contained():
    """End-to-end twin of the test above: a store whose ``uri_setting`` is a
    Unicode drive-letter lookalike PATH must not be silently treated as
    on-box. ``allow_dns=False`` keeps this off the network — the lookalike is
    not a literal IP, so the classifier it falls through to answers
    "undetermined", which is not contained. Restoring the Unicode-wide
    ``isalpha()`` makes the file-path check short-circuit LOCAL instead and
    fails this."""
    with _store(_RemoteStore):
        assert (
            P.vector_store_is_contained(
                "library",
                ctx(),
                {URI_SETTING: "\u0441:/data/store.db"},
                allow_dns=False,
            )
            is False
        )


def test_unicode_drive_letter_authority_is_not_a_local_file():
    """``file://<letter>:/path`` is the Windows drive-authority form ONLY
    for an ASCII drive letter. ``str.isalpha()`` is Unicode-wide, so a
    Cyrillic, fullwidth, or accented lookalike authority names a HOST to
    any resolver that reads the netloc, not an on-box drive. Restoring the
    Unicode-wide check (dropping the ``isascii()`` guard) re-classifies all
    three attack forms LOCAL and fails these assertions."""
    for value in (
        "file://\u0441:/share/db",  # Cyrillic "es", not ASCII "c"
        "file://\uff23:/share/db",  # fullwidth "C"
        "file://\u00e9:/share/db",  # accented "e"
    ):
        assert P._is_local_filesystem_uri(value) is False
    # Real Windows drive-letter authorities (ASCII, either case) are
    # unaffected.
    assert P._is_local_filesystem_uri("file://C:/data/store.db") is True
    assert P._is_local_filesystem_uri("file://c:/data/store.db") is True


def test_unicode_drive_letter_authority_endpoint_is_not_contained():
    """End-to-end: a store whose ``uri_setting`` uses a Unicode drive-letter
    lookalike authority (rejected as a local FILE path by the test above)
    must NOT be silently treated as an on-box drive path. ``allow_dns=False``
    keeps this test off the network: the lookalike authority is not a literal
    IP, so the DNS classifier it falls through to answers "undetermined",
    which is not contained — restoring the Unicode-wide ``isalpha()`` makes
    the file-path check short-circuit LOCAL instead and fails this."""
    unicode_drive = {URI_SETTING: "file://\u0441:/share/db"}
    with _store(_RemoteStore):
        assert (
            P.vector_store_is_contained(
                "library", ctx(), unicode_drive, allow_dns=False
            )
            is False
        )
    # The ASCII drive-authority form (either case) stays contained.
    for local_uri in ("file://C:/data/store.db", "file://c:/data/store.db"):
        with _store(_RemoteStore):
            assert (
                P.vector_store_is_contained(
                    "library", ctx(), {URI_SETTING: local_uri}
                )
                is True
            )


# ---------------------------------------------------------------------------
# Parser differential: the guards and ``urlsplit`` must read the same string
# ---------------------------------------------------------------------------

# Each entry is (id, uri). Every one names ``attacker.example.com`` off-box
# while carrying a control character that ``urllib.parse.urlsplit`` deletes
# (tab/CR/LF anywhere, leading C0 controls) but ``str.strip()`` does not.
_PARSER_DIFFERENTIAL_URIS = (
    ("tab_in_file_scheme", "fi\tle://localhost//attacker.example.com/share/db"),
    ("cr_in_file_scheme", "fi\rle://localhost//attacker.example.com/share/db"),
    ("lf_in_file_scheme", "fi\nle://localhost//attacker.example.com/share/db"),
    ("nul_before_file_scheme", "\x00file://localhost//attacker.example.com/db"),
    ("c0_before_file_scheme", "\x01file://localhost//attacker.example.com/db"),
    ("tab_in_drive_separator", "x:/\t/attacker.example.com:19530"),
    ("cr_in_drive_separator", "x:/\r/attacker.example.com:19530"),
    ("tab_in_bare_double_separator", "/\t/attacker.example.com/share/db"),
)


@pytest.mark.parametrize(
    "uri",
    [uri for _, uri in _PARSER_DIFFERENTIAL_URIS],
    ids=[name for name, _ in _PARSER_DIFFERENTIAL_URIS],
)
def test_control_characters_cannot_smuggle_a_remote_store_past_the_guards(uri):
    """A control character must not split the containment guards away from
    the URL parser that decides the host.

    ``urlsplit`` follows the WHATWG parser: it DELETES every tab, CR and LF
    from anywhere in a URL and lstrips C0 controls. ``str.strip()`` removes
    neither ``"\\x00"`` nor a tab in the middle of a string. So while
    ``_file_uri_names_a_remote_share`` and ``_is_local_filesystem_uri``
    judged the RAW value, each URI here was simultaneously "not a
    ``file://`` URI" / "a local drive path" to them and a local-looking
    ``file://`` URI / an ``attacker.example.com:19530`` authority to
    ``_classify_engine_url`` — every one of them classified CONTAINED.

    WHICH REVERT EACH CASE CATCHES (measured, one independent cause per
    row):

    * ``*_drive_separator`` / ``*_double_separator`` — held ONLY by the
      ``_normalize_uri_like_url_parser`` call in ``_is_local_filesystem_uri``.
      Revert that to ``value.strip()`` and all three short-circuit LOCAL with
      no host classification at all. The parsed-scheme gate is irrelevant to
      them (their scheme is ``x``/empty, never ``file``).
    * ``*_file_scheme`` — held ONLY by the parsed-scheme gate in
      ``_file_uri_names_a_remote_share``. Revert that to
      ``value.lower().startswith("file://")`` and all five classify
      CONTAINED. The ``_is_local_filesystem_uri`` normalisation is irrelevant
      to them: it answers "not a local file" for these either way, and the
      containment then comes from the classifier, not from it.

    ``allow_dns=False`` keeps this off the network, but it does NOT by itself
    make these values non-contained. The two ``*_drive_separator`` rows reach
    ``_classify_engine_url`` as
    ``"http://x:/<CTL>/attacker.example.com:19530"``, whose hostname is the
    bare ``"x"`` — not a literal IP, so the classifier answers
    "undetermined". ``tab_in_bare_double_separator`` reaches it as
    ``"http:///<TAB>/attacker.example.com/share/db"``, which after the
    parser's tab deletion has netloc ``""`` and ``hostname is None``, so the
    entry is skipped for having no host at all. The ``*_file_scheme`` values
    parse to authority ``localhost``, which classifies LOCAL with no DNS at
    all — that is exactly why the network-share rejection has to see them,
    and why reverting the gate flips those five rows to contained."""
    with _store(_RemoteStore):
        assert (
            P.vector_store_is_contained(
                "library", ctx(), {URI_SETTING: uri}, allow_dns=False
            )
            is False
        )


def test_file_uri_scheme_is_decided_by_the_parser_not_the_prefix():
    """``_file_uri_names_a_remote_share`` must ask ``urlsplit`` what the
    scheme is, not ``str.startswith("file://")``.

    ``file:%2f%2fhost/share`` has scheme ``file`` and a path that decodes to
    the UNC prefix ``//host/share``, but it never contains the literal
    ``"file://"``, so a raw-prefix gate declines to look at it at all.
    Reverting the gate to that prefix test flips these three assertions to
    False. For THESE percent-encoded forms the gate is defence in depth —
    end-to-end they still fail closed via the DNS classifier, which finds no
    local host in them — but the same gate is load-bearing for the
    control-character forms above, whose parsed authority is ``localhost``:
    see
    ``test_control_characters_cannot_smuggle_a_remote_store_past_the_guards``,
    where reverting it flips all five ``*_file_scheme`` rows to CONTAINED.
    Note this function DOES pre-normalise its input, and that call is a
    second guard rather than a no-op: ``urlsplit`` lstrips only
    ``\\x00``-``\\x20``, while the normaliser's final ``strip()`` is
    bidirectional and also removes the 19 leading Unicode-whitespace
    codepoints (U+0085, U+00A0, U+1680, U+2000-U+200A, U+2028, U+2029,
    U+202F, U+205F, U+3000) the parser leaves in place. Drop it and
    "\\xa0file://localhost//attacker.example.com/share/db" parses with an
    EMPTY scheme, so the network-share rejection never sees it — see
    ``test_leading_unicode_whitespace_cannot_hide_a_network_share``."""
    for attack_uri in (
        "file:%2f%2fattacker.example.com/share/db",
        "file:/%2f%2fattacker.example.com/share/db",
        "FILE:%5C%5Cattacker.example.com/share/db",
    ):
        assert P._file_uri_names_a_remote_share(attack_uri) is True
    # A scheme-gated check must not start rejecting the ordinary forms.
    for local_uri in (
        "file:///var/lib/store.db",
        "file://localhost/var/lib/store.db",
        "file://C:/data/store.db",
        "file:relative/store.db",
    ):
        assert P._file_uri_names_a_remote_share(local_uri) is False


# Leading whitespace ``str.strip()`` removes and ``urlsplit`` does NOT.
# ``urlsplit`` follows the WHATWG parser and lstrips only "\x00"-"\x20";
# ``str.strip()`` additionally removes 19 Unicode-whitespace codepoints, and
# a leading one of those makes ``urlsplit`` report an EMPTY scheme.
_LEADING_UNICODE_WHITESPACE = (
    ("nbsp", "\xa0"),
    ("nel", "\x85"),
    ("ideographic_space", "\u3000"),
    ("thin_space", "\u2009"),
)
_SHARE_URI = "file://localhost//attacker.example.com/share/db"


@pytest.mark.parametrize(
    "prefix",
    [prefix for _, prefix in _LEADING_UNICODE_WHITESPACE],
    ids=[name for name, _ in _LEADING_UNICODE_WHITESPACE],
)
def test_leading_unicode_whitespace_cannot_hide_a_network_share(prefix):
    """Leading Unicode whitespace must not hide a UNC share from the parser.

    ``_file_uri_names_a_remote_share`` asks ``urlsplit`` for the scheme, and
    ``urlsplit``'s lstrip covers only "\\x00"-"\\x20". Every prefix here is
    OUTSIDE that range, so on the RAW value the scheme parses as "" — not
    "file" — and the network-share rejection declines to look at the URI at
    all. The ``_normalize_uri_like_url_parser`` call in that function is what
    closes this: its final ``strip()`` is bidirectional and removes exactly
    these codepoints before the parse.

    The LIST shape is where it bites end to end. On the raw value the entry
    also parses to ``hostname is None``, so ``_classify_engine_url`` skips it
    entirely and the sibling "127.0.0.1" — a loopback IP literal, classified
    LOCAL with NO DNS — makes the whole setting answer CONTAINED.

    Measured on every prefix here: drop the ``_normalize_uri_like_url_parser``
    call and hand ``value`` to ``urlsplit`` raw, and BOTH assertions below
    flip — the direct one to False, the containment one to True.
    """
    attack = prefix + _SHARE_URI
    assert P._file_uri_names_a_remote_share(attack) is True
    with _store(_RemoteStore):
        for value in ([attack, "127.0.0.1"], {"value": [attack, "127.0.0.1"]}):
            assert (
                P.vector_store_is_contained(
                    "library", ctx(), {URI_SETTING: value}, allow_dns=False
                )
                is False
            ), value


# Each entry is (id, raw, expected). The expectation is an EXPLICIT string,
# not "whatever ``urlsplit`` happens to agree with": an
# ``urlsplit(f(raw)) == urlsplit(raw)`` assertion holds for the identity
# function and for ``str.strip()`` on every row here EXCEPT
# ``trailing_whitespace_trimmed`` (``urlsplit`` keeps the trailing spaces in
# ``path``, so ``str.strip()`` changes the parse there), so it can only ever
# catch OVER-normalisation, never the under-normalisation that is the
# security direction.
_NORMALISER_CASES = (
    # Interior tab/CR/LF deletion — ``str.strip()`` never touches these, so
    # each of these rows fails under BOTH ``str.strip()`` and the identity.
    (
        "interior_tab_deleted",
        "fi\tle://localhost//attacker.example.com/db",
        "file://localhost//attacker.example.com/db",
    ),
    (
        "interior_cr_lf_deleted",
        "http://loc\r\nalhost:19530",
        "http://localhost:19530",
    ),
    (
        "tab_in_drive_separator_deleted",
        "x:/\t/attacker.example.com:19530",
        "x://attacker.example.com:19530",
    ),
    # Leading NON-whitespace C0 controls — ``str.strip()`` stops at the first
    # of them, so these rows fail under ``str.strip()`` and the identity too.
    (
        "leading_c0_stripped",
        "\x01\x1bfile://localhost/db",
        "file://localhost/db",
    ),
    (
        "leading_nul_after_whitespace_stripped",
        "\t\x00 file:///var/lib/store.db",
        "file:///var/lib/store.db",
    ),
    # Trailing whitespace: the deliberate delta from ``urlsplit`` (which
    # lstrips only). Fails under the identity, passes under ``str.strip()``.
    (
        "trailing_whitespace_trimmed",
        "file:///var/lib/store.db  ",
        "file:///var/lib/store.db",
    ),
    # Over-normalisation controls: an INTERIOR space and a percent-encoded
    # tab are part of the path and must survive untouched. These are the rows
    # that fail if the normaliser starts removing more than the parser does.
    (
        "interior_space_kept",
        "file:///home/u/my files/store.db",
        "file:///home/u/my files/store.db",
    ),
    (
        "percent_encoded_tab_kept",
        "file:///var/lib/%09store.db",
        "file:///var/lib/%09store.db",
    ),
)


@pytest.mark.parametrize(
    ("raw", "expected"),
    [(raw, expected) for _, raw, expected in _NORMALISER_CASES],
    ids=[name for name, _, _ in _NORMALISER_CASES],
)
def test_normaliser_output_is_the_string_urlsplit_would_parse(raw, expected):
    """The normaliser's OUTPUT is pinned to an explicit string per row.

    ``urlsplit`` follows the WHATWG parser: it lstrips C0 controls and space,
    then deletes every tab, CR and LF from anywhere in the URL. The
    normaliser must reproduce that (plus a trailing trim the callers relied
    on before it existed).

    WHICH REPLACEMENT EACH ROW CATCHES: the three deletion rows and the two
    leading-C0 rows fail under BOTH ``str.strip()`` and the identity
    function — ``str.strip()`` never removes an interior tab/CR/LF and stops
    at the first non-whitespace C0 character. ``trailing_whitespace_trimmed``
    fails under the identity only (``str.strip()`` already trims it). The two
    ``*_kept`` rows fail only under an OVER-normaliser: an interior space and
    a ``%09`` are path content the parser preserves, and stripping them would
    be the opposite mistake.

    Deliberately NOT written as ``urlsplit(f(raw)) == urlsplit(raw)``: that
    assertion holds for the identity function and for ``str.strip()`` on
    every row here except ``trailing_whitespace_trimmed`` — for that one row
    ``urlsplit`` keeps the trailing spaces in ``path``
    (``urlsplit("file:///var/lib/store.db  ").path`` is
    ``"/var/lib/store.db  "``), so ``str.strip()`` does change the parse. On
    every other row the assertion can only catch over-normalisation, never
    the under-normalisation this test exists to catch."""
    assert P._normalize_uri_like_url_parser(raw) == expected


# Positive control for the tests above: the ordinary, control-character-
# free forms must keep the classification they had before the normalisation
# was introduced. A normaliser that over-strips would show up here.
_BENIGN_LOCAL_URIS = (
    "/var/lib/ldr/store.db",
    "file:///var/lib/ldr/store.db",
    "file://localhost/var/lib/ldr/store.db",
    "file://C:/data/store.db",
    "C:\\data\\store.db",
    "C:/data/store.db",
    "C:\\\\data\\store.db",
    "C:/\\data\\store.db",
    "C:\\/data/store.db",
    "./data/store.db",
    "~/data/store.db",
    "file:///var/lib/%2fdir/store.db",
    "file:///%252fdir/store.db",
)


@pytest.mark.parametrize("uri", _BENIGN_LOCAL_URIS)
def test_benign_local_uris_stay_contained_after_normalisation(uri):
    """Positive control: normalising the string the guards read must not
    reclassify any ordinary local path or ``file://`` URI. Covers all four
    drive-separator truth-table rows (``C:\\\\``, ``C:/\\``, ``C:\\/``,
    ``C:/``) plus the RFC 8089 and relative-path forms. ``allow_dns=False``
    keeps it off the network — every URI here must be answered by the
    filesystem-path check alone, so a regression that pushes one into the
    DNS classifier fails this too."""
    assert P._is_local_filesystem_uri(uri) is True
    with _store(_RemoteStore):
        assert (
            P.vector_store_is_contained(
                "library", ctx(), {URI_SETTING: uri}, allow_dns=False
            )
            is True
        )


def test_file_uri_percent_encoded_unc_in_path_is_not_a_local_file():
    """A ``file://`` URI may percent-encode its path (RFC 8089), so the
    leading-separator check must run on the DECODED path:
    "file:///%2fattacker.example.com/share/db" and the "%5c" twin read
    as single-separator paths while encoded, but ``unquote`` yields
    exactly the "//host" / "/\\host" UNC prefixes that
    ``ntpath.normpath`` resolves to a remote share on Windows. Reverting
    the decode (checking the literal ``parsed.path`` again, as before
    the fix) makes both attack forms classify LOCAL again — these
    assertions are what catch that revert."""
    assert (
        P._is_local_filesystem_uri("file:///%2fattacker.example.com/share/db")
        is False
    )
    assert (
        P._is_local_filesystem_uri("file:///%5cattacker.example.com/share/db")
        is False
    )
    # The mixed-separator combination of the same encoding, and the
    # localhost-authority form: the netloc check passes for both, so only
    # the decoded-path separator check stands between them and a LOCAL
    # classification.
    assert (
        P._is_local_filesystem_uri("file:///%5c/attacker.example.com/share/db")
        is False
    )
    assert (
        P._is_local_filesystem_uri(
            "file://localhost/%2fattacker.example.com/share/db"
        )
        is False
    )
    # The RFC 8089 local forms stay local after the decode — including a
    # path whose percent-encoding sits AFTER the leading separator, where
    # decoding changes nothing about locality.
    assert P._is_local_filesystem_uri("file:///var/lib/store.db") is True
    assert (
        P._is_local_filesystem_uri("file://localhost/var/lib/store.db") is True
    )
    assert P._is_local_filesystem_uri("file://C:/data/store.db") is True
    assert P._is_local_filesystem_uri("file:///C:/data/store.db") is True
    assert (
        P._is_local_filesystem_uri("file:///home/u/my%20files/store.db") is True
    )


def test_file_uri_percent_encoded_unc_endpoint_is_not_contained():
    """End-to-end: a store whose ``uri_setting`` percent-encodes the UNC
    prefix of its path must be classified REMOTE, exactly like the literal
    four-slash / mixed-slash forms.

    What this pins is the END-TO-END outcome, not the ``unquote`` inside
    ``_is_local_filesystem_uri``: ``_vector_store_endpoint_is_local`` runs
    the same decode-and-check via ``_file_uri_names_a_remote_share`` one
    frame earlier, so reverting that ``unquote`` alone would NOT flip these
    assertions. That revert is caught by
    ``test_file_uri_percent_encoded_unc_in_path_is_not_a_local_file`` (the
    unit test above); this test catches the loss of the fail-closed
    *result* through whichever frame decodes."""
    for attack_uri in (
        "file:///%2fattacker.example.com/share/db",
        "file:///%5cattacker.example.com/share/db",
    ):
        with _store(_RemoteStore):
            assert (
                P.vector_store_is_contained(
                    "library", ctx(), {URI_SETTING: attack_uri}
                )
                is False
            )
    # The plain RFC 8089 local forms remain contained.
    for local_uri in (
        "file:///var/lib/store.db",
        "file://localhost/var/lib/store.db",
        "file://C:/data/store.db",
    ):
        with _store(_RemoteStore):
            assert (
                P.vector_store_is_contained(
                    "library", ctx(), {URI_SETTING: local_uri}
                )
                is True
            )


def test_rejected_file_paths_cannot_be_allowed_by_hostname_fallback():
    """A nominally local authority cannot make a decoded share path local."""
    rejected_uris = (
        "file://localhost/%2freview.invalid/share/db",
        "file://localhost/%5creview.invalid/share/db",
        "file://localhost//review.invalid/share/db",
        "FILE://LOCALHOST/%5c/review.invalid/share/db",
        "file://127.0.0.1/%2freview.invalid/share/db",
        "file://C:/%2freview.invalid/share/db",
        "file://[::1/db",
    )
    with (
        _store(_RemoteStore),
        patch(
            f"{POLICY}._classify_engine_url", return_value=True
        ) as classify_url,
    ):
        for uri in rejected_uris:
            assert not P.vector_store_is_contained(
                "library", ctx(), {URI_SETTING: uri}
            ), uri
            # A setting value need not be a STRING. ``unwrap_setting``
            # returns whatever the snapshot held, uncoerced, and
            # ``_classify_engine_url`` explicitly supports the list shape,
            # so the rejection must run per ENTRY. Reverting it to the
            # string-only guard lets the one-element list skip the
            # rejection and be rescued by the local-looking authority
            # ("localhost" / "127.0.0.1" / drive letter), which the patched
            # classifier above stands in for. Because that patch makes
            # ``_classify_engine_url`` return True unconditionally, ALL SEVEN
            # flip to contained under the revert (unpatched, only the five
            # with a resolvably-local authority would — the "C:" and the
            # unbalanced-bracket forms fail hostname classification on their
            # own), and ``classify_url.assert_not_called()`` fires too.
            assert not P.vector_store_is_contained(
                "library", ctx(), {URI_SETTING: [uri]}
            ), uri
            # ...including through the ``{"value": ...}`` snapshot wrapper
            # ``_get_setting_value`` peels: unwrapping yields the same list,
            # never a string, so this shape needs the same per-entry pass.
            assert not P.vector_store_is_contained(
                "library", ctx(), {URI_SETTING: {"value": [uri]}}
            ), uri
        classify_url.assert_not_called()

        # ANY rejected entry sinks the whole setting, even alongside a
        # legitimately local endpoint: containment is a property of the
        # whole configured endpoint set, mirroring the "any public entry
        # wins" fail-up. Reverting to the string-only guard (or to a
        # first-entry-only / all-entries-must-be-rejected check) makes this
        # mixed list pass as contained via the local entry.
        assert not P.vector_store_is_contained(
            "library",
            ctx(),
            {URI_SETTING: ["http://127.0.0.1:19530", rejected_uris[0]]},
        )
        assert not P.vector_store_is_contained(
            "library",
            ctx(),
            {
                URI_SETTING: {
                    "value": [LOCAL_URI[URI_SETTING], rejected_uris[2]]
                }
            },
        )
        classify_url.assert_not_called()

        # Local paths still pass without DNS, including encoded spaces.
        for uri in (
            "file:///var/lib/store.db",
            "file://localhost/var/lib/store.db",
            "file://C:/data/store.db",
            "file:///home/u/my%20files/store.db",
        ):
            assert P.vector_store_is_contained(
                "library", ctx(), {URI_SETTING: uri}
            ), uri
        classify_url.assert_not_called()

        # Network endpoints still consult the configured host classifier.
        assert P.vector_store_is_contained("library", ctx(), LOCAL_URI)
        classify_url.assert_called_once()


def test_file_uri_truth_table_full_regression():
    """Full before/after truth table for ``_is_local_filesystem_uri``,
    pinning the RFC 8089 local forms, the round-6 fixes (4-slash
    UNC-in-path bypass rejected; Windows drive-letter authority accepted),
    the round-7 mixed-slash UNC fix (``ntpath.normpath`` treats ANY pair of
    leading "/"/"\\" separators as a UNC prefix on Windows, not just "//"),
    the malformed-IPv6-authority fix (a bare ``urlsplit`` used to raise
    instead of returning False), the single-letter-scheme-authority fix
    (``x://host`` used to be misread
    as a Windows drive path), the percent-encoded-UNC fix (the separator
    check now runs on the unquoted path, so a "%2f"/"%5c"-encoded UNC
    prefix can no longer classify local), the Unicode-drive-letter fix (the
    drive-authority netloc check AND the bare drive-path branch now require an
    ASCII letter, so Cyrillic/fullwidth/accented lookalikes no longer classify
    local in either position), the drive-path narrowing (only a literal "//"
    after the drive letter is a scheme authority, so doubled-/mixed-backslash
    drive paths are local again), and every previously-covered row
    unchanged."""
    cases = [
        # (uri, expected, label)
        (
            "file:////attacker.example.com/share/db",
            False,
            "4-slash UNC bypass",
        ),
        (
            "file://[attacker.example.com/share/db",
            False,
            "malformed IPv6-literal authority (unbalanced bracket)",
        ),
        (
            "file://[::1/db",
            False,
            "malformed IPv6-literal authority (truncated)",
        ),
        (
            "x://attacker.example.com:19530",
            False,
            "single-letter-scheme authority, not a drive path",
        ),
        (
            "\u0441://attacker.example.com:19530",
            False,
            "Cyrillic-letter-scheme authority",
        ),
        (
            "\uff23://attacker.example.com:19530",
            False,
            "fullwidth-letter-scheme authority",
        ),
        (
            "file://\u0441:/share/db",
            False,
            "Cyrillic drive-letter authority",
        ),
        (
            "file://\uff23:/share/db",
            False,
            "fullwidth drive-letter authority",
        ),
        (
            "file://\u00e9:/share/db",
            False,
            "accented drive-letter authority",
        ),
        # The bare-path twins: a drive-absolute path needs an ASCII drive
        # letter too, or the drive-path branch short-circuits LOCAL on a
        # value no resolver reads as a drive path.
        (
            "\u0441:/data/store.db",
            False,
            "Cyrillic drive-letter path",
        ),
        (
            "\uff23:/data/store.db",
            False,
            "fullwidth drive-letter path",
        ),
        (
            "\u00e9:\\data\\store.db",
            False,
            "accented drive-letter path (backslash)",
        ),
        (
            "/\\attacker.example.com/share/store.db",
            False,
            "mixed-slash UNC bypass (bare path)",
        ),
        (
            "file:///\\attacker.example.com/share/store.db",
            False,
            "mixed-slash UNC bypass (file:// path)",
        ),
        (
            "\\/attacker.example.com/share/store.db",
            False,
            "mixed-slash UNC bypass (backslash-first bare path)",
        ),
        (
            "file:///%2fattacker.example.com/share/db",
            False,
            "percent-encoded UNC bypass (%2f)",
        ),
        (
            "file:///%5cattacker.example.com/share/db",
            False,
            "percent-encoded UNC bypass (%5c)",
        ),
        ("file://C:/data/store.db", True, "windows drive-letter authority"),
        (
            "file://c:/data/store.db",
            True,
            "windows drive-letter authority (lowercase)",
        ),
        ("file:///var/lib/store.db", True, "RFC8089 empty authority"),
        (
            "file://localhost/var/lib/store.db",
            True,
            "RFC8089 localhost authority",
        ),
        (
            "file:///home/u/my%20files/store.db",
            True,
            "percent-encoded but plainly local path",
        ),
        ("FILE://LOCALHOST/var/lib/x", True, "case-insensitive localhost"),
        (
            "file://attacker.example.com/share/db",
            False,
            "remote host authority",
        ),
        ("file://evil-host/share/db", False, "remote host authority 2"),
        ("./store.db", True, "relative path"),
        ("/var/lib/store.db", True, "absolute path"),
        ("~/store.db", True, "home-relative"),
        ("C:\\data\\store.db", True, "windows backslash drive path"),
        ("C:/data/store.db", True, "windows forward-slash drive path"),
        # A drive letter always wins over a UNC prefix on Windows
        # (``ntpath.normpath("C:\\\\data") == "C:\\data"``) and ``urlsplit``
        # gives an authority only to a literal "//", so these doubled- and
        # mixed-separator drive paths cannot name a remote host under
        # either reading. Re-widening the guard to reject every doubled
        # separator makes them non-contained and locks Windows users out
        # of a legitimately local store.
        (
            "C:\\\\data\\store.db",
            True,
            "windows doubled-backslash drive path",
        ),
        ("C:/\\data/store.db", True, "windows mixed-separator drive path"),
        (
            "C:\\/data/store.db",
            True,
            "windows mixed-separator drive path (backslash first)",
        ),
        # The forward-slash twin stays rejected: "C://data/store.db" is
        # byte-for-byte a single-letter-scheme URL (scheme "c", hostname
        # "data"), indistinguishable from the "x://host" authority the row
        # above pins as remote. Documented in changelog.d/5763.security.md.
        (
            "C://data/store.db",
            False,
            "drive letter + literal // is a scheme authority",
        ),
        ("//attacker.example.com:19530", False, "scheme-less authority"),
        ("//host/path", False, "scheme-less authority 2"),
        ("\\\\server\\share\\store.db", False, "UNC path"),
        ("http://example.com", False, "http url"),
        ("https://example.com", False, "https url"),
        ("", False, "empty string"),
        (None, False, "None"),
        (b"/store.db", False, "bytes"),
    ]
    for uri, expected, label in cases:
        assert P._is_local_filesystem_uri(uri) is expected, (
            f"{label}: expected {expected} for {uri!r}"
        )


def test_mixed_endpoint_list_with_a_public_entry_fails_up():
    """Any public entry in a list-typed ``uri_setting`` wins (fail up): a store
    reachable at both a local and a public endpoint is treated as remote."""
    mixed = {URI_SETTING: ["http://127.0.0.1:19530", PUBLIC_URI[URI_SETTING]]}
    with _store(_RemoteStore):
        assert P.vector_store_is_contained("library", ctx(), mixed) is False


def test_scheme_less_authority_is_not_treated_as_a_local_file():
    """A ``//host[:port]`` URI authority (a scheme-less network endpoint, e.g.
    how a Milvus/vector-store URI can be written) starts with a single ``/``
    just like an absolute path, but it is NOT one: it names a remote host, not
    a filesystem location. Misclassifying it as local would let a remote
    endpoint skip the DNS/egress check entirely (``_vector_store_endpoint_is_local``
    would short-circuit to True), so it must fall through to the DNS
    classifier instead."""
    for value in ("//attacker.example.com:19530", "//host/path"):
        assert P._is_local_filesystem_uri(value) is False
    # A real absolute path (single leading "/") is unaffected.
    assert P._is_local_filesystem_uri("/var/lib/store.db") is True


def test_scheme_less_authority_endpoint_is_not_contained():
    """End-to-end: a store whose ``uri_setting`` is a scheme-less
    ``//host:port`` authority (rejected as a local FILE path by the test
    above) must still be correctly classified as REMOTE by the DNS
    classifier it falls through to, not silently pass as undetermined.

    ``_classify_engine_url`` used to reconstruct the URL to parse as
    ``f"http://{entry}"`` whenever the entry lacked a literal ``"://"``.
    For ``//attacker.example.com:19530`` that produced
    ``http:////attacker.example.com:19530``, which ``urlsplit`` mis-parses
    with an empty netloc and the host stuffed into ``path`` — so
    ``hostname`` was ``None`` and the entry was skipped, leaving the store
    undetermined instead of classified public. Uses a public IP LITERAL
    (matching this file's convention) so the classifier answers from the
    address itself and the test never touches DNS.
    """
    scheme_less_public = {URI_SETTING: "//93.184.216.34:19530"}
    with _store(_RemoteStore):
        assert (
            P.vector_store_is_contained("library", ctx(), scheme_less_public)
            is False
        )
        # "library" is always classified via _resolve_collection_is_public
        # (patched to False by ``_store``), so the fail-up preserves that
        # private flag rather than overwriting it with True.
        assert P.classify_engine(
            "library", ctx(), settings_snapshot=scheme_less_public
        ) == P.EngineClassification(is_public=False, is_local=False)

    # The local counterpart (loopback, still scheme-less) stays contained.
    scheme_less_local = {URI_SETTING: "//127.0.0.1:19530"}
    with _store(_RemoteStore):
        assert (
            P.vector_store_is_contained("library", ctx(), scheme_less_local)
            is True
        )


# ---------------------------------------------------------------------------
# Regression: fail-up must not RELAX a private collection into PUBLIC_ONLY
# eligibility, and ADAPTIVE must not flip a private-collection-primary run
# to PUBLIC_ONLY just because the collection's store isn't contained.
#
# This is dormant on today's default install (only FAISS — a local-file
# store — is registered), but becomes live the moment a server-backed
# vector store is registered. Pure-function coverage; no app/DB import.
# ---------------------------------------------------------------------------


def test_public_only_denies_a_private_remote_collection():
    """A private collection with a non-contained store must NOT become
    newly eligible under PUBLIC_ONLY. Before the fix, the fail-up
    unconditionally set ``is_public=True``, which made this a false PASS."""
    with _store(_RemoteStore):
        decision = P.evaluate_engine(
            "collection_x", ctx("public_only"), settings_snapshot=PUBLIC_URI
        )
    assert decision.allowed is False


def test_adaptive_scope_does_not_flip_private_collection_primary_to_public():
    """``_resolve_adaptive_scope`` must not resolve a private-collection-
    primary run to PUBLIC_ONLY (which would admit public web engines
    alongside the private corpus) merely because the collection's vector
    store isn't contained.

    ROUND-6 FIX: this used to assert a fall-back to the permissive BOTH
    bucket, on the theory that ``evaluate_engine`` denying the collection
    itself (see ``test_public_only_denies_a_private_remote_collection`` /
    ``test_unrestricted_scope_denies_a_private_remote_store`` above) was
    enough. It is NOT enough: BOTH also admits every OTHER public engine
    and lifts the forced-local-LLM/embeddings coupling for the rest of the
    run, silently widening egress for the run as a whole. A private
    non-contained collection buckets (is_public=False, is_local=False) —
    neither exclusive branch in ``_resolve_adaptive_scope`` matches that —
    so it must now resolve to the RESTRICTIVE PRIVATE_ONLY instead (which
    still denies the collection engine itself as "unclassified", exactly as
    before, while ALSO keeping every other engine/inference path local)."""
    with _store(_RemoteStore):
        resolved = P._resolve_adaptive_scope(
            "collection_x",
            PUBLIC_URI,
            username=None,
            local_hostnames=(),
        )
    assert resolved != P.EgressScope.PUBLIC_ONLY
    assert resolved != P.EgressScope.BOTH
    assert resolved == P.EgressScope.PRIVATE_ONLY