~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
"""Security coverage for the settings surface lost in the Flask -> FastAPI
migration (PR #3299).
The migration deleted ``tests/web/routes/test_settings_routes*.py`` (7 files,
~330 tests) while porting the blueprint into ``web/routers/settings.py``. The
historical review summarized by ADR-0010 identified four behaviors that
survived in ``src/`` but lost their direct assertions. This file is the
committed regression evidence for those four areas.
COVERAGE AREA 1 -- model-discovery egress policy on
``POST /settings/api/available-models?force_refresh=true``
(6 deleted tests). This route reads STORED PROVIDER CREDENTIALS and contacts
provider model-listing endpoints, so ``_resolve_model_discovery_policy``
and ``_model_discovery_provider_allowed`` (both in
``web/routers/settings.py``) are the gate that decides whether a user's
API key leaves the box. At the review snapshot, only the
``settings_unavailable`` 503 arm had coverage
(``tests/security/test_strict_policy_settings_snapshots.py::
test_model_discovery_rejects_query_failure_before_cache_provider_or_credential``);
the scope-refusal arms, the private_only cache filter, the remote-URL
"local" provider and the mixed-DNS deny-before-credential-read all had
none. Every deny test here is paired with an allow counterpart -- without
one, a route that refused *everything* would score green.
COVERAGE AREA 2 -- secret write-back / echo redaction (6 deleted tests). Two
contracts:
a setting held secret must never be echoed back in a response, and writing
the redaction sentinel (or an empty string) back must be a no-op rather
than storing the sentinel as the value. ``tests/web/routers/
test_settings_api.py`` already covers the single-key PUT no-op
(``test_put_empty_secret_is_noop``) and the GET redaction + sentinel
round-trip (``test_sensitive_setting_get_is_redacted_and_roundtrip_safe``);
this file covers only what that file does NOT -- the two BULK endpoints,
the no-JS form POST, the ``ui_element != "password"`` sensitive-suffix
asymmetry, the bulk-save RESPONSE echo (the
``"settings"`` key of ``_save_all_settings_sync``'s response, a payload
nothing on the branch reads), and secret redaction surviving the ``LDR_*``
env overlay on both branches of ``GET /settings/api/{key}``.
Post-conditions are asserted against the STORED DB ROW, not the API read
-- the API read is redacted, so it cannot tell "secret preserved" from
"sentinel persisted over the secret", which is the exact bug these guards
exist to prevent.
COVERAGE AREA 3 -- the non-editable-setting 403 on
``PUT``/``DELETE /settings/api/{key}``
(the ``if not db_setting.editable`` branch of
``_api_update_setting_sync`` / ``api_delete_setting``). The two nearest
branch tests pass
vacuously: ``test_settings_env_lock_403.py::TestEnvLockedSettingReturns403::
test_put_without_env_lock_is_unaffected`` asserts only that the error text
lacks "environment-locked" (true of a 200), and
``test_settings_cache_invalidation.py::test_delete_invalidates`` sets
``existing.editable = True`` so it never reaches the branch. Verified by
experiment, not by reading: with the ``editable`` check disabled in BOTH
handlers, all 12 tests across those two files still pass, while both
refusal tests below fail.
COVERAGE AREA 4 -- ``_filter_editable_settings`` had no
direct references under ``tests/`` at the review snapshot. It is the authz
gate on both bulk
write paths (``_save_all_settings_sync``, the JSON save, and
``_save_settings_sync``, the no-JS form save): without it any
logged-in user can flip a non-editable global-policy flag such as
``app.allow_registrations`` through a bulk save. Covered here directly and
at both call sites. The two call-site tests are defence-in-depth: with only
the filter disabled they still pass, because ``SettingsManager.set_setting``
carries its own ``editable`` guard (the ``if not setting.editable``
branch of ``SettingsManager.set_setting``). Disabling
both layers fails them, which is the property worth pinning -- the outcome
the user sees, not one particular implementation of it.
Harness: the ``auth_client`` idiom from ``tests/web/routers/test_settings_api.py``
-- a real, in-process ``TestClient(app, raise_server_exceptions=False)`` against
the live FastAPI app, a freshly registered + logged-in throwaway user, and a
real CSRF token (CSRF is ASGI-middleware-enforced, not a config flag). The
per-client ``X-Forwarded-For`` comes from a MONOTONIC counter rather than a
random address; note this is defence-in-depth only, NOT what keeps this
module under the limit. ``settings_limit``
(``web/dependencies/rate_limit.py``) is a shared limit keyed by
``_user_key`` -- ``user:<username>`` for a logged-in request -- so a
distinct source IP buys the settings routes nothing once the throwaway user
is logged in. What actually keeps the suite green is the autouse
``limiter.reset()`` in ``tests/conftest.py``. The counter still matters for
the routes and middleware that DO bucket on client IP (the unauthenticated
register/login calls the fixture makes, and the default-key limits), where
random addresses collide across a large session and produce 429s unrelated
to the guard under test. Every setting mutated is restored in a ``finally``.
No network, no LLM:
the only outbound calls the route would make are stubbed and asserted on.
"""
import itertools
import json
import uuid
from contextlib import ExitStack, contextmanager
from datetime import UTC, datetime
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
import pytest
from fastapi.testclient import TestClient
from local_deep_research.security.data_sanitizer import DataSanitizer
SETTINGS = "local_deep_research.web.routers.settings"
PROVIDERS = "local_deep_research.llm.providers"
POLICY = "local_deep_research.security.egress.policy"
REDACTED = "[REDACTED]"
# The NAME of a setting marked secret — not a secret itself. Deliberately
# not called SECRET_KEY: the repo's file-whitelist scanner matches
# `SECRET_KEY = "..."` as a hardcoded Flask secret and fails CI on it.
SECRET_SETTING_NAME = "llm.openai.api_key"
# The NON-SECRET sibling leaf used by the mixed-container tests below, whose
# premise is "one leaf stays masked while a VISIBLE sibling is edited". The
# sibling name must be one `_matches_sensitive_name`
# (``security/data_sanitizer.py``) does NOT match, or GET masks it too and the
# premise is unsatisfiable: the test can neither read "old-value" back from the
# masked GET nor assert "new-value" on the GET that follows the edit.
# `page_token` is the trap -- it looks innocuous but ends in `_token`, so the
# broadened underscore-suffix arm masks it, exactly as
# `test_nested_json_leaf_sentinel_round_trip_is_a_noop` (which deliberately
# uses TWO sensitive-named leaves) asserts. Executed against the sanitizer at
# this nesting, `description`, `model`, `stop_sequence`, `temperature`,
# `endpoint` and `note` all stay readable; `description` is used here. The
# choice keeps the tests revert-sensitive: `str({"stop_token": <secret>,
# "description": "new-value"})` still embeds the secret verbatim, so both the
# `type(stored) is dict` and the `real_secret not in ...text` assertions flip
# if the coercion skip is reverted.
PLAIN_SIBLING_LEAF = "description"
# Non-editable + user-visible consequence: a regression here lets any logged-in
# user re-open registrations on a locked-down deployment.
NON_EDITABLE_KEY = "app.allow_registrations"
NON_EDITABLE_ENV = "LDR_APP_ALLOW_REGISTRATIONS"
# An ordinary editable key used as the allow counterpart on every write path,
# so "refused everything" can never be mistaken for "refused the right thing".
EDITABLE_KEY = "llm.temperature"
_IP_COUNTER = itertools.count(1)
def _next_forwarded_for() -> str:
"""A fresh client IP from a monotonic counter.
The IP-keyed rate limits (the default ``key_func``, and the
unauthenticated register/login calls the module fixture makes before a
session exists) bucket on the client IP. A random address collides
across a large session and produces 429s that look like guard failures;
a counter cannot. The SETTINGS routes are not among them --
``settings_limit`` is keyed per USER (``_user_key`` in
``web/dependencies/rate_limit.py``), and the autouse ``limiter.reset()``
in ``tests/conftest.py`` is what keeps those within budget.
"""
n = next(_IP_COUNTER)
return f"10.{n // 254 % 254 + 1}.{n % 254 + 1}.23"
# ---------------------------------------------------------------------------
# Harness
# ---------------------------------------------------------------------------
@pytest.fixture(scope="module")
def settings_user():
"""Yield ``(client, username, password)`` for one throwaway user.
The password is yielded so tests can open a direct session on the user's
encrypted DB and assert on the STORED row -- the HTTP read of a secret is
redacted and therefore cannot distinguish "preserved" from "clobbered".
"""
from local_deep_research.web.fastapi_app import app
client = TestClient(app, raise_server_exceptions=False)
client.headers.update({"X-Forwarded-For": _next_forwarded_for()})
username = f"test_settings_sec_{uuid.uuid4().hex[:8]}"
password = "TestPassword123!" # noqa: S105 — test-only credential
def _csrf():
client.get("/auth/login")
resp = client.get("/auth/csrf-token")
return (
resp.json().get("csrf_token", "") if resp.status_code == 200 else ""
)
reg = client.post(
"/auth/register",
data={
"username": username,
"password": password,
"confirm_password": password,
"acknowledge": "true",
"csrf_token": _csrf(),
},
follow_redirects=False,
)
if reg.status_code not in (200, 302):
pytest.fail(
f"Auth bootstrap broken: registration returned {reg.status_code} "
f"(expected 200/302): {reg.text[:300]}"
)
login = client.post(
"/auth/login",
data={
"username": username,
"password": password,
"csrf_token": _csrf(),
},
follow_redirects=False,
)
if login.status_code != 302:
pytest.fail(
f"Auth bootstrap broken: login returned {login.status_code} "
f"(expected 302): {login.text[:300]}"
)
token = client.get("/auth/csrf-token").json().get("csrf_token")
if token:
client.headers.update({"X-CSRFToken": token})
yield client, username, password
client.post("/auth/logout", follow_redirects=False)
@pytest.fixture(scope="module")
def auth_client(settings_user):
return settings_user[0]
def _csrf_token(client) -> str:
"""CSRF is enforced by ASGI middleware -- form POSTs need a real token."""
return client.get("/auth/csrf-token").json()["csrf_token"]
_MISSING = object()
def _stored_value(username, password, key):
"""The value as PERSISTED, bypassing the redacting read path."""
from local_deep_research.database.models.settings import Setting
from local_deep_research.database.session_context import get_user_db_session
with get_user_db_session(username, password) as session:
row = session.query(Setting).filter(Setting.key == key).first()
return _MISSING if row is None else row.value
@contextmanager
def _captured_warnings():
"""Collect every rendered WARNING-or-worse loguru line emitted inside
the block, as a list of strings.
``local_deep_research/__init__.py`` calls
``logger.disable("local_deep_research")``, so without the explicit
``enable`` here the routers' own warnings never reach ANY sink and a
"the secret was not logged" assertion would pass vacuously -- which is
why every test using this also asserts a POSITIVE CONTROL line was
captured. Same enable/add/remove/disable idiom as
``tests/security/test_egress_fullsearch.py``; the ``disable`` in the
``finally`` restores the package's own default state, not a state this
helper invented.
"""
from loguru import logger
rendered: list[str] = []
def _sink(message):
rendered.append(str(message))
logger.enable("local_deep_research")
sink_id = logger.add(_sink, level="WARNING", diagnose=False)
try:
yield rendered
finally:
logger.remove(sink_id)
logger.disable("local_deep_research")
def _set_stored_value(username, password, key, value):
"""Write a value straight onto the stored row (used only to restore state
a test perturbed -- the API cannot restore a secret, because writing a
secret's previous empty value back through the API is itself a no-op)."""
from local_deep_research.database.models.settings import Setting
from local_deep_research.database.session_context import get_user_db_session
with get_user_db_session(username, password) as session:
row = session.query(Setting).filter(Setting.key == key).first()
if row is not None:
row.value = value
session.commit()
def _reset_model_cache(username, password, rows=()):
"""Replace the ``ProviderModel`` cache with exactly *rows*.
Each row is ``(provider, model_key, model_label)``.
"""
from local_deep_research.database.models.providers import ProviderModel
from local_deep_research.database.session_context import get_user_db_session
with get_user_db_session(username, password) as session:
session.query(ProviderModel).delete()
for provider, model_key, model_label in rows:
session.add(
ProviderModel(
provider=provider,
model_key=model_key,
model_label=model_label,
last_updated=datetime.now(UTC),
)
)
session.commit()
# ---------------------------------------------------------------------------
# COVERAGE AREA 1 -- model-discovery egress policy
# ---------------------------------------------------------------------------
CACHED_LOCAL = ("OLLAMA", "llama3:cached", "Llama3 Cached (local)")
CACHED_CLOUD = ("OPENAI", "gpt-cached", "GPT Cached (cloud)")
def _discovery_probe(client, ollama_url, resolver=None):
"""Drive a FORCE-REFRESH discovery with every outbound leg stubbed.
Returns ``(response, credential_reads, list_models_mock)``. ``OLLAMA`` is
the probe provider precisely because it is a *local-default* provider: it
is allowed under ``private_only`` with its default localhost URL, so a
denial can only come from classifying its configured ENDPOINT -- which is
the contract a static provider-name allow-list would miss.
"""
credential_reads = []
def _fake_get_setting(key, username, default=None):
credential_reads.append(key)
if key == "llm.ollama.url":
return ollama_url
return "sk-stored-provider-credential"
list_models = MagicMock(
return_value=[{"value": "m1", "label": "Model One"}]
)
provider_class = SimpleNamespace(
api_key_setting="llm.ollama.api_key",
url_setting="llm.ollama.url",
list_models_for_api=list_models,
)
discovered = {
"OLLAMA": SimpleNamespace(
provider_class=provider_class, provider_name="Ollama"
)
}
managers = [
patch(
f"{PROVIDERS}.get_discovered_provider_options",
return_value=[{"value": "OLLAMA", "label": "Ollama"}],
),
patch(f"{PROVIDERS}.discover_providers", return_value=discovered),
patch(
f"{SETTINGS}._get_setting_from_session",
side_effect=_fake_get_setting,
),
]
if resolver is not None:
managers.append(
patch(f"{POLICY}._resolve_with_timeout", side_effect=resolver)
)
with ExitStack() as stack:
for manager in managers:
stack.enter_context(manager)
response = client.post(
"/settings/api/available-models?force_refresh=true"
)
return response, credential_reads, list_models
@pytest.mark.timeout(180)
class TestModelDiscoveryEgressPolicy:
"""Model reads and discovery resolve the egress scope BEFORE
it reads the model cache or contacts a provider, refuses providers outside
the resolved scope, and fails closed when the scope cannot be resolved."""
@pytest.mark.parametrize(
("scope_value", "reason"),
[
("unprotected", "unprotected_egress_disabled"),
("not-a-real-scope", "unknown_egress_scope"),
],
)
def test_unresolvable_scope_refuses_before_cache_provider_or_credential(
self, settings_user, monkeypatch, scope_value, reason
):
"""A scope that cannot be resolved (the operator-gated ``unprotected``
escape hatch, or a corrupt/tampered value) must 400 BEFORE the model
cache is read, before providers are discovered and before any stored
credential is touched.
The seeded cache rows are the ordering assertion: a route that read the
cache first and only then evaluated policy would answer 200 with these
models, which is exactly the bypass ``_resolve_model_discovery_policy``
was written to close.
"""
client, username, password = settings_user
monkeypatch.delenv("LDR_POLICY_ALLOW_UNPROTECTED_EGRESS", raising=False)
monkeypatch.setenv("LDR_POLICY_EGRESS_SCOPE", scope_value)
_reset_model_cache(
username, password, rows=[CACHED_LOCAL, CACHED_CLOUD]
)
try:
with (
patch(
f"{PROVIDERS}.get_discovered_provider_options"
) as provider_options,
patch(f"{PROVIDERS}.discover_providers") as discover,
patch(f"{SETTINGS}._get_setting_from_session") as credential,
):
resp = client.get("/settings/api/available-models")
assert resp.status_code == 400, resp.text[:300]
assert resp.json() == {
"status": "error",
"message": f"Egress policy refused this request: {reason}",
}
provider_options.assert_not_called()
discover.assert_not_called()
credential.assert_not_called()
# Fail-closed means "no data", not "filtered data": the refusal
# body carries neither a providers payload nor any cached model.
body = resp.json()
assert "providers" not in body
assert "provider_options" not in body
assert CACHED_LOCAL[1] not in resp.text
assert CACHED_CLOUD[1] not in resp.text
finally:
_reset_model_cache(username, password)
def test_private_only_filters_cloud_out_of_the_cached_response(
self, settings_user, monkeypatch
):
"""Under ``private_only`` a cache HIT must be filtered: cloud models
and cloud provider options are dropped, local ones survive.
The surviving local entries are the positive control -- they prove the
cache really was read and rendered, so the absent cloud entries mean
"filtered", not "endpoint broken".
"""
client, username, password = settings_user
monkeypatch.setenv("LDR_POLICY_EGRESS_SCOPE", "private_only")
monkeypatch.delenv("LDR_LLM_OLLAMA_URL", raising=False)
_reset_model_cache(
username, password, rows=[CACHED_LOCAL, CACHED_CLOUD]
)
try:
options = [
{"value": "OLLAMA", "label": "Ollama"},
{"value": "OPENAI", "label": "OpenAI"},
]
with (
patch(
f"{PROVIDERS}.get_discovered_provider_options",
return_value=options,
),
patch(f"{PROVIDERS}.discover_providers", return_value={}),
):
resp = client.get("/settings/api/available-models")
assert resp.status_code == 200, resp.text[:300]
data = resp.json()
# Positive control: the allowed local provider IS reachable.
assert data["providers"]["ollama_models"] == [
{
"value": CACHED_LOCAL[1],
"label": CACHED_LOCAL[2],
"provider": "OLLAMA",
}
]
assert {
"value": "OLLAMA",
"label": "Ollama",
"disabled": False,
"disabled_reason": None,
} in data["provider_options"]
# The cloud provider's cached MODELS are filtered out -- key
# absent, not merely empty -- so a model the backend would refuse
# to call is never offered. The provider OPTION itself is still
# advertised, marked disabled with the policy reason, which is
# what the dropdown renders (#5922 / #5662). Filtering the option
# away entirely made the dropdown look short after a user
# configured a cloud key, with no explanation.
assert "openai_models" not in data["providers"]
blocked = [
option
for option in data["provider_options"]
if option["value"] == "OPENAI"
]
assert len(blocked) == 1
assert blocked[0]["disabled"] is True
assert "Blocked" in blocked[0]["disabled_reason"]
assert CACHED_CLOUD[1] not in resp.text
finally:
_reset_model_cache(username, password)
def test_fresh_discovery_denies_local_provider_with_a_remote_url(
self, settings_user, monkeypatch
):
"""A nominally-local provider pointed at a PUBLIC URL must not be
listed, must not have its stored key read and must not be contacted.
This is the case a static ``LOCAL_PROVIDERS`` name check would wave
through: the provider is "ollama", but its endpoint is 8.8.8.8.
"""
client, username, password = settings_user
monkeypatch.setenv("LDR_POLICY_EGRESS_SCOPE", "private_only")
monkeypatch.setenv("LDR_LLM_OLLAMA_URL", "http://8.8.8.8:11434")
try:
resp, reads, list_models = _discovery_probe(
client, "http://8.8.8.8:11434"
)
assert resp.status_code == 200, resp.text[:300]
data = resp.json()
# Advertised but disabled with the policy reason, not dropped
# (#5922): the user has to be able to see WHY their configured
# endpoint is not selectable.
assert data["provider_options"] == [
{
"value": "OLLAMA",
"label": "Ollama",
"disabled": True,
"disabled_reason": (
'Blocked by egress scope "Private only" '
"(endpoint URL is not local)"
),
}
]
# A denied provider is absent from the model map. The option
# remains visible above so the UI can explain the policy denial.
assert data["providers"] == {}
assert reads == [], (
"the stored provider credential must not be read for a "
f"policy-denied provider; reads={reads}"
)
list_models.assert_not_called()
assert "sk-stored-provider-credential" not in resp.text
finally:
_reset_model_cache(username, password)
def test_fresh_discovery_contacts_local_provider_with_a_local_url(
self, settings_user, monkeypatch
):
"""Allow counterpart for the row above: the SAME provider with a
loopback URL is listed and contacted under the same ``private_only``
scope. Without this, a route that denied everything would satisfy the
deny tests while making model discovery useless."""
client, username, password = settings_user
monkeypatch.setenv("LDR_POLICY_EGRESS_SCOPE", "private_only")
monkeypatch.setenv("LDR_LLM_OLLAMA_URL", "http://127.0.0.1:11434")
try:
resp, reads, list_models = _discovery_probe(
client, "http://127.0.0.1:11434"
)
assert resp.status_code == 200, resp.text[:300]
data = resp.json()
assert data["provider_options"] == [
{
"value": "OLLAMA",
"label": "Ollama",
"disabled": False,
"disabled_reason": None,
}
]
assert data["providers"]["ollama_models"] == [
{"value": "m1", "label": "Model One", "provider": "OLLAMA"}
]
assert "llm.ollama.api_key" in reads
list_models.assert_called_once_with(
"sk-stored-provider-credential", "http://127.0.0.1:11434"
)
finally:
_reset_model_cache(username, password)
def test_mixed_dns_denies_before_credential_read_or_provider_call(
self, settings_user, monkeypatch
):
"""A host resolving to BOTH a private and a public address classifies
public: the HTTP client may pick the public answer, so a single public
record has to prevent the "local" classification.
The assertion that matters is the ordering -- the deny lands before
``_get_setting_from_session`` reads the API key and before
``list_models_for_api`` opens a socket. A denial that happened after
the credential read would still have loaded the secret into a request
aimed at an attacker-chosen host.
"""
client, username, password = settings_user
url = "http://mixed-dns.probe.test:11434"
monkeypatch.setenv("LDR_POLICY_EGRESS_SCOPE", "private_only")
monkeypatch.setenv("LDR_LLM_OLLAMA_URL", url)
def _mixed(hostname):
return [
(2, 1, 6, "", ("10.0.0.5", 11434)),
(2, 1, 6, "", ("93.184.216.34", 11434)),
]
try:
resp, reads, list_models = _discovery_probe(
client, url, resolver=_mixed
)
assert resp.status_code == 200, resp.text[:300]
data = resp.json()
assert data["provider_options"] == [
{
"value": "OLLAMA",
"label": "Ollama",
"disabled": True,
"disabled_reason": (
'Blocked by egress scope "Private only" '
"(endpoint URL is not local)"
),
}
]
# A denied provider is absent from the model map. The option
# remains visible above so the UI can explain the policy denial.
assert data["providers"] == {}
assert reads == [], (
"mixed private/public DNS answers must deny BEFORE the "
f"credential read; reads={reads}"
)
list_models.assert_not_called()
finally:
_reset_model_cache(username, password)
def test_all_private_dns_answers_allow_the_provider(
self, settings_user, monkeypatch
):
"""Allow counterpart for the mixed-DNS row: the identical host with
only private answers is allowed, so the denial above is attributable to
the public record and not to "any hostname is refused"."""
client, username, password = settings_user
url = "http://all-private.probe.test:11434"
monkeypatch.setenv("LDR_POLICY_EGRESS_SCOPE", "private_only")
monkeypatch.setenv("LDR_LLM_OLLAMA_URL", url)
def _private(hostname):
return [
(2, 1, 6, "", ("10.0.0.5", 11434)),
(2, 1, 6, "", ("10.0.0.6", 11434)),
]
try:
resp, reads, list_models = _discovery_probe(
client, url, resolver=_private
)
assert resp.status_code == 200, resp.text[:300]
data = resp.json()
assert data["provider_options"] == [
{
"value": "OLLAMA",
"label": "Ollama",
"disabled": False,
"disabled_reason": None,
}
]
assert data["providers"]["ollama_models"] == [
{"value": "m1", "label": "Model One", "provider": "OLLAMA"}
]
assert "llm.ollama.api_key" in reads
list_models.assert_called_once_with(
"sk-stored-provider-credential", url
)
finally:
_reset_model_cache(username, password)
def test_adaptive_resolved_to_private_only_names_adaptive_not_private(
self, settings_user, monkeypatch
):
"""With no ``LDR_POLICY_EGRESS_SCOPE`` override and the default
``adaptive`` scope resolving to PRIVATE_ONLY (a local primary search
engine), the disabled reason must name the "Adaptive" control the
user's Egress Scope select actually shows -- not "Private only",
a selection they never made.
Reverting the call site at ``settings.py:2091-2094`` to pass ``None``
instead of ``policy_snapshot`` restores the pre-fix bug and this test
catches it: ``_configured_egress_scope(None)`` returns ``None``
(the env override is also unset here), so the ``== EgressScope.
ADAPTIVE`` check fails and the control falls through to naming
"Private only" instead -- and the message loses the parenthetical
entirely, so the exact-string assertion below fails either way.
"""
client, username, password = settings_user
monkeypatch.delenv("LDR_POLICY_EGRESS_SCOPE", raising=False)
original_search_tool = client.get("/settings/api/search.tool").json()[
"value"
]
assert (
client.put(
"/settings/api/search.tool", json={"value": "paperless"}
).status_code
== 200
)
_reset_model_cache(username, password, rows=[CACHED_LOCAL])
try:
options = [
{"value": "OLLAMA", "label": "Ollama"},
{"value": "OPENAI", "label": "OpenAI"},
]
with (
patch(
f"{PROVIDERS}.get_discovered_provider_options",
return_value=options,
),
patch(f"{PROVIDERS}.discover_providers", return_value={}),
):
resp = client.get("/settings/api/available-models")
assert resp.status_code == 200, resp.text[:300]
data = resp.json()
blocked = [
option
for option in data["provider_options"]
if option["value"] == "OPENAI"
]
assert len(blocked) == 1
assert blocked[0]["disabled"] is True
assert "Adaptive" in blocked[0]["disabled_reason"]
assert blocked[0]["disabled_reason"] == (
'Blocked by egress scope "Adaptive" (resolved to Private '
"only from your primary search engine; cloud-only provider)"
)
finally:
_reset_model_cache(username, password)
client.put(
"/settings/api/search.tool",
json={"value": original_search_tool},
)
@pytest.mark.parametrize(
"research_url,evaluation_url,research_allowed,evaluation_allowed",
[
("https://8.8.8.8/v1", "http://127.0.0.1:8080/v1", False, True),
("http://127.0.0.1:8080/v1", "https://8.8.8.8/v1", True, False),
("https://8.8.8.8/v1", "http://127.0.0.1 ", False, True),
],
)
def test_benchmark_metadata_uses_its_endpoint_without_changing_model_cache(
settings_user,
monkeypatch,
research_url,
evaluation_url,
research_allowed,
evaluation_allowed,
):
client, username, password = settings_user
monkeypatch.setenv("LDR_LLM_REQUIRE_LOCAL_ENDPOINT", "true")
monkeypatch.setenv("LDR_LLM_OPENAI_ENDPOINT_URL", research_url)
monkeypatch.setenv("LDR_BENCHMARK_EVALUATION_ENDPOINT_URL", evaluation_url)
_reset_model_cache(
username,
password,
rows=[
CACHED_LOCAL,
("OPENAI_ENDPOINT", "endpoint-cached", "Endpoint cached"),
],
)
list_models = MagicMock(
return_value=[{"value": "m1", "label": "Model One"}]
)
def _stub(api_key_setting, url_setting, provider_name):
return SimpleNamespace(
provider_class=SimpleNamespace(
api_key_setting=api_key_setting,
url_setting=url_setting,
list_models_for_api=list_models,
),
provider_name=provider_name,
)
# The stub has to advertise the providers under test. The policy's
# user-registered-LLM exemption (``_is_user_registered_llm``) treats a
# name that IS in the LLM registry -- every built-in is auto-registered
# by the real ``discover_providers()`` -- but is NOT in the discovered
# map as in-process code, and allows it WITHOUT classifying its
# endpoint. An empty map therefore answers "allowed" for every provider
# and makes both directions of this parametrization vacuous.
discovered = {
"OPENAI_ENDPOINT": _stub(
"llm.openai_endpoint.api_key", "llm.openai_endpoint.url", "Endpoint"
),
"OLLAMA": _stub("llm.ollama.api_key", "llm.ollama.url", "Ollama"),
}
try:
with (
patch(
f"{PROVIDERS}.get_discovered_provider_options",
return_value=[
{"value": "OPENAI_ENDPOINT", "label": "Endpoint"},
],
),
patch(f"{PROVIDERS}.discover_providers", return_value=discovered),
):
response = client.get("/settings/api/available-models")
assert response.status_code == 200, response.text[:300]
data = response.json()
assert data["provider_options"][0]["disabled"] is not research_allowed
assert (
data["benchmark_provider_options"][0]["disabled"]
is not evaluation_allowed
)
assert (
"openai_endpoint_models" in data["providers"]
) is research_allowed
# The seeded cache answers the request, so the benchmark metadata is
# built without contacting a provider or refreshing the model cache.
# (``discover_providers`` itself is not the guard: the policy calls
# it to tell a built-in from an in-process LLM.)
list_models.assert_not_called()
finally:
_reset_model_cache(username, password)
# ---------------------------------------------------------------------------
# COVERAGE AREA 2 -- secret write-back / echo redaction
# ---------------------------------------------------------------------------
@pytest.mark.timeout(180)
class TestBulkSecretWriteBackAndEcho:
"""The bulk write paths must not echo a secret and must treat an empty /
sentinel secret as a no-op. Deliberately disjoint from
``tests/web/routers/test_settings_api.py``, which covers the single-key
PUT no-op and the single-key + bulk GET redaction."""
def test_bulk_save_response_echo_redacts_secrets(self, settings_user):
"""``POST /settings/save_all_settings`` echoes the whole settings dict
back (``settings.py:933``). At the review snapshot no test inspected
that payload; this case pins ``redact_settings_snapshot`` at the
response boundary."""
client, username, password = settings_user
secret = "sk-echo-must-not-leak-9271" # noqa: S105
original_secret = _stored_value(username, password, SECRET_SETTING_NAME)
original_temp = client.get(f"/settings/api/{EDITABLE_KEY}").json()[
"value"
]
try:
assert (
client.put(
f"/settings/api/{SECRET_SETTING_NAME}",
json={"value": secret},
).status_code
== 200
)
assert (
_stored_value(username, password, SECRET_SETTING_NAME) == secret
)
resp = client.post(
"/settings/save_all_settings", json={EDITABLE_KEY: 0.55}
)
assert resp.status_code == 200, resp.text[:300]
data = resp.json()
assert data["status"] == "success"
# Non-vacuity: the echo is a real, populated snapshot -- the key is
# present and an ordinary setting carries its true value.
echoed = data["settings"]
assert SECRET_SETTING_NAME in echoed
assert echoed[EDITABLE_KEY]["value"] == 0.55
assert echoed[SECRET_SETTING_NAME]["ui_element"] == "password"
# ... and the secret in it is masked, in the parsed payload and in
# the raw bytes on the wire.
assert echoed[SECRET_SETTING_NAME]["value"] == REDACTED
assert secret not in json.dumps(data)
assert secret not in resp.text
finally:
client.put(
f"/settings/api/{EDITABLE_KEY}", json={"value": original_temp}
)
if original_secret is not _MISSING:
_set_stored_value(
username, password, SECRET_SETTING_NAME, original_secret
)
def test_bulk_save_empty_secret_is_a_noop(self, settings_user):
"""An empty password field on a dashboard save must not wipe the
stored key. ``test_settings_api.py`` covers the SENTINEL half of this
guard on this route and the empty half on the single-key PUT; the
empty half on the bulk route is this row."""
client, username, password = settings_user
secret = "sk-bulk-keep-me-4417" # noqa: S105
original_secret = _stored_value(username, password, SECRET_SETTING_NAME)
original_temp = client.get(f"/settings/api/{EDITABLE_KEY}").json()[
"value"
]
try:
client.put(
f"/settings/api/{SECRET_SETTING_NAME}", json={"value": secret}
)
assert (
_stored_value(username, password, SECRET_SETTING_NAME) == secret
)
resp = client.post(
"/settings/save_all_settings",
json={SECRET_SETTING_NAME: "", EDITABLE_KEY: 0.44},
)
assert resp.status_code == 200, resp.text[:300]
data = resp.json()
assert data["status"] == "success"
# Positive control: the batch really was applied -- the ordinary
# key in the SAME payload was written.
assert EDITABLE_KEY in data["updated"]
assert (
client.get(f"/settings/api/{EDITABLE_KEY}").json()["value"]
== 0.44
)
# The secret was skipped, not written: neither "" nor the sentinel
# replaced it in storage.
assert SECRET_SETTING_NAME not in data["updated"]
assert (
_stored_value(username, password, SECRET_SETTING_NAME) == secret
)
finally:
client.put(
f"/settings/api/{EDITABLE_KEY}", json={"value": original_temp}
)
if original_secret is not _MISSING:
_set_stored_value(
username, password, SECRET_SETTING_NAME, original_secret
)
@pytest.mark.parametrize("submitted", ["", REDACTED])
def test_no_js_form_post_secret_write_back_is_a_noop(
self, settings_user, submitted
):
"""The no-JS ``POST /settings/save_settings`` fallback carries its own
copy of the write-back guard (``settings.py:1123``). A browser with
JavaScript disabled renders password inputs empty and re-submits the
redacted sentinel, so both values must be idempotent on this route
too."""
client, username, password = settings_user
secret = "sk-form-keep-me-8823" # noqa: S105
original_secret = _stored_value(username, password, SECRET_SETTING_NAME)
original_temp = client.get(f"/settings/api/{EDITABLE_KEY}").json()[
"value"
]
try:
client.put(
f"/settings/api/{SECRET_SETTING_NAME}", json={"value": secret}
)
assert (
_stored_value(username, password, SECRET_SETTING_NAME) == secret
)
resp = client.post(
"/settings/save_settings",
data={
SECRET_SETTING_NAME: submitted,
EDITABLE_KEY: "0.33",
"csrf_token": _csrf_token(client),
},
follow_redirects=False,
)
assert resp.status_code == 302, resp.text[:300]
# Positive control: this form POST did write.
assert (
client.get(f"/settings/api/{EDITABLE_KEY}").json()["value"]
== 0.33
)
assert (
_stored_value(username, password, SECRET_SETTING_NAME) == secret
)
finally:
client.put(
f"/settings/api/{EDITABLE_KEY}", json={"value": original_temp}
)
if original_secret is not _MISSING:
_set_stored_value(
username, password, SECRET_SETTING_NAME, original_secret
)
def test_no_js_form_post_rejects_sentinel_on_a_brand_new_key(
self, settings_user
):
"""Regression for the post-#5771 review: the no-JS ``POST
/settings/save_settings`` fallback (``_save_settings_sync``) is the
one write route out of three that never wired in
``_embeds_sentinel_on_create``. ``save_all_settings`` and
``api_update_setting`` both reject a sentinel-bearing value on
creation (there is no prior stored value for it to be an untouched
round-trip of, so every occurrence is a corrupted client value); this
route let it straight through to ``set_setting``, persisting the
literal ``"[REDACTED]"`` string as a brand-new credential's value.
The key is chosen so it does not exist yet (namespace validation
takes the creation branch) and ends in a sensitive suffix, so the
guard actually has something to check.
"""
client, username, password = settings_user
key = f"llm.probe_{uuid.uuid4().hex[:6]}.api_key"
assert _stored_value(username, password, key) is _MISSING
try:
resp = client.post(
"/settings/save_settings",
data={
key: f"sk-corrupted-{REDACTED}-value",
"csrf_token": _csrf_token(client),
},
follow_redirects=False,
)
assert resp.status_code == 302, resp.text[:300]
# The row must never have been created with the sentinel spliced
# into it -- neither present with the corrupted value, nor
# present at all.
stored = _stored_value(username, password, key)
assert stored is _MISSING, (
f"sentinel-bearing value was persisted for a brand-new key: {stored!r}"
)
finally:
client.delete(f"/settings/api/{key}")
def test_nested_json_leaf_sentinel_round_trip_is_a_noop(
self, settings_user
):
"""Regression for the post-#5771 review: a secret nested INSIDE a
JSON-typed setting whose OUTER key is not itself sensitive.
``redact_value`` recurses a container by sub-key name, so
``llm.<probe>.model_kwargs`` (not a sensitive key) can still have a
nested ``stop_token``/``page_token`` leaf masked on GET (same
recursion a ``get_setting("llm")`` subtree request uses). But all
three write-back guards gated their dict/list check on
``is_sensitive_setting(OUTER key)`` -- which is False here -- so an
untouched GET round-trip sailed straight past every guard and
persisted the literal ``"[REDACTED]"`` string over the real nested
secret. The guards now check every dict/list write for the sentinel
regardless of the outer key.
"""
client, username, password = settings_user
key = f"llm.probe_{uuid.uuid4().hex[:6]}.model_kwargs"
real_value = {
"stop_token": "s3cr3t-nested-9911",
"page_token": "s3cr3t-nested-4420",
}
try:
create = client.put(
f"/settings/api/{key}", json={"value": real_value}
)
assert create.status_code == 201, create.text[:300]
# Premise: the outer key is not itself a sensitive name.
assert not any(
key.rsplit(".", 1)[-1] == n
for n in ("api_key", "password", "secret", "token")
)
meta = client.get(f"/settings/api/{key}").json()
# ... yet the nested sensitive-named leaves are masked on GET.
assert meta["value"] == {
"stop_token": REDACTED,
"page_token": REDACTED,
}
# An untouched round-trip: submit exactly what the GET returned.
resp = client.put(
f"/settings/api/{key}", json={"value": meta["value"]}
)
assert resp.status_code == 200, resp.text[:300]
stored = _stored_value(username, password, key)
assert stored == real_value, (
"nested secret was clobbered by the redaction sentinel on "
f"an untouched round-trip: {stored!r}"
)
finally:
client.delete(f"/settings/api/{key}")
def test_nested_json_leaf_sentinel_string_encoded_round_trip_is_a_noop(
self, settings_user
):
"""Regression for defect (1) in the post-#6201 review: the SAME
nested secret as ``test_nested_json_leaf_sentinel_round_trip_is_a_noop``
above, but submitted in its JSON-STRING encoding rather than as a
native dict.
``PUT /settings/api/<key>`` accepting ``{"value": "<json string>"}``
is a supported input shape -- ``_parse_json_value``
(``settings/manager.py``) exists precisely to decode it ("Form POST
and env var overrides arrive as raw strings"). Every write-back
guard call site coerced the submitted value AFTER running the
sentinel guards, so the container arm (which only fires on an
actual ``dict``/``list``) never saw this shape: the still-string
value fell through to the string arm, gated on the OUTER key's
sensitivity (False here, same as the dict-shaped case above), sailed
through untouched, and was THEN coerced into a dict carrying the
literal sentinel -- clobbering the real secret the same way a
dict-shaped submission did before #6201's original fix, just
through a different encoding of the identical payload.
This test explicitly sets ``ui_element="json"`` to keep the
string-to-dict coercion premise stable. A text-typed row would store
the JSON string literally, rather than decoding a container.
"""
client, username, password = settings_user
key = f"llm.probe_{uuid.uuid4().hex[:6]}.model_kwargs"
real_value = {
"stop_token": "s3cr3t-nested-string-9911",
"page_token": "s3cr3t-nested-string-4420",
}
try:
create = client.put(
f"/settings/api/{key}",
json={"value": real_value, "ui_element": "json"},
)
assert create.status_code == 201, create.text[:300]
meta = client.get(f"/settings/api/{key}").json()
assert meta["value"] == {
"stop_token": REDACTED,
"page_token": REDACTED,
}
# Submit the masked value in its JSON-STRING encoding -- the
# shape a <textarea> form POST, or a client that
# JSON.stringify()'d the field before sending, would produce --
# not as a native dict.
resp = client.put(
f"/settings/api/{key}",
json={"value": json.dumps(meta["value"])},
)
assert resp.status_code == 200, resp.text[:300]
stored = _stored_value(username, password, key)
assert stored == real_value, (
"nested secret was clobbered by the STRING-ENCODED "
f"redaction sentinel on an untouched round-trip: {stored!r}"
)
# An edit to one leaf alongside an untouched, still-masked
# sibling must also survive the string encoding: the secret
# leaf is restored, the edit is kept.
edited = dict(meta["value"])
edited["page_token"] = "new-nested-value"
resp2 = client.put(
f"/settings/api/{key}", json={"value": json.dumps(edited)}
)
assert resp2.status_code == 200, resp2.text[:300]
stored2 = _stored_value(username, password, key)
assert stored2 == {
"stop_token": real_value["stop_token"],
"page_token": "new-nested-value",
}
finally:
client.delete(f"/settings/api/{key}")
def test_mixed_container_partial_mask_round_trips_and_edits_without_400(
self, settings_user
):
"""Regression for defect (2) in the post-#6201 review: a container
where only SOME leaves are sensitive-named -- the shape every
shipped ``search.engine.web.<engine>`` config dict has (an
``api_key``/``requires_api_key``/``description`` mix), and the
changelog's own motivating "user-added engine" scenario -- must
stay editable.
``redact_value`` masks a container by SUB-KEY, so GET on a mixed
container masks only its sensitive-named leaf; ordinary siblings
come back plain. #6201 required EVERY maskable leaf to be exactly
the sentinel before treating a dict/list write as anything but a
corrupted edit, which made this GET's own output impossible to
PUT back: an untouched round-trip, or an edit to a plain sibling,
both 400'd with "contains the redaction placeholder" even though
the client never saw, and therefore could not resubmit, the real
secret. The write must merge per-leaf instead.
"""
client, username, password = settings_user
key = f"llm.probe_{uuid.uuid4().hex[:6]}.engine_config"
real_value = {
"api_key": "sk-real-engine-secret-7734",
"description": "My self-hosted engine",
"requires_api_key": True,
"max_results": 10,
}
try:
create = client.put(
f"/settings/api/{key}",
json={"value": real_value, "ui_element": "json"},
)
assert create.status_code == 201, create.text[:300]
meta = client.get(f"/settings/api/{key}").json()
# Only the sensitive-named leaf is masked; ordinary siblings
# -- including the OTHER boolean/string fields -- stay plain.
assert meta["value"] == {
"api_key": REDACTED,
"description": "My self-hosted engine",
"requires_api_key": True,
"max_results": 10,
}
# An untouched GET -> PUT round-trip must be a no-op, not a 400.
resp = client.put(
f"/settings/api/{key}", json={"value": meta["value"]}
)
assert resp.status_code == 200, resp.text[:300]
assert _stored_value(username, password, key) == real_value
# Editing a NON-masked sibling while the masked secret leaf
# stays "[REDACTED]" must save -- secret preserved, edit
# applied -- not 400.
edited = dict(meta["value"])
edited["description"] = ""
edited["max_results"] = 25
resp2 = client.put(f"/settings/api/{key}", json={"value": edited})
assert resp2.status_code == 200, resp2.text[:300]
assert _stored_value(username, password, key) == {
"api_key": real_value["api_key"],
"description": "",
"requires_api_key": True,
"max_results": 25,
}
# A genuinely new sentinel -- a key with no stored counterpart
# to restore it from -- is still rejected, not silently merged.
orphaned = dict(edited)
orphaned["new_field"] = REDACTED
resp3 = client.put(f"/settings/api/{key}", json={"value": orphaned})
assert resp3.status_code == 400, resp3.text[:300]
assert _stored_value(username, password, key) == {
"api_key": real_value["api_key"],
"description": "",
"requires_api_key": True,
"max_results": 25,
}
finally:
client.delete(f"/settings/api/{key}")
# ------------------------------------------------------------------
# BL1 (post-#6201 review; #6213 item c): a masked leaf whose STORED
# value is not a string.
#
# ``redact_value`` collapses ANY non-empty value to the BARE sentinel
# when the sub-key is an exact ``DEFAULT_SENSITIVE_KEYS`` match -- a
# list, a dict, an int and a bool as readily as a string (the exact-
# match arm runs before the container arm, so the value's type never
# gets a say). The write-back merge, however, used to restore a
# sentinel leaf only from ``isinstance(stored_value, str) and
# stored_value.strip()``. That made the merge rule STRICTER than the
# masking rule, so for every one of the 18 exact sensitive leaf names
# an untouched GET-then-save of a list/dict/int/bool leaf was a
# permanent 400 the client could not possibly satisfy -- it cannot see
# the value the GET collapsed to one string, and on
# ``save_all_settings`` the rejection rolls back the WHOLE batch.
#
# Each case is ``(leaf name, stored value)``; the last is the string
# control, the one shape that always worked, kept so a regression that
# broke restoration outright cannot hide behind these tests.
_MASKED_LEAF_CASES = [
("api_key", ["sk-list-9911", "sk-list-4420"]),
("token", {"header": "X-Auth", "value": "sk-dict-9911"}),
("secret", 19530),
("password", True),
("api_key", "sk-string-control-9911"),
]
_MASKED_LEAF_IDS = ["list", "dict", "int", "bool", "str-control"]
@pytest.mark.parametrize("route", ["put", "save_all", "form"])
@pytest.mark.parametrize(
"leaf,stored_leaf", _MASKED_LEAF_CASES, ids=_MASKED_LEAF_IDS
)
def test_masked_non_string_leaf_round_trips_on_every_write_route(
self, settings_user, route, leaf, stored_leaf
):
"""An untouched GET -> save of a masked NON-STRING leaf is a 200
no-op that leaves the stored value byte-identical, on all three
write routes.
EXACT REVERT THIS CATCHES: restoring the
``if isinstance(stored_value, str) and stored_value.strip():``
condition in ``_merge_redacted_container``'s string arm in place of
``if not _is_empty_value(stored_value):``. Under the reverted
condition every non-``str-control`` case below is rejected --
``_merge_redacted_container`` returns ``(value, True)``,
``_embeds_redaction_sentinel`` returns that verbatim, and each
route answers 400 (the form route answers 302 with the write
dropped) -- while the ``str-control`` case stays green, which is
exactly the asymmetry the parametrization exists to expose.
"""
client, username, password = settings_user
suffix = uuid.uuid4().hex[:6]
key = f"llm.probe_{suffix}.model_kwargs"
real_value = {leaf: stored_leaf, PLAIN_SIBLING_LEAF: "old-value"}
try:
create = client.put(
f"/settings/api/{key}", json={"value": real_value}
)
assert create.status_code == 201, create.text[:300]
# PREMISE 1: the OUTER key is not itself sensitive, so this is
# the nested-leaf path, not the whole-setting one.
assert not DataSanitizer.is_sensitive_setting(key)
# PREMISE 2: the leaf IS an exact sensitive name, and the
# sibling is not -- otherwise the shape below is unreachable.
assert leaf in DataSanitizer.DEFAULT_SENSITIVE_KEYS
assert not DataSanitizer.is_sensitive_setting(PLAIN_SIBLING_LEAF)
# PREMISE 3 (the one BL1 turns on): the GET collapses a
# non-string leaf to the BARE sentinel, exactly like a string
# one -- the client has no way to resubmit the real value.
meta = client.get(f"/settings/api/{key}").json()
masked = {leaf: REDACTED, PLAIN_SIBLING_LEAF: "old-value"}
assert meta["value"] == masked, (
f"a stored {type(stored_leaf).__name__} under the exact "
f"sensitive leaf {leaf!r} did not mask to the bare "
f"sentinel: {meta['value']!r}"
)
# Submit back EXACTLY what the GET returned -- no edit at all.
if route == "put":
resp = client.put(
f"/settings/api/{key}", json={"value": meta["value"]}
)
assert resp.status_code == 200, resp.text[:300]
elif route == "save_all":
resp = client.post(
"/settings/save_all_settings", json={key: meta["value"]}
)
assert resp.status_code == 200, resp.text[:300]
assert "errors" not in resp.json(), resp.text[:300]
else:
resp = client.post(
"/settings/save_settings",
data={
key: json.dumps(meta["value"]),
"csrf_token": _csrf_token(client),
},
follow_redirects=False,
)
assert resp.status_code == 302, resp.text[:300]
stored = _stored_value(username, password, key)
assert stored == real_value, (
f"an untouched round-trip of a masked "
f"{type(stored_leaf).__name__} leaf via {route} did not "
f"preserve the stored value: {stored!r}"
)
# `==` alone would let a bool through as an int and vice versa.
assert type(stored[leaf]) is type(stored_leaf), (
f"the restored leaf changed type: got "
f"{type(stored[leaf]).__name__}, want "
f"{type(stored_leaf).__name__}"
)
finally:
client.delete(f"/settings/api/{key}")
@pytest.mark.parametrize(
"leaf,stored_leaf", _MASKED_LEAF_CASES, ids=_MASKED_LEAF_IDS
)
def test_sibling_edit_beside_a_masked_non_string_leaf_keeps_the_secret(
self, settings_user, leaf, stored_leaf
):
"""The ordinary workflow: GET, edit a VISIBLE sibling, PUT back
with the secret still masked. The stored non-string leaf must
survive unchanged and the edit must land.
This is the case the round-trip test above cannot cover: a pure
round-trip short-circuits as a no-op inside
``_resolve_container_secret_write`` before the merged value is ever
persisted, so only a GENUINE edit proves the restored value is what
actually reaches the row.
"""
client, username, password = settings_user
suffix = uuid.uuid4().hex[:6]
key = f"llm.probe_{suffix}.model_kwargs"
real_value = {leaf: stored_leaf, PLAIN_SIBLING_LEAF: "old-value"}
try:
create = client.put(
f"/settings/api/{key}", json={"value": real_value}
)
assert create.status_code == 201, create.text[:300]
meta = client.get(f"/settings/api/{key}").json()
edited = dict(meta["value"])
edited[PLAIN_SIBLING_LEAF] = "new-value"
resp = client.put(f"/settings/api/{key}", json={"value": edited})
assert resp.status_code == 200, resp.text[:300]
stored = _stored_value(username, password, key)
assert stored == {
leaf: stored_leaf,
PLAIN_SIBLING_LEAF: "new-value",
}, (
"the sibling edit either dropped the edit or clobbered the "
f"stored secret: {stored!r}"
)
assert type(stored[leaf]) is type(stored_leaf)
# And the restored value never reaches a reader.
get_after = client.get(f"/settings/api/{key}")
assert get_after.json()["value"] == {
leaf: REDACTED,
PLAIN_SIBLING_LEAF: "new-value",
}
finally:
client.delete(f"/settings/api/{key}")
@pytest.mark.parametrize(
"orphan_stored",
[
{"host": "h"},
{"api_key": ""},
{"api_key": " "},
{"api_key": []},
{"api_key": {}},
{"api_key": None},
],
ids=[
"absent",
"empty-str",
"blank-str",
"empty-list",
"empty-dict",
"none",
],
)
def test_orphan_sentinel_is_still_rejected_after_the_non_string_widening(
self, settings_user, orphan_stored
):
"""NEGATIVE CONTROL for the two tests above: widening the restore
from "non-empty string" to "non-empty value" must not turn a
sentinel with NO stored counterpart into an accepted write.
Every stored row here is one ``redact_value`` would NOT have masked
-- the leaf is absent, or its value is empty by ``_is_empty_value``'s
own rule -- so a submitted ``[REDACTED]`` under it is a fabricated
placeholder, not a round-trip, and persisting it would store the
literal text as the credential. Without this the widening could be
silently loosened to "restore whatever is there, including nothing"
and every test above would still pass.
"""
client, username, password = settings_user
suffix = uuid.uuid4().hex[:6]
key = f"llm.probe_{suffix}.model_kwargs"
try:
create = client.put(
f"/settings/api/{key}", json={"value": orphan_stored}
)
assert create.status_code == 201, create.text[:300]
# PREMISE: the GET does NOT produce this shape -- it is a
# client fabrication, which is why rejecting it costs nothing.
assert client.get(f"/settings/api/{key}").json()["value"] != {
"api_key": REDACTED
}
resp = client.put(
f"/settings/api/{key}", json={"value": {"api_key": REDACTED}}
)
assert resp.status_code == 400, resp.text[:300]
stored = _stored_value(username, password, key)
assert stored == orphan_stored, (
f"a rejected orphan sentinel still changed the row: {stored!r}"
)
finally:
client.delete(f"/settings/api/{key}")
def test_put_create_echo_uses_the_same_redactor_as_every_read(
self, settings_user
):
"""The 201 CREATE echo must go through ``redact_value``, not the
bare ``is_sensitive_setting`` NAME predicate (post-#6201 review,
R4).
``is_sensitive_setting`` has no type guard; ``redact_value`` does.
After this PR narrowed the qualifier exemption to the literal
``requires_api_key`` leaf, a CHECKBOX row whose name merely matches
the broadened snake_case arm (``..._supports_api_key``) is a
sensitive NAME with a boolean value -- so the old echo answered the
string "[REDACTED]" for a row every GET returns as ``False``,
corrupting the checkbox the client had just created (the settings
UI renders ``checked`` only for a literal ``True``).
EXACT REVERT THIS CATCHES: replacing the echo's
``DataSanitizer.redact_value(key, db_setting.ui_element,
db_setting.value)`` with
``REDACTION_TEXT if is_sensitive_setting(key, db_setting.ui_element)
else db_setting.value``. Both halves below flip under it: the
boolean becomes the sentinel string, and the container's real
nested secret is no longer masked leaf-by-leaf.
"""
client, username, password = settings_user
suffix = uuid.uuid4().hex[:6]
bool_key = f"llm.probe_{suffix}_supports_api_key"
container_key = f"llm.probe_{suffix}.model_kwargs"
real_secret = f"s3cr3t-create-echo-{suffix}"
try:
# PREMISE: the name IS sensitive -- otherwise the old echo
# never fired and this test would pass vacuously.
assert DataSanitizer.is_sensitive_setting(bool_key, "checkbox")
create = client.put(
f"/settings/api/{bool_key}",
json={"value": False, "ui_element": "checkbox"},
)
assert create.status_code == 201, create.text[:300]
echoed = create.json()["setting"]["value"]
assert echoed is False, (
"the 201 create echo answered "
f"{echoed!r} for a checkbox row -- every GET of the same "
"row returns False, so the echo corrupted it"
)
# The echo must agree with the read path it is standing in for.
assert (
client.get(f"/settings/api/{bool_key}").json()["value"] is False
)
# The other half: the old form could only answer a BARE
# sentinel, so for a container under a NON-sensitive outer key
# it answered the raw container -- nested secret included.
create2 = client.put(
f"/settings/api/{container_key}",
json={
"value": {
"api_key": real_secret,
PLAIN_SIBLING_LEAF: "d",
}
},
)
assert create2.status_code == 201, create2.text[:300]
assert real_secret not in create2.text, (
"the 201 create echo shipped a freshly-created nested "
f"secret in plaintext: {create2.text[:300]}"
)
assert create2.json()["setting"]["value"] == {
"api_key": REDACTED,
PLAIN_SIBLING_LEAF: "d",
}
finally:
client.delete(f"/settings/api/{bool_key}")
client.delete(f"/settings/api/{container_key}")
def test_put_create_rejects_a_json_string_encoded_masked_container(
self, settings_user
):
"""The CREATE path must decode a JSON-STRING encoded container
before its sentinel guard runs (post-#6201 review, R6).
Creation has no prior value, so every occurrence of the sentinel --
exact match included -- is a corrupted client value that would be
stored verbatim as the credential. ``_embeds_sentinel_on_create``
catches that for a NATIVE dict, but a ``<textarea>`` (or a client
that ``JSON.stringify()``'d the field) sends the same container as
a string, and the outer key here is not a sensitive name, so the
string arm of that guard does not fire either.
EXACT REVERT THIS CATCHES: dropping the ``_create_guard_value =
_coerced_for_guard(...)`` line in ``_api_update_setting_sync``'s
create branch and passing the raw ``value`` to
``_embeds_sentinel_on_create``. Verified by reading the guard:
without the coercion it sees the plain string
'{"api_key": "[REDACTED]"}', ``_container_embeds_sentinel`` never
runs on it, ``is_sensitive_setting`` is False for a
``*.model_kwargs`` key, and the create returns 201 with the literal
placeholder text persisted as the value -- so the
``status_code == 400`` assertion below is the one that flips.
"""
client, username, password = settings_user
suffix = uuid.uuid4().hex[:6]
key = f"llm.probe_{suffix}.model_kwargs"
encoded = json.dumps({"api_key": REDACTED, PLAIN_SIBLING_LEAF: "d"})
try:
# PREMISE: the outer key is not sensitive, so nothing but the
# coercion can make the guard see a container here.
assert not DataSanitizer.is_sensitive_setting(key, "textarea")
resp = client.put(
f"/settings/api/{key}",
json={"value": encoded, "ui_element": "textarea"},
)
assert resp.status_code == 400, (
"a JSON-string encoded masked container was accepted on "
f"create: {resp.status_code} {resp.text[:300]}"
)
# Nothing was created, so the placeholder was not persisted.
assert _stored_value(username, password, key) is _MISSING, (
"the rejected create still wrote a row"
)
# ALLOW COUNTERPART: the same shape with no sentinel in it must
# still create normally, so "rejects everything" cannot pass.
clean = json.dumps({"api_key": f"sk-real-{suffix}"})
ok = client.put(
f"/settings/api/{key}",
json={"value": clean, "ui_element": "textarea"},
)
assert ok.status_code == 201, ok.text[:300]
finally:
client.delete(f"/settings/api/{key}")
def test_sensitive_suffix_setting_with_text_ui_element_round_trips_safely(
self, settings_user
):
"""A secret stored with ``ui_element="text"`` but a sensitive ``.api_key``
leaf is the read/write asymmetry case: the GET redactor keys off
``DataSanitizer.is_sensitive_setting`` (key leaf OR ui_element), so the
SENTINEL guard must use the SAME predicate. If it checked only
``ui_element == "password"``, a redacted dashboard round-trip would
persist the literal sentinel over the real credential.
The EMPTY-string half is deliberately narrower (#5602 / #5960):
``_is_secret_empty_noop`` swallows ``""`` only for ``password``
inputs, which render blank so an untouched field must not wipe the
secret. A non-password control renders its (redacted) value, so an
empty submit there is an explicit clear gesture and must reach the
database -- otherwise ``notifications.service_url``, the first
non-password sensitive setting, would be unclearable from the UI.
"""
client, username, password = settings_user
key = f"llm.probe_{uuid.uuid4().hex[:6]}.api_key"
secret = "sk-suffix-secret-5590" # noqa: S105
try:
create = client.put(f"/settings/api/{key}", json={"value": secret})
assert create.status_code == 201, create.text[:300]
# The premise of the test: the row is NOT password-typed.
assert _stored_value(username, password, key) == secret
meta = client.get(f"/settings/api/{key}").json()
assert meta["ui_element"] == "text"
# ... yet the read path still masks it.
assert meta["value"] == REDACTED
# The sentinel is a no-op on both bulk routes, whatever the
# ui_element: it is a round-tripped read, never an edit.
for route, payload in (
("save_all_settings", {"json": {key: REDACTED}}),
(
"save_settings",
{
"data": {
key: REDACTED,
"csrf_token": _csrf_token(client),
},
"follow_redirects": False,
},
),
):
resp = client.post(f"/settings/{route}", **payload)
assert resp.status_code in (200, 302), resp.text[:300]
if resp.status_code == 200:
assert key not in resp.json()["updated"]
assert _stored_value(username, password, key) == secret
# An empty submit on this non-password control DOES clear it.
resp = client.post("/settings/save_all_settings", json={key: ""})
assert resp.status_code == 200, resp.text[:300]
assert key in resp.json()["updated"]
assert _stored_value(username, password, key) == ""
# ... and so does the no-JS form fallback, which must agree.
client.put(f"/settings/api/{key}", json={"value": secret})
assert _stored_value(username, password, key) == secret
resp = client.post(
"/settings/save_settings",
data={key: "", "csrf_token": _csrf_token(client)},
follow_redirects=False,
)
assert resp.status_code == 302, resp.text[:300]
assert _stored_value(username, password, key) == ""
finally:
client.delete(f"/settings/api/{key}")
def test_nested_secret_edit_under_default_text_ui_element_does_not_leak(
self, settings_user
):
"""Regression, end-to-end, for defect (1) in the post-#6201 review:
the previous round's fix for this exact scenario converted a data
LOSS bug into a secret DISCLOSURE bug.
An explicitly text-typed row can still hold a dict (for example,
legacy metadata); new dict settings now infer ``ui_element="json"``.
This test creates the text-typed row and performs a
GENUINE EDIT (one leaf changes, the sibling stays masked), which is
the shape that actually reaches the coercion call: a pure no-op
round-trip short-circuits before it. Every write-back call site ran
``coerce_setting_for_write(value, ui_element="text")`` on the
MERGED container afterward, and "text"'s target type is ``str`` --
flattening the dict to its Python repr and persisting the
just-restored secret as a plain, non-redacted string under a key
nothing downstream treats as sensitive.
Assertions follow the review's own instruction: check the
PERSISTED value's TYPE (proves coercion was skipped, not just that
the request returned 200) and that a subsequent GET does not leak
the secret in the clear (proves no disclosure), rather than
trusting the status code alone.
"""
client, username, password = settings_user
key = f"llm.probe_{uuid.uuid4().hex[:6]}.model_kwargs"
real_secret = "s3cr3t-nested-genuine-edit-8823"
real_value = {"stop_token": real_secret, "description": "old-value"}
# PREMISE (round-5 regression): the sibling leaf must be one the
# sanitizer does NOT mask, or GET returns it as "[REDACTED]" and
# the "edit a still-visible sibling" scenario below is
# unsatisfiable. Asserted rather than assumed -- the round-5
# version of this test used `page_token`, which ends in `_token`
# and IS masked. See PLAIN_SIBLING_LEAF.
assert PLAIN_SIBLING_LEAF in real_value
assert not DataSanitizer.is_sensitive_setting(PLAIN_SIBLING_LEAF)
assert DataSanitizer.is_sensitive_setting("stop_token")
try:
# Preserve the legacy text metadata premise despite newer
# inference of "json" for newly created dict settings.
create = client.put(
f"/settings/api/{key}",
json={"value": real_value, "ui_element": "text"},
)
assert create.status_code == 201, create.text[:300]
meta = client.get(f"/settings/api/{key}").json()
assert meta["ui_element"] == "text"
assert meta["value"] == {
"stop_token": REDACTED,
"description": "old-value",
}
# A GENUINE edit: description changes, stop_token stays masked.
edited = dict(meta["value"])
edited["description"] = "new-value"
resp = client.put(f"/settings/api/{key}", json={"value": edited})
assert resp.status_code == 200, resp.text[:300]
# (1a) The persisted TYPE must be a dict, never a str -- a str
# here means the merged container was flattened by coercion.
stored = _stored_value(username, password, key)
assert type(stored) is dict, (
"reconciled container was coerced to a "
f"{type(stored).__name__} instead of persisted as a dict "
f"-- got {stored!r}"
)
assert stored == {
"stop_token": real_secret,
"description": "new-value",
}
# (1b) No subsequent read discloses the secret in the clear.
get_after = client.get(f"/settings/api/{key}")
assert real_secret not in get_after.text, (
"the real secret leaked through a GET response: "
f"{get_after.text[:300]}"
)
assert get_after.json()["value"] == {
"stop_token": REDACTED,
"description": "new-value",
}
# Same check against the bulk listing, which echoes every
# setting's value in one response.
# NOTE (post-#6201 review, R2): this is
# ``api_get_all_settings`` (@router.get("/api")), NOT
# ``get_bulk_settings`` (@router.get("/api/bulk")). An earlier
# version of this assertion called the same URL while its
# message claimed the BULK endpoint -- and the bulk endpoint
# does leak this exact row (#6222), so the message asserted a
# false negative about a route it never touched. The bulk
# endpoint is covered on its own, as a strict xfail, in
# ``test_bulk_settings_get_leaks_a_container_row_xfail_6222``.
all_settings = client.get("/settings/api")
assert real_secret not in all_settings.text, (
"the real secret leaked through GET /settings/api "
"(api_get_all_settings)"
)
finally:
client.delete(f"/settings/api/{key}")
def test_nested_json_leaf_sentinel_string_edit_under_text_ui_element(
self, settings_user
):
"""Regression, end-to-end, for defect (2) in the post-#6201 review,
extending ``test_nested_json_leaf_sentinel_string_encoded_round_trip_
is_a_noop`` above (which only covers ``ui_element="json"``) to
"text"/"textarea", the row shape defect (1)'s own test uses.
``coerce_setting_for_write``'s target type for "text"/"textarea" IS
``str``, so it can NEVER decode a JSON string into a dict -- the
ui_element-based half of ``_coerced_for_guard``'s fix is a no-op
for these ui_elements. Without the ui_element-independent JSON
decode fallback, a masked container submitted in its JSON-STRING
encoding under "text"/"textarea" would still sail past the
container arm, fall to the outer-key-gated string arm (not
sensitive here), and overwrite the stored dict with the literal
string -- silent data loss of both the structure and the sibling
edit.
"""
client, username, password = settings_user
key = f"llm.probe_{uuid.uuid4().hex[:6]}.model_kwargs"
real_secret = "s3cr3t-nested-string-text-4420"
real_value = {"stop_token": real_secret, "description": "old-value"}
# PREMISE (round-5 regression): the sibling leaf must be one the
# sanitizer does NOT mask, or GET returns it as "[REDACTED]" and
# the "edit a still-visible sibling" scenario below is
# unsatisfiable. Asserted rather than assumed -- the round-5
# version of this test used `page_token`, which ends in `_token`
# and IS masked. See PLAIN_SIBLING_LEAF.
assert PLAIN_SIBLING_LEAF in real_value
assert not DataSanitizer.is_sensitive_setting(PLAIN_SIBLING_LEAF)
assert DataSanitizer.is_sensitive_setting("stop_token")
try:
create = client.put(
f"/settings/api/{key}",
json={"value": real_value, "ui_element": "textarea"},
)
assert create.status_code == 201, create.text[:300]
meta = client.get(f"/settings/api/{key}").json()
assert meta["ui_element"] == "textarea"
assert meta["value"] == {
"stop_token": REDACTED,
"description": "old-value",
}
# Submit the masked value in its JSON-STRING encoding, with an
# edit to the non-masked sibling leaf.
edited = dict(meta["value"])
edited["description"] = "new-value"
resp = client.put(
f"/settings/api/{key}", json={"value": json.dumps(edited)}
)
assert resp.status_code == 200, resp.text[:300]
stored = _stored_value(username, password, key)
assert stored == {
"stop_token": real_secret,
"description": "new-value",
}, (
"nested secret was clobbered by the STRING-ENCODED "
f"redaction sentinel under ui_element='textarea': {stored!r}"
)
finally:
client.delete(f"/settings/api/{key}")
def test_save_all_settings_reconciled_container_is_not_coerced_to_str(
self, settings_user
):
"""Round-5 defect (1): the coercion skip in ``_save_all_settings_sync``
had NO end-to-end test, so reverting it -- which reinstates the
round-1 repr-flattening DISCLOSURE bug on this route -- failed
nothing.
``test_nested_secret_edit_under_default_text_ui_element_does_not_leak``
above pins the identical property, but only for
``_api_update_setting_sync``. Every other
``save_all_settings`` sentinel test on this class is scalar-only, and
the unit test in
``tests/security/test_settings_secret_redaction_isolation.py``
exercises ``_reconciled_container_value_for_write`` in isolation --
neither reaches the ``value if _is_reconciled_container else
coerce_setting_for_write(...)`` expression in this route.
EXACT REVERT THIS CATCHES: replacing that expression in
``_save_all_settings_sync`` with an unconditional
``coerce_setting_for_write(key=..., value=value,
ui_element=current_setting.ui_element)``. The row here is
``ui_element="text"`` (explicit metadata on this PUT-created key),
whose converter IS ``str``, so the reverted call
flattens the just-merged dict to its Python repr and persists the
restored secret as a plain string under a key nothing downstream
treats as sensitive -- which ``redact_value`` then ships back
verbatim on the next GET.
"""
client, username, password = settings_user
suffix = uuid.uuid4().hex[:6]
key = f"llm.probe_{suffix}.model_kwargs"
real_secret = f"s3cr3t-saveall-{suffix}"
real_value = {"stop_token": real_secret, "description": "old-value"}
# PREMISE (round-5 regression): the sibling leaf must be one the
# sanitizer does NOT mask, or GET returns it as "[REDACTED]" and
# the "edit a still-visible sibling" scenario below is
# unsatisfiable. Asserted rather than assumed -- the round-5
# version of this test used `page_token`, which ends in `_token`
# and IS masked. See PLAIN_SIBLING_LEAF.
assert PLAIN_SIBLING_LEAF in real_value
assert not DataSanitizer.is_sensitive_setting(PLAIN_SIBLING_LEAF)
assert DataSanitizer.is_sensitive_setting("stop_token")
try:
# Keep the str-converter premise with explicit text metadata.
create = client.put(
f"/settings/api/{key}",
json={"value": real_value, "ui_element": "text"},
)
assert create.status_code == 201, create.text[:300]
meta = client.get(f"/settings/api/{key}").json()
assert meta["ui_element"] == "text"
assert meta["value"] == {
"stop_token": REDACTED,
"description": "old-value",
}
# A GENUINE edit: description changes, stop_token stays masked.
# A pure round-trip short-circuits as a no-op before ever
# reaching the coercion call, so it cannot pin this.
edited = dict(meta["value"])
edited["description"] = "new-value"
resp = client.post(
"/settings/save_all_settings", json={key: edited}
)
assert resp.status_code == 200, resp.text[:300]
assert key in resp.json()["updated"], resp.text[:300]
# (a) The persisted TYPE proves coercion was skipped. A `str`
# here is the reverted behaviour, and it embeds the secret.
stored = _stored_value(username, password, key)
assert type(stored) is dict, (
"reconciled container was coerced to a "
f"{type(stored).__name__} by save_all_settings instead of "
f"persisted as a dict -- got {stored!r}"
)
assert stored == {
"stop_token": real_secret,
"description": "new-value",
}
# (b) The secret reaches no reader. The save response echoes
# every setting (`response_data["settings"]`), and the GET
# would ship a flattened string through `redact_value`
# untouched, so both are checked -- not just the status code.
assert real_secret not in resp.text, (
"the real secret leaked through the save_all_settings "
f"response echo: {resp.text[:300]}"
)
get_after = client.get(f"/settings/api/{key}")
assert real_secret not in get_after.text, (
"the real secret leaked through a GET after "
f"save_all_settings: {get_after.text[:300]}"
)
assert get_after.json()["value"] == {
"stop_token": REDACTED,
"description": "new-value",
}
# NOTE (post-#6201 review, R2): this is
# ``api_get_all_settings`` (@router.get("/api")), NOT
# ``get_bulk_settings`` (@router.get("/api/bulk")). An earlier
# version of this assertion called the same URL while its
# message claimed the BULK endpoint -- and the bulk endpoint
# does leak this exact row (#6222), so the message asserted a
# false negative about a route it never touched. The bulk
# endpoint is covered on its own, as a strict xfail, in
# ``test_bulk_settings_get_leaks_a_container_row_xfail_6222``.
all_settings = client.get("/settings/api")
assert real_secret not in all_settings.text, (
"the real secret leaked through GET /settings/api "
"(api_get_all_settings)"
)
finally:
client.delete(f"/settings/api/{key}")
def test_save_settings_form_reconciled_container_is_not_coerced_to_str(
self, settings_user
):
"""Round-5 defect (1), the no-JS form half: the coercion skip in
``_save_settings_sync`` had no end-to-end test either.
This route reads ``await request.form()``, so every value arrives as
a STRING -- the ``<textarea>`` JSON-string shape. ``_coerced_for_guard``
decodes it for the guards, ``_reconciled_container_value_for_write``
returns the merged dict, and the skip is what stops that dict being
handed straight back to ``coerce_setting_for_write``.
EXACT REVERT THIS CATCHES: dropping the ``if not
_is_reconciled_container:`` condition around the
``value = coerce_setting_for_write(...)`` call in
``_save_settings_sync``. Under ``ui_element="textarea"`` (converter
``str``) the merged dict is flattened to its Python repr, storing
the just-restored secret in the clear -- and, because a Python repr
is not valid JSON, silently destroying the container shape as well.
The route answers with a 302 + flash message rather than a JSON
body, so the post-conditions are asserted against the STORED ROW and
a subsequent GET, which is where a leak would actually surface.
"""
client, username, password = settings_user
suffix = uuid.uuid4().hex[:6]
key = f"llm.probe_{suffix}.model_kwargs"
real_secret = f"s3cr3t-formpost-{suffix}"
real_value = {"stop_token": real_secret, "description": "old-value"}
# PREMISE (round-5 regression): the sibling leaf must be one the
# sanitizer does NOT mask, or GET returns it as "[REDACTED]" and
# the "edit a still-visible sibling" scenario below is
# unsatisfiable. Asserted rather than assumed -- the round-5
# version of this test used `page_token`, which ends in `_token`
# and IS masked. See PLAIN_SIBLING_LEAF.
assert PLAIN_SIBLING_LEAF in real_value
assert not DataSanitizer.is_sensitive_setting(PLAIN_SIBLING_LEAF)
assert DataSanitizer.is_sensitive_setting("stop_token")
try:
create = client.put(
f"/settings/api/{key}",
json={"value": real_value, "ui_element": "textarea"},
)
assert create.status_code == 201, create.text[:300]
meta = client.get(f"/settings/api/{key}").json()
assert meta["ui_element"] == "textarea"
assert meta["value"] == {
"stop_token": REDACTED,
"description": "old-value",
}
edited = dict(meta["value"])
edited["description"] = "new-value"
resp = client.post(
"/settings/save_settings",
data={
key: json.dumps(edited),
"csrf_token": _csrf_token(client),
},
follow_redirects=False,
)
assert resp.status_code == 302, resp.text[:300]
stored = _stored_value(username, password, key)
assert type(stored) is dict, (
"reconciled container was coerced to a "
f"{type(stored).__name__} by the no-JS form save instead of "
f"persisted as a dict -- got {stored!r}"
)
assert stored == {
"stop_token": real_secret,
"description": "new-value",
}
get_after = client.get(f"/settings/api/{key}")
assert real_secret not in get_after.text, (
"the real secret leaked through a GET after the no-JS form "
f"save: {get_after.text[:300]}"
)
assert get_after.json()["value"] == {
"stop_token": REDACTED,
"description": "new-value",
}
# NOTE (post-#6201 review, R2): this is
# ``api_get_all_settings`` (@router.get("/api")), NOT
# ``get_bulk_settings`` (@router.get("/api/bulk")). An earlier
# version of this assertion called the same URL while its
# message claimed the BULK endpoint -- and the bulk endpoint
# does leak this exact row (#6222), so the message asserted a
# false negative about a route it never touched. The bulk
# endpoint is covered on its own, as a strict xfail, in
# ``test_bulk_settings_get_leaks_a_container_row_xfail_6222``.
all_settings = client.get("/settings/api")
assert real_secret not in all_settings.text, (
"the real secret leaked through GET /settings/api "
"(api_get_all_settings)"
)
finally:
client.delete(f"/settings/api/{key}")
@pytest.mark.xfail(
strict=True,
raises=AssertionError,
reason=(
"#6222: GET /settings/api/bulk reads through the TYPED getter, "
"which maps ui_element 'text' to str and hands redact_value a "
"Python repr of the container instead of the container. The "
"outer key is not a sensitive name, so the repr -- nested "
"secret and all -- ships in the clear. Pre-existing and "
"byte-identical on base; tracked in #6222. When #6222 lands "
"this test XPASSes and strict=True fails it, which is the "
"signal to drop the mark."
),
)
def test_bulk_settings_get_leaks_a_container_row_xfail_6222(
self, settings_user
):
"""The assertion three sibling tests were TRYING to make.
Post-#6201 review, R2: those tests called ``GET /settings/api``
(``api_get_all_settings``) while their message named the BULK
endpoint. This is the real ``GET /settings/api/bulk``
(``get_bulk_settings``, ``@router.get("/api/bulk")``) against the
same row shape, so the gap is pinned by an executable expectation
instead of a passing assertion about an untested route.
Every PREMISE below uses ``pytest.fail`` rather than ``assert``, so
only the #6222 leak itself can satisfy the ``raises=AssertionError``
xfail -- a broken fixture, a changed create contract or a renamed
route surfaces as a real failure instead of hiding inside it.
"""
client, username, password = settings_user
suffix = uuid.uuid4().hex[:6]
key = f"llm.probe_{suffix}.model_kwargs"
real_secret = f"s3cr3t-bulk-{suffix}"
real_value = {"stop_token": real_secret, PLAIN_SIBLING_LEAF: "d"}
try:
create = client.put(
f"/settings/api/{key}",
json={"value": real_value, "ui_element": "text"},
)
if create.status_code != 201:
pytest.fail(f"premise: create failed: {create.text[:300]}")
meta = client.get(f"/settings/api/{key}")
if (
meta.status_code != 200
or meta.json().get("ui_element") != "text"
):
pytest.fail(
f"premise: unexpected row metadata: {meta.text[:300]}"
)
# The SINGULAR GET is correctly redacted -- the contrast is the
# point, so a redaction regression there cannot pass this test.
if real_secret in meta.text:
pytest.fail(
"premise: the singular GET leaked the secret too, so "
"this test no longer isolates the bulk endpoint"
)
bulk = client.get("/settings/api/bulk", params={"keys[]": key})
if bulk.status_code != 200:
pytest.fail(f"premise: bulk GET failed: {bulk.text[:300]}")
if key not in bulk.json().get("settings", {}):
pytest.fail(
"premise: the bulk GET did not return the requested key, "
f"so it cannot be leaking it: {bulk.text[:300]}"
)
assert real_secret not in bulk.text, (
"the real secret leaked through GET /settings/api/bulk "
f"(#6222): {bulk.text[:300]}"
)
finally:
client.delete(f"/settings/api/{key}")
def test_reconciled_container_on_a_number_row_is_never_logged(
self, settings_user
):
"""Legacy containers retain secrets across guarded writes and errors.
Custom row creation used to persist a dict under numeric UI
metadata. It now refuses that row, because every strict read would
reject it, but a database written by an older release can still
hold one. The strict effective reader must reject that invalid
schema/value combination, while the existing secret reconciliation
write paths must preserve the stored container and its masked
leaves. A failed read must not flatten it, substitute a default, or
reveal it in a response, converter exception, or post-commit
notification log.
The three write routes deliberately retain their existing guarded
merge behavior. Text/textarea tests above cover the separate hazard
of coercing a reconciled container into a secret-bearing repr.
"""
from local_deep_research.database.models.settings import (
Setting,
SettingType,
)
from local_deep_research.database.session_context import (
get_user_db_session,
)
client, username, password = settings_user
suffix = uuid.uuid4().hex[:6]
key = f"llm.probe_{suffix}.cfg"
control_key = f"auth.probe_{suffix}_control"
real_secret = f"s3cr3t-number-row-{suffix}"
real_value = {"stop_token": real_secret, "retries": 3}
try:
create = client.put(
f"/settings/api/{key}",
json={"value": real_value, "ui_element": "number"},
)
assert create.status_code == 400, create.text[:300]
assert real_secret not in create.text
assert create.json() == {
"error": (
f"Invalid value for setting {key}: "
"expected a valid number value"
)
}
# Plant the legacy row an older release could create, then
# verify its shape directly: an effective GET is required to
# reject its invalid numeric value.
with get_user_db_session(username, password) as session:
session.add(
Setting(
key=key,
value=real_value,
type=SettingType.LLM,
name="Cfg",
ui_element="number",
)
)
session.commit()
with get_user_db_session(username, password) as session:
row = session.query(Setting).filter(Setting.key == key).one()
assert row.ui_element == "number"
assert row.value == real_value
with _captured_warnings() as lines:
rejected_read = client.get(f"/settings/api/{key}")
assert rejected_read.status_code == 500, rejected_read.text
# The repair diagnostic names the row, never its value.
assert rejected_read.json() == {
"error": (
f"Invalid setting {key!r} from value: "
"expected a valid number value"
)
}
assert real_secret not in rejected_read.text
assert _stored_value(username, password, key) == real_value
# A genuine edit through each of the three write routes in
# turn: `retries` changes, `stop_token` stays masked.
put_resp = client.put(
f"/settings/api/{key}",
json={"value": {"stop_token": REDACTED, "retries": 4}},
)
assert put_resp.status_code == 200, put_resp.text[:300]
assert real_secret not in put_resp.text
bulk_resp = client.post(
"/settings/save_all_settings",
json={key: {"stop_token": REDACTED, "retries": 5}},
)
assert bulk_resp.status_code == 200, bulk_resp.text[:300]
assert key in bulk_resp.json()["updated"], bulk_resp.text[:300]
assert real_secret not in bulk_resp.text
form_resp = client.post(
"/settings/save_settings",
data={
key: json.dumps({"stop_token": REDACTED, "retries": 6}),
"csrf_token": _csrf_token(client),
},
follow_redirects=False,
)
assert form_resp.status_code == 302, form_resp.text[:300]
# POSITIVE CONTROL: a key outside the allowed namespaces
# makes `_save_all_settings_sync` log
# "Security: Rejected setting outside allowed namespaces:
# '<key>'" at WARNING. If this line is missing, the sink is
# not wired up and the secret assertions below prove
# nothing.
control = client.post(
"/settings/save_all_settings",
json={control_key: "x"},
)
assert control.status_code == 400, control.text[:300]
assert any(control_key in line for line in lines), (
"log capture is not wired up: the router's namespace-"
"rejection WARNING for the control key never reached the "
f"sink (captured {len(lines)} line(s))"
)
# The invalid read reached the strict converter; an unrelated
# server error must not satisfy the expected failure above.
assert any(
f"Invalid setting {key!r} from value" in line for line in lines
), lines
leaked = [line for line in lines if real_secret in line]
assert not leaked, (
"the restored secret was written to the log on a number "
"row -- by the converter's own WARNING (see "
"get_typed_setting_value in settings/manager.py), by "
"validate_setting/coerce_setting_for_write, or by the "
f"post-commit settings-changed read: {leaked[0][:300]}"
)
# All three edits landed, so none of them was silently rejected
# by a validator that should never have seen this value.
stored = _stored_value(username, password, key)
assert stored == {"stop_token": real_secret, "retries": 6}, (
f"a write route dropped the reconciled edit: {stored!r}"
)
finally:
client.delete(f"/settings/api/{key}")
def test_typed_value_converter_errors_do_not_expose_values(self):
"""Both parser failure paths retain a key without retaining secrets."""
import traceback
from local_deep_research.settings.exceptions import SettingsValueError
from local_deep_research.settings.manager import get_typed_setting_value
key = "llm.probe_converter_unit.cfg"
db_secret = "s3cr3t-converter-db-7731"
env_secret = "s3cr3t-converter-env-7731"
with _captured_warnings() as lines:
with pytest.raises(SettingsValueError) as db_error:
get_typed_setting_value(
key=key,
value={"stop_token": db_secret, "retries": 4},
ui_element="number",
default=10,
check_env=False,
)
with patch.dict(
"os.environ",
{"LDR_LLM_PROBE_CONVERTER_UNIT_CFG": env_secret},
):
with pytest.raises(SettingsValueError) as env_error:
get_typed_setting_value(
key=key,
value=3,
ui_element="number",
default=10,
)
for caught, source in ((db_error, "value"), (env_error, "environment")):
error = caught.value
assert error.key == key
assert error.source == source
assert key in str(error)
assert error.__cause__ is None
assert error.__context__ is None
rendered = "".join(traceback.format_exception(error))
for secret in (db_secret, env_secret):
assert secret not in rendered
assert not any(secret in line for line in lines)
assert not any("stop_token" in line for line in lines)
def test_masked_list_edit_survives_javascript_number_normalization(
self, settings_user
):
"""Round-5 defect (2): ``_values_equal`` required ``type(a) is
type(b)`` for numerics, which made an ordinary browser round-trip of
a >=2-item list UNSATISFIABLE.
``_non_sentinel_fields_match`` (the anti-permutation gate) demands
every visible field of a list item equal its stored counterpart
before a masked secret is restored by position. A stored ``30.0``
can never be echoed back as a float by a browser: JavaScript's only
number type is a double that serializes as ``30``, and
``settings.js`` additionally ``parseInt``s a decimal-free numeric
before submitting. Under type-strict equality the gate therefore
rejected EVERY submission -- including an untouched round-trip --
with a 400 "contains the redaction placeholder" that no client could
ever construct a passing request for.
EXACT REVERT THIS CATCHES: restoring ``type(a) is type(b) and
a == b`` in ``_values_equal``'s numeric arm. Case (a) then 400s
instead of 200, and case (b)'s genuine edit is refused.
Case (c) is the falsifier: the gate must still reject a real
permutation submitted with the SAME int-vs-float normalization, so
the fix cannot be mistaken for "the numeric comparison was what
closed the permutation hole".
"""
client, username, password = settings_user
suffix = uuid.uuid4().hex[:6]
key = f"llm.probe_{suffix}.endpoints"
secret_a = f"K1-{suffix}"
secret_b = f"K2-{suffix}"
# `timeout` is a float in the DB; `url`/`timeout` are non-sensitive
# leaves, so they stay visible on GET and become the gate's
# comparison material.
real_value = [
{"url": "https://a.example", "api_key": secret_a, "timeout": 30.0},
{"url": "https://b.example", "api_key": secret_b, "timeout": 30.0},
]
try:
create = client.put(
f"/settings/api/{key}",
json={"value": real_value, "ui_element": "json"},
)
assert create.status_code == 201, create.text[:300]
meta = client.get(f"/settings/api/{key}").json()
assert meta["value"] == [
{
"url": "https://a.example",
"api_key": REDACTED,
"timeout": 30.0,
},
{
"url": "https://b.example",
"api_key": REDACTED,
"timeout": 30.0,
},
]
# What a JS client actually sends back: ints, not floats.
round_trip = [
{
"url": "https://a.example",
"api_key": REDACTED,
"timeout": 30,
},
{
"url": "https://b.example",
"api_key": REDACTED,
"timeout": 30,
},
]
# (a) The untouched round-trip is a no-op, not a 400.
resp = client.put(
f"/settings/api/{key}", json={"value": round_trip}
)
assert resp.status_code == 200, (
"a plain GET-then-PUT round-trip of a masked 2-item list "
f"was rejected: {resp.text[:300]}"
)
assert _stored_value(username, password, key) == real_value
# (b) A genuine edit -- entry 0's key replaced, entry 1's left
# masked -- saves, and entry 1's secret is restored to the entry
# it actually belonged to.
new_secret = f"K1-rotated-{suffix}"
edited = [dict(item) for item in round_trip]
edited[0]["api_key"] = new_secret
resp2 = client.put(f"/settings/api/{key}", json={"value": edited})
assert resp2.status_code == 200, resp2.text[:300]
assert _stored_value(username, password, key) == [
{
"url": "https://a.example",
"api_key": new_secret,
"timeout": 30,
},
{
"url": "https://b.example",
"api_key": secret_b,
"timeout": 30,
},
]
assert new_secret not in client.get(f"/settings/api/{key}").text
# (c) FALSIFIER: the same int-normalized shape, but the two
# entries swapped, is still refused -- positional restoration
# would rebind each secret to the wrong url.
permuted = [
{
"url": "https://b.example",
"api_key": REDACTED,
"timeout": 30,
},
{
"url": "https://a.example",
"api_key": REDACTED,
"timeout": 30,
},
]
resp3 = client.put(f"/settings/api/{key}", json={"value": permuted})
assert resp3.status_code == 400, (
"a same-length permutation of a masked list was accepted: "
f"{resp3.text[:300]}"
)
assert _stored_value(username, password, key) == [
{
"url": "https://a.example",
"api_key": new_secret,
"timeout": 30,
},
{
"url": "https://b.example",
"api_key": secret_b,
"timeout": 30,
},
]
finally:
client.delete(f"/settings/api/{key}")
@pytest.mark.parametrize(
"route", ["put", "save_all_settings", "save_settings"]
)
def test_multiselect_json_preserves_nested_masked_secrets(
self, settings_user, route
):
"""Strict JSON containers must reach the guards before multiselect coercion."""
client, username, password = settings_user
key = f"llm.probe_{uuid.uuid4().hex[:6]}.config"
real_value = [{"stop_token": "synthetic-alpha", "retries": 3}]
try:
created = client.put(
f"/settings/api/{key}",
json={"value": real_value, "ui_element": "multiselect"},
)
assert created.status_code == 201, created.text[:300]
masked = client.get(f"/settings/api/{key}").json()["value"]
assert masked == [{"stop_token": REDACTED, "retries": 3}]
# Cover both an unchanged round-trip and an ordinary sibling edit.
for retries in (3, 4):
masked[0]["retries"] = retries
encoded = json.dumps(masked)
if route == "put":
response = client.put(
f"/settings/api/{key}", json={"value": encoded}
)
elif route == "save_all_settings":
response = client.post(
"/settings/save_all_settings", json={key: encoded}
)
else:
response = client.post(
"/settings/save_settings",
data={key: encoded, "csrf_token": _csrf_token(client)},
follow_redirects=False,
)
assert response.status_code in (200, 302, 303), response.text[
:300
]
assert _stored_value(username, password, key) == [
{"stop_token": "synthetic-alpha", "retries": retries}
]
assert (
client.get(f"/settings/api/{key}").json()["value"] == masked
)
finally:
client.delete(f"/settings/api/{key}")
@pytest.mark.timeout(180)
class TestSecretRedactionSurvivesEnvOverlay:
"""``GET /settings/api/{key}`` overlays the ``LDR_*`` env value and THEN
redacts (``settings.py:3103`` DB branch, ``:3145`` default branch). Both
orderings return 200, so only an assertion on the value catches an overlay
that overwrites the redacted value with the operator's plaintext secret."""
def test_env_override_secret_is_redacted_on_the_db_branch(
self, settings_user, monkeypatch
):
client, username, password = settings_user
db_secret = "sk-db-row-secret-1102" # noqa: S105
env_secret = "sk-env-override-secret-3345" # noqa: S105
original_secret = _stored_value(username, password, SECRET_SETTING_NAME)
try:
client.put(
f"/settings/api/{SECRET_SETTING_NAME}",
json={"value": db_secret},
)
# Control: without the env var the key is an ordinary editable row.
before = client.get(f"/settings/api/{SECRET_SETTING_NAME}").json()
assert before["editable"] is True
monkeypatch.setenv("LDR_LLM_OPENAI_API_KEY", env_secret)
resp = client.get(f"/settings/api/{SECRET_SETTING_NAME}")
assert resp.status_code == 200, resp.text[:300]
data = resp.json()
# Non-vacuity: the overlay demonstrably ran (it flipped editable),
# so a redacted value here is redaction-after-overlay, not a
# response that never reached the overlay at all.
assert data["key"] == SECRET_SETTING_NAME
assert data["editable"] is False
assert data["value"] == REDACTED
assert env_secret not in resp.text
assert db_secret not in resp.text
finally:
if original_secret is not _MISSING:
_set_stored_value(
username, password, SECRET_SETTING_NAME, original_secret
)
def test_env_override_secret_is_redacted_on_the_default_only_branch(
self, settings_user, monkeypatch
):
"""Same guard on the no-DB-row branch, reached by deleting the row
first. This branch is a separate copy of the overlay-then-redact
sequence, so it needs its own pin."""
client, username, password = settings_user
env_secret = "sk-env-default-branch-7714" # noqa: S105
original_secret = _stored_value(username, password, SECRET_SETTING_NAME)
try:
assert (
client.delete(
f"/settings/api/{SECRET_SETTING_NAME}"
).status_code
== 200
)
assert (
_stored_value(username, password, SECRET_SETTING_NAME)
is _MISSING
)
# Control: the default branch is live and readable (the registered
# default is an empty string, which is NOT masked).
before = client.get(f"/settings/api/{SECRET_SETTING_NAME}")
assert before.status_code == 200, before.text[:300]
assert before.json()["value"] == ""
assert before.json()["editable"] is True
monkeypatch.setenv("LDR_LLM_OPENAI_API_KEY", env_secret)
resp = client.get(f"/settings/api/{SECRET_SETTING_NAME}")
assert resp.status_code == 200, resp.text[:300]
data = resp.json()
assert data["key"] == SECRET_SETTING_NAME
assert data["editable"] is False
assert data["value"] == REDACTED
assert env_secret not in resp.text
finally:
monkeypatch.delenv("LDR_LLM_OPENAI_API_KEY", raising=False)
client.put(
f"/settings/api/{SECRET_SETTING_NAME}",
json={"value": "sk-restore"},
)
if original_secret is not _MISSING:
_set_stored_value(
username, password, SECRET_SETTING_NAME, original_secret
)
# ---------------------------------------------------------------------------
# COVERAGE AREA 3 -- non-editable settings write controls
# ---------------------------------------------------------------------------
@pytest.mark.timeout(180)
class TestNonEditableSettingAuthz:
"""``editable = False`` marks a setting the deployment owns, not the user.
Both write verbs on ``/settings/api/{key}`` must refuse it, and the STORED
value must be unchanged afterwards -- a 403 that still mutated the row
would be worse than no check at all."""
def test_put_refuses_a_non_editable_setting_and_leaves_it_unchanged(
self, settings_user, monkeypatch
):
client, username, password = settings_user
# Keep the refusal attributable: with the env var set the handler would
# return the *environment-locked* 403 from an earlier guard instead.
monkeypatch.delenv(NON_EDITABLE_ENV, raising=False)
before = _stored_value(username, password, NON_EDITABLE_KEY)
assert before is not _MISSING and before is True
resp = client.put(
f"/settings/api/{NON_EDITABLE_KEY}", json={"value": False}
)
assert resp.status_code == 403, resp.text[:300]
assert resp.json() == {
"error": f"Setting {NON_EDITABLE_KEY} is not editable"
}
assert _stored_value(username, password, NON_EDITABLE_KEY) is True
# Allow counterpart: the same verb on an ordinary key still writes, so
# the 403 above is about editability and not a dead PUT handler.
original_temp = client.get(f"/settings/api/{EDITABLE_KEY}").json()[
"value"
]
try:
allowed = client.put(
f"/settings/api/{EDITABLE_KEY}", json={"value": 0.61}
)
assert allowed.status_code == 200, allowed.text[:300]
assert (
client.get(f"/settings/api/{EDITABLE_KEY}").json()["value"]
== 0.61
)
finally:
client.put(
f"/settings/api/{EDITABLE_KEY}", json={"value": original_temp}
)
def test_delete_refuses_a_non_editable_setting_and_the_row_survives(
self, settings_user, monkeypatch
):
"""Delete-then-recreate is the escalation path: a deletable
"non-editable" row lets a user drop a governed setting and re-create it
with their own value, which is precisely what
``tests/security/test_egress_validation_on_setting_create.py`` exists to
prevent on the create side."""
client, username, password = settings_user
monkeypatch.delenv(NON_EDITABLE_ENV, raising=False)
before = _stored_value(username, password, NON_EDITABLE_KEY)
assert before is not _MISSING and before is True
resp = client.delete(f"/settings/api/{NON_EDITABLE_KEY}")
assert resp.status_code == 403, resp.text[:300]
assert resp.json() == {
"error": f"Setting {NON_EDITABLE_KEY} is not editable"
}
# The row is still there with its original value -- not merely a 403.
assert _stored_value(username, password, NON_EDITABLE_KEY) is True
readback = client.get(f"/settings/api/{NON_EDITABLE_KEY}")
assert readback.status_code == 200
assert readback.json()["value"] is True
assert readback.json()["editable"] is False
def test_delete_of_an_editable_setting_succeeds(self, settings_user):
"""Allow counterpart for the row above: DELETE works on an editable
key, so the 403 is attributable to editability rather than to a DELETE
route that refuses everything."""
client, username, password = settings_user
key = f"llm.probe_delete_{uuid.uuid4().hex[:6]}"
created = client.put(f"/settings/api/{key}", json={"value": "bye"})
assert created.status_code == 201, created.text[:300]
assert _stored_value(username, password, key) == "bye"
resp = client.delete(f"/settings/api/{key}")
assert resp.status_code == 200, resp.text[:300]
assert _stored_value(username, password, key) is _MISSING
assert client.get(f"/settings/api/{key}").status_code == 404
# ---------------------------------------------------------------------------
# COVERAGE AREA 4 -- _filter_editable_settings
# ---------------------------------------------------------------------------
@pytest.mark.timeout(180)
class TestFilterEditableSettings:
"""``_filter_editable_settings`` is the authz gate shared by both bulk
write paths. This class is its direct regression contract."""
def test_drops_non_editable_keys_in_place_and_returns_every_db_row(self):
"""Unit contract: non-editable keys are removed from ``form_data`` IN
PLACE (callers keep using the same dict), unknown keys are left for the
caller's namespace guard to judge, and the full ``{key: Setting}`` map
is returned for the egress validators that run next."""
from local_deep_research.web.routers.settings import (
_filter_editable_settings,
)
editable_row = SimpleNamespace(key=EDITABLE_KEY, editable=True)
locked_row = SimpleNamespace(key=NON_EDITABLE_KEY, editable=False)
db_session = MagicMock()
db_session.query.return_value.all.return_value = [
editable_row,
locked_row,
]
form_data = {
EDITABLE_KEY: 0.7,
NON_EDITABLE_KEY: False,
"llm.not_in_db_yet": "x",
}
same_dict = form_data
all_rows = _filter_editable_settings(form_data, db_session)
assert form_data is same_dict, "must filter in place, not copy"
assert form_data == {EDITABLE_KEY: 0.7, "llm.not_in_db_yet": "x"}
assert all_rows == {
EDITABLE_KEY: editable_row,
NON_EDITABLE_KEY: locked_row,
}
def test_bulk_json_save_leaves_a_non_editable_setting_unchanged(
self, settings_user, monkeypatch
):
"""Call site 1 (``settings.py:566``): ``POST /settings/save_all_settings``.
Without the filter, any logged-in user re-opens registrations on a
locked-down deployment through the ordinary dashboard save."""
client, username, password = settings_user
monkeypatch.delenv(NON_EDITABLE_ENV, raising=False)
original_temp = client.get(f"/settings/api/{EDITABLE_KEY}").json()[
"value"
]
assert _stored_value(username, password, NON_EDITABLE_KEY) is True
try:
resp = client.post(
"/settings/save_all_settings",
json={NON_EDITABLE_KEY: False, EDITABLE_KEY: 0.29},
)
assert resp.status_code == 200, resp.text[:300]
data = resp.json()
assert data["status"] == "success"
# Positive control: the editable key in the same payload WAS saved,
# so the untouched flag is attributable to the filter and not to a
# rejected batch.
assert EDITABLE_KEY in data["updated"]
assert (
client.get(f"/settings/api/{EDITABLE_KEY}").json()["value"]
== 0.29
)
assert NON_EDITABLE_KEY not in data["updated"]
assert NON_EDITABLE_KEY not in data["created"]
assert _stored_value(username, password, NON_EDITABLE_KEY) is True
finally:
client.put(
f"/settings/api/{EDITABLE_KEY}", json={"value": original_temp}
)
def test_no_js_form_save_leaves_a_non_editable_setting_unchanged(
self, settings_user, monkeypatch
):
"""Call site 2 (``settings.py:1080``): the no-JS form POST. A separate
entry point, so the JSON-route test above does not cover it -- and this
one writes through ``SettingsManager.set_setting`` on a different code
path."""
client, username, password = settings_user
monkeypatch.delenv(NON_EDITABLE_ENV, raising=False)
original_temp = client.get(f"/settings/api/{EDITABLE_KEY}").json()[
"value"
]
assert _stored_value(username, password, NON_EDITABLE_KEY) is True
try:
resp = client.post(
"/settings/save_settings",
data={
NON_EDITABLE_KEY: "false",
EDITABLE_KEY: "0.27",
"csrf_token": _csrf_token(client),
},
follow_redirects=False,
)
assert resp.status_code == 302, resp.text[:300]
# Positive control: this form POST did write.
assert (
client.get(f"/settings/api/{EDITABLE_KEY}").json()["value"]
== 0.27
)
assert _stored_value(username, password, NON_EDITABLE_KEY) is True
finally:
client.put(
f"/settings/api/{EDITABLE_KEY}", json={"value": original_temp}
)