~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
"""Elasticsearch host dictionaries and host strings are classified for egress."""
import json
from types import SimpleNamespace
from unittest.mock import patch
from urllib.parse import urlsplit
import pytest
import requests
from elastic_transport.client_utils import url_to_node_config
from elasticsearch._sync.client.utils import client_node_configs
from urllib3 import HTTPConnectionPool
from local_deep_research.security.egress.classification import Exposure
from local_deep_research.security.egress.policy import (
EgressContext,
EgressScope,
_classify_engine_url,
_vector_store_endpoint_is_local,
classify_engine,
evaluate_engine,
)
from local_deep_research.security.egress.run_classification import engine_label
from local_deep_research.web_search_engines.engines.search_engine_elasticsearch import (
ElasticsearchSearchEngine,
)
SETTING = "search.engine.web.elasticsearch.default_params.hosts"
PROTECTED_SCOPES = [
EgressScope.PUBLIC_ONLY,
EgressScope.PRIVATE_ONLY,
EgressScope.STRICT,
EgressScope.BOTH,
]
def classify_hosts(hosts, scope, *, local_hostnames=()):
# This parses the real SDK configuration without opening a connection.
assert len(client_node_configs(hosts, None)) == len(hosts)
context = EgressContext(
scope=scope,
primary_engine="elasticsearch",
require_local_llm=False,
require_local_embeddings=False,
local_hostnames=local_hostnames,
)
snapshot = {SETTING: hosts}
return (
classify_engine("elasticsearch", context, settings_snapshot=snapshot),
evaluate_engine("elasticsearch", context, settings_snapshot=snapshot),
)
@pytest.mark.parametrize(
"scope",
[
EgressScope.PUBLIC_ONLY,
EgressScope.PRIVATE_ONLY,
EgressScope.STRICT,
EgressScope.BOTH,
],
)
@pytest.mark.parametrize("other_host", ["10.0.0.42", "93.184.216.34"])
@pytest.mark.parametrize(
"fake_host", ["198.18.3.4", "::ffff:198.18.3.4", "mixed.fakeip.test"]
)
@pytest.mark.parametrize("dictionary_first", [False, True])
def test_fake_ip_dictionary_endpoint_makes_whole_host_list_ineligible(
scope, other_host, fake_host, dictionary_first
):
hosts = [
f"http://{other_host}:9200",
{"host": fake_host, "port": 9200, "scheme": "http"},
]
if dictionary_first:
hosts.reverse()
def resolve(host):
address = "198.18.3.4" if host == "mixed.fakeip.test" else host
return [(None, None, None, None, (address, 0))]
with patch(
"local_deep_research.security.egress.policy._resolve_with_timeout",
side_effect=resolve,
):
classification, decision = classify_hosts(hosts, scope)
assert classification.is_public is False
assert classification.is_local is False
assert not decision.allowed
@pytest.mark.parametrize("host", ["10.0.0.43", "::1"])
@pytest.mark.parametrize("include_url", [False, True])
def test_private_dictionary_endpoints_remain_eligible(host, include_url):
hosts = [{"host": host, "port": 9200, "scheme": "http"}]
if include_url:
hosts.insert(0, "http://10.0.0.42:9200")
classification, decision = classify_hosts(hosts, EgressScope.PRIVATE_ONLY)
assert classification.is_local is True
assert classification.is_public is False
assert decision.allowed
@pytest.mark.parametrize("include_url", [False, True])
def test_public_dictionary_endpoint_reclassifies_engine_as_public(include_url):
hosts = [{"host": "93.184.216.34", "port": 443, "scheme": "https"}]
if include_url:
hosts.insert(0, "http://10.0.0.42:9200")
with patch(
"local_deep_research.security.egress.policy._resolve_with_timeout",
return_value=[(None, None, None, None, ("93.184.216.34", 0))],
):
classification, decision = classify_hosts(
hosts, EgressScope.PUBLIC_ONLY
)
assert classification.is_local is False
assert classification.is_public is True
assert decision.allowed
def test_declared_local_dictionary_hostname_keeps_operator_override():
hosts = [{"host": "known.local.test", "port": 9200, "scheme": "http"}]
with patch(
"local_deep_research.security.egress.policy._resolve_with_timeout"
) as resolve:
classification, decision = classify_hosts(
hosts,
EgressScope.PRIVATE_ONLY,
local_hostnames=("known.local.test",),
)
assert classification.is_local is True
assert decision.allowed
resolve.assert_not_called()
@pytest.mark.parametrize(
"host",
[
None,
"",
123,
"localhost/path",
"localhost?query",
"localhost@remote",
"localhost%2fremote",
"localhost%00remote",
"[::1]",
],
)
def test_unparseable_dictionary_does_not_hide_behind_a_private_url(host):
context = EgressContext(
scope=EgressScope.PRIVATE_ONLY,
primary_engine="elasticsearch",
require_local_llm=False,
require_local_embeddings=False,
)
snapshot = {SETTING: ["http://10.0.0.42:9200", {"host": host}]}
assert not evaluate_engine(
"elasticsearch", context, settings_snapshot=snapshot
).allowed
@pytest.mark.parametrize("host", ["KNOWN.LOCAL.TEST", "known.local.test."])
def test_dictionary_hostname_normalization_keeps_local_override(host):
with patch(
"local_deep_research.security.egress.policy._resolve_with_timeout"
) as resolve:
classification, decision = classify_hosts(
[{"host": host, "port": 9200, "scheme": "http"}],
EgressScope.PRIVATE_ONLY,
local_hostnames=("known.local.test",),
)
assert classification.is_local is True
assert decision.allowed
resolve.assert_not_called()
@pytest.mark.parametrize("as_list", [False, True])
def test_elasticsearch_dictionary_support_does_not_widen_vector_store_uris(
as_list,
):
uri = {"host": "127.0.0.1", "scheme": "http", "port": 19530}
snapshot = {"vector_store.test.uri": [uri] if as_list else uri}
context = EgressContext(
scope=EgressScope.PRIVATE_ONLY,
primary_engine="library",
require_local_llm=False,
require_local_embeddings=False,
)
with patch(
"local_deep_research.security.egress.policy._vector_store_class",
return_value=SimpleNamespace(uri_setting="vector_store.test.uri"),
):
assert not _vector_store_endpoint_is_local("test", snapshot, context)
def resolve_names_privately(host):
# An IP literal answers itself; every name resolves to a private address,
# so only the host-string parsing can make an entry unknown.
address = host if host[:1].isdigit() else "10.0.0.9"
return [(None, None, None, None, (address, 0))]
def hosts_string(nodes, form):
if form == "json":
return json.dumps(nodes)
return (", " if form == "comma_space" else ",").join(nodes)
def classify_hosts_string(value, scope):
"""Classify a string ``hosts`` value, which the engine splits itself."""
nodes = ElasticsearchSearchEngine._ensure_list(value)
# The engine hands every node to the SDK, which parses all of them.
assert len(client_node_configs(nodes, None)) == len(nodes)
context = EgressContext(
scope=scope,
primary_engine="elasticsearch",
require_local_llm=False,
require_local_embeddings=False,
)
snapshot = {SETTING: value}
with patch(
"local_deep_research.security.egress.policy._resolve_with_timeout",
side_effect=resolve_names_privately,
):
return (
classify_engine(
"elasticsearch", context, settings_snapshot=snapshot
),
evaluate_engine(
"elasticsearch", context, settings_snapshot=snapshot
),
)
HOSTS_STRING_FORMS = ["comma", "comma_space", "json"]
@pytest.mark.parametrize("scope", PROTECTED_SCOPES, ids=lambda s: s.value)
@pytest.mark.parametrize("other_host", ["10.0.0.42", "93.184.216.34"])
@pytest.mark.parametrize("fake_first", [False, True])
@pytest.mark.parametrize("form", HOSTS_STRING_FORMS)
def test_fake_ip_node_in_a_hosts_string_makes_the_engine_ineligible(
scope, other_host, fake_first, form
):
"""A comma-separated or JSON string is split into nodes by the engine, so
a fake-IP node outside the first position is still a connect target."""
nodes = [f"http://{other_host}:9200", "http://198.18.3.4:9200"]
if fake_first:
nodes.reverse()
classification, decision = classify_hosts_string(
hosts_string(nodes, form), scope
)
assert classification.is_public is False
assert classification.is_local is False
assert not decision.allowed
@pytest.mark.parametrize("public_first", [False, True])
@pytest.mark.parametrize("form", HOSTS_STRING_FORMS)
def test_public_node_in_a_hosts_string_reclassifies_the_engine_public(
public_first, form
):
nodes = ["http://10.0.0.42:9200", "http://93.184.216.34:9200"]
if public_first:
nodes.reverse()
classification, decision = classify_hosts_string(
hosts_string(nodes, form), EgressScope.PRIVATE_ONLY
)
assert classification.is_public is True
assert classification.is_local is False
assert decision.reason == "scope_mismatch_private_only"
@pytest.mark.parametrize("form", HOSTS_STRING_FORMS)
def test_private_hosts_string_remains_eligible(form):
nodes = ["http://10.0.0.42:9200", "http://[::1]:9200"]
classification, decision = classify_hosts_string(
hosts_string(nodes, form), EgressScope.PRIVATE_ONLY
)
assert classification.is_local is True
assert classification.is_public is False
assert decision.allowed
# Host URLs that urlsplit and urllib3 (the SDK's parser) read differently.
SPLIT_HOST_URLS = [
# urlsplit takes the host after "@"; urllib3 ends the authority at "\".
("https://198.18.3.4:9200\\@10.0.0.42:9200", "198.18.3.4"),
("http://93.184.216.34:9200\\@10.0.0.42:9200", "93.184.216.34"),
# urllib3 applies IDNA 2008; the resolver would look up "strasse".
("http://straße.example:9200", "xn--strae-oqa.example"),
]
@pytest.mark.parametrize("scope", PROTECTED_SCOPES, ids=lambda s: s.value)
@pytest.mark.parametrize("as_list", [False, True])
@pytest.mark.parametrize(
("url", "sdk_host"),
SPLIT_HOST_URLS,
ids=["backslash_fake_ip", "backslash_public", "idna"],
)
def test_host_the_sdk_reads_differently_is_unknown(
scope, as_list, url, sdk_host
):
"""The client connects to the host urllib3 reads, so a URL whose urlsplit
host differs was never classified and keeps the engine ineligible."""
assert url_to_node_config(url).host == sdk_host
assert urlsplit(url).hostname != sdk_host
if as_list:
context = EgressContext(
scope=scope,
primary_engine="elasticsearch",
require_local_llm=False,
require_local_embeddings=False,
)
snapshot = {SETTING: [url]}
with patch(
"local_deep_research.security.egress.policy._resolve_with_timeout",
side_effect=resolve_names_privately,
):
classification = classify_engine(
"elasticsearch", context, settings_snapshot=snapshot
)
decision = evaluate_engine(
"elasticsearch", context, settings_snapshot=snapshot
)
else:
classification, decision = classify_hosts_string(url, scope)
assert classification.is_public is False
assert classification.is_local is False
assert not decision.allowed
@pytest.mark.parametrize(
"url",
[
"http://[::1]:9200",
"HTTP://LOCALHOST:9200",
"http://[email protected]:9200",
],
)
def test_host_urls_both_parsers_read_alike_remain_classified(url):
assert url_to_node_config(url).host == urlsplit(url).hostname
classification, decision = classify_hosts_string(
url, EgressScope.PRIVATE_ONLY
)
assert classification.is_local is True
assert decision.allowed
def classify_private_names(hosts, scope):
"""Classify a ``hosts`` list whose names all resolve to a private address.
Only the parsing of an entry can then make it unknown. Also returns the
names the resolver was asked for.
"""
assert len(client_node_configs(hosts, None)) == len(hosts)
context = EgressContext(
scope=scope,
primary_engine="elasticsearch",
require_local_llm=False,
require_local_embeddings=False,
)
snapshot = {SETTING: hosts}
with patch(
"local_deep_research.security.egress.policy._resolve_with_timeout",
side_effect=resolve_names_privately,
) as resolve:
return (
classify_engine(
"elasticsearch", context, settings_snapshot=snapshot
),
evaluate_engine(
"elasticsearch", context, settings_snapshot=snapshot
),
[call.args[0] for call in resolve.call_args_list],
)
def dictionary_host(host):
return {"host": host, "port": 9200, "scheme": "http"}
def sdk_connects_to(host):
"""The name the SDK's urllib3 pool connects to for a host dictionary."""
(node,) = client_node_configs([dictionary_host(host)], None)
return HTTPConnectionPool(node.host, port=node.port).host
# The resolver encodes a non-ASCII name with IDNA 2003 and the SDK's urllib3
# with IDNA 2008: "faß" is looked up as "fass" but connected to as
# "xn--fa-hia". The tuples are (host, name the SDK connects to, name the
# resolver looks up).
NON_ASCII_DICTIONARY_HOSTS = [
pytest.param(
"faß.corp.example",
"xn--fa-hia.corp.example",
"fass.corp.example",
id="sharp_s",
),
pytest.param(
"straße.example",
"xn--strae-oqa.example",
"strasse.example",
id="strasse",
),
# The two encodings agree here, yet the same host in a URL is unknown.
pytest.param(
"münchen.example",
"xn--mnchen-3ya.example",
"xn--mnchen-3ya.example",
id="encodings_agree",
),
]
@pytest.mark.parametrize("scope", PROTECTED_SCOPES, ids=lambda s: s.value)
@pytest.mark.parametrize("include_url", [False, True])
@pytest.mark.parametrize(
("host", "sdk_host", "resolver_host"), NON_ASCII_DICTIONARY_HOSTS
)
def test_non_ascii_dictionary_host_is_unknown(
scope, include_url, host, sdk_host, resolver_host
):
"""The resolver would look up ``resolver_host`` while the client connects
to ``sdk_host``, so a name that resolves private under one encoding says
nothing about the other. A dictionary host stays unknown, as it does in
a URL."""
assert sdk_connects_to(host) == sdk_host
assert host.encode("idna").decode("ascii") == resolver_host
hosts = [dictionary_host(host)]
if include_url:
hosts.insert(0, "http://10.0.0.42:9200")
classification, decision, looked_up = classify_private_names(hosts, scope)
assert classification.is_public is False
assert classification.is_local is False
assert not decision.allowed
assert host not in looked_up
# (is_public, is_local) of the engine when its only node is the host.
LOCAL, PUBLIC, UNKNOWN = (False, True), (True, False), (False, False)
HOST_IN_URL_AND_DICTIONARY = [
pytest.param("10.0.0.42", LOCAL, id="private_ip"),
pytest.param("93.184.216.34", PUBLIC, id="public_ip"),
pytest.param("kibana.corp.example", LOCAL, id="name"),
pytest.param("Kibana.Corp.Example", LOCAL, id="mixed_case"),
# Punycode is ASCII: classified as the client connects to it.
pytest.param("xn--fa-hia.corp.example", LOCAL, id="punycode"),
pytest.param("faß.corp.example", UNKNOWN, id="sharp_s"),
pytest.param("straße.example", UNKNOWN, id="strasse"),
pytest.param("münchen.example", UNKNOWN, id="encodings_agree"),
# Percent-bearing hosts stay unknown in both shapes, regardless of the
# installed HTTP client's normalization (covered below).
pytest.param("10%2e0%2e0%2e42", UNKNOWN, id="percent_encoded_ip"),
pytest.param("fa%C3%9F.corp.example", UNKNOWN, id="percent_encoded_utf8"),
]
@pytest.mark.parametrize(("host", "expected"), HOST_IN_URL_AND_DICTIONARY)
def test_dictionary_host_is_classified_like_the_same_host_in_a_url(
host, expected
):
for entry in (dictionary_host(host), f"http://{host}:9200"):
classification, _, _ = classify_private_names(
[entry], EgressScope.PRIVATE_ONLY
)
assert (
classification.is_public,
classification.is_local,
) == expected, entry
def test_kelvin_sign_host_is_classified_by_its_shape():
"""U+212A KELVIN SIGN lower-cases to an ASCII "k". A URL host is
lower-cased by the parser before classification (``urlsplit`` and
urllib3's ``parse_url`` both read ``kibana.corp.example``, the name the
client connects to), so the URL is classified as that ASCII name. A
dictionary host is classified as written, and the canonical-host rule
checks for ASCII before case folding, so the raw non-ASCII spelling is
unclassified. This is the one host on which the two shapes differ, and
the dictionary shape errs closed: unknown is refused under every
protected scope, and the engine's own connection check refuses the
same non-ASCII dictionary host."""
host = "\u212aibana.corp.example"
assert sdk_connects_to(host) == "kibana.corp.example"
for entry, expected in (
(dictionary_host(host), UNKNOWN),
(f"http://{host}:9200", LOCAL),
):
classification, _, _ = classify_private_names(
[entry], EgressScope.PRIVATE_ONLY
)
assert (
classification.is_public,
classification.is_local,
) == expected, entry
def test_punycode_dictionary_host_is_classified_as_the_client_connects():
"""Writing the host in its ``xn--`` form is the way to keep an
internationalized name classified."""
host = "xn--fa-hia.corp.example"
assert sdk_connects_to(host) == host
classification, decision, looked_up = classify_private_names(
[dictionary_host(host)], EgressScope.PRIVATE_ONLY
)
assert classification.is_local is True
assert decision.allowed
assert set(looked_up) == {host}
# urllib3 2.8 decodes unreserved percent escapes that 2.7 passed through,
# and rejects these scoped IPv6 entries before DNS. The policy deliberately
# withholds all percent-bearing hosts under either client behavior, including
# IPv6 zone IDs. Keep that invariant independent of the SDK's normalization.
PERCENT_BEARING_HOSTS = [
pytest.param(
dictionary_host("10%2e0%2e0%2e42"),
id="dictionary_ip",
),
pytest.param("http://10%2E0%2E0%2E42:9200", id="url_ip"),
pytest.param(dictionary_host("%6cocalhost"), id="dictionary_name"),
pytest.param(
dictionary_host("fd00::1%eth0"),
id="dictionary_scoped_ipv6",
),
pytest.param("http://[fd00::1%eth0]:9200", id="url_scoped_ipv6"),
pytest.param(
"http://[fd00::1%25eth0]:9200",
id="url_scoped_ipv6_rfc6874",
),
]
@pytest.mark.parametrize("scope", PROTECTED_SCOPES, ids=lambda s: s.value)
@pytest.mark.parametrize("include_url", [False, True])
@pytest.mark.parametrize("entry", PERCENT_BEARING_HOSTS)
def test_percent_bearing_host_is_unknown_without_dns(scope, include_url, entry):
"""Every protected scope refuses the entry before policy DNS lookup,
even beside a valid private URL and regardless of SDK normalization."""
hosts = [entry]
if include_url:
hosts.insert(0, "http://10.0.0.42:9200")
classification, decision, looked_up = classify_private_names(hosts, scope)
assert classification.is_public is False
assert classification.is_local is False
assert not decision.allowed
assert looked_up == []
def test_other_consumers_still_decode_a_percent_escaped_host():
"""``requests`` decodes the host, so only the Elasticsearch opt-in
(``allow_host_dicts``) stops doing it."""
url = "http://10%2e0%2e0%2e42:8000"
prepared = requests.Request("GET", url).prepare()
assert prepared.url == "http://10.0.0.42:8000/"
context = EgressContext(
scope=EgressScope.PRIVATE_ONLY,
primary_engine="paperless",
require_local_llm=False,
require_local_embeddings=False,
)
snapshot = {"engine.url": url}
assert _classify_engine_url("engine.url", snapshot, context) is True
assert (
_classify_engine_url(
"engine.url", snapshot, context, allow_host_dicts=True
)
is None
)
CONTAINED, EXPOSING = Exposure.CONTAINED, Exposure.EXPOSING
PRIVATE_URL = "http://10.0.0.42:9200"
PUBLIC_URL = "http://93.184.216.34:9200"
FAKE_IP_URL = "http://198.18.3.4:9200"
LABEL_SHAPES = [
pytest.param(None, CONTAINED, id="unset"),
pytest.param(PRIVATE_URL, CONTAINED, id="private_string"),
pytest.param(
[PRIVATE_URL, dictionary_host("10.0.0.43")],
CONTAINED,
id="private_url_and_dictionary",
),
pytest.param(PUBLIC_URL, EXPOSING, id="public_string"),
pytest.param(
f"{PRIVATE_URL},{PUBLIC_URL}", EXPOSING, id="public_second_comma"
),
pytest.param(
json.dumps([PRIVATE_URL, PUBLIC_URL]),
EXPOSING,
id="public_second_json",
),
pytest.param(
[PRIVATE_URL, dictionary_host("93.184.216.34")],
EXPOSING,
id="public_dictionary",
),
pytest.param(FAKE_IP_URL, EXPOSING, id="fake_ip_string"),
pytest.param(
f"{PRIVATE_URL},{FAKE_IP_URL}", EXPOSING, id="fake_ip_second_comma"
),
pytest.param(
[PRIVATE_URL, dictionary_host("198.18.3.4")],
EXPOSING,
id="fake_ip_dictionary",
),
pytest.param(
[dictionary_host("faß.corp.example")],
EXPOSING,
id="non_ascii_dictionary",
),
pytest.param(
[PRIVATE_URL, dictionary_host("localhost/path")],
EXPOSING,
id="unparseable_dictionary",
),
]
@pytest.mark.parametrize(("hosts", "exposure"), LABEL_SHAPES)
def test_two_axis_label_agrees_with_the_scope_policy(hosts, exposure):
"""``engine_label`` reads the node list the way ``classify_engine`` does:
the engine stays CONTAINED only where the policy keeps it local, and it
fails up to EXPOSING where the policy withholds it or rates it public."""
snapshot = {}
if hosts is not None:
snapshot = {SETTING: hosts}
nodes = (
ElasticsearchSearchEngine._ensure_list(hosts)
if isinstance(hosts, str)
else hosts
)
assert len(client_node_configs(nodes, None)) == len(nodes)
context = EgressContext(
scope=EgressScope.BOTH,
primary_engine="elasticsearch",
require_local_llm=False,
require_local_embeddings=False,
)
with patch(
"local_deep_research.security.egress.policy._resolve_with_timeout",
side_effect=resolve_names_privately,
):
label = engine_label("elasticsearch", snapshot, context)
classification = classify_engine(
"elasticsearch", context, settings_snapshot=snapshot
)
assert label.exposure is exposure
assert (classification.is_local is True) == (exposure is CONTAINED)