~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
"""Who guards the guards: correctness of ``security/`` itself.
Every other suite in ``tests/security/`` tests code that *calls* these
guards. This file tests the guards' own decision logic:
* :class:`PathValidator` — does ``validate_safe_path`` actually confine a
path to ``base_dir``?
* :class:`URLValidator` — does the redirect guard survive the standard
bypass corpus, and does the trusted-domain allowlist ever fail open?
* ``log_sanitizer.sanitize_error_message`` — which credential *shapes*
does it not match?
* ``account_lockout`` — is the ``_MAX_SINGLE_FAILURES`` eviction reachable
from the public API, and what does an attacker get by reaching it?
* fail-open vs fail-closed — when a guard's own dependency raises, does
it deny or allow?
* consistency — where two guards answer the same question differently.
Everything here is a pure function call. Nothing boots the app, opens a
socket, or resolves a hostname (IP literals only, so ``validate_url``
never reaches ``getaddrinfo``).
``xfail(strict=True)`` marks a **confirmed defect**: the test body
asserts the behaviour the guard's own docstring promises, and it does not
hold today. When the defect is fixed the test XPASSes and this file goes
red, which is the intended signal.
"""
import ipaddress
import os
import tempfile
import time
from datetime import datetime, timedelta, timezone
from pathlib import Path
import pytest
from local_deep_research.security.account_lockout import (
AccountLockoutManager,
)
from local_deep_research.security.ip_ranges import PRIVATE_IP_RANGES
from local_deep_research.security.log_sanitizer import (
redact_log_record,
redact_secrets,
sanitize_error_message,
)
from local_deep_research.security.network_utils import is_private_ip
from local_deep_research.security.path_validator import PathValidator
from local_deep_research.security.url_validator import URLValidator
# Addresses are assembled from parts: `.pre-commit-hooks/
# file-whitelist-check.sh` flags literal IPv4 quads in ``.py`` files.
LOOPBACK_V4 = ".".join(["127", "0", "0", "1"])
CGNAT_V4 = ".".join(["100", "64", "0", "1"])
RFC1918_V4 = ".".join(["192", "168", "1", "10"])
PUBLIC_V4 = ".".join(["93", "184", "216", "34"])
# ---------------------------------------------------------------------
# 1. PathValidator.validate_safe_path — containment
# ---------------------------------------------------------------------
@pytest.fixture
def confined_tree(tmp_path):
"""A base dir, an out-of-base dir, and a symlink bridging them.
Returns ``(base, outside)``. ``base/inside.txt`` holds ``INSIDE``,
``outside/secret.txt`` holds ``OUTSIDE``, ``base/linkdir`` is a
symlink to ``outside`` and ``base/leaflink`` is a symlink to
``outside/secret.txt``.
"""
base = tmp_path / "base"
outside = tmp_path / "outside"
base.mkdir()
outside.mkdir()
(base / "inside.txt").write_text("INSIDE", encoding="utf-8")
(outside / "secret.txt").write_text("OUTSIDE", encoding="utf-8")
os.symlink(str(outside), str(base / "linkdir"))
os.symlink(str(outside / "secret.txt"), str(base / "leaflink"))
return base, outside
def _is_confined(base, user_input):
"""Did ``validate_safe_path`` keep ``user_input`` inside ``base``?
Three outcomes count as confined: raising, returning ``None``, or
returning a path whose *real* (symlink-resolved) location is under
``base``. Written this way so the assertion detects a fix that
chooses any of those, rather than only the ``None`` form -- a
fix-shaped patch was applied to a throwaway copy of
``path_validator.py`` to confirm this flips.
"""
try:
result = PathValidator.validate_safe_path(user_input, base)
except ValueError:
return True
if result is None:
return True
return result.resolve().is_relative_to(Path(base).resolve())
class TestValidateSafePathContainment:
"""``validate_safe_path`` confines the *resolved* path to ``base_dir``.
``werkzeug.safe_join`` is a *string* operation: it rejects ``..``
segments and absolute inputs, and that is all it claims to do. The
module docstring of ``path_validator`` promises more — "prevent path
traversal attacks and other filesystem-based security
vulnerabilities" — and a ``confine_to_base()`` helper that did the
realpath containment check was removed (see the comment referencing
#4868 in ``path_validator.py``). The realpath containment was
restored (GHSA-gv87-qcw7-gjr8): the joined path is resolved and
required to stay under the resolved base, so a symlink inside
``base_dir`` is accepted only when its target is inside too —
#3090's bind-mount / macOS ``/tmp`` layouts keep working, and only
the escapes are refused.
"""
def test_a_path_inside_the_base_is_accepted(self, confined_tree):
"""Positive control for every rejection below.
Without this, "the guard blocked it" would be indistinguishable
from "the guard blocks everything".
"""
base, _ = confined_tree
result = PathValidator.validate_safe_path("inside.txt", base)
assert result is not None
assert result.read_text(encoding="utf-8") == "INSIDE"
def test_a_dotdot_escape_is_rejected(self, confined_tree):
"""The string-level guard that safe_join does provide."""
base, outside = confined_tree
with pytest.raises(ValueError, match="traversal"):
PathValidator.validate_safe_path(
f"../{outside.name}/secret.txt", base
)
def test_an_absolute_path_is_rejected(self, confined_tree):
base, outside = confined_tree
with pytest.raises(ValueError, match="traversal"):
PathValidator.validate_safe_path(str(outside / "secret.txt"), base)
def test_a_symlinked_directory_does_not_escape_the_base(
self, confined_tree
):
"""``linkdir/secret.txt`` must not resolve outside ``base``.
Resolved-state contract (GHSA-gv87-qcw7-gjr8): the guard either
refuses the input or confines its real location — a symlinked
directory inside ``base_dir`` can no longer be traversed
straight through to a file outside it.
"""
base, _ = confined_tree
assert _is_confined(base, "linkdir/secret.txt"), (
"validate_safe_path returned a path whose real location is "
"outside base_dir"
)
def test_a_leaf_symlink_does_not_escape_the_base(self, confined_tree):
"""A leaf symlink resolving outside ``base`` is refused or
confined (same root cause as the directory case)."""
base, _ = confined_tree
assert _is_confined(base, "leaflink")
def test_allow_absolute_true_is_silently_ignored(self, confined_tree):
"""``allow_absolute`` is accepted and never read.
The signature and docstring advertise "Whether to allow absolute
paths (with restrictions)"; the parameter appears nowhere in the
body. Absolute inputs are rejected either way, so this fails
*closed* -- but a caller that passes ``allow_absolute=True``
believing it opts into something is silently wrong.
"""
base, outside = confined_tree
with pytest.raises(ValueError, match="traversal"):
PathValidator.validate_safe_path(
str(outside / "secret.txt"), base, allow_absolute=True
)
def test_a_literal_percent_escape_is_rejected_even_when_benign(
self, confined_tree
):
"""``_has_encoded_traversal`` also lists ``%2f``.
Any filename containing the two-character sequence ``%2f`` --
including one that is simply named that way on disk -- is
refused. Fails closed; recorded so the false-positive surface is
not mistaken for a bypass.
"""
base, _ = confined_tree
(base / "a%2fb.txt").write_text("BENIGN", encoding="utf-8")
with pytest.raises(ValueError, match="encoded traversal"):
PathValidator.validate_safe_path("a%2fb.txt", base)
# Control: the same file under a name without the escape works.
(base / "a_b.txt").write_text("BENIGN", encoding="utf-8")
assert PathValidator.validate_safe_path("a_b.txt", base) is not None
class TestLibraryCallSiteInheritsTheGap:
"""The real call sites compensate -- but only for the leaf.
``research_library.utils._resolve_within_root`` and
``PDFStorageManager._safe_path_in_root`` both follow
``validate_safe_path`` with ``result.is_symlink()``. ``Path.is_symlink``
inspects the *final component only*, so a symlinked intermediate
directory sails past it. This exercises the production function, not
a copy of it.
"""
def test_a_leaf_symlink_is_caught_by_the_callsite_check(
self, confined_tree
):
"""Positive control: the compensating check does fire."""
from local_deep_research.research_library.utils import (
_resolve_within_root,
)
base, _ = confined_tree
assert _resolve_within_root("leaflink", base) is None
def test_an_ordinary_file_still_resolves(self, confined_tree):
"""Positive control: the call site is not refusing everything."""
from local_deep_research.research_library.utils import (
_resolve_within_root,
)
base, _ = confined_tree
resolved = _resolve_within_root("inside.txt", base)
assert resolved is not None
assert resolved.read_text(encoding="utf-8") == "INSIDE"
def test_an_intermediate_symlink_is_not_caught(self, confined_tree):
"""Resolved-state contract: with realpath containment inside
``validate_safe_path`` (GHSA-gv87-qcw7-gjr8), an intermediate
symlinked directory can no longer smuggle a read outside the
per-user library root — the guard itself refuses it, so the
call site's leaf-only ``is_symlink()`` check is no longer the
only line of defence."""
from local_deep_research.research_library.utils import (
_resolve_within_root,
)
base, _ = confined_tree
resolved = _resolve_within_root("linkdir/secret.txt", base)
assert (
resolved is None
or resolved.read_text(encoding="utf-8") != "OUTSIDE"
)
# ---------------------------------------------------------------------
# 2. URLValidator
# ---------------------------------------------------------------------
APP_HOST = "http://app.example.com/"
# The standard open-redirect corpus. Each entry must be refused.
REDIRECT_BYPASS_CORPUS = [
"https://evil.example.net/",
"//evil.example.net",
"///evil.example.net",
"////evil.example.net",
"\\\\evil.example.net",
"/\\evil.example.net",
"https://[email protected]/",
"https://app.example.com%40evil.example.net/",
"https:/evil.example.net",
"/%2f%2fevil.example.net",
"/%5c%5cevil.example.net",
"/..%2f..%2fadmin",
"/../admin",
"/dash\r\nLocation: https://evil.example.net",
"/dash%0d%0aLocation:%20https://evil.example.net",
"/dash\x00https://evil.example.net",
"http://app.example.com:8080/",
"HTTPS://APP.EXAMPLE.COM/x",
"\t//evil.example.net",
"javascript:alert(1)",
"data:text/html,<script>alert(1)</script>",
]
REDIRECT_ALLOWED_CONTROLS = [
"/dashboard",
"/dashboard?tab=settings",
"/dashboard#anchor",
"dashboard",
"http://app.example.com/dashboard",
]
class TestRedirectGuard:
"""``is_safe_redirect_url`` / ``get_safe_redirect_path``."""
@pytest.mark.parametrize("target", REDIRECT_BYPASS_CORPUS)
def test_bypass_corpus_is_refused(self, target):
assert URLValidator.is_safe_redirect_url(target, APP_HOST) is False
assert URLValidator.get_safe_redirect_path(target, APP_HOST) is None
@pytest.mark.parametrize("target", REDIRECT_ALLOWED_CONTROLS)
def test_legitimate_targets_are_allowed(self, target):
"""Positive control: the corpus result is not "refuses all"."""
assert URLValidator.is_safe_redirect_url(target, APP_HOST) is True
path = URLValidator.get_safe_redirect_path(target, APP_HOST)
assert path is not None and path.startswith("/")
def test_scheme_only_relative_target_is_reduced_to_a_path(self):
"""``http:evil.example.net`` is a *relative* reference.
RFC 3986 resolves it against the base path, so it is same-host
and correctly accepted. ``get_safe_redirect_path`` strips it to
the path form, which is the defence-in-depth the docstring
promises: even a hypothetical validator bypass cannot leave the
host.
"""
target = "http:evil.example.net"
assert URLValidator.is_safe_redirect_url(target, APP_HOST) is True
assert (
URLValidator.get_safe_redirect_path(target, APP_HOST)
== "/evil.example.net"
)
class TestTrustedDomainAllowlist:
"""``is_safe_url(..., trusted_domains=[...])``.
The allowlist is applied under ``if trusted_domains and
parsed.hostname:``. A URL that ``urlparse`` cannot assign a hostname
to therefore skips the allowlist entirely and is reported safe.
"""
def test_allowlist_admits_a_listed_host(self):
assert (
URLValidator.is_safe_url(
"https://good.example.com/x",
trusted_domains=["good.example.com"],
)
is True
)
def test_allowlist_refuses_an_unlisted_host(self):
assert (
URLValidator.is_safe_url(
"https://evil.example.net/x",
trusted_domains=["good.example.com"],
)
is False
)
@pytest.mark.parametrize(
"url",
[
"https:////evil.example.net",
"https:/evil.example.net",
],
)
@pytest.mark.xfail(
strict=True,
reason=(
"DEFECT (security, latent): the trusted-domain allowlist is "
"skipped whenever urlparse yields hostname=None, so a URL "
"with extra or missing slashes is reported safe against any "
"allowlist. WHATWG URL parsers (every browser, and "
"requests/urllib3) skip the surplus slashes for special "
"schemes and reach evil.example.net. No production call site "
"passes trusted_domains today, so this is latent rather than "
"live -- but it is a public, documented API."
),
)
def test_hostname_less_url_does_not_bypass_the_allowlist(self, url):
assert (
URLValidator.is_safe_url(url, trusted_domains=["good.example.com"])
is False
)
def test_sanitize_url_manufactures_a_hostname_less_url(self):
"""``sanitize_url`` is the natural way to reach the state above.
A scheme-less, protocol-relative input is turned into a
four-slash URL and returned verbatim rather than refused. The
string a browser is handed resolves to ``evil.example.net``.
"""
assert (
URLValidator.sanitize_url("//evil.example.net")
== "https:////evil.example.net"
)
# Control: an ordinary scheme-less host is normalised sanely.
assert (
URLValidator.sanitize_url("good.example.com")
== "https://good.example.com"
)
class TestSuspiciousPatternHeuristic:
"""``_has_suspicious_patterns`` rejects ordinary URLs."""
def test_html_entity_regex_refuses_an_escaped_ampersand(self):
"""``&`` is how every HTML-sourced URL spells ``&``.
``is_safe_url`` returns False and ``validate_http_url`` *raises*
for such a URL. Fails closed, so not exploitable -- recorded
because it is a live functional break for callback URLs copied
out of a web page.
"""
entity = "https://good.example.com/s?a=1&b=2"
plain = "https://good.example.com/s?a=1&b=2"
assert URLValidator.is_safe_url(plain) is True
assert URLValidator.is_safe_url(entity) is False
class TestUnsafeSchemeNormalisation:
"""``is_unsafe_scheme`` only strips whitespace at the edges."""
def test_embedded_tab_defeats_the_scheme_prefix_check(self):
"""``java<TAB>script:`` is not recognised as a javascript URL.
``is_unsafe_scheme`` does a ``startswith`` on the stripped,
lowercased string; browsers strip interior TAB/CR/LF from URLs
before scheme detection, so they *do* execute it.
"""
assert URLValidator.is_unsafe_scheme("javascript:alert(1)") is True
assert URLValidator.is_unsafe_scheme("java\tscript:alert(1)") is False
def test_but_the_composed_guards_still_refuse_it(self):
"""Not exploitable through the public entry points.
``is_safe_url`` and ``sanitize_url`` re-parse with ``urlparse``,
which *does* strip the tab, so the scheme allowlist catches it.
``is_unsafe_scheme`` must not be used on its own; today no call
site outside this module does.
"""
payload = "java\tscript:alert(1)"
assert URLValidator.is_safe_url(payload) is False
assert URLValidator.sanitize_url(payload) is None
# ---------------------------------------------------------------------
# 3. log_sanitizer — which credential shapes escape
# ---------------------------------------------------------------------
# Assembled so no token-shaped literal appears in the file (gitleaks).
_SECRET_BODY = "A" * 8 + "1234567890abcdef"
_HEX_TOKEN = "b3d1f0a9c2e847d5" * 2
class TestCredentialShapeCoverage:
"""``sanitize_error_message`` matches shapes, not everything."""
def test_x_prefixed_api_key_header_is_redacted(self):
"""Positive control for the header family."""
msg = f"upstream said x-api-key: {_SECRET_BODY}"
assert _SECRET_BODY not in sanitize_error_message(msg)
@pytest.mark.parametrize("label", ["Api-Key", "api-key", "api_key"])
@pytest.mark.xfail(
strict=True,
reason=(
"DEFECT (security): only the literal 'x-api-key' header name "
"is anchored in sanitize_error_message. The unprefixed "
"'Api-Key:' header -- what Anthropic, Azure Cognitive Services "
"and several search APIs actually send -- is not matched by "
"any of its patterns, so a raw request-header dump inside an "
"exception message ships the key verbatim to the client via "
"sanitize_error_for_client() and in scrub_error text. Log "
"records redact it (TestCredentialShapesOnTheLogRecordPath)."
),
)
def test_unprefixed_api_key_header_is_redacted(self, label):
msg = f"upstream said {label}: {_SECRET_BODY}"
assert _SECRET_BODY not in sanitize_error_message(msg)
def test_url_userinfo_is_redacted(self):
"""Positive control for the userinfo pattern."""
msg = "postgresql://admin:[email protected]/app"
assert "Sup3rS3cret" not in sanitize_error_message(msg)
@pytest.mark.xfail(
strict=True,
reason=(
"DEFECT (security): the userinfo regex excludes '/' from the "
"password class ([^@\\s/]+), so a password containing an "
"unescaped '/' -- routine for base64/base64-ish generated "
"passwords pasted into a DSN -- makes the whole match fail "
"and sanitize_error_message keeps the entire credential. Log "
"records redact it (TestCredentialShapesOnTheLogRecordPath)."
),
)
def test_url_userinfo_with_a_slash_in_the_password_is_redacted(self):
msg = "postgresql://admin:Sup3r/[email protected]/app"
assert "Sup3r/S3cret" not in sanitize_error_message(msg)
def test_query_parameter_credential_is_redacted(self):
"""Positive control for the query-parameter pattern."""
msg = f"GET /v1/search?api_key={_SECRET_BODY}&q=x failed"
assert _SECRET_BODY not in sanitize_error_message(msg)
def test_unanchored_key_assignment_is_not_redacted(self):
"""The query-parameter pattern requires a leading ``?`` or ``&``.
A form-encoded body or a config line echoed into an exception
message carries the same ``api_key=<value>`` text with no
delimiter in front of it, and is left intact. Documented
limitation of a shape-based scrubber, not a regression --
recorded so the boundary is explicit.
"""
msg = f"body was api_key={_SECRET_BODY}"
assert _SECRET_BODY in sanitize_error_message(msg)
def test_a_bare_token_has_no_shape_to_match(self):
"""An unlabelled high-entropy value is not redactable by shape.
This is by design -- the module docstring names
:func:`redact_secrets` with the known literal as the backstop.
The control below shows that backstop working, so this is a
boundary, not a hole. It does mean any catch site that calls
only ``sanitize_error_message`` leaks an unlabelled key.
"""
msg = f"auth rejected value {_HEX_TOKEN}"
assert _HEX_TOKEN in sanitize_error_message(msg)
# Control: the literal-value pass does redact it.
assert _HEX_TOKEN not in redact_secrets(msg, _HEX_TOKEN)
def test_redact_secrets_skips_values_under_the_length_floor(self):
"""``min_length=8`` means a short secret is silently kept.
Fails open by construction. The docstring says so; pinned here
because "I passed the secret to redact_secrets" is not on its
own a guarantee that it was removed.
"""
short = "hunter7"
assert short in redact_secrets(f"pw was {short}", short)
# Control: one character longer and it is removed.
longer = "hunter77"
assert longer not in redact_secrets(f"pw was {longer}", longer)
def _logged(msg: str) -> str:
"""*msg* as the loguru patcher (``redact_log_record``) hands it to the
sinks."""
record = {"message": msg}
redact_log_record(record)
return record["message"]
class TestCredentialShapesOnTheLogRecordPath:
"""The shapes the log-record path adds on top of the patterns above.
``redact_log_record`` (the loguru patcher every sink sits behind) runs
``_ADDITIONAL_CREDENTIAL_PATTERNS`` as the last step over the message
and every ``extra`` string; ``sanitize_error_message`` does not run
them, which the two strict-xfails above pin.
"""
@pytest.mark.parametrize("label", ["Api-Key", "api-key", "api_key"])
def test_unprefixed_api_key_header_is_redacted(self, label):
"""The unprefixed ``Api-Key:`` header -- what Anthropic, Azure
Cognitive Services and several search APIs actually send -- does
not ship the key verbatim to the logs."""
msg = f"upstream said {label}: {_SECRET_BODY}"
assert _SECRET_BODY not in _logged(msg)
@pytest.mark.parametrize(
("msg", "password"),
[
("postgresql://admin:Sup3r/[email protected]/app", "Sup3r/S3cret"),
("postgresql://ldr:ab/cd+ef@postgres:5432/ldr", "ab/cd+ef"),
("redis://:ab/cdEF12345@redis:6379/0", "ab/cdEF12345"),
("postgresql://ldr:ab/cd@localhost/db", "ab/cd"),
("mysql+pymysql://u:x/[email protected]:3306/db", "x/y=z"),
],
ids=[
"dotted-host",
"compose-service",
"password-only",
"localhost",
"ip",
],
)
def test_url_userinfo_with_a_slash_in_the_password_is_redacted(
self, msg, password
):
"""A password containing an unescaped ``/`` (routine for
base64-ish generated passwords pasted into a DSN) is redacted in a
log record, whatever the host after the ``@`` looks like:
container DSNs name a compose service, ``localhost`` or an IP, not
a dot-qualified domain."""
assert password not in _logged(msg)
@pytest.mark.parametrize(
"msg",
[
"GET https://api.example.com:443/users/[email protected] failed",
"http://localhost:5000/notes/api/u/[email protected] returned 404",
"https://h.example:8080?next=a/[email protected] rejected",
"http://[::1]:8080/a/[email protected] timed out",
"https://example.com/a:b/[email protected] moved",
],
ids=["port-path", "localhost-port-path", "port-query", "ipv6", "path"],
)
def test_a_host_port_path_with_an_at_sign_is_not_userinfo(self, msg):
"""Admitting ``/`` in the password must not read
``host:port/path@x`` as ``user:password@host``: that erased the
real host and port from the message and kept the email-shaped
path segment instead."""
assert _logged(msg) == msg
_X_API_KEY_SHIELDS_A_DSN = pytest.mark.xfail(
strict=True,
reason=(
"DEFECT (pre-existing, unchanged by #6888): the x-api-key "
"pattern in _CREDENTIAL_PATTERNS runs before the URL-credential "
"pattern, so it eats the DSN's scheme as part of its >=16-char "
"value and the resulting '[REDACTED]://user:pass@' no longer "
"matches any scheme-anchored pattern, in sanitize_error_message "
"and in the log record's passes alike."
),
)
@pytest.mark.parametrize(
"label",
[
"api_key: ",
"api-key: ",
pytest.param("x-api-key: ", marks=_X_API_KEY_SHIELDS_A_DSN),
pytest.param("x-api-key=", marks=_X_API_KEY_SHIELDS_A_DSN),
],
ids=[
"api_key-colon",
"api-key-colon",
"x-api-key-colon",
"x-api-key-equals",
],
)
def test_key_label_does_not_shield_a_following_dsn(self, label):
"""The API-key header value class admits ``+``, ``/`` and letters,
so a header pattern that runs before the URL-credential patterns
eats a pasted DSN's scheme as a bare header value -- and the
resulting ``[REDACTED]://user:pass@...`` can never match the
scheme-anchored URL-credential patterns, so the password survives.
The unprefixed ``api_key:``/``api-key:`` pattern runs after every
other pass of the log record, so the userinfo is already redacted
when it runs. The ``x-api-key`` pattern in ``_CREDENTIAL_PATTERNS``
runs before the URL-credential patterns, which is the strict-xfail
defect pinned here, with a plain and a slash-bearing password."""
for password in ("secretpw", "secr/etpw"):
msg = (
f"{label}postgresql+asyncpg://user:{password}@db.example.com/x"
)
out = _logged(msg)
assert password not in out
assert "@db.example.com/x" in out
@pytest.mark.parametrize(
("msg", "key"),
[
(
"api-key: 0123456789abcdefhttps://user:[email protected]/x",
"0123456789abcdef",
),
("Api-Key: sk_live_9f8e7d6c5b4a3210ZZZZ://x", "9f8e7d6c5b4a3210"),
("x-api-key: ABCDEFGHIJKLMNOPQRSTUVWX://foo", "ABCDEFGHIJKLMNOP"),
(
"x-api-key=ABCDEFGHIJKLMNOPQRSTUVWXYZ012345.https://h.example.com/",
"ABCDEFGHIJKLMNOP",
),
(
"x-api-key: AbCdEf0123456789XyZaBcDeFgHiJkLmhttps://example.com/v1",
"AbCdEf0123456789",
),
],
ids=[
"digits-then-scheme",
"unprefixed-glued-to-separator",
"x-prefixed-glued-to-separator",
"equals-dot-scheme",
"long-key-then-scheme",
],
)
def test_key_glued_to_a_scheme_is_still_redacted(self, msg, key):
"""A >=16-char value run that ends in ``://`` (a key glued to a
URL scheme, or to a bare ``://``) is still a key, and is redacted
whole, scheme included, when the value's scheme stop does not fire
at the start of the run: the run of scheme characters before
``://`` starts with a digit (``0123...https``), starts after a
``_`` with a digit (``sk_live_9f8e...``), or is longer than 32
characters; the ``x-api-key`` pattern takes the whole run
regardless. The URL-credentials pattern is not anchored to a run's
start and still finds a scheme inside such a run, and it runs
first, so any userinfo after it is redacted too. A run that does
read as a scheme from its first letter is the known gap pinned
below."""
out = _logged(msg)
assert key not in out
assert "secretpw" not in out
@pytest.mark.xfail(
strict=True,
reason=(
"Known gap (as on main): a key glued to a URL scheme whose run of "
"letters, digits, '+', '.' and '-' starts with a letter and "
"reaches '://' within 32 characters reads as that scheme. The "
"unprefixed Api-Key value stops where the scheme starts (so a "
"URL after a value stays legible), and the key is not redacted "
"when fewer than 16 characters come before it."
),
)
@pytest.mark.parametrize(
("msg", "key"),
[
(
"Api-Key: AbCdEf0123456789XyZaBcDehttps://example.com/v1",
"AbCdEf0123456789",
),
("api_key: AbCdEf0123456789XyZaBcDe://x", "AbCdEf0123456789"),
(
"Api-Key: sk_live_AbCdEf0123456789https://example.com/v1",
"AbCdEf0123456789",
),
],
ids=["scheme-from-first-char", "bare-separator", "short-head"],
)
def test_key_read_as_a_glued_scheme_is_a_known_gap(self, msg, key):
assert key not in _logged(msg)
@pytest.mark.parametrize(
"label", ["Api-Key: ", "api_key: ", "x-api_key: ", "x-apikey= "]
)
def test_userinfo_the_strip_completes_after_a_key_label_is_redacted(
self, label
):
"""A password holding a character the log record's strip removes
(``\\x1c``, which ``\\s`` also matches, so no pass before the strip
reads it as userinfo) becomes userinfo only after the strip. The
added patterns run after the record's second pass, so the
URL-credential pattern has already redacted it there."""
msg = label + "postgresql+asyncpg://user:pas\x1c[email protected]/x"
logged = _logged(msg)
assert "user:pass@" not in logged
assert "[REDACTED]:[REDACTED]@db.example.com/x" in logged
@pytest.mark.parametrize(
"dsn",
[
"redis://:pa/ss@redis:6379/0",
"postgres://u:pa/ss@localhost:5432/db",
"postgresql://user:Secr/t@"
+ ".".join(["10", "0", "0", "1"])
+ ":5432/db",
"postgres://u:pa/ss@[::1]:5432/db",
"postgresql://admin:Sup3r/[email protected]/app",
],
ids=[
"service-name-host",
"localhost",
"ipv4-literal",
"ipv6-literal",
"dotted-host",
],
)
def test_slash_bearing_dsn_is_redacted_across_host_shapes(self, dsn):
"""Any host shape a real DSN uses is accepted after the ``@``
(service names such as ``redis`` on container networks,
``localhost``, IP literals), and the host[:port] stays legible."""
password = dsn.split(":", 2)[2].split("@", 1)[0]
assert password in dsn # sanity: we really extracted the password
out = _logged(dsn)
assert password not in out
assert out.endswith(dsn.split("@", 1)[1])
@pytest.mark.parametrize(
"url",
[
"https://example.com:8080/a:[email protected]/x",
"https://example.com:8080/[email protected]/x",
"https://localhost:8080/a:[email protected]/x",
"https://[::1]:8080/a:[email protected]/x",
"https://example.com/a:[email protected]/x",
"https://api.example.com:443/users/[email protected]",
"http://localhost:8888/search?q=@alice",
],
ids=[
"port-colon-path",
"port-nocolon-path",
"localhost-port",
"ipv6-port",
"portless-path",
"port-path-email",
"port-path-query-at",
],
)
def test_host_port_url_with_userinfo_shaped_path_is_not_mangled(self, url):
"""Counterexample contract: ``scheme://host:port/path`` whose
path, query or fragment merely contains ``@`` must keep its real
host:port. The slash-DSN pattern refuses a would-be password
that is an all-digit run followed by ``/``, ``?`` or ``#`` (a
port), and its user class refuses ``[``/``]`` (an IPv6
authority)."""
assert _logged(url) == url
def test_benign_slash_bearing_strings_are_untouched(self):
"""A slash alone is not a credential: config paths and ratio
strings without ``://`` never match any pattern."""
for msg in (
"config pair key/value = enabled",
"retry count 3/5 for job db-migrate/12",
):
assert _logged(msg) == msg
@pytest.mark.parametrize(
("msg", "password"),
[
("postgres://svc.app:12345/[email protected]/x", "12345/678"),
("postgresql://user:1234/56:[email protected]/x", "1234/56:78"),
("postgres://u:12345/678@db/x", "12345/678"),
("postgres://u:/abcdefgh@db/x", "/abcdefgh"),
("postgres://u:12?ab/cd@db/x", "12?ab/cd"),
],
ids=[
"dotted-user",
"colon-after-slash",
"plain-user",
"leading-slash",
"digits-then-query",
],
)
def test_digit_run_password_is_the_documented_gap(self, msg, password):
"""Documented gap — recorded so the boundary is explicit: a
slash-bearing password whose characters before its first ``/``,
``?`` or ``#`` are all digits (none at all included) is
indistinguishable from ``host:port/path`` and is left alone here.
Redacting it would mangle every ``scheme://host:port/...@...``
URL; the dual-scrub literal pass (:func:`redact_secrets` with the
known DSN password) remains the backstop."""
assert _logged(msg) == msg
assert password not in redact_secrets(msg, password)
def test_overlong_slash_bearing_password_is_the_documented_gap(self):
"""Documented gap: the slash-DSN pattern caps the password at
256 characters (the cap is what keeps the scan linear, see
test_log_sanitizer's linear-time tests), so a longer
slash-bearing password is left to the dual-scrub literal pass.
A 256-character one is still redacted."""
at_cap = "a/" * 128
assert at_cap not in _logged(f"postgres://u:{at_cap}@db/x")
over = "a/" * 129
msg = f"postgres://u:{over}@db/x"
assert _logged(msg) == msg
assert over not in redact_secrets(msg, over)
@pytest.mark.timeout(15)
@pytest.mark.parametrize(
"unit",
[
'"redis://cache:6379",',
'"https://api.example.com:443",',
'"postgresql://db:5432",',
'"https://a.com:443/x",',
"https://u:p",
"ab://u:p/",
"postgresqlpostgresql://a:",
],
ids=[
"json-redis-host-port",
"json-https-host-port",
"json-pg-host-port",
"json-host-port-path",
"glued-userinfo-no-at",
"short-scheme-slash-pw",
"long-scheme-letters",
],
)
def test_slash_dsn_pattern_is_linear_on_host_port_runs(self, unit):
"""The realistic trigger for the slash-DSN pattern's former
quadratic scan: a whitespace-free run of ``scheme://host:port``
entries with no ``@`` (a minified JSON list of service URLs).
Uncapped, every scheme start scanned to the end of the run
(measured 2-10 s CPU at 32 KB, GIL held); the length caps bound
the work per start. The run is sized to the 32 KB log-record
bound; CPU-clock budget as in test_log_sanitizer."""
run = unit * (32_760 // len(unit))
t0 = time.process_time()
_logged(run)
elapsed = time.process_time() - t0
assert elapsed < 2.0, (
f"the log-record redaction took {elapsed:.2f}s cpu on a "
f"{len(run)}-char run of {unit!r} (budget: 2.0s)"
)
def test_bracket_in_password_is_redacted(self):
"""A ``]`` in the password is exactly as illegal unencoded as
the ``/`` this pattern exists to tolerate, so it must redact;
``[`` and ``]`` are excluded only from the username class so an
IPv6 authority cannot be read as user:pass."""
msg = "postgres://u:pa]ss/[email protected]/x"
assert _logged(msg) == (
"postgres://[REDACTED]:[REDACTED]@db.example.com/x"
)
# ---------------------------------------------------------------------
# 4. account_lockout
# ---------------------------------------------------------------------
def _make_manager(threshold=10, minutes=15):
return AccountLockoutManager(threshold=threshold, lockout_minutes=minutes)
def _lock_out(mgr, username):
for _ in range(mgr.threshold):
mgr.record_failure(username)
def _saturate(mgr):
"""Lock out more usernames than ``_MAX_SINGLE_FAILURES`` using only the
public API -- ``record_failure`` and nothing else.
~100k calls with the real constant; measured at ~0.1s, so this is a
reachability proof rather than a stress test.
"""
for i in range(mgr._MAX_SINGLE_FAILURES + 1):
_lock_out(mgr, f"spray-{i}")
def _ballast(mgr, usernames, failures):
"""Raise each of *usernames* to *failures* failed attempts."""
for username in usernames:
for _ in range(failures):
mgr.record_failure(username)
def _spray(mgr, prefix, count):
"""One failed attempt each for *count* new usernames."""
for i in range(count):
mgr.record_failure(f"{prefix}-{i}")
def _count(mgr, username):
"""The failures *username* has on record, 0 once evicted."""
entry = mgr._state.get(username)
return 0 if entry is None else entry["count"]
class TestLockoutStateEviction:
"""``_MAX_SINGLE_FAILURES`` eviction, reached only via the public API.
The unit suite shrinks ``_MAX_SINGLE_FAILURES`` to prove the mechanism.
These tests use the real constants and only ``record_failure`` /
``is_locked``, and assert what an attacker *gets*.
"""
def test_the_eviction_threshold_is_reachable_by_spraying(self):
"""~10k requests with distinct usernames fills the single-failure
pool; the next one evicts the oldest."""
mgr = _make_manager()
for i in range(mgr._MAX_SINGLE_FAILURES + 1):
mgr.record_failure(f"spray-{i}")
assert len(mgr._state) == mgr._MAX_SINGLE_FAILURES
assert "spray-0" not in mgr._state
def test_spraying_does_not_reset_a_victims_partial_counter(self):
"""Eviction used to delete every partial failure counter, so ~10k
junk usernames reset a victim sitting at threshold-1 (#6546). The
fewest-failure counters now go first, which here are the junk ones.
"""
mgr = _make_manager()
victim = "victim"
for _ in range(mgr.threshold - 1):
mgr.record_failure(victim)
for i in range(mgr._MAX_SINGLE_FAILURES + 1):
mgr.record_failure(f"spray-{i}")
# One more failure should be the one that locks the account.
mgr.record_failure(victim)
assert mgr.is_locked(victim) is True
def test_a_saturating_spray_cannot_release_an_active_lockout(self):
"""Live lockouts survive a saturating spray.
Eviction never removes an active lockout, and there is no
last-resort clear, so ~10k locked junk usernames leave the victim
locked: an attacker cannot buy a victim's release this way. Every
spray account is locked too, because a counter still climbing is
no longer evicted from under its own failures (see the test
below).
"""
mgr = _make_manager()
victim = "victim"
_lock_out(mgr, victim)
assert mgr.is_locked(victim) is True # precondition
_saturate(mgr)
assert mgr.is_locked(victim) is True
assert len(mgr._state) == mgr._MAX_SINGLE_FAILURES + 2
assert all(mgr.is_locked(f"spray-{i}") for i in (0, 9_999, 10_000))
def test_a_new_account_can_still_be_locked_when_the_table_is_full(
self,
):
"""Once the table was saturated with locked entries, eviction
deleted a new username's own partial counter on each of its
failures, so it never climbed past 1 and lockout was off for every
new account (#6546). The counter being recorded is now never
evicted.
"""
mgr = _make_manager()
_saturate(mgr)
late_victim = "late-victim"
for _ in range(mgr.threshold * 20):
mgr.record_failure(late_victim)
assert mgr.is_locked(late_victim) is True
def test_a_ballast_above_the_victims_count_cannot_evict_it(self):
"""Ballast reset (review of #6787): 9,000 junk usernames at nine
failures each outranked a victim at two, so every 1,001-name spray
evicted the victim. A counter that has reached two failures is
never evicted."""
mgr = _make_manager()
_ballast(mgr, [f"ballast-{i}" for i in range(9_000)], 9)
for _ in range(2):
mgr.record_failure("victim")
for cycle in range(3):
_spray(mgr, f"spray{cycle}", 1_001)
assert _count(mgr, "victim") == 2
for _ in range(mgr.threshold - 2):
mgr.record_failure("victim")
assert mgr.is_locked("victim") is True
def test_an_equal_count_spray_cannot_evict_the_victim(self):
"""Junk raised to the victim's count after the victim's last
failure made the victim the least recent of its equals, which is
the entry eviction took first."""
mgr = _make_manager()
for _ in range(3):
mgr.record_failure("victim")
_ballast(mgr, [f"equal-{i}" for i in range(9_001)], 3)
_spray(mgr, "spray", 1_001)
assert _count(mgr, "victim") == 3
def test_a_victim_one_failure_short_survives_locked_ballast(self):
"""Behind 9,001 locked entries, a 1,001-name spray evicted a
victim at threshold - 1."""
mgr = _make_manager()
for i in range(9_001):
_lock_out(mgr, f"locked-{i}")
for _ in range(mgr.threshold - 1):
mgr.record_failure("victim")
_spray(mgr, "spray", 1_001)
mgr.record_failure("victim")
assert mgr.is_locked("victim") is True
def test_a_counter_climbing_between_sprays_reaches_the_lockout(self):
"""Mid-climb eviction: with ballast present, a spray between each
of an account's failures evicted its counter every time, so nine
of ten failures got through without a lockout."""
mgr = _make_manager()
_ballast(mgr, [f"ballast-{i}" for i in range(9_000)], 9)
for cycle in range(mgr.threshold):
mgr.record_failure("victim")
_spray(mgr, f"spray{cycle}", 1_001)
assert mgr.is_locked("victim") is True
def test_a_single_failure_is_evicted_only_behind_the_whole_pool(self):
"""What is left: a counter that holds a single failure can be
reset, but only by ``_MAX_SINGLE_FAILURES`` newer single failures,
so an attacker who also makes the victim's failures gets one guess
per ~10,000 failed logins."""
mgr = _make_manager()
_spray(mgr, "before", mgr._MAX_SINGLE_FAILURES)
mgr.record_failure("victim")
_spray(mgr, "after", mgr._MAX_SINGLE_FAILURES - 1)
assert "victim" in mgr._state
_spray(mgr, "last", 1)
assert "victim" not in mgr._state
def test_recording_a_failure_does_not_scan_the_table(self):
"""Each failure is constant work: with 100,000 entries, a
full-table scan per failure took ~6 ms, so 1,000 failures took
seconds."""
mgr = _make_manager()
_ballast(mgr, [f"ballast-{i}" for i in range(90_000)], 2)
_spray(mgr, "fill", mgr._MAX_SINGLE_FAILURES)
assert len(mgr._state) == 100_000
started = time.perf_counter()
_spray(mgr, "timed", 1_000)
assert time.perf_counter() - started < 1.0
def test_eviction_is_a_noop_while_the_table_is_small(self):
"""Positive control: ordinary traffic never triggers eviction."""
mgr = _make_manager()
_lock_out(mgr, "victim")
for i in range(100):
mgr.record_failure(f"noise-{i}")
assert mgr.is_locked("victim") is True
def test_a_failure_waiting_for_the_lock_is_stamped_after_its_holder(
self, monkeypatch
):
"""Expiry scans the table from the front, so the table has to stay
ordered by last failure. The first failure is held inside its clock
read while a second one arrives: a clock read before taking the lock
let the second one append first, with the later time."""
import threading
from local_deep_research.security import account_lockout
start = datetime(2026, 1, 1, tzinfo=timezone.utc)
reading = threading.Event()
release = threading.Event()
class HeldClock:
@staticmethod
def now(tz=None):
if threading.current_thread().name == "first":
reading.set()
release.wait(5)
return start
return start + timedelta(seconds=1)
monkeypatch.setattr(account_lockout, "datetime", HeldClock)
mgr = _make_manager()
first = threading.Thread(
target=mgr.record_failure, args=("first-user",), name="first"
)
second = threading.Thread(
target=mgr.record_failure, args=("second-user",), name="second"
)
first.start()
assert reading.wait(5)
second.start()
second.join(0.2)
release.set()
first.join(5)
second.join(5)
stamps = [entry["last_failure"] for entry in mgr._state.values()]
assert stamps == sorted(stamps)
class TestLockoutScope:
"""Lockout is keyed on username and nothing else."""
def test_a_single_username_is_locked_after_threshold_failures(self):
mgr = _make_manager(threshold=3)
_lock_out(mgr, "alice")
assert mgr.is_locked("alice") is True
def test_credential_stuffing_across_usernames_is_unbounded(self):
"""No global or per-source brake lives in this guard.
Each distinct username buys a fresh ``threshold`` attempts, so a
stuffing run over N accounts gets N x threshold guesses with the
lockout manager never firing once. The only global brake is the
per-IP rate limiter in ``web/dependencies/rate_limit.py``, which
is a different guard with a different key and its own trust
decision (see ``TestGuardConsistency`` below). This is the
documented design -- pinned so the composition is visible.
"""
mgr = _make_manager(threshold=3)
for i in range(200):
for _ in range(mgr.threshold - 1):
mgr.record_failure(f"user-{i}")
assert not any(mgr.is_locked(f"user-{i}") for i in range(200))
def test_an_unknown_username_locks_out_the_same_way(self):
"""No enumeration oracle from the lockout guard itself.
The login route calls ``record_failure`` for any failed attempt,
including one for a username that does not exist, so the 429
"temporarily locked" response is reachable for non-accounts too
and does not distinguish them.
"""
mgr = _make_manager(threshold=3)
_lock_out(mgr, "does-not-exist")
assert mgr.is_locked("does-not-exist") is True
def test_case_and_whitespace_variants_are_distinct_keys(self):
"""The key is the raw submitted string.
If any login path ever normalises the username *after* the
lockout check (or the store is case-insensitive), an attacker
gets ``threshold`` fresh attempts per casing variant against the
same account.
"""
mgr = _make_manager(threshold=3)
_lock_out(mgr, "alice")
assert mgr.is_locked("alice") is True
assert mgr.is_locked("Alice") is False
assert mgr.is_locked("alice ") is False
class TestLockoutDependencyFailure:
"""Fail-open vs fail-closed for the settings dependency."""
def test_a_broken_settings_lookup_falls_back_to_the_safe_default(
self, monkeypatch
):
"""A raising settings backend must not yield a disabled guard.
It fails closed the blunt way: the constructor propagates, so
the singleton is never built with a silently-permissive
threshold. Pinned because the alternative -- swallowing the
error and defaulting to a very large threshold -- would look
identical from the call site.
"""
import local_deep_research.security.account_lockout as mod
def boom(key, default):
raise RuntimeError("settings backend down")
monkeypatch.setattr(mod, "get_security_default", boom)
with pytest.raises(RuntimeError):
AccountLockoutManager()
def test_an_out_of_range_env_threshold_is_clamped_not_honoured(self):
"""The floor comes from ``settings_security.json`` (min 3).
A threshold of 0 would lock every account on its first failed
login -- a one-request denial of service against any known
username. ``_validate_bounds`` clamps it. Control below shows an
in-range value is honoured rather than always clamped.
"""
from local_deep_research.security.security_settings import (
_validate_bounds,
)
key = "security.account_lockout_threshold"
assert _validate_bounds(0, 3, 50, key) == 3
assert _validate_bounds(9999, 3, 50, key) == 50
assert _validate_bounds(10, 3, 50, key) == 10
def test_expiry_is_evaluated_against_wall_clock_on_read(self):
"""``is_locked`` self-heals; control for the eviction tests."""
mgr = _make_manager(threshold=3, minutes=15)
_lock_out(mgr, "alice")
assert mgr.is_locked("alice") is True
mgr._state["alice"]["locked_until"] = datetime.now(
timezone.utc
) - timedelta(seconds=1)
assert mgr.is_locked("alice") is False
assert "alice" not in mgr._state
# ---------------------------------------------------------------------
# 5. Cross-guard consistency
# ---------------------------------------------------------------------
def _in_ssrf_table(addr):
ip = ipaddress.ip_address(addr)
return any(ip in net for net in PRIVATE_IP_RANGES)
class TestGuardConsistency:
"""Two guards, one question, two answers.
``network_utils.is_private_ip`` delegates to the stdlib
(``ip.is_private or ip.is_loopback or ip.is_link_local``).
``ip_ranges.PRIVATE_IP_RANGES`` is a hand-maintained table consumed
by the SSRF validator and the egress PDP. They are not the same
predicate, and ``is_private_ip`` is the base of what decides whether
``X-Forwarded-For`` is trusted (``rate_limit._is_trusted_peer``, via
``is_private_peer_ip``) and
whether a URL is downgraded to plaintext http
(``utilities.url_utils.normalize_url``).
"""
@pytest.mark.parametrize(
"addr", [LOOPBACK_V4, RFC1918_V4, "::1", "fe80::1", "fc00::1"]
)
def test_the_two_tables_agree_on_the_ordinary_cases(self, addr):
"""Positive control for the divergence test below."""
assert is_private_ip(addr) is True
assert _in_ssrf_table(addr) is True
def test_the_two_tables_agree_that_a_public_address_is_public(self):
assert is_private_ip(PUBLIC_V4) is False
assert _in_ssrf_table(PUBLIC_V4) is False
@pytest.mark.xfail(
not ipaddress.ip_address(CGNAT_V4).is_private,
strict=True,
reason=(
"DEFECT (consistency): CPython's IPv4Address.is_private does "
"not cover 100.64.0.0/10 (RFC 6598 shared address space, "
"which ipaddress reports as neither private nor global). "
"ip_ranges.PRIVATE_IP_RANGES and ssrf_validator's "
"PRIVATE_RANGES both list it, and both docstrings call "
"it out as 'used by Podman/rootless containers'. So on an "
"interpreter whose is_private leaves it out (probed above) "
"the same CGNAT address is "
"private to the SSRF validator and public to is_private_ip. "
"The observable split: rate_limit._is_trusted_peer does not "
"trust X-Forwarded-For from a rootless-container proxy "
"(all clients collapse into one rate-limit bucket), and "
"egress/policy._classify_host labels an on-box CGNAT Ollama "
"as an EXPOSING public sink. Both directions fail closed, "
"which is why nothing has caught it -- but the existing "
"coverage at test_network_utils_behavior.py::"
"TestIsPrivateCarrierGradeNAT asserts only "
"isinstance(result, bool), so the behaviour is unpinned."
),
)
def test_cgnat_is_classified_the_same_by_both_guards(self):
assert _in_ssrf_table(CGNAT_V4) is True
assert is_private_ip(CGNAT_V4) is True
def test_is_private_ip_treats_any_dot_local_name_as_private(self):
"""A hostname suffix, not an address, satisfies the predicate.
``rate_limit._is_trusted_peer`` feeds it ``request.client.host``,
which is an address for real transports -- but any code path
that hands it a *name* gets a trust decision an attacker
controls by choosing their hostname.
"""
assert is_private_ip("anything-at-all.local") is True
assert is_private_ip("anything-at-all.example.com") is False
def test_is_private_ip_does_not_normalise_alternative_ip_notations(
self,
):
"""Decimal / octal / hex forms of a loopback address read public.
``ipaddress.ip_address`` refuses them, so the ``except
ValueError`` branch falls through to the ``.local`` suffix test
and returns False. Every OS resolver and ``requests`` accepts
them and connects to loopback. The SSRF validator does not have
this gap -- it parses the host with urllib3 and rejects the URL
-- so this is another place the two guards diverge.
"""
decimal_loopback = str(int(ipaddress.ip_address(LOOPBACK_V4)))
assert is_private_ip(decimal_loopback) is False
# Control: the canonical form is recognised.
assert is_private_ip(LOOPBACK_V4) is True
class TestFailClosedComposition:
"""When a guard's own dependency raises, does it deny or allow?"""
def test_policy_aware_validate_url_fails_closed_on_a_broken_context(
self,
):
"""``egress.fetch`` swallows the error and re-applies strict SSRF.
The ``except Exception`` in ``policy_aware_validate_url`` is the
kind of blanket handler that usually fails open; here it does
not -- the strict ``validate_url(url)`` still runs afterwards.
"""
from local_deep_research.security.egress.fetch import (
policy_aware_validate_url,
)
from local_deep_research.security.egress.policy import EgressScope
url = f"http://{LOOPBACK_V4}:11434/api/tags"
class BrokenContext:
@property
def scope(self):
raise RuntimeError("context is torn down")
assert policy_aware_validate_url(url, BrokenContext()) is False
assert policy_aware_validate_url(url, None) is False
# Control: a well-formed PRIVATE_ONLY context does permit it, so
# the two refusals above are a decision, not a blanket denial.
class PrivateOnly:
scope = EgressScope.PRIVATE_ONLY
assert policy_aware_validate_url(url, PrivateOnly()) is True
def test_scrub_error_survives_an_exception_that_cannot_be_stringified(
self,
):
"""``scrub_error`` runs inside ``except`` blocks and must not raise."""
from local_deep_research.security.log_sanitizer import scrub_error
class Unprintable(Exception):
def __str__(self):
raise RuntimeError("nope")
out = scrub_error(Unprintable())
assert "Unprintable" in out
class BadSecret:
def __str__(self):
raise RuntimeError("nope")
assert scrub_error("plain message", BadSecret()) == "plain message"
# ---------------------------------------------------------------------
# 6. sqlcipher salt helper
# ---------------------------------------------------------------------
class TestSaltHelperFailureMode:
"""``get_salt_for_database`` on a damaged salt file."""
def test_a_present_salt_file_is_used(self):
"""Positive control."""
from local_deep_research.database.sqlcipher_utils import (
create_database_salt,
get_salt_for_database,
)
with tempfile.TemporaryDirectory() as tmp:
db = Path(tmp) / "user.db"
created = create_database_salt(db)
assert get_salt_for_database(db) == created
def test_a_missing_salt_file_downgrades_to_the_shared_legacy_salt(
self,
):
"""Deleting the ``.salt`` file swaps in a hardcoded constant.
This is the intended v1 back-compat path, but the *only* signal
is a ``logger.warning``: the helper returns a globally known
value rather than refusing. Any operator whose backup or volume
mount drops ``.salt`` files silently moves every affected
database onto one shared, published salt.
"""
from local_deep_research.database.sqlcipher_utils import (
LEGACY_PBKDF2_SALT,
create_database_salt,
get_salt_for_database,
)
with tempfile.TemporaryDirectory() as tmp:
db = Path(tmp) / "user.db"
created = create_database_salt(db)
assert created != LEGACY_PBKDF2_SALT
Path(str(db) + ".salt").unlink()
assert get_salt_for_database(db) == LEGACY_PBKDF2_SALT
def test_a_truncated_salt_file_fails_closed(self):
"""Contrast with the missing-file case: a short salt raises."""
from local_deep_research.database.sqlcipher_utils import (
create_database_salt,
get_salt_for_database,
)
with tempfile.TemporaryDirectory() as tmp:
db = Path(tmp) / "user.db"
create_database_salt(db)
Path(str(db) + ".salt").write_bytes(b"short")
with pytest.raises(ValueError, match="unexpected size"):
get_salt_for_database(db)