~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Escape-hatch (UNPROTECTED scope) tests — ADR-0007 stage C.
UNPROTECTED disables the egress-scope restrictions (the opt-out) but must NOT
lift the hard SSRF / cloud-metadata invariant, and must lift the forced
local-inference requirements.
"""
from __future__ import annotations
import pytest
from local_deep_research.security.egress.policy import (
DEFAULT_EGRESS_SCOPE,
EgressContext,
EgressScope,
PolicyDeniedError,
USER_SELECTABLE_PROTECTED_SCOPES,
context_from_snapshot,
effective_scope_for_display,
evaluate_engine,
evaluate_llm_endpoint,
evaluate_retriever,
evaluate_url,
parse_user_egress_scope,
)
def unprot_ctx(primary: str = "arxiv") -> EgressContext:
return EgressContext(
scope=EgressScope.UNPROTECTED,
primary_engine=primary,
require_local_llm=False,
require_local_embeddings=False,
)
def test_user_selectable_protected_scopes_exclude_internal_and_escape_hatch():
assert USER_SELECTABLE_PROTECTED_SCOPES == {
EgressScope.ADAPTIVE,
EgressScope.PUBLIC_ONLY,
EgressScope.PRIVATE_ONLY,
EgressScope.STRICT,
}
assert EgressScope.BOTH not in USER_SELECTABLE_PROTECTED_SCOPES
assert EgressScope.UNPROTECTED not in USER_SELECTABLE_PROTECTED_SCOPES
@pytest.mark.parametrize(
("raw", "expected"),
[
(" adaptive ", EgressScope.ADAPTIVE),
("PUBLIC_ONLY", EgressScope.PUBLIC_ONLY),
("private_only", EgressScope.PRIVATE_ONLY),
("strict", EgressScope.STRICT),
],
)
def test_parse_user_egress_scope_canonicalizes_protected_values(raw, expected):
assert parse_user_egress_scope(raw) is expected
@pytest.mark.parametrize("raw", ["both", "unknown", "", None, 1])
def test_parse_user_egress_scope_rejects_non_user_values(raw):
with pytest.raises(PolicyDeniedError) as exc_info:
parse_user_egress_scope(raw)
assert exc_info.value.decision.reason == "unknown_egress_scope"
def test_parse_user_egress_scope_enforces_gate_and_runtime_fallback(
monkeypatch,
):
monkeypatch.delenv("LDR_POLICY_ALLOW_UNPROTECTED_EGRESS", raising=False)
with pytest.raises(PolicyDeniedError) as exc_info:
parse_user_egress_scope("unprotected")
assert exc_info.value.decision.reason == "unprotected_egress_disabled"
assert (
parse_user_egress_scope("unprotected", disabled_unprotected="adaptive")
is EgressScope.ADAPTIVE
)
monkeypatch.setenv("LDR_POLICY_ALLOW_UNPROTECTED_EGRESS", "true")
assert parse_user_egress_scope(" UNPROTECTED ") is EgressScope.UNPROTECTED
@pytest.mark.parametrize("raw", ["both", "unknown", "", None, 1])
def test_effective_scope_for_display_falls_back_safely(raw):
assert effective_scope_for_display(raw) == DEFAULT_EGRESS_SCOPE
def test_effective_scope_for_display_respects_unprotected_gate(monkeypatch):
monkeypatch.delenv("LDR_POLICY_ALLOW_UNPROTECTED_EGRESS", raising=False)
assert effective_scope_for_display("unprotected") == DEFAULT_EGRESS_SCOPE
monkeypatch.setenv("LDR_POLICY_ALLOW_UNPROTECTED_EGRESS", "true")
assert effective_scope_for_display(" UNPROTECTED ") == "unprotected"
def test_engine_any_allowed_under_unprotected():
# A normally-unclassified/unknown engine is permitted by the escape hatch.
d = evaluate_engine(
"some_unknown_engine", unprot_ctx(), settings_snapshot={}
)
assert d.allowed
assert d.reason == "egress_unprotected"
def test_retriever_allowed_under_unprotected():
assert evaluate_retriever("anything", unprot_ctx()).allowed
def test_url_public_allowed_under_unprotected():
assert evaluate_url("http://example.com/", unprot_ctx()).allowed
def test_metadata_ssrf_still_blocked_under_unprotected():
# The hard invariant survives the escape hatch.
d = evaluate_url("http://169.254.169.254/latest/meta-data/", unprot_ctx())
assert not d.allowed
def test_cloud_llm_allowed_under_unprotected():
# require_local_llm is False under the hatch -> cloud provider allowed.
assert evaluate_llm_endpoint(
"openai", unprot_ctx(), settings_snapshot={}
).allowed
def test_context_unprotected_coerces_to_adaptive_by_default():
ctx = context_from_snapshot(
{"policy.egress_scope": "unprotected", "search.tool": "arxiv"},
"arxiv",
)
assert ctx.scope is EgressScope.PUBLIC_ONLY
def test_context_reapplies_current_environment_scope_to_stale_snapshot(
monkeypatch,
):
monkeypatch.setenv("LDR_POLICY_ALLOW_UNPROTECTED_EGRESS", "true")
monkeypatch.setenv("LDR_POLICY_EGRESS_SCOPE", "private_only")
ctx = context_from_snapshot(
{"policy.egress_scope": "unprotected", "search.tool": "arxiv"},
"arxiv",
)
assert ctx.scope is EgressScope.PRIVATE_ONLY
assert ctx.require_local_llm is True
assert ctx.require_local_embeddings is True
def test_unprotected_cannot_clear_environment_locality_locks(monkeypatch):
monkeypatch.setenv("LDR_POLICY_ALLOW_UNPROTECTED_EGRESS", "true")
monkeypatch.setenv("LDR_LLM_REQUIRE_LOCAL_ENDPOINT", "true")
monkeypatch.setenv("LDR_EMBEDDINGS_REQUIRE_LOCAL", "true")
ctx = context_from_snapshot(
{
"policy.egress_scope": "unprotected",
"llm.require_local_endpoint": False,
"embeddings.require_local": False,
"search.tool": "arxiv",
},
"arxiv",
)
assert ctx.scope is EgressScope.UNPROTECTED
assert ctx.require_local_llm is True
assert ctx.require_local_embeddings is True
def test_context_reapplies_current_environment_locality_flags(monkeypatch):
monkeypatch.setenv("LDR_LLM_REQUIRE_LOCAL_ENDPOINT", "true")
monkeypatch.setenv("LDR_EMBEDDINGS_REQUIRE_LOCAL", "true")
ctx = context_from_snapshot(
{
"policy.egress_scope": "public_only",
"llm.require_local_endpoint": False,
"embeddings.require_local": False,
"search.tool": "arxiv",
},
"arxiv",
)
assert ctx.require_local_llm is True
assert ctx.require_local_embeddings is True
def test_context_unprotected_lifts_local_requirements(monkeypatch):
monkeypatch.setenv("LDR_POLICY_ALLOW_UNPROTECTED_EGRESS", "true")
# Even with the require-local flags saved on, UNPROTECTED lifts them.
snap = {
"policy.egress_scope": "unprotected",
"llm.require_local_endpoint": True,
"embeddings.require_local": True,
"search.tool": "arxiv",
}
ctx = context_from_snapshot(snap, "arxiv")
assert ctx.scope is EgressScope.UNPROTECTED
assert ctx.require_local_llm is False
assert ctx.require_local_embeddings is False
def test_unprotected_warning_fires_and_is_non_dismissible():
from local_deep_research.security.egress.warnings import (
check_unprotected_egress,
)
w = check_unprotected_egress("unprotected")
assert w is not None
assert w["type"] == "egress_unprotected"
assert w["dismissKey"] is None # non-dismissible
assert check_unprotected_egress("adaptive") is None
assert check_unprotected_egress("private_only") is None
def test_trusted_destinations_warning():
from local_deep_research.security.egress.warnings import (
check_trusted_destinations,
)
assert check_trusted_destinations([], []) is None
w = check_trusted_destinations(["anthropic"], [])
assert w is not None
assert w["type"] == "egress_trusted_destinations"
assert "anthropic" in w["message"]
# tolerates the JSON-string shape a snapshot may carry
w2 = check_trusted_destinations('["openai"]', "[]")
assert w2 is not None and "openai" in w2["message"]
def test_effective_dispatch_snapshot_reapplies_all_environment_overrides(
monkeypatch,
):
from local_deep_research.settings.manager import (
apply_environment_overrides_to_snapshot,
)
monkeypatch.setenv("LDR_POLICY_TRUSTED_SEARCH_ENGINES", "[]")
monkeypatch.setenv("LDR_LLM_ALLOWED_LOCAL_HOSTNAMES", '["localhost"]')
snapshot = {
"policy.trusted_search_engines": {
"value": ["stale-engine"],
"ui_element": "json",
"editable": True,
},
"llm.allowed_local_hostnames": ["stale.internal"],
}
effective = apply_environment_overrides_to_snapshot(snapshot)
assert effective["policy.trusted_search_engines"]["value"] == []
assert effective["policy.trusted_search_engines"]["editable"] is False
assert effective["llm.allowed_local_hostnames"] == ["localhost"]
assert snapshot["policy.trusted_search_engines"]["value"] == [
"stale-engine"
]
partial = apply_environment_overrides_to_snapshot({})
assert partial["policy.trusted_search_engines"]["value"] == []
assert partial["policy.trusted_search_engines"]["editable"] is False
assert partial["llm.allowed_local_hostnames"]["value"] == ["localhost"]
assert partial["llm.allowed_local_hostnames"]["editable"] is False
def test_validate_unprotected_scope_respects_operator_gate(monkeypatch):
from local_deep_research.security.egress.validators import (
validate_egress_scope,
)
form = {"policy.egress_scope": "unprotected"}
assert validate_egress_scope(form, {}) is not None
monkeypatch.setenv("LDR_POLICY_ALLOW_UNPROTECTED_EGRESS", "true")
assert validate_egress_scope(form, {}) is None
assert (
validate_egress_scope({"policy.egress_scope": "invalid"}, {})
is not None
)
def test_validate_trusted_search_engines_rejects_public():
from local_deep_research.security.egress.validators import (
validate_trusted_search_engines,
)
# An inherently-public engine cannot be trusted (would launder a sink).
err = validate_trusted_search_engines(
{"policy.trusted_search_engines": ["searxng"]}, {}
)
assert err is not None and "searxng" in err["error"]
# A local-nature store / unknown name is accepted (runtime still gates).
assert (
validate_trusted_search_engines(
{"policy.trusted_search_engines": ["elasticsearch"]}, {}
)
is None
)
assert validate_trusted_search_engines({}, {}) is None