~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
"""
Tests for DataSanitizer security module.
"""
import pytest
from local_deep_research.security.data_sanitizer import (
DataSanitizer,
filter_research_metadata,
redact_data,
sanitize_data,
strip_settings_snapshot,
)
class TestDataSanitizerSanitize:
"""Tests for DataSanitizer.sanitize()."""
def test_sanitize_removes_api_key(self):
"""Removes api_key from data."""
data = {"username": "user", "api_key": "sk-secret-12345"}
result = DataSanitizer.sanitize(data)
assert "username" in result
assert "api_key" not in result
def test_sanitize_removes_password(self):
"""Removes password from data."""
data = {"user": "test", "password": "supersecret"}
result = DataSanitizer.sanitize(data)
assert "user" in result
assert "password" not in result
def test_sanitize_removes_access_token(self):
"""Removes access_token from data."""
data = {"id": 1, "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"}
result = DataSanitizer.sanitize(data)
assert "id" in result
assert "access_token" not in result
def test_sanitize_case_insensitive(self):
"""Sanitization is case-insensitive."""
data = {"API_KEY": "secret1", "ApiKey": "secret2", "apikey": "secret3"}
result = DataSanitizer.sanitize(data)
assert len(result) == 0
def test_sanitize_nested_dict(self):
"""Sanitizes nested dictionaries."""
data = {
"user": "test",
"settings": {
"theme": "dark",
"api_key": "nested_secret",
},
}
result = DataSanitizer.sanitize(data)
assert result["user"] == "test"
assert "theme" in result["settings"]
assert "api_key" not in result["settings"]
def test_sanitize_deeply_nested(self):
"""Sanitizes deeply nested structures."""
data = {
"level1": {
"level2": {
"level3": {
"password": "deep_secret",
"value": "keep_this",
}
}
}
}
result = DataSanitizer.sanitize(data)
assert result["level1"]["level2"]["level3"]["value"] == "keep_this"
assert "password" not in result["level1"]["level2"]["level3"]
def test_sanitize_list_of_dicts(self):
"""Sanitizes list of dictionaries."""
data = [
{"name": "item1", "secret": "value1"},
{"name": "item2", "secret": "value2"},
]
result = DataSanitizer.sanitize(data)
assert len(result) == 2
assert result[0]["name"] == "item1"
assert "secret" not in result[0]
assert result[1]["name"] == "item2"
assert "secret" not in result[1]
def test_sanitize_mixed_structure(self):
"""Sanitizes mixed nested structures."""
data = {
"users": [
{"username": "user1", "auth_token": "token1"},
{"username": "user2", "auth_token": "token2"},
],
"config": {
"public": True,
"private_key": "key123",
},
}
result = DataSanitizer.sanitize(data)
assert result["users"][0]["username"] == "user1"
assert "auth_token" not in result["users"][0]
assert result["config"]["public"] is True
assert "private_key" not in result["config"]
def test_sanitize_preserves_primitives(self):
"""Preserves primitive values unchanged."""
assert DataSanitizer.sanitize("string") == "string"
assert DataSanitizer.sanitize(123) == 123
assert DataSanitizer.sanitize(12.5) == 12.5
assert DataSanitizer.sanitize(True) is True
assert DataSanitizer.sanitize(None) is None
def test_sanitize_empty_dict(self):
"""Handles empty dictionary."""
assert DataSanitizer.sanitize({}) == {}
def test_sanitize_empty_list(self):
"""Handles empty list."""
assert DataSanitizer.sanitize([]) == []
def test_sanitize_custom_keys(self):
"""Uses custom sensitive keys."""
data = {
"custom_secret": "value",
"api_key": "standard_secret",
"name": "keep",
}
result = DataSanitizer.sanitize(data, sensitive_keys={"custom_secret"})
assert "custom_secret" not in result
assert "api_key" in result # Not in custom keys
assert "name" in result
def test_sanitize_all_default_keys(self):
"""Tests all default sensitive keys."""
data = {
"api_key": "1",
"apikey": "2",
"password": "3",
"secret": "4",
"access_token": "5",
"refresh_token": "6",
"private_key": "7",
"auth_token": "8",
"session_token": "9",
"csrf_token": "10",
"safe_key": "keep",
}
result = DataSanitizer.sanitize(data)
assert result == {"safe_key": "keep"}
class TestDataSanitizerRedact:
"""Tests for DataSanitizer.redact()."""
def test_redact_replaces_api_key(self):
"""Replaces api_key value with redaction text."""
data = {"username": "user", "api_key": "sk-secret-12345"}
result = DataSanitizer.redact(data)
assert result["username"] == "user"
assert result["api_key"] == "[REDACTED]"
def test_redact_preserves_structure(self):
"""Preserves data structure while redacting values."""
data = {"id": 1, "password": "secret123"}
result = DataSanitizer.redact(data)
assert "id" in result
assert "password" in result
assert result["password"] == "[REDACTED]"
def test_redact_custom_text(self):
"""Uses custom redaction text."""
data = {"password": "secret"}
result = DataSanitizer.redact(data, redaction_text="***")
assert result["password"] == "***"
def test_redact_case_insensitive(self):
"""Redaction is case-insensitive."""
data = {"API_KEY": "secret1", "ApiKey": "secret2", "apikey": "secret3"}
result = DataSanitizer.redact(data)
assert all(v == "[REDACTED]" for v in result.values())
def test_redact_nested_dict(self):
"""Redacts nested dictionaries."""
data = {
"user": "test",
"settings": {
"theme": "dark",
"api_key": "nested_secret",
},
}
result = DataSanitizer.redact(data)
assert result["user"] == "test"
assert result["settings"]["theme"] == "dark"
assert result["settings"]["api_key"] == "[REDACTED]"
def test_redact_list_of_dicts(self):
"""Redacts list of dictionaries."""
data = [
{"name": "item1", "secret": "value1"},
{"name": "item2", "secret": "value2"},
]
result = DataSanitizer.redact(data)
assert result[0]["name"] == "item1"
assert result[0]["secret"] == "[REDACTED]"
assert result[1]["name"] == "item2"
assert result[1]["secret"] == "[REDACTED]"
def test_redact_preserves_primitives(self):
"""Preserves primitive values unchanged."""
assert DataSanitizer.redact("string") == "string"
assert DataSanitizer.redact(123) == 123
assert DataSanitizer.redact(True) is True
def test_redact_custom_keys(self):
"""Uses custom sensitive keys."""
data = {"custom_secret": "value", "api_key": "keep", "name": "keep"}
result = DataSanitizer.redact(data, sensitive_keys={"custom_secret"})
assert result["custom_secret"] == "[REDACTED]"
assert result["api_key"] == "keep" # Not in custom keys
assert result["name"] == "keep"
class TestConvenienceFunctions:
"""Tests for convenience functions."""
def test_sanitize_data_function(self):
"""sanitize_data() works correctly."""
data = {"username": "user", "password": "secret"}
result = sanitize_data(data)
assert "username" in result
assert "password" not in result
def test_sanitize_data_with_custom_keys(self):
"""sanitize_data() with custom keys."""
data = {"custom": "secret", "keep": "value"}
result = sanitize_data(data, sensitive_keys={"custom"})
assert "custom" not in result
assert "keep" in result
def test_redact_data_function(self):
"""redact_data() works correctly."""
data = {"username": "user", "password": "secret"}
result = redact_data(data)
assert result["username"] == "user"
assert result["password"] == "[REDACTED]"
def test_redact_data_with_custom_text(self):
"""redact_data() with custom redaction text."""
data = {"password": "secret"}
result = redact_data(data, redaction_text="<hidden>")
assert result["password"] == "<hidden>"
class TestDefaultSensitiveKeys:
"""Tests for DEFAULT_SENSITIVE_KEYS constant."""
def test_default_keys_exist(self):
"""Default sensitive keys are defined."""
assert isinstance(DataSanitizer.DEFAULT_SENSITIVE_KEYS, set)
assert len(DataSanitizer.DEFAULT_SENSITIVE_KEYS) > 0
def test_default_keys_include_common_secrets(self):
"""Default keys include common secret patterns."""
expected = {
"api_key",
"password",
"secret",
"access_token",
"refresh_token",
"private_key",
}
assert expected.issubset(DataSanitizer.DEFAULT_SENSITIVE_KEYS)
class TestExpandedSecretLeafNames:
"""Secret leaf-names beyond the classic set must be caught by the
name-only predicate the bulk settings GET relies on (ui_element=None).
"""
NEW_NAMES = [
"client_secret",
"secret_key",
"bearer_token",
"api_secret",
"app_secret",
]
def test_present_in_default_set(self):
assert set(self.NEW_NAMES).issubset(
DataSanitizer.DEFAULT_SENSITIVE_KEYS
)
def test_recognized_by_leaf(self):
for name in self.NEW_NAMES:
key = f"integrations.oauth.{name}"
assert DataSanitizer.is_sensitive_setting(key)
# mirrors the bulk GET call: redact_value(key, None, value)
assert (
DataSanitizer.redact_value(key, None, "topsecret-value")
== DataSanitizer.REDACTION_TEXT
)
def test_no_over_redaction_of_similar_names(self):
# exact-leaf match must NOT redact these legit non-secret keys.
# The last two leaves CONTAIN a sensitive name as a proper substring
# ("password" in "passwordless", "secret" in "secretary") — they pin
# the predicate to exact-match, so a substring-broadening regression
# (e.g. `any(s in leaf ...)`) would be caught here.
for key in (
"llm.max_tokens",
"cache.key",
"search.public_key",
"app.token_count",
"oauth.client_id",
"llm.passwordless",
"integrations.secretary",
):
assert DataSanitizer.redact_value(key, None, "value") == "value"
class TestSensitiveLeafNormalization:
"""A key whose leaf is padded with whitespace or a zero-width/invisible
char must still be recognized as a secret (fail closed), not slip past the
redactor.
"""
def test_surrounding_whitespace_secret_is_sensitive(self):
# space (leading/trailing), tab, newline
for key in (
"llm.openai.api_key ",
"llm.openai. api_key",
"llm.openai.api_key" + chr(9),
"llm.openai.api_key" + chr(10),
):
assert DataSanitizer.is_sensitive_setting(key), repr(key)
assert (
DataSanitizer.redact_value(key, None, "sk-x")
== DataSanitizer.REDACTION_TEXT
)
def test_invisible_padded_secret_is_sensitive(self):
# Padding across the invisible-char families, trailing and interleaved.
# The non-space chars (zero-width, control, tag chars, ...) pass
# is_valid_setting_key so a padded top-level key is creatable; NBSP is
# str.isspace() so it's write-rejected at top level but still reaches
# redaction as a nested JSON-value dict key.
pads = [
chr(0x200B), # zero-width space
chr(0xFEFF), # BOM
chr(0x7F), # C0 (DEL) control char
chr(0x0080), # C1 control char
chr(0x00AD), # soft hyphen
chr(0x180E), # Mongolian vowel separator (format char)
chr(0x034F), # combining grapheme joiner (printable Mn)
chr(0x180B), # Mongolian free variation selector (printable Mn)
chr(0x115F), # Hangul choseong filler (printable Lo)
chr(0x17B4), # Khmer vowel inherent AQ (printable Mn)
chr(0x17B5), # Khmer vowel inherent AA (printable Mn)
chr(0x2800), # Braille pattern blank (printable So)
chr(0x13441), # Egyptian hieroglyph full blank (printable Lo)
chr(0x13442), # Egyptian hieroglyph half blank (printable Lo)
chr(0x1D159), # Musical symbol null notehead (printable So)
chr(0xE0020), # Unicode tag char (invisible-smuggling family)
chr(0xFE0F), # variation selector (BMP)
chr(0xE0100), # variation selector (supplement plane)
chr(0x00A0), # NBSP (non-ASCII space)
]
for pad in pads:
for key in ("x.api_key" + pad, "x.api" + pad + "key"):
assert DataSanitizer.is_sensitive_setting(key), repr(key)
assert (
DataSanitizer.redact_value(key, None, "sk-x")
== DataSanitizer.REDACTION_TEXT
)
def test_normalization_strips_only_invisible_chars(self):
# A non-secret leaf that is a secret name plus a normal char must stay
# readable — proves normalization removes ONLY invisible chars. "api_keyz"
# guards against stripping a legit ASCII char (-> "api_key"); "secreté"
# guards against stripping a legit non-ASCII letter (-> "secret").
for key in (
"x.api_keyz",
"x.password1",
"x.secretx",
"x.secreté",
"x.secret\u0301",
):
assert not DataSanitizer.is_sensitive_setting(key), key
assert DataSanitizer.redact_value(key, None, "v") == "v"
def test_exact_custom_sensitive_name_with_invisible_char_is_preserved(self):
"""Normalization must not break a pre-existing exact custom match."""
key = "x.custom_secret\u200b"
sensitive_keys = {"custom_secret\u200b"}
assert DataSanitizer.is_sensitive_setting(
key, sensitive_keys=sensitive_keys
)
assert (
DataSanitizer.redact_value(
key, None, "secret-value", sensitive_keys=sensitive_keys
)
== DataSanitizer.REDACTION_TEXT
)
class TestEdgeCases:
"""Edge case tests for DataSanitizer."""
def test_sanitize_with_none_sensitive_keys(self):
"""Uses default keys when sensitive_keys is None."""
data = {"api_key": "secret", "name": "keep"}
result = DataSanitizer.sanitize(data, sensitive_keys=None)
assert "api_key" not in result
assert "name" in result
def test_sanitize_with_empty_sensitive_keys(self):
"""Empty sensitive keys keeps all data."""
data = {"api_key": "secret", "password": "pass"}
result = DataSanitizer.sanitize(data, sensitive_keys=set())
assert "api_key" in result
assert "password" in result
def test_sanitize_list_with_primitives(self):
"""Handles list with primitive values."""
data = [1, 2, "string", None, True]
result = DataSanitizer.sanitize(data)
assert result == [1, 2, "string", None, True]
def test_sanitize_does_not_mutate_original(self):
"""Sanitization does not mutate original data."""
original = {"api_key": "secret", "name": "keep"}
_ = DataSanitizer.sanitize(original)
assert "api_key" in original
assert original["api_key"] == "secret"
def test_redact_does_not_mutate_original(self):
"""Redaction does not mutate original data."""
original = {"password": "secret", "name": "keep"}
_ = DataSanitizer.redact(original)
assert original["password"] == "secret"
class TestFilterResearchMetadata:
"""Tests for filter_research_metadata() helper."""
def test_none_input(self):
"""None returns default."""
assert filter_research_metadata(None) == {"is_news_search": False}
def test_empty_dict(self):
"""Empty dict returns default."""
assert filter_research_metadata({}) == {"is_news_search": False}
def test_strips_settings_snapshot(self):
"""Extracts only is_news_search, strips settings_snapshot."""
meta = {
"is_news_search": True,
"settings_snapshot": {"api_key": "sk-secret"},
}
result = filter_research_metadata(meta)
assert result == {"is_news_search": True}
assert "settings_snapshot" not in result
def test_explicit_false_preserved(self):
"""Explicit is_news_search=False is preserved."""
assert filter_research_metadata({"is_news_search": False}) == {
"is_news_search": False
}
def test_json_string_input(self):
"""JSON string is parsed correctly."""
result = filter_research_metadata('{"is_news_search": true}')
assert result == {"is_news_search": True}
def test_invalid_json_string(self):
"""Invalid JSON string returns default."""
assert filter_research_metadata("invalid json") == {
"is_news_search": False
}
def test_non_dict_json(self):
"""Non-dict JSON (e.g. array) returns default."""
assert filter_research_metadata("[]") == {"is_news_search": False}
def test_non_string_non_dict(self):
"""Non-string, non-dict input (e.g. int) returns default."""
assert filter_research_metadata(42) == {"is_news_search": False}
class TestStripSettingsSnapshot:
"""Tests for strip_settings_snapshot() helper."""
@pytest.mark.parametrize("empty_error", [None, "", False, 0, [], {}])
def test_empty_error_does_not_become_a_failure(self, empty_error):
metadata = {"phase": "complete", "error": empty_error}
assert strip_settings_snapshot(metadata) == metadata
def test_none_input(self):
"""None returns empty dict."""
assert strip_settings_snapshot(None) == {}
def test_strips_settings_snapshot(self):
"""Removes settings_snapshot, keeps other keys."""
meta = {
"phase": "complete",
"settings_snapshot": {"api_key": "sk-secret"},
}
assert strip_settings_snapshot(meta) == {"phase": "complete"}
def test_persisted_error_is_not_returned_to_api_clients(self):
"""Old rows can hold str(OSError), including a private report path."""
raw = (
"[Errno 13] Permission denied: '/srv/ldr/users/alice/reports/5.md'"
)
meta = {
"phase": "error",
"error": raw,
"settings_snapshot": {"api_key": "sk-secret"},
}
public = strip_settings_snapshot(meta)
assert public == {
"phase": "error",
"error": "Research failed. Check the server logs for details.",
}
assert meta["error"] == raw
@pytest.mark.parametrize("prefix", ["", "Research failed: "])
@pytest.mark.parametrize(
"message",
[
"Authentication with the configured LLM provider failed.",
"Could not connect to the configured LLM endpoint.",
"The configured LLM endpoint timed out.",
"The configured model was not found at the LLM endpoint.",
"The LLM endpoint rejected the request.",
"Could not connect to the configured LLM server.",
"The configured LLM server timed out.",
"The configured model was not found on the LLM server.",
"The configured LLM model was not found.",
"The LLM server rejected the request.",
"There was a problem with the search engine configuration.",
],
)
def test_known_worker_message_is_preserved(self, message, prefix):
public = strip_settings_snapshot({"error": prefix + message})
assert public["error"] == message
@pytest.mark.parametrize("prefix", ["", "Research failed: "])
@pytest.mark.parametrize("separator", [" | Details: ", "\n"])
@pytest.mark.parametrize(
"message",
[
"Could not connect to the configured LLM endpoint.",
"The configured LLM endpoint timed out.",
"The configured model was not found at the LLM endpoint.",
"The LLM endpoint rejected the request.",
],
)
def test_endpoint_message_with_added_detail_is_not_trusted(
self, message, prefix, separator
):
public = strip_settings_snapshot(
{"error": f"{prefix}{message}{separator}/srv/private/key"}
)
assert public["error"] == (
"Research failed. Check the server logs for details."
)
@pytest.mark.parametrize(
"error",
[
"Authentication with the configured LLM provider failed. | Details: /srv/private/key",
"Authentication with the configured LLM provider failed.\n/srv/private/key",
"/srv/private/key: Authentication with the configured LLM provider failed.",
{
"message": "The configured LLM server timed out.",
"path": "/srv/private/key",
},
],
)
def test_safe_message_does_not_authorize_appended_details(self, error):
public = strip_settings_snapshot({"error": error})
assert public["error"] == (
"Research failed. Check the server logs for details."
)
@pytest.mark.parametrize(
"message,expected",
[
(
"Egress policy could not verify this run, so it was refused (fail-closed).",
"Egress policy could not verify this run, so it was refused (fail-closed).",
),
(
"Egress policy refused this run: a sensitive source would reach an exposing destination (sensitive_to_exposing_private_reason).",
"Egress policy refused this run: a sensitive source would reach an exposing destination.",
),
(
"Egress policy refused this run (private_reason).",
"Egress policy refused this run.",
),
],
)
def test_policy_message_keeps_category_without_internal_reason(
self, message, expected
):
public = strip_settings_snapshot({"error": message})
assert public["error"] == expected
def test_no_op_when_absent(self):
"""Returns same dict when settings_snapshot absent."""
meta = {"phase": "error", "error_type": "timeout"}
assert strip_settings_snapshot(meta) == meta
def test_only_exact_key_stripped(self):
"""Only strips exact 'settings_snapshot' key, not similar names."""
meta = {"settings_version": 2, "settings_snapshot": {}}
assert strip_settings_snapshot(meta) == {"settings_version": 2}
def test_json_string_input(self):
"""JSON string is parsed and stripped."""
result = strip_settings_snapshot(
'{"phase": "done", "settings_snapshot": {}}'
)
assert result == {"phase": "done"}
def test_invalid_json_string(self):
"""Invalid JSON string returns empty dict."""
assert strip_settings_snapshot("invalid json") == {}
def test_does_not_mutate_original(self):
"""Original input dict is NOT modified."""
original = {
"phase": "complete",
"settings_snapshot": {"api_key": "sk-secret"},
}
_ = strip_settings_snapshot(original)
assert "settings_snapshot" in original
assert original["settings_snapshot"]["api_key"] == "sk-secret"
class TestRedactValueNestedDict:
"""redact_value must recurse into subtree dicts.
A namespace request (e.g. get_setting("llm")) returns a nested dict whose
outer key is not sensitive but whose inner keys are — without recursion
those nested secrets would ship in the clear via GET /settings/api/bulk.
"""
def test_nested_sensitive_leaf_redacted(self):
value = {"provider": "openai", "openai.api_key": "sk-secret"}
out = DataSanitizer.redact_value("llm", None, value)
assert out["openai.api_key"] == DataSanitizer.REDACTION_TEXT
assert out["provider"] == "openai"
def test_deeply_nested_secret_redacted(self):
value = {"openai": {"api_key": "sk-secret"}}
out = DataSanitizer.redact_value("llm", None, value)
assert out["openai"]["api_key"] == DataSanitizer.REDACTION_TEXT
def test_sensitive_outer_key_with_dict_redacted_wholesale(self):
# A sensitive-named key holding a dict is masked entirely (not recursed)
out = DataSanitizer.redact_value("x.api_key", None, {"nested": "v"})
assert out == DataSanitizer.REDACTION_TEXT
def test_primitive_value_unchanged(self):
assert (
DataSanitizer.redact_value("llm.provider", None, "openai")
== "openai"
)
def test_empty_dict_unchanged(self):
assert DataSanitizer.redact_value("llm", None, {}) == {}
def test_list_of_dicts_secret_redacted(self):
# a JSON list-of-dicts leaf (e.g. mcp.servers) must not ship secrets
value = [{"name": "prod", "api_key": "sk-secret"}]
out = DataSanitizer.redact_value("mcp.servers", None, value)
assert out[0]["api_key"] == DataSanitizer.REDACTION_TEXT
assert out[0]["name"] == "prod"
def test_plain_list_unchanged(self):
# a list of scalars under a non-sensitive key is not mangled
value = ["host1", "host2"]
assert DataSanitizer.redact_value("search.hosts", None, value) == [
"host1",
"host2",
]
def test_sensitive_key_with_list_redacted_wholesale(self):
out = DataSanitizer.redact_value("x.secret", None, ["a", "b"])
assert out == DataSanitizer.REDACTION_TEXT