~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
fix(security): redact notifications.service_url on settings read paths (#5602)
LearningCircuit69eca236ca70
diff --git a/changelog.d/+redact-notification-service-url.security.md b/changelog.d/+redact-notification-service-url.security.mdnew file mode 100644index 0000000000..ac60b19acf--- /dev/null+++ b/changelog.d/+redact-notification-service-url.security.md@@ -0,0 +1,34 @@+The notification service URL is now redacted on the settings API read paths.+`notifications.service_url` (apprise-style) embeds credentials — e.g.+`mailto://user:pass@host`, `discord://webhook_id/token`, Slack/ntfy tokens —+and was returned in plaintext by the settings API (single, bulk, and full GET,+and the save-all echo), unlike API keys which are masked. It is now treated as+a secret and returned as `[REDACTED]` on read (an unconfigured/empty URL stays+readable; whitespace-only counts as unconfigured), so the credential no longer+reaches the browser at all and cannot be picked up by anything that records a+settings response — devtools, a saved HAR, or a logging proxy. Clearing the URL+from the UI now stores the empty value instead of silently retaining the old+URL, and Test Notification uses the stored URL when the field contains the+redaction sentinel. The app already masked it in notification call-site logs;+the settings debug logger now redacts it too.++`notifications.service_url` is the first sensitive setting that is not a+password input, so it is the first one whose control renders its value into an+editable field. The settings UI now renders any redacted control blank with a+"Saved — type to replace, or press Enter while empty to clear" placeholder,+the way password inputs already do, and the write routes reject a submitted+value that merely *contains* `[REDACTED]` with a 400 instead of storing it.+Without both, a stale tab produced `[REDACTED],discord://webhook/tok` — not an+exact sentinel match, so the existing no-op did not fire, and the saved list+made `NotificationManager` refuse every URL in it, silently stopping all+notifications. The reverse order, `discord://webhook/tok,[REDACTED]`, passed+URL validation and appended the sentinel to the webhook token. An exact+sentinel keeps its existing idempotent no-op behaviour on update; on the+create/recreate path, where there is no stored value to preserve, it is+rejected too.++Because Test Notification can now fall back to the stored URL, it accepts an+empty body and becomes a zero-argument send trigger, so that path gets its own+rate-limit bucket; testing a URL supplied in the request stays unlimited. The+endpoint also treats a whitespace-only URL as unconfigured, matching the+notification manager, instead of handing Apprise literal whitespace.diff --git a/src/local_deep_research/security/data_sanitizer.py b/src/local_deep_research/security/data_sanitizer.pyindex e28f6b08fd..5347a75532 100644--- a/src/local_deep_research/security/data_sanitizer.py+++ b/src/local_deep_research/security/data_sanitizer.py@@ -71,6 +71,20 @@ def _visible_leaf(key: str) -> str:return leaf.strip().lower()+def _is_empty_value(value: Any) -> bool:+ """True when a value counts as unconfigured for the redaction empty rule.++ Covers ``None``, ``""``, ``[]``, ``{}`` plus whitespace-only strings:+ the notification manager already treats ``" "`` as unconfigured+ (``not service_urls.strip()``), so redacting it to the sentinel would+ make an unset URL LOOK configured. Everything else (0, False,+ non-empty strings, non-empty containers) counts as set.+ """+ if isinstance(value, str):+ return not value.strip()+ return value in (None, "", [], {})++class DataSanitizer:"""Utility class for removing sensitive information from data structures."""@@ -99,6 +113,13 @@ class DataSanitizer:"bearer_token","api_secret","app_secret",+ # The notification service URL (apprise-style) embeds credentials --+ # e.g. mailto://user:pass@host, discord://webhook_id/token, Slack/ntfy+ # tokens. The app already masks it in logs (mask_sensitive_url); this+ # keeps it out of the settings API read paths too. Only+ # notifications.service_url has this leaf, so no other setting is+ # affected. The empty-value rule keeps an unconfigured URL readable.+ "service_url",}@staticmethod@@ -148,9 +169,9 @@ class DataSanitizer:masks identically. Returns ``redaction_text`` for a non-emptysensitive value, otherwise ``value`` unchanged.- Empty values (``None``, ``""``, ``[]``, ``{}``) are left readable so- the UI can tell "configured" from "not configured" without leaking- that a secret is set.+ Empty values (``None``, ``""``, ``[]``, ``{}`` and whitespace-only+ strings) are left readable so the UI can tell "configured" from "not+ configured" without leaking that a secret is set.Nested containers are redacted recursively: a subtree request such as``get_setting("llm")`` returns ``{"openai.api_key": "sk-…", …}`` where@@ -163,7 +184,7 @@ class DataSanitizer:"""if DataSanitizer.is_sensitive_setting(key, ui_element, sensitive_keys- ) and value not in (None, "", [], {}):+ ) and not _is_empty_value(value):return redaction_textif isinstance(value, dict):redacted: dict = {}@@ -299,8 +320,9 @@ class DataSanitizer:segment of the outer key matches a sensitive name).- Preserves all metadata (``ui_element``, ``type``, ``description``,etc.) so YAML diffs can still show "this key existed."- - Leaves empty values (``None``, ``""``, ``[]``, ``{}``) unredacted- so diffs of "unset" settings stay readable.+ - Leaves empty values (``None``, ``""``, ``[]``, ``{}`` and+ whitespace-only strings) unredacted so diffs of "unset" settings+ stay readable.- Pure function: does not mutate the input.Entries that don't have the metadata-wrapper shape (e.g. mixeddiff --git a/src/local_deep_research/settings/logger.py b/src/local_deep_research/settings/logger.pyindex b40734cd0e..f5f48580ea 100644--- a/src/local_deep_research/settings/logger.py+++ b/src/local_deep_research/settings/logger.py@@ -107,6 +107,7 @@ def redact_sensitive_keys(settings: Dict[str, Any]) -> Dict[str, Any]:"credential","auth","private",+ "service_url",]redacted = {}diff --git a/src/local_deep_research/web/routes/settings_routes.py b/src/local_deep_research/web/routes/settings_routes.pyindex cea5cc08f8..5a672fa04d 100644--- a/src/local_deep_research/web/routes/settings_routes.py+++ b/src/local_deep_research/web/routes/settings_routes.py@@ -85,7 +85,12 @@ from ...security.egress.validators import ()from ..auth.decorators import login_requiredfrom ..utils.request_helpers import parse_bool_arg-from ...security.rate_limiter import settings_limit+from ...security.rate_limiter import (+ SETTINGS_RATE_LIMIT,+ limiter,+ settings_limit,+)+from ...security.data_sanitizer import DataSanitizerfrom ...settings.manager import (SettingsManager,check_env_setting,@@ -204,6 +209,76 @@ BLOCKED_SETTING_PREFIXES = frozenset()+def _is_secret_empty_noop(+ key: str, ui_element: str | None, value: object+) -> bool:+ """True when a secret write must be ignored: the redaction sentinel+ for any sensitive setting, or an empty string for password inputs+ (which render blank, so an untouched field must not wipe the secret)."""+ return (+ isinstance(value, str)+ and DataSanitizer.is_sensitive_setting(key, ui_element)+ and (+ value == DataSanitizer.REDACTION_TEXT+ or (value == "" and ui_element == "password")+ )+ )+++def _redaction_sentinel_error(ui_element: str | None) -> str:+ """Explain the 400 raised for a value that embeds the redaction sentinel.++ Shared by every write route so the message is identical wherever it+ fires, but the "submit an empty value to clear it" hint only holds for+ non-password sensitive settings such as the ``notifications.service_url``+ textarea. ``_is_secret_empty_noop`` makes an empty write to a+ ``password`` input a deliberate no-op (those fields render blank, so an+ untouched form must not wipe the secret), and+ ``_embeds_redaction_sentinel`` gates on sensitivity alone, so a+ password-backed setting can reach this error through a direct API call+ even though the UI cannot produce it. Advertising the empty-value+ escape there would tell the caller to do something that provably+ cannot work, so the hint is dropped for password inputs.+ """+ message = (+ "Value contains the redaction placeholder "+ f"{DataSanitizer.REDACTION_TEXT!r}. The stored value is hidden, so "+ "it cannot be edited in place — retype the whole value"+ )+ if ui_element == "password":+ return (+ f"{message}. To clear a password setting, clear the source "+ "environment variable or use settings import."+ )+ return f"{message}, or submit an empty value to clear it."+++def _embeds_redaction_sentinel(+ key: str, ui_element: str | None, value: object+) -> bool:+ """True when a submitted sensitive value *embeds* the redaction sentinel.++ ``_is_secret_empty_noop`` covers the exact sentinel: an untouched field+ round-tripped from a settings API read, which is benign and silently+ ignored. This covers the *edited* field. Password inputs render blank,+ so they cannot produce this, but ``notifications.service_url`` is a+ sensitive setting on a ``textarea`` (the first non-password sensitive+ setting in the codebase), and editing its comma-separated URL list is+ the normal workflow. A stale client that rendered the sentinel yields+ values like ``"[REDACTED],discord://webhook/tok"``, which is not an+ exact match and would otherwise persist verbatim and silently break+ every notification. No legitimate secret contains the sentinel, so this+ is a hard 400 rather than a no-op: unlike the exact-match case the user+ made an edit, and silently dropping it would look like a successful save.+ """+ return (+ isinstance(value, str)+ and DataSanitizer.is_sensitive_setting(key, ui_element)+ and DataSanitizer.REDACTION_TEXT in value+ and value != DataSanitizer.REDACTION_TEXT+ )++def _is_allowed_new_setting_key(key: str) -> bool:"""Return True if *key* is permitted to be created via the web API."""# Reject malformed keys (blank, trailing/leading dot, empty ".." segment,@@ -483,8 +558,6 @@ def save_all_settings():"""Handle saving all settings at once from the unified settings page"""try:- from ...security.data_sanitizer import DataSanitizer-# Process JSON dataform_data = request.get_json()if not form_data:@@ -537,33 +610,38 @@ def save_all_settings(# Get the setting metadata from pre-fetched dictcurrent_setting = all_db_settings.get(key)- # SAFETY NET: an empty string OR the redaction sentinel for a- # password-typed setting is a no-op, never a "clear the value"- # request. Reasons:- # 1. The form templates (Jinja2 + JS-rendered) deliberately- # render password inputs empty so the saved value never- # enters the HTML source. A user who blurs the field- # without typing must not wipe their stored API key.- # 2. /settings/api redacts password values to the redaction- # sentinel ("[REDACTED]"). A stale browser tab could submit- # either "" or the sentinel — both must be idempotent on- # the DB, or the round-trip would persist the literal- # sentinel over the real secret.- # 3. Defense-in-depth against direct cURL or automation- # mistakes that POST an empty/sentinel value.- # To unset a password setting, clear the source env var or- # use settings import.- if (- current_setting- and DataSanitizer.is_sensitive_setting(- key, current_setting.ui_element- )- and isinstance(value, str)- and value in ("", DataSanitizer.REDACTION_TEXT)+ # SAFETY NET: the redaction sentinel is a no-op for every+ # sensitive setting because settings API reads can round-trip it+ # from a stale browser tab. An empty string is a no-op only for a+ # password input: those controls render blank to keep the saved+ # value out of HTML, so an untouched field must retain its secret.+ # Non-password sensitive controls, such as the notification URL+ # textarea, must accept "" so users can explicitly clear them.+ # To unset a password setting, clear the source env var or use+ # settings import.+ if current_setting and _is_secret_empty_noop(+ key, current_setting.ui_element, value):logger.debug(f"Skipping empty secret write for {key} (no-op)")continue+ # A value that merely *embeds* the sentinel is a corrupted edit,+ # not an untouched round-trip: reject it loudly instead of+ # persisting a secret with "[REDACTED]" spliced into it.+ if current_setting and _embeds_redaction_sentinel(+ key, current_setting.ui_element, value+ ):+ validation_errors.append(+ {+ "key": key,+ "name": current_setting.name,+ "error": _redaction_sentinel_error(+ current_setting.ui_element+ ),+ }+ )+ continue+# EARLY VALIDATION: Convert checkbox values BEFORE any other processing# This prevents incorrect triggering of corrupted value detectionif current_setting and current_setting.ui_element == "checkbox":@@ -702,6 +780,37 @@ def save_all_settings()continue+ # Creation has no prior value, so the redaction sentinel+ # cannot mean "keep the stored secret" the way it does on an+ # update — every occurrence of it (exact match included) is a+ # corrupted client value that would be stored verbatim as the+ # credential. Mirror the api_update_setting create-path guard+ # so a new sensitive-leaf key (e.g. a re-created+ # notifications.service_url) cannot be persisted with+ # "[REDACTED]" spliced into its value. ui_element is not yet+ # known here, so sensitivity is decided by the key's leaf name.+ if (+ isinstance(value, str)+ and DataSanitizer.is_sensitive_setting(key, None)+ and DataSanitizer.REDACTION_TEXT in value+ ):+ logger.warning(+ "Rejected redaction-sentinel value for new key {!r} "+ "via save_all_settings (user={!r})",+ key,+ session["username"],+ )+ validation_errors.append(+ {+ "key": key,+ "name": key.split(".")[-1]+ .replace("_", " ")+ .title(),+ "error": _redaction_sentinel_error(None),+ }+ )+ continue+# Create a new settingnew_setting = {"key": key,@@ -913,8 +1022,6 @@ def reset_to_defaults(def save_settings(db_session: Optional[Session] = None, settings_manager=None):"""Save all settings from the form using POST method - fallback when JavaScript is disabled"""try:- from ...security.data_sanitizer import DataSanitizer-# Get form dataform_data = request.form.to_dict()@@ -924,6 +1031,10 @@ def save_settings(db_session: Optional[Session] = None, settings_manager=None):updated_count = 0failed_count = 0rejected_count = 0+ # ui_element of every setting rejected for embedding the sentinel.+ # The batch flash below can only carry one "how to clear it" hint,+ # so it needs to know whether a password input is in the batch.+ sentinel_rejected_ui_elements: list[str | None] = []# Fetch all settings and remove non-editable keysall_db_settings = _filter_editable_settings(form_data, db_session)@@ -955,19 +1066,11 @@ def save_settings(db_session: Optional[Session] = None, settings_manager=None):rejected_count += 1continue- # SAFETY NET: empty string OR the redaction sentinel for a- # password-typed setting is a no-op — never "clear my key".- # The no-JS form renders password inputs empty (and GET- # redacts them to the sentinel), so a plain form submit- # must not wipe the stored secret. Matches the guards in- # save_all_settings + api_update_setting.- if (- db_setting- and DataSanitizer.is_sensitive_setting(- key, db_setting.ui_element- )- and isinstance(value, str)- and value in ("", DataSanitizer.REDACTION_TEXT)+ # Match the shared secret-write contract: every sentinel is a+ # no-op, while an empty value is protected only for password+ # inputs. Non-password sensitive controls remain clearable.+ if db_setting and _is_secret_empty_noop(+ key, db_setting.ui_element, value):logger.debug(f"Skipping empty secret write for {key} via "@@ -975,6 +1078,22 @@ def save_settings(db_session: Optional[Session] = None, settings_manager=None):)continue+ # An embedded sentinel is a corrupted edit, not a+ # round-trip. This route reports per-setting failures by+ # flash message, so count it as rejected rather than saving+ # a secret with "[REDACTED]" spliced into it.+ if db_setting and _embeds_redaction_sentinel(+ key, db_setting.ui_element, value+ ):+ logger.warning(+ "Rejected redaction-sentinel value for {!r} via "+ "save_settings (user={!r})",+ key,+ session["username"],+ )+ sentinel_rejected_ui_elements.append(db_setting.ui_element)+ continue+# Coerce form POST string to correct Python type.if db_setting:value = coerce_setting_for_write(@@ -1033,6 +1152,22 @@ def save_settings(db_session: Optional[Session] = None, settings_manager=None):"This may indicate a bug or an attempted injection.","error",)+ if sentinel_rejected_ui_elements:+ # Drop the empty-value hint if any rejected setting is a+ # password input, where an empty write is a no-op rather+ # than a clear (see ``_redaction_sentinel_error``).+ _hint_ui = (+ "password"+ if any(+ ui == "password" for ui in sentinel_rejected_ui_elements+ )+ else None+ )+ flash(+ f"Rejected {len(sentinel_rejected_ui_elements)} settings: "+ + _redaction_sentinel_error(_hint_ui),+ "error",+ )invalidate_settings_caches(session["username"])except Exception:@@ -1072,8 +1207,6 @@ def api_get_all_settings(the model directly), so redacting the response does not break the UI."""try:- from ...security.data_sanitizer import DataSanitizer-# Get query parameterscategory = request.args.get("category")@@ -1120,7 +1253,6 @@ def api_get_db_setting(a single GET. Metadata fields (``ui_element``, ``type``, ...) stayintact so the front-end can still render the correct input control."""- from ...security.data_sanitizer import DataSanitizertry:# Get setting from database using the same session@@ -1199,8 +1331,6 @@ def api_get_db_setting(def api_update_setting(key, db_session: Optional[Session] = None):"""Update a setting"""try:- from ...security.data_sanitizer import DataSanitizer-# Get request datadata = request.get_json()if "value" not in data:@@ -1252,29 +1382,36 @@ def api_update_setting(key, db_session: Optional[Session] = None):if not db_setting.editable:return jsonify({"error": f"Setting {key} is not editable"}), 403- # SAFETY NET: an empty string OR the redaction sentinel on a- # password-typed setting is a no-op (never "clear my key").- # Companion guard to the same check in save_all_settings — see- # the longer rationale there. Returning 200 with a message keeps- # the endpoint idempotent so client-side save indicators don't- # error.- if (- DataSanitizer.is_sensitive_setting(key, db_setting.ui_element)- and isinstance(value, str)- and value in ("", DataSanitizer.REDACTION_TEXT)- ):+ # Apply the shared sentinel/password-empty no-op contract. The+ # idempotent 200 keeps client-side save indicators from erroring.+ if _is_secret_empty_noop(key, db_setting.ui_element, value):logger.debug(- f"Skipping empty secret write for {key} via "+ f"Skipping sensitive value write for {key} via ""api_update_setting (no-op)")return jsonify({"message": (- f"Setting {key} unchanged (empty password ignored)"+ f"Setting {key} unchanged "+ "(sensitive value not overwritten)")}), 200+ # An embedded sentinel is a corrupted edit rather than an+ # untouched round-trip, so it is a hard error here (400) while+ # the exact-match case above stays an idempotent 200 no-op.+ if _embeds_redaction_sentinel(key, db_setting.ui_element, value):+ logger.warning(+ "Rejected redaction-sentinel value for {!r} via "+ "api_update_setting (user={!r})",+ key,+ session["username"],+ )+ return jsonify(+ {"error": _redaction_sentinel_error(db_setting.ui_element)}+ ), 400+if value is not None and not is_openai_chunk_size:# Coerce to correct Python type before saving.# Without this, values from JSON API requests are stored@@ -1408,6 +1545,32 @@ def api_update_setting(key, db_session: Optional[Session] = None):if field in data:setting_dict[field] = data[field]+ # Creation has no prior value, so the sentinel cannot mean "keep the+ # stored secret" the way it does on the update path — every+ # occurrence of it, exact match included, is a corrupted client+ # value that would be stored verbatim as the credential.+ _create_ui = setting_dict.get("ui_element")+ if (+ isinstance(value, str)+ and DataSanitizer.is_sensitive_setting(+ key, _create_ui if isinstance(_create_ui, str) else None+ )+ and DataSanitizer.REDACTION_TEXT in value+ ):+ logger.warning(+ "Rejected redaction-sentinel value for {!r} via "+ "api_update_setting create (user={!r})",+ key,+ session["username"],+ )+ return jsonify(+ {+ "error": _redaction_sentinel_error(+ _create_ui if isinstance(_create_ui, str) else None+ )+ }+ ), 400+# Apply egress validation to creation as well as updates. Otherwise,# DELETE followed by PUT can recreate a governed key without guards._all_db_settings = {s.key: s for s in db_session.query(Setting).all()}@@ -2845,8 +3008,6 @@ def get_bulk_settings():still tell whether the key is configured."""try:- from ...security.data_sanitizer import DataSanitizer-# Get requested settings from query parametersrequested = request.args.getlist("keys[]")if not requested:@@ -2963,35 +3124,89 @@ def api_get_data_location():return jsonify({"error": "Failed to retrieve data location"}), 500+def _is_blank_service_url(value) -> bool:+ """True when a notification URL counts as unset.++ ``DataSanitizer._is_empty_value`` and ``NotificationManager`` both treat+ a whitespace-only ``notifications.service_url`` as unconfigured, so the+ test endpoint has to agree -- a stored ``" "`` is truthy, and without+ this it would be handed to Apprise verbatim.+ """+ if isinstance(value, str):+ return not value.strip()+ return not value+++def _caller_supplied_notification_url() -> bool:+ """True when the test-url request names its own destination.++ Rate-limit exemption predicate. Testing a URL the caller just typed is+ the case the endpoint exists to serve and stays unlimited. Falling back+ to the caller's STORED URL does not: that path is a zero-argument send+ trigger, so it is the one that gets a bucket.+ """+ payload = request.get_json(silent=True)+ if not isinstance(payload, dict):+ return False+ submitted = payload.get("service_url")+ if _is_blank_service_url(submitted):+ return False+ return submitted != DataSanitizer.REDACTION_TEXT+++# Own bucket, not the shared "settings" one: this caps the stored-URL+# fallback without spending the quota a user needs for saving settings.+notification_test_limit = limiter.shared_limit(+ SETTINGS_RATE_LIMIT,+ scope="notification_test",+ exempt_when=_caller_supplied_notification_url,+)++@settings_bp.route("/api/notifications/test-url", methods=["POST"])@login_required+@notification_test_limitdef api_test_notification_url():"""- Test a notification service URL.-- This endpoint creates a temporary NotificationService instance to test- the provided URL. No database session or password is required because:- - The service URL is provided directly in the request body- - Test notifications use a temporary Apprise instance- - No user settings or database queries are performed-- Security note: Rate limiting is not applied here because users need to- test URLs when configuring notifications. Abuse is mitigated by the- @login_required decorator and the fact that users can only spam their- own notification services.+ Test a submitted notification URL or the calling user's stored URL.++ When ``service_url`` is missing, blank, or the redaction sentinel, the+ request-scoped settings lookup supplies the authenticated user's stored+ ``notifications.service_url``. Blank includes whitespace-only, matching+ ``DataSanitizer._is_empty_value`` and the notification manager, which+ both treat ``" "`` as unconfigured -- otherwise Apprise is handed+ literal whitespace. An unconfigured stored URL returns 400. Test+ notifications still use a temporary Apprise instance.++ Security note: this endpoint was deliberately unlimited, on the grounds+ that users need to test URLs while configuring notifications. That+ reasoning covers a caller who submits a URL, and that path is still+ exempt. It does not cover the stored-URL fallback added here: with no+ body, this becomes a zero-argument trigger that sends to a destination+ the caller never has to name, so an authenticated caller could loop on+ an empty body to spam their configured service. That path now consumes+ a dedicated rate-limit bucket. @login_required still bounds the blast+ radius to the caller's own notification services."""try:from ...notifications.service import NotificationServicefrom ...settings.env_registry import get_env_setting- data = request.get_json()- if not data or "service_url" not in data:+ data = request.get_json(silent=True)+ if not isinstance(data, dict):+ data = {}+ service_url = data.get("service_url")+ if _is_blank_service_url(service_url) or (+ service_url == DataSanitizer.REDACTION_TEXT+ ):+ service_url = _get_setting_from_session(+ "notifications.service_url", default=""+ )+ if _is_blank_service_url(service_url):return jsonify(- {"success": False, "error": "service_url is required"}+ {"success": False, "error": "No notification URL configured"}), 400- service_url = data["service_url"]-# Create notification service instance and test the URL.# Gate by the env-only master switch so the test endpoint cannot# bypass the operator's risk-acceptance decision (see SECURITY.mddiff --git a/src/local_deep_research/web/static/js/components/settings.js b/src/local_deep_research/web/static/js/components/settings.jsindex 421da1a352..9cbc190c7b 100644--- a/src/local_deep_research/web/static/js/components/settings.js+++ b/src/local_deep_research/web/static/js/components/settings.js@@ -17,6 +17,40 @@// for testability (see resolveProviderOptions there).const { resolveProviderOptions } = window.LdrProviderOptions;+ // The sentinel /settings/api substitutes for the stored value of every+ // sensitive setting (DataSanitizer.REDACTION_TEXT, PR #3947). It must+ // never be written back: the route handlers treat an exact match as a+ // no-op and reject anything that merely *contains* it, so a control+ // that renders the sentinel turns any edit into a failed or silently+ // dropped save. Single source of truth for the JS side.+ const REDACTED_SENTINEL = '[REDACTED]';++ // Keys whose value the settings API redacted on the current load. Used+ // to mask values in save confirmations for sensitive settings that are+ // not password inputs (e.g. the notifications.service_url textarea).+ const redactedSettingKeys = new Set();++ /**+ * True when the settings API redacted this value instead of returning it.+ * @param {*} value - The value as delivered by /settings/api+ * @returns {boolean}+ */+ function isRedactedValue(value) {+ return value === REDACTED_SENTINEL;+ }++ /**+ * True when a control must render blank because its stored value is a+ * secret we either never receive or must not echo. Password inputs+ * always qualify (the server render does the same); every other+ * sensitive control qualifies once the API has redacted it.+ * @param {Object} setting - The setting object+ * @returns {boolean}+ */+ function rendersBlankForSecrecy(setting) {+ return setting.ui_element === 'password' || isRedactedValue(setting.value);+ }+// DOM elements and global variableslet settingsForm;let settingsContent;@@ -472,7 +506,20 @@// Compare with original valueconst originalValue = Object.hasOwn(originalSettings, key) ? originalSettings[key] : undefined;- const hasChanged = !areValuesEqual(value, originalValue);+ // A redacted control renders blank with a blank baseline, so+ // "delete the contents" is neither visible nor detectable by the+ // dirty-check — yet the stored secret is still there. Enter on an+ // empty redacted control is the explicit clear gesture, advertised+ // in the placeholder. Without it, a redacted non-password setting+ // (notifications.service_url) would stop being clearable from the+ // UI, which the route handlers deliberately allow.+ const isExplicitClear =+ input.dataset.redacted === 'true' &&+ eventType === 'keydown' &&+ e.key === 'Enter' &&+ !e.shiftKey &&+ value === '';+ const hasChanged = !areValuesEqual(value, originalValue) || isExplicitClear;if (hasChanged) {// Mark parent item as modified@@ -1018,17 +1065,23 @@allSettings = processSettings(data.settings);// Store original values for the auto-save dirty-check.- // For password fields the JS render leaves the input- // empty (see the renderSetting password branch), so- // seed the baseline as empty too — otherwise the- // first keystroke would compare against "[REDACTED]"- // (the redacted sentinel from /settings/api per- // PR #3947) instead of "" and the save semantics get- // weird. Treating the redacted sentinel as "we don't- // know the real value" is the right model.+ // Every control the render leaves empty for secrecy+ // (see rendersBlankForSecrecy) must get an empty+ // baseline too — otherwise the first keystroke would+ // compare against "[REDACTED]" (the redacted sentinel+ // from /settings/api per PR #3947) instead of "" and+ // the save semantics get weird, and merely focusing+ // and leaving an untouched blank control would look+ // like "the user emptied this" and save "". Treating+ // the redacted sentinel as "we don't know the real+ // value" is the right model.+ redactedSettingKeys.clear();allSettings.forEach(setting => {+ if (isRedactedValue(setting.value)) {+ redactedSettingKeys.add(setting.key);+ }originalSettings[setting.key] =- setting.ui_element === 'password'+ rendersBlankForSecrecy(setting)? '': setting.value;});@@ -2099,14 +2152,38 @@// Generate the appropriate input element based on UI element typeswitch(setting.ui_element) {- case 'textarea':+ case 'textarea': {+ // A redacted textarea renders blank for the same reason a+ // password input does: writing "[REDACTED]" into the field+ // means an untouched save persists the sentinel. The+ // textarea case is worse than the password case, because+ // editing is the *normal* workflow for the one setting+ // that uses it (notifications.service_url is a+ // comma-separated URL list). A partial edit such as+ // "[REDACTED],discord://webhook/tok" is not an exact+ // sentinel match, so the route's no-op does not fire, and+ // the corrupted list silently breaks every notification.+ const isRedacted = isRedactedValue(setting.value);+ const textareaBody = isRedacted || setting.value === null+ ? ''+ : escapeHtml(String(setting.value));+ // "press Enter while empty to clear" is the explicit clear+ // gesture: the control renders blank, so deleting its+ // contents is not a visible change and the dirty-check+ // cannot see it (handleInputChange honours this).+ const textareaPlaceholder = isRedacted+ ? 'Saved — type to replace, or press Enter while empty to clear'+ : '';inputElement = `<textarea id="${settingId}" name="${setting.key}"class="ldr-settings-textarea"+ placeholder="${escapeHtml(textareaPlaceholder)}"+ ${isRedacted ? 'data-redacted="true"' : ''}${!setting.editable ? 'disabled' : ''}- >${setting.value !== null ? escapeHtml(String(setting.value)) : ''}</textarea>+ >${textareaBody}</textarea>`;break;+ }case 'json': {const jsonClass = ' ldr-json-content';@@ -2317,7 +2394,7 @@// sentinel as a positive signal — we know the value// is set, we just don't know its plaintext.const isConfigured =- setting.value === '[REDACTED]' ||+ isRedactedValue(setting.value) ||(setting.value !== null && setting.value !== '');const placeholder = isConfigured? '(saved — type to change)'@@ -2332,6 +2409,22 @@${!setting.editable ? 'disabled' : ''}>`;+ } else if (isRedactedValue(setting.value)) {+ // Sensitive setting rendered by a non-password control.+ // Same contract as the password branch: never write the+ // sentinel into the field, since an edit would persist+ // it (or, once it merely *contains* the sentinel, be+ // rejected by the route handler).+ inputElement = `+ <input type="text"+ id="${settingId}" name="${setting.key}"+ class="ldr-settings-input ldr-form-control"+ value=""+ placeholder="${escapeHtml('Saved — type to replace, or press Enter while empty to clear')}"+ data-redacted="true"+ ${!setting.editable ? 'disabled' : ''}+ >+ `;} else {// Default to text inputinputElement = `@@ -2822,9 +2915,15 @@const displayName = key.split('.').pop().replace(/_/g, ' ');const capitalizedName = displayName.charAt(0).toUpperCase() + displayName.slice(1);- // Check if this is a sensitive field (password/api_key)+ // Check if this is a sensitive field (password/api_key,+ // or any setting the API redacted on load — e.g. the+ // notifications.service_url textarea, whose value is+ // an apprise URL with embedded credentials).const setting = allSettings.find(s => s.key === key);- const isSensitive = setting && setting.ui_element === 'password';+ const isSensitive = Boolean(+ (setting && setting.ui_element === 'password') ||+ redactedSettingKeys.has(key)+ );// Format the values for display - mask sensitive valuesconst oldDisplay = isSensitive ? '[hidden]' : formatValueForDisplay(oldValue);diff --git a/tests/security/test_data_sanitizer_coverage.py b/tests/security/test_data_sanitizer_coverage.pyindex 8e6af3b352..2d52c140f7 100644--- a/tests/security/test_data_sanitizer_coverage.py+++ b/tests/security/test_data_sanitizer_coverage.py@@ -17,6 +17,7 @@ Targets gaps not covered by existing test suites:- strip_settings_snapshot with empty string input- filter_research_metadata with empty string input- Numeric and boolean JSON strings for metadata functions+- redact_value treating whitespace-only strings as empty (unset)"""import json@@ -555,11 +556,13 @@ class TestDefaultSensitiveKeysComplete:"bearer_token","api_secret","app_secret",+ # Notification webhook URL (apprise-style) embeds credentials.+ "service_url",}assert DataSanitizer.DEFAULT_SENSITIVE_KEYS == expecteddef test_count(self):- assert len(DataSanitizer.DEFAULT_SENSITIVE_KEYS) == 15+ assert len(DataSanitizer.DEFAULT_SENSITIVE_KEYS) == 16def test_all_lowercase(self):"""All default keys are stored in lowercase."""@@ -631,3 +634,28 @@ class TestCaseVariantKeys:assert result["PASSWORD"] == "[REDACTED]"assert result["password"] == "[REDACTED]"assert result["name"] == "ok"+++# ---------------------------------------------------------------------------+# redact_value -- whitespace-only values in the empty rule+# ---------------------------------------------------------------------------+++class TestRedactValueWhitespaceIsEmpty:+ """Whitespace-only strings count as unconfigured: masking them to the+ sentinel would make an unset secret LOOK configured, diverging from the+ notification manager's ``not service_urls.strip()`` check."""++ def test_whitespace_only_service_url_stays_readable(self):+ assert (+ DataSanitizer.redact_value("notifications.service_url", value=" ")+ == " "+ )++ def test_whitespace_only_api_key_stays_readable(self):+ assert (+ DataSanitizer.redact_value(+ "llm.openai.api_key", "password", " "+ )+ == " \t "+ )diff --git a/tests/security/test_settings_secret_redaction_isolation.py b/tests/security/test_settings_secret_redaction_isolation.pynew file mode 100644index 0000000000..cca79437b6--- /dev/null+++ b/tests/security/test_settings_secret_redaction_isolation.py@@ -0,0 +1,484 @@+"""The notification service URL must be redacted on the settings read paths.++`notifications.service_url` is an apprise-style URL that embeds credentials+(`mailto://user:pass@host`, `discord://webhook_id/token`, Slack/ntfy tokens).+The app already masks it in logs via `mask_sensitive_url()`, but it was returned+in PLAINTEXT on every `/settings/api` read path -- unlike API keys, which come+back `[REDACTED]`. That ships a secret to the browser / JSON API where it can be+cached by proxies or pasted into bug reports. The fix adds `service_url` to+`DataSanitizer.DEFAULT_SENSITIVE_KEYS`.++These tests pin: the configured URL is redacted on single/bulk/full GET and in+the save-all echo; an unconfigured (empty) URL stays readable (so the+configured/unconfigured distinction survives); and the write-back no-op guard+refuses to overwrite the real value with the redaction sentinel (no round-trip+clobber). A non-secret unit check confirms `is_sensitive_setting` now catches+the key.++Follow-up coverage pins the write/test paths introduced alongside the+redaction: clearing a configured URL stores the empty string (no silent+retention), a sentinel write is a no-op that leaves the stored secret intact,+Test Notification falls back to the stored URL when the field shows the+sentinel, and the settings debug logger redacts the key. Whitespace-only+values count as unconfigured (they stay readable, matching the notification+manager's `.strip()` check).+"""++import uuid++import pytest++from local_deep_research.security.data_sanitizer import DataSanitizer+from local_deep_research.settings.logger import redact_sensitive_keys++WEBHOOK_KEY = "notifications.service_url"+SECRET_URL = "discord://HOOKID_XYZ/TOKEN_SECRET_abcdefghijklmnop"+ROTATED_SECRET_URL = "discord://HOOKID_ABC/ROTATED_TOKEN_zyxwvutsrqponmlk"+REDACTED = DataSanitizer.REDACTION_TEXT+++# --------------------------------------------------------------------------- #+# Unit: the predicate now treats the notification service URL as a secret.+# --------------------------------------------------------------------------- #+def test_service_url_redacts_only_when_configured():+ assert DataSanitizer.redact_value(WEBHOOK_KEY, value=SECRET_URL) == REDACTED+ assert DataSanitizer.redact_value(WEBHOOK_KEY, value="") == ""+ assert DataSanitizer.redact_value(WEBHOOK_KEY, value=" ") == " "+++# --------------------------------------------------------------------------- #+# End-to-end through the real settings API.+# --------------------------------------------------------------------------- #+def logged_in_client(tmp_path, monkeypatch):+ monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))+ monkeypatch.setenv("LDR_TESTING_WITH_MOCKS", "true")+ monkeypatch.setenv("LDR_DISABLE_RATE_LIMITING", "true")+ monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")++ from local_deep_research.database.auth_db import init_auth_database+ from local_deep_research.database.encrypted_db import db_manager+ from local_deep_research.web.app_factory import create_app+ import local_deep_research.web.auth.routes as auth_routes++ if not db_manager.has_encryption:+ pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")++ original_data_dir = db_manager.data_dir+ try:+ db_manager.data_dir = tmp_path / "encrypted_databases"+ init_auth_database()+ app, _ = create_app()+ app.config["TESTING"] = True+ app.config["WTF_CSRF_ENABLED"] = False+ app.config["SESSION_COOKIE_SECURE"] = False+ monkeypatch.setattr(+ auth_routes, "_perform_post_login_tasks", lambda _u, _p: None+ )+ client = app.test_client()+ username = f"notif_{uuid.uuid4().hex[:8]}"+ reg = client.post(+ "/auth/register",+ data={+ "username": username,+ "password": "TestPass123",+ "confirm_password": "TestPass123",+ "acknowledge": "true",+ },+ follow_redirects=False,+ )+ assert reg.status_code in (200, 302)+ yield client+ finally:+ db_manager.close_all_databases()+ db_manager.data_dir = original_data_dir+++def _put(client, key, value):+ r = client.put(f"/settings/api/{key}", json={"value": value})+ assert r.status_code == 200, (key, r.status_code, r.get_data(as_text=True))+ return r+++def _single(client, key):+ r = client.get(f"/settings/api/{key}")+ assert r.status_code == 200+ return r.get_json().get("value")+++def _bulk(client, key):+ r = client.get(f"/settings/api/bulk?keys[]={key}")+ assert r.status_code == 200+ return r.get_json()["settings"][key]["value"]+++def _full(client, key):+ r = client.get("/settings/api")+ assert r.status_code == 200+ return r.get_json()["settings"][key]["value"]+++def test_configured_service_url_is_redacted_on_every_read_path(+ logged_in_client,+):+ c = logged_in_client+ _put(c, WEBHOOK_KEY, SECRET_URL)++ assert _single(c, WEBHOOK_KEY) == REDACTED, "single GET leaks the webhook"+ assert _bulk(c, WEBHOOK_KEY) == REDACTED, "bulk GET leaks the webhook"+ assert _full(c, WEBHOOK_KEY) == REDACTED, "full GET leaks the webhook"++ # The plaintext secret must not appear anywhere in a full settings dump.+ full = c.get("/settings/api").get_data(as_text=True)+ assert "TOKEN_SECRET_abcdefghijklmnop" not in full++ # The save-all echo must not ship the plaintext back either.+ resp = c.post(+ "/settings/save_all_settings",+ json={WEBHOOK_KEY: "discord://HOOKID_XYZ/ROTATED_TOKEN_zzzzzzzz"},+ )+ assert resp.status_code == 200+ assert "ROTATED_TOKEN" not in resp.get_data(as_text=True)+++def test_empty_service_url_stays_readable(logged_in_client):+ """An unconfigured URL is not a secret -- the empty-value rule keeps the+ configured/unconfigured distinction visible to the UI/API."""+ c = logged_in_client+ _put(c, WEBHOOK_KEY, "")+ assert _single(c, WEBHOOK_KEY) == ""+ assert _bulk(c, WEBHOOK_KEY) == ""+ assert _full(c, WEBHOOK_KEY) == ""+++def test_sensitive_predicate_covers_service_url():+ assert DataSanitizer.is_sensitive_setting(WEBHOOK_KEY) is True+++def test_clear_configured_service_url(logged_in_client):+ c = logged_in_client+ _put(c, WEBHOOK_KEY, SECRET_URL)+ assert _single(c, WEBHOOK_KEY) == REDACTED++ _put(c, WEBHOOK_KEY, "")+ assert _single(c, WEBHOOK_KEY) == ""++ _put(c, WEBHOOK_KEY, ROTATED_SECRET_URL)+ assert _single(c, WEBHOOK_KEY) == REDACTED+++def test_sentinel_write_is_noop_for_configured_secret(logged_in_client):+ c = logged_in_client+ _put(c, WEBHOOK_KEY, SECRET_URL)++ response = _put(c, WEBHOOK_KEY, REDACTED)++ assert "unchanged" in response.get_json()["message"].lower()+ assert _single(c, WEBHOOK_KEY) == REDACTED+++def test_sentinel_noop_leaves_stored_secret_usable(+ logged_in_client, monkeypatch+):+ """The sentinel no-op must leave the STORED secret intact, not store the+ sentinel: after a refused [REDACTED] write, the test-url fallback still+ resolves and tests the original secret (GET alone cannot distinguish a+ retained secret from a stored sentinel because redaction masks both)."""+ from local_deep_research.notifications.service import NotificationService++ captured_urls = []++ def capture_url(_service, url):+ captured_urls.append(url)+ return {"success": True, "message": "Notification sent"}++ monkeypatch.setattr(NotificationService, "test_service", capture_url)+ c = logged_in_client+ _put(c, WEBHOOK_KEY, SECRET_URL)+ _put(c, WEBHOOK_KEY, REDACTED) # refused no-op++ response = c.post(+ "/settings/api/notifications/test-url",+ json={"service_url": REDACTED},+ )+ assert response.status_code == 200+ assert captured_urls == [SECRET_URL]+++def test_test_url_uses_stored_when_sentinel(logged_in_client, monkeypatch):+ from local_deep_research.notifications.service import NotificationService++ captured_urls = []++ def capture_url(_service, url):+ captured_urls.append(url)+ return {"success": True, "message": "Notification sent"}++ monkeypatch.setattr(NotificationService, "test_service", capture_url)+ c = logged_in_client+ _put(c, WEBHOOK_KEY, SECRET_URL)++ response = c.post(+ "/settings/api/notifications/test-url",+ json={"service_url": REDACTED},+ )++ assert response.status_code == 200+ assert response.get_json()["success"] is True+ assert captured_urls == [SECRET_URL]++ _put(c, WEBHOOK_KEY, "")+ unconfigured_response = c.post(+ "/settings/api/notifications/test-url",+ json={"service_url": REDACTED},+ )+ assert unconfigured_response.status_code == 400+ assert unconfigured_response.get_json() == {+ "success": False,+ "error": "No notification URL configured",+ }+ assert captured_urls == [SECRET_URL]+++def test_test_url_treats_whitespace_stored_url_as_unconfigured(+ logged_in_client, monkeypatch+):+ """A stored " " is truthy, so a falsiness check would hand Apprise+ literal whitespace. ``DataSanitizer._is_empty_value`` and the+ notification manager both ``.strip()``; the test endpoint must agree."""+ from local_deep_research.notifications.service import NotificationService++ captured_urls = []++ def capture_url(_service, url):+ captured_urls.append(url)+ return {"success": True, "message": "Notification sent"}++ monkeypatch.setattr(NotificationService, "test_service", capture_url)+ c = logged_in_client+ _put(c, WEBHOOK_KEY, " ")++ for body in ({}, {"service_url": " "}, {"service_url": REDACTED}):+ response = c.post("/settings/api/notifications/test-url", json=body)+ assert response.status_code == 400, body+ assert response.get_json() == {+ "success": False,+ "error": "No notification URL configured",+ }++ assert captured_urls == []+++def test_logger_redacts_service_url():+ result = redact_sensitive_keys(+ {+ "notifications.service_url": "discord://x/y",+ "llm.openai.api_key": "k",+ }+ )++ assert result == {+ "notifications.service_url": "***REDACTED***",+ "llm.openai.api_key": "***REDACTED***",+ }+++# --------------------------------------------------------------------------- #+# Containment guard: a value that EMBEDS the sentinel is a corrupted edit.+#+# `notifications.service_url` is the first non-password sensitive setting, so+# it is the first one whose control renders its (redacted) value into an+# editable field. A stale client that rendered "[REDACTED]" produces partial+# edits, and neither failure mode is caught by the exact-match no-op:+#+# "[REDACTED],discord://webhook/tok"+# parse_notification_url_list reports invalid_fragment="[REDACTED]", so+# NotificationManager._filter_urls_by_egress_policy refuses EVERY url in+# the list. Notifications stop entirely, with only a policy_audit+# warning to show for it.+#+# "discord://webhook/tok,[REDACTED]"+# validate_multiple_urls returns True (the comma is not followed by a+# scheme, so it stays one URL) and "[REDACTED]" is appended to the+# Discord token: a permanently broken webhook that validation blesses.+#+# No legitimate secret contains the sentinel, so every write route rejects it.+# --------------------------------------------------------------------------- #+CORRUPT_PREFIX = f"{REDACTED},{SECRET_URL}"+CORRUPT_SUFFIX = f"{SECRET_URL},{REDACTED}"+CORRUPT_VALUES = (CORRUPT_PREFIX, CORRUPT_SUFFIX, f" {REDACTED} ")+++def _stored_url(client, monkeypatch):+ """Return the URL the server actually has stored.++ A GET cannot tell a retained secret from a corrupted one -- redaction+ masks every non-empty value to the same sentinel. The test-url endpoint+ falls back to the stored value, so capturing what it hands Apprise is the+ only way to observe the plaintext the server kept.+ """+ from local_deep_research.notifications.service import NotificationService++ captured = []++ def capture_url(_service, url):+ captured.append(url)+ return {"success": True, "message": "Notification sent"}++ monkeypatch.setattr(NotificationService, "test_service", capture_url)+ response = client.post(+ "/settings/api/notifications/test-url", json={"service_url": REDACTED}+ )+ assert response.status_code == 200, response.get_data(as_text=True)+ assert len(captured) == 1+ return captured[0]+++def test_embedded_sentinel_predicate():+ """Unit contract: embedding is rejected, an exact match is not (that stays+ the untouched-field no-op), and non-sensitive settings are untouched."""+ from local_deep_research.web.routes.settings_routes import (+ _embeds_redaction_sentinel,+ )++ for corrupt in CORRUPT_VALUES:+ assert _embeds_redaction_sentinel(WEBHOOK_KEY, "textarea", corrupt), (+ corrupt+ )+ # Exact match belongs to _is_secret_empty_noop, not here.+ assert not _embeds_redaction_sentinel(WEBHOOK_KEY, "textarea", REDACTED)+ assert not _embeds_redaction_sentinel(WEBHOOK_KEY, "textarea", SECRET_URL)+ assert not _embeds_redaction_sentinel(WEBHOOK_KEY, "textarea", "")+ # Password inputs render blank so they cannot produce this, but the+ # predicate covers them anyway -- it keys off sensitivity, not ui_element.+ assert _embeds_redaction_sentinel(+ "llm.openai.api_key", "password", f"sk-{REDACTED}"+ )+ # A non-sensitive setting may legitimately contain the string.+ assert not _embeds_redaction_sentinel(+ "report.title", "text", f"How we {REDACTED} things"+ )+ # Non-strings are never rejected.+ assert not _embeds_redaction_sentinel(WEBHOOK_KEY, "textarea", None)+ assert not _embeds_redaction_sentinel(WEBHOOK_KEY, "textarea", 17)+++def test_sentinel_error_matches_the_clear_semantics_per_ui_element():+ """The 400 message must not advertise an escape that cannot work.++ ``_is_secret_empty_noop`` swallows an empty write to a ``password``+ input, so "submit an empty value to clear it" is only true for the+ non-password sensitive settings (the ``notifications.service_url``+ textarea). ``_embeds_redaction_sentinel`` keys off sensitivity alone,+ so a password setting reaches this error via a direct API call and must+ be told how it actually clears.+ """+ from local_deep_research.web.routes.settings_routes import (+ _is_secret_empty_noop,+ _redaction_sentinel_error,+ )++ clearable = _redaction_sentinel_error("textarea")+ assert not _is_secret_empty_noop(WEBHOOK_KEY, "textarea", "")+ assert "submit an empty value to clear it" in clearable+ assert REDACTED in clearable++ password = _redaction_sentinel_error("password")+ assert _is_secret_empty_noop("llm.openai.api_key", "password", "")+ assert "empty value" not in password+ assert "environment variable" in password+ assert REDACTED in password++ # An unknown ui_element keeps the general advice both cases share.+ for message in (clearable, password, _redaction_sentinel_error(None)):+ assert "retype the whole value" in message++[email protected]("corrupt", CORRUPT_VALUES)+def test_put_rejects_embedded_sentinel(logged_in_client, monkeypatch, corrupt):+ c = logged_in_client+ _put(c, WEBHOOK_KEY, SECRET_URL)++ response = c.put(f"/settings/api/{WEBHOOK_KEY}", json={"value": corrupt})++ assert response.status_code == 400, response.get_data(as_text=True)+ assert REDACTED in response.get_json()["error"]+ assert _stored_url(c, monkeypatch) == SECRET_URL++[email protected]("corrupt", CORRUPT_VALUES)+def test_save_all_settings_rejects_embedded_sentinel(+ logged_in_client, monkeypatch, corrupt+):+ c = logged_in_client+ _put(c, WEBHOOK_KEY, SECRET_URL)++ response = c.post(+ "/settings/save_all_settings", json={WEBHOOK_KEY: corrupt}+ )++ assert response.status_code == 400, response.get_data(as_text=True)+ payload = response.get_json()+ assert payload["status"] == "error"+ assert [e["key"] for e in payload["errors"]] == [WEBHOOK_KEY]+ assert REDACTED in payload["errors"][0]["error"]+ assert _stored_url(c, monkeypatch) == SECRET_URL++[email protected]("corrupt", CORRUPT_VALUES)+def test_save_settings_form_post_rejects_embedded_sentinel(+ logged_in_client, monkeypatch, corrupt+):+ """The JS-disabled form fallback must agree with the JSON routes, or it+ becomes the way around the guard."""+ c = logged_in_client+ _put(c, WEBHOOK_KEY, SECRET_URL)++ response = c.post("/settings/save_settings", data={WEBHOOK_KEY: corrupt})++ assert response.status_code == 302+ assert _stored_url(c, monkeypatch) == SECRET_URL+++def test_recreated_setting_rejects_sentinel_entirely(logged_in_client):+ """On the DELETE-then-PUT recreate path there is no stored value, so the+ sentinel cannot mean "keep what is there" -- the exact match is a+ corrupted value too, and storing it would make "[REDACTED]" the webhook."""+ c = logged_in_client+ _put(c, WEBHOOK_KEY, SECRET_URL)+ delete = c.delete(f"/settings/api/{WEBHOOK_KEY}")+ assert delete.status_code == 200, delete.get_data(as_text=True)++ for corrupt in (*CORRUPT_VALUES, REDACTED):+ response = c.put(+ f"/settings/api/{WEBHOOK_KEY}", json={"value": corrupt}+ )+ assert response.status_code == 400, (+ corrupt,+ response.get_data(as_text=True),+ )+ assert REDACTED in response.get_json()["error"]++ # A real value still recreates the setting (201, not the 200 an update+ # returns), so the guard rejects only the corrupted values.+ recreate = c.put(+ f"/settings/api/{WEBHOOK_KEY}", json={"value": ROTATED_SECRET_URL}+ )+ assert recreate.status_code == 201, recreate.get_data(as_text=True)+ assert _single(c, WEBHOOK_KEY) == REDACTED+++def test_exact_sentinel_stays_a_noop_not_an_error(logged_in_client):+ """Regression fence for the containment guard: the untouched-field+ round-trip must keep its idempotent 200, not become a 400."""+ c = logged_in_client+ _put(c, WEBHOOK_KEY, SECRET_URL)++ put = c.put(f"/settings/api/{WEBHOOK_KEY}", json={"value": REDACTED})+ assert put.status_code == 200+ assert "unchanged" in put.get_json()["message"].lower()++ save_all = c.post(+ "/settings/save_all_settings", json={WEBHOOK_KEY: REDACTED}+ )+ assert save_all.status_code == 200diff --git a/tests/web/routes/test_settings_routes_gaps_coverage.py b/tests/web/routes/test_settings_routes_gaps_coverage.pyindex 744d37bc26..495653710a 100644--- a/tests/web/routes/test_settings_routes_gaps_coverage.py+++ b/tests/web/routes/test_settings_routes_gaps_coverage.py@@ -728,26 +728,41 @@ class TestApiTestNotificationUrl:assert data["success"] is Truedef test_missing_service_url_returns_400(self):+ """New contract: a missing service_url key falls back to the+ calling user's stored notifications.service_url. With nothing+ stored, the endpoint must return 400, not error out."""app = _create_test_app()with _authenticated_client(app) as client:- resp = client.post(- "/settings/api/notifications/test-url",- json={"wrong_key": "value"},- content_type="application/json",- )- assert resp.status_code == 400- data = resp.get_json()- assert data["success"] is False+ with patch(+ f"{MODULE}._get_setting_from_session",+ return_value="",+ ):+ resp = client.post(+ "/settings/api/notifications/test-url",+ json={"wrong_key": "value"},+ content_type="application/json",+ )+ assert resp.status_code == 400+ data = resp.get_json()+ assert data["success"] is Falsedef test_empty_body_returns_400(self):+ """Empty JSON body also falls back to the stored URL; with none+ configured the endpoint returns 400."""app = _create_test_app()with _authenticated_client(app) as client:- resp = client.post(- "/settings/api/notifications/test-url",- json={},- content_type="application/json",- )- assert resp.status_code == 400+ with patch(+ f"{MODULE}._get_setting_from_session",+ return_value="",+ ):+ resp = client.post(+ "/settings/api/notifications/test-url",+ json={},+ content_type="application/json",+ )+ assert resp.status_code == 400+ data = resp.get_json()+ assert data["success"] is Falsedef test_service_exception_text_is_not_leaked_to_client(self):"""Response boundary must stay generic when test_service raises.diff --git a/tests/web/routes/test_settings_routes_rate_limiting.py b/tests/web/routes/test_settings_routes_rate_limiting.pyindex c3aaf3a582..eb1480c992 100644--- a/tests/web/routes/test_settings_routes_rate_limiting.py+++ b/tests/web/routes/test_settings_routes_rate_limiting.py@@ -357,8 +357,10 @@ class TestApiTestNotificationUrl:response = authenticated_client.post(f"{SETTINGS_PREFIX}/api/notifications/test-url",)- # No JSON body causes get_json() to return None, hitting the error handler- assert response.status_code == 500, response.status_code+ # No JSON body parses as empty data; the endpoint falls back to+ # the stored URL and returns 400 when none is configured (the+ # old behavior was a 500 via the error handler).+ assert response.status_code == 400, response.status_code@patch("local_deep_research.notifications.service.NotificationService")def test_does_not_leak_internal_details(