Skip to content

~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.

30.5 KB
Raw Blame
"""Security module for sanitizing sensitive data from data structures.

This module ensures that sensitive information like API keys, passwords, and tokens
are not accidentally leaked in logs, files, or API responses.

Includes helpers for filtering research metadata in API responses to prevent
settings_snapshot (which contains all application settings including API keys)
from being sent to the frontend.
"""

import json
from typing import Any, Set

from ..error_handling.error_messages import get_known_research_error_message


# The placeholder a redacted value is replaced with. Single source of truth
# so that write-back guards (which must treat this sentinel as a no-op to
# avoid persisting it over a real secret on a redacted GET round-trip)
# cannot drift from what the redactor actually emits.
REDACTION_TEXT = "[REDACTED]"

# Older research rows persisted str(exception) in research_meta["error"].
# That value can include report paths and other server-side details. API
# responses retain only known authored messages; other text is replaced.
PUBLIC_RESEARCH_ERROR = "Research failed. Check the server logs for details."

# Unicode 16.0 DerivedCoreProperties.txt, Default_Ignorable_Code_Point.
# Most Unicode Default_Ignorable_Code_Point characters are non-printable and
# are removed by str.isprintable(). These exceptions are printable combining
# marks or letter-category fillers, so list them explicitly. Do not drop Mn as
# a category: ordinary combining marks are visible parts of legitimate names.
_PRINTABLE_DEFAULT_IGNORABLES = (
    frozenset({0x034F})  # COMBINING GRAPHEME JOINER
    | frozenset(range(0x115F, 0x1161))  # Hangul fillers
    | frozenset(range(0x17B4, 0x17B6))  # Khmer inherent vowels
    | frozenset(range(0x180B, 0x1810))  # Mongolian variation separators
    | frozenset({0x3164, 0xFFA0})  # Hangul fillers
    | frozenset(range(0xFE00, 0xFE10))  # Variation Selectors
    | frozenset(range(0xE0100, 0xE01F0))  # Variation Selectors Supplement
)

# Printable characters that Unicode specifies as rendering blank but that are
# not default ignorables. They pass str.isprintable() and setting-key validation,
# so they can otherwise disguise a sensitive leaf in the bulk settings endpoint.
_PRINTABLE_BLANKS = frozenset(
    {
        0x2800,  # BRAILLE PATTERN BLANK
        0x13441,  # EGYPTIAN HIEROGLYPH FULL BLANK
        0x13442,  # EGYPTIAN HIEROGLYPH HALF BLANK
        0x1D159,  # MUSICAL SYMBOL NULL NOTEHEAD
    }
)


def _visible_leaf(key: str) -> str:
    """The key's last dotted segment, normalized for the sensitive-name check.

    Defense in depth for the bulk-settings GET, which lacks the
    authoritative ``ui_element == "password"`` signal and falls back to the key
    name. Drops non-printable chars (control, format \u2014 zero-width/BOM/bidi/tag
    chars/soft hyphen \u2014 and non-ASCII spaces) plus printable Unicode default
    ignorables (variation selectors, combining grapheme joiner, and fillers)
    and explicitly known printable blank characters, then strips ASCII space
    and lowercases. Ordinary visible combining marks are retained. This
    intentionally fails closed: removing invisible padding can collapse a
    distinct key onto a sensitive name and over-redact its value, which is
    safer than exposing a potentially disguised secret.
    """
    leaf = key.rsplit(".", 1)[-1]
    leaf = "".join(
        ch
        for ch in leaf
        if ch.isprintable()
        and ord(ch) not in _PRINTABLE_DEFAULT_IGNORABLES
        and ord(ch) not in _PRINTABLE_BLANKS
    )
    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, "", [], {})


# Leading qualifiers that mark a leaf as a boolean FLAG *about* a secret rather
# than the secret itself. ``search.engine.web.brave.requires_api_key`` is a
# checkbox saying an engine needs a key; masking it would ship "[REDACTED]" to
# the settings UI in place of true/false and make the write-back no-op guards
# refuse legitimate writes. This vocabulary is retained for documentation and
# so a future confirmed shape can be added deliberately; only the single
# hardcoded leaf below is actually exempted (see ``_has_non_secret_qualifier``).
_NON_SECRET_LEAF_PREFIXES = (
    "allow_",
    "allows_",
    "disable_",
    "enable_",
    "enabled_",
    "has_",
    "is_",
    "need_",
    "needs_",
    "require_",
    "requires_",
    "show_",
    "skip_",
    "support_",
    "supports_",
    "use_",
    "uses_",
)

# The ONE confirmed, load-bearing qualifier+suffix shape: used verbatim as a
# config dict key throughout the search-engine factories
# (``tests/security/test_factory_secret_redaction.py``,
# ``tests/security/test_noncollection_agent_enabled.py``) and shipped as the
# last dotted segment of ``search.engine.web.*.requires_api_key`` for every
# engine that needs one (``defaults/settings_*.json``).
_EXEMPT_QUALIFIER_LEAF = "requires_api_key"


def _has_non_secret_qualifier(leaf: str) -> bool:
    """True only for the ONE confirmed, load-bearing exemption: ``leaf`` is
    literally ``requires_api_key``.

    An earlier version exempted ANY leaf STARTING WITH ANY
    ``_NON_SECRET_LEAF_PREFIXES`` qualifier, combined with ANY sensitive
    suffix from ``DEFAULT_SENSITIVE_KEYS`` -- a combinatorial exemption this
    module's own docstrings never actually claimed ("only the exact
    ``requires_api_key`` shape ... is a carve-out") but the code granted
    anyway. That let a dotted key whose LAST SEGMENT genuinely starts with a
    qualifier immediately followed by an unrelated sensitive suffix --
    ``rag.skip_password``, ``llm.x.use_auth_token`` -- ship a real string
    credential in the clear, purely because its shape happened to resemble
    the one verified flag. Nothing confirms ``skip_password`` or
    ``use_auth_token`` are booleans rather than a badly-named credential the
    way ``requires_api_key`` is verified to be.

    An even earlier version matched the qualifier at ANY underscore
    boundary (``f"_{prefix}" in leaf``), not just the start; that was fixed
    first (see ``test_bulk_get_qualifier_mid_key_no_longer_exempts_a_real_secret``)
    and is a separate, already-closed gap from the one this narrows.

    Audit scope (post-#6201 review, item d -- corrected): the loader
    (``SettingsManager.default_settings``, ``settings/manager.py``) does
    ``defaults_path.rglob("*.json")``, which walks every subdirectory --
    not just the top-level ``defaults/settings_*.json`` files. The 478-key
    figure this docstring previously cited was only ``default_settings.json``
    (341 keys) plus the top-level ``settings_*.json`` files (137 keys); it
    omitted three subdirectories the loader also reads
    (``llm_providers/``, ``research_library/``, ``settings/search_engines/``,
    108 more keys). Audited again against the FULL rglob'd scope -- 586
    keys as of this writing -- narrowing from the qualifier-prefix list to
    this single hardcoded leaf still changes zero shipped keys'
    classification, confirming the prefix list was never doing anything
    beyond gating this one shape in practice, over the scope the loader
    actually reads rather than a subset of it. A boolean/int value stays
    protected regardless of this function's answer -- ``redact_value``'s
    type guard leaves a bool/int/None leaf untouched on the broadened arm
    (see ``_is_exact_sensitive_match``).

    Blast radius, precisely (post-#6201 review, item R3): this changes
    behavior for a STRING value stored under a leaf shaped like this, AND
    for a dict/list one. A container that matches ONLY the broadened arm
    does not hit the type guard -- it takes the ``_force_redact_strings``
    branch instead, which masks every STRING leaf inside it regardless of
    the sub-key's name. Executed: with ``llm.x.use_auth_token`` exempted,
    ``{"a": "hello"}`` stays ``{"a": "hello"}``; without the exemption it
    becomes ``{"a": "[REDACTED]"}``. So a future addition to this
    vocabulary widens or narrows redaction for strings and for the string
    leaves nested inside containers -- only the bool/int/None half is
    genuinely unaffected.
    """
    return leaf == _EXEMPT_QUALIFIER_LEAF


def _matches_sensitive_name(leaf: str, sensitive_names: Set[str]) -> bool:
    """True when a normalized leaf names a secret.

    Two arms:

    1. Exact match (``llm.openai.api_key`` -> ``api_key``).
    2. Underscore-boundary suffix match. Settings keys use two separator
       conventions: dotted (``llm.openai.api_key``) and flat snake_case
       (``local_search_milvus_token``). A flat key has no dots, so its dotted
       "leaf" is the entire key and arm 1 can never match it — which is how a
       password-typed setting shipped in the clear from the bulk settings GET
       (#5762). Note the miss was never specific to ``token``:
       ``local_search_milvus_api_key`` or ``some_password`` would have evaded
       arm 1 just as completely.

    Arm 2 skips a leaf that IS the ``requires_api_key`` shape (see
    ``_has_non_secret_qualifier``), so ``requires_api_key`` stays readable
    while ``milvus_api_key`` does not. This carve-out is a single hardcoded
    leaf, not a qualifier-prefix-plus-any-suffix combination: a namespaced
    flat key such as ``llm_requires_api_key``, a key where the qualifier
    word appears anywhere else before an unrelated sensitive suffix
    (``local_search_use_milvus_token``), and a DIFFERENT qualifier+suffix
    pairing that merely resembles the shape (``rag.skip_password``,
    ``llm.x.use_auth_token``) are all NOT exempted -- only the exact
    ``requires_api_key`` shape (as a whole key, or as a dotted key's last
    segment) is a confirmed, load-bearing carve-out. Plural token counts
    (``max_tokens``, ``supports_max_tokens``) end in ``_tokens``, a
    different suffix than ``_token``, and stay readable too.
    """
    if leaf in sensitive_names:
        return True
    if _has_non_secret_qualifier(leaf):
        return False
    return any(leaf.endswith(f"_{name}") for name in sensitive_names)


def _is_exact_sensitive_match(
    key: str, ui_element: str | None, sensitive_names: Set[str]
) -> bool:
    """True under the ORIGINAL (pre-#5771) sensitivity rule: ``ui_element ==
    "password"`` or an exact dotted-leaf match — i.e. everything
    ``is_sensitive_setting`` matched before the broadened underscore-suffix
    arm (arm 2 of ``_matches_sensitive_name``) was added.

    ``redact_value`` uses this to scope its non-string type guard to only
    the NEW broadened match. The exact/password arms have masked non-string
    values (e.g. a boolean under a key literally named ``api_key``) since
    before this PR; that established behavior is intentionally left as-is.
    The broadened arm is a lexical suffix heuristic backed by a
    single literal ``requires_api_key`` exemption, so a boolean/number
    setting whose name happens to lexically match a sensitive suffix
    (for example, a future ``verify_token``-style flag) must NOT be redacted: replacing a bool/int
    with the "[REDACTED]" string corrupts it in the settings UI, which
    only special-cases real booleans when rendering a checkbox
    (``web/static/js/components/settings.js``), and silently writes back
    ``false``/empty on the next save.
    """
    if ui_element == "password":
        return True
    raw_leaf = key.rsplit(".", 1)[-1].lower()
    return raw_leaf in sensitive_names or _visible_leaf(key) in sensitive_names


def _force_redact_strings(value: Any, redaction_text: str) -> Any:
    """Mask every string leaf inside a container, regardless of its own
    sub-key name, while leaving bool/int/None leaves untouched.

    Used when a KEY matches the broadened suffix-only sensitive arm (arm 2
    of ``_matches_sensitive_name``) and its VALUE is a dict/list rather than
    a plain string. ``redact_value``'s normal recursion masks by SUB-KEY
    name, so a secret nested under a non-sensitive sub-key -- e.g.
    ``{"value": "s3cr3t"}`` under a ``milvus_token`` setting -- would
    otherwise survive unredacted: ``value`` is not itself a sensitive leaf
    name. This walks the container and replaces every non-empty string leaf
    with the sentinel unconditionally, so the secret cannot hide behind an
    innocuous sub-key. It preserves round 2's guarantee that a bool/int
    leaf is never corrupted into a string, and leaves empty/whitespace-only
    string leaves readable so an unset nested field doesn't look configured
    (matching ``_is_empty_value``'s rule for the top-level value).
    """
    if isinstance(value, str):
        return redaction_text if value.strip() else value
    if isinstance(value, dict):
        return {
            sub_key: _force_redact_strings(sub_val, redaction_text)
            for sub_key, sub_val in value.items()
        }
    if isinstance(value, list):
        return [_force_redact_strings(item, redaction_text) for item in value]
    return value


class DataSanitizer:
    """Utility class for removing sensitive information from data structures."""

    # Public alias of the module-level sentinel (see REDACTION_TEXT above).
    REDACTION_TEXT: str = REDACTION_TEXT

    # Default set of sensitive key names to redact
    DEFAULT_SENSITIVE_KEYS: Set[str] = {
        "api_key",
        "apikey",
        "password",
        "secret",
        "access_token",
        "refresh_token",
        "private_key",
        "auth_token",
        "session_token",
        "csrf_token",
        # Additional unambiguous secret leaf-names. The predicate is
        # exact-match on the last dotted segment, so these matter especially
        # for the bulk settings GET, which can only use the key-name heuristic
        # (it passes no ui_element). All are unambiguously secrets and no
        # current setting key uses them for non-secret data.
        "client_secret",
        "secret_key",
        "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",
        # A bare "token" leaf. The qualified spellings above (access_token,
        # auth_token, bearer_token, ...) left the unqualified name uncovered,
        # so a setting keyed "<prefix>_token" with no dots in it (its leaf is
        # the whole key) matched nothing and shipped in the clear from the
        # bulk settings GET (#5762). "token" and "api_token" name a secret in
        # every credential convention we ship; token COUNTS are spelled
        # max_tokens / context_tokens / supports_max_tokens, which are
        # different leaves and stay readable.
        "token",
        "api_token",
    }

    @staticmethod
    def is_sensitive_setting(
        key: str,
        ui_element: str | None = None,
        sensitive_keys: Set[str] | None = None,
    ) -> bool:
        """True when a setting holds a secret: it is ``ui_element ==
        "password"`` OR the last dotted segment of its key names a secret
        (``llm.openai.api_key`` -> ``api_key``), either exactly or as an
        underscore-delimited suffix (``local_search_milvus_token`` ->
        ``_token``). See ``_matches_sensitive_name`` for both arms and for
        the qualifier carve-out that keeps ``requires_api_key`` readable.

        Single source of truth for "is this a secret" so the GET redactor
        and the write-back no-op guards apply the SAME predicate — a value
        the redactor masks to the sentinel must also be one the guards
        refuse to overwrite, or a redacted GET could round-trip the
        sentinel back over the real secret.
        """
        if ui_element == "password":
            return True
        sens = {
            k.lower()
            for k in (sensitive_keys or DataSanitizer.DEFAULT_SENSITIVE_KEYS)
        }
        # _visible_leaf normalizes away invisible/whitespace padding so a key
        # like "api_key " or "api_key<zero-width>" still matches (see its
        # docstring). Keep the raw-leaf match as well: callers may provide a
        # custom sensitive name containing one of those characters, and an
        # exact match must remain sensitive for backward compatibility.
        raw_leaf = key.rsplit(".", 1)[-1].lower()
        return _matches_sensitive_name(
            raw_leaf, sens
        ) or _matches_sensitive_name(_visible_leaf(key), sens)

    @staticmethod
    def redact_value(
        key: str,
        ui_element: str | None = None,
        value: Any = None,
        sensitive_keys: Set[str] | None = None,
        redaction_text: str = REDACTION_TEXT,
    ) -> Any:
        """Redact a single setting's value when it holds a set secret.

        The single-value counterpart of ``redact_settings_snapshot``: it
        applies the SAME ``is_sensitive_setting`` predicate and the SAME
        empty-value rule, so every read path that ships a setting to the
        browser (the bulk GET, the singular GET, the run-time snapshot)
        masks identically. Returns ``redaction_text`` for a non-empty
        sensitive value, otherwise ``value`` unchanged.

        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
        the outer key (``llm``) is not sensitive but inner keys are, and a JSON
        setting value may be a list of dicts (e.g. ``[{"api_key": "sk-…"}]``).
        Without the recursion, ``GET /settings/api/bulk?keys[]=llm`` would ship
        those nested secrets in the clear. A sensitive OUTER key still masks its
        whole value wholesale (the check below runs first), so plain lists under
        a non-sensitive key pass through untouched.

        Non-string values are only redacted through the original exact-leaf
        / ``password`` match (see ``_is_exact_sensitive_match``). The
        broadened snake_case suffix match added for #5771 is a lexical
        heuristic and must not fire on a bool/int/dict value: a checkbox or
        number setting whose name happens to lexically match a sensitive
        suffix cannot hold a real credential, and replacing its typed value
        with the "[REDACTED]" string corrupts it in the settings UI (a
        checkbox only renders ``checked`` for a literal ``True``) and can
        silently write back the wrong value on the next save.

        A dict/list value under a key that matches ONLY the broadened arm
        (not the exact/password match) is neither a plain string nor caught
        by the type guard above, so it falls through this first check
        untouched. Left alone, the general recursion below would then mask
        it by SUB-KEY name only -- missing a secret nested under a
        non-sensitive sub-key such as ``{"value": "s3cr3t"}``. So that case
        is handled separately: every string leaf inside the container is
        force-masked (``_force_redact_strings``), while bool/int/None
        leaves stay untouched, same as the type guard above.
        """
        if DataSanitizer.is_sensitive_setting(
            key, ui_element, sensitive_keys
        ) and not _is_empty_value(value):
            sens = {
                k.lower()
                for k in (
                    sensitive_keys or DataSanitizer.DEFAULT_SENSITIVE_KEYS
                )
            }
            if isinstance(value, str) or _is_exact_sensitive_match(
                key, ui_element, sens
            ):
                return redaction_text
            if isinstance(value, (dict, list)):
                return _force_redact_strings(value, redaction_text)
        if isinstance(value, dict):
            redacted: dict = {}
            for sub_key, sub_val in value.items():
                nested_key = f"{key}.{sub_key}" if key else sub_key
                redacted[sub_key] = DataSanitizer.redact_value(
                    nested_key, None, sub_val, sensitive_keys, redaction_text
                )
            return redacted
        if isinstance(value, list):
            # List items reuse the parent key; a dict item is caught by the
            # branch above (its own sensitive leaves get masked), a plain
            # scalar item passes through.
            return [
                DataSanitizer.redact_value(
                    key, None, item, sensitive_keys, redaction_text
                )
                for item in value
            ]
        return value

    @staticmethod
    def sanitize(data: Any, sensitive_keys: Set[str] | None = None) -> Any:
        """
        Recursively remove sensitive keys from data structures.

        This method traverses dictionaries and lists, removing any keys that match
        the sensitive keys list (case-insensitive). This prevents accidental
        credential leakage in optimization results, logs, or API responses.

        Args:
            data: The data structure to sanitize (dict, list, or primitive)
            sensitive_keys: Set of key names to remove (case-insensitive).
                          If None, uses DEFAULT_SENSITIVE_KEYS.

        Returns:
            Sanitized copy of the data with sensitive keys removed

        Example:
            >>> sanitizer = DataSanitizer()
            >>> data = {"username": "user", "api_key": "secret123"}
            >>> sanitizer.sanitize(data)
            {"username": "user"}
        """
        if sensitive_keys is None:
            sensitive_keys = DataSanitizer.DEFAULT_SENSITIVE_KEYS

        # Convert to lowercase for case-insensitive comparison
        sensitive_keys_lower = {key.lower() for key in sensitive_keys}

        if isinstance(data, dict):
            return {
                k: DataSanitizer.sanitize(v, sensitive_keys)
                for k, v in data.items()
                if k.lower() not in sensitive_keys_lower
            }
        if isinstance(data, list):
            return [
                DataSanitizer.sanitize(item, sensitive_keys) for item in data
            ]
        # Return primitives unchanged
        return data

    @staticmethod
    def redact(
        data: Any,
        sensitive_keys: Set[str] | None = None,
        redaction_text: str = REDACTION_TEXT,
    ) -> Any:
        """
        Recursively redact (replace with placeholder) sensitive values in data structures.

        Unlike sanitize() which removes keys entirely, this method replaces their
        values with a redaction placeholder, preserving the structure.

        Args:
            data: The data structure to redact (dict, list, or primitive)
            sensitive_keys: Set of key names to redact (case-insensitive).
                          If None, uses DEFAULT_SENSITIVE_KEYS.
            redaction_text: Text to replace sensitive values with

        Returns:
            Copy of the data with sensitive values redacted

        Example:
            >>> sanitizer = DataSanitizer()
            >>> data = {"username": "user", "api_key": "secret123"}
            >>> sanitizer.redact(data)
            {"username": "user", "api_key": "[REDACTED]"}
        """
        if sensitive_keys is None:
            sensitive_keys = DataSanitizer.DEFAULT_SENSITIVE_KEYS

        # Convert to lowercase for case-insensitive comparison
        sensitive_keys_lower = {key.lower() for key in sensitive_keys}

        if isinstance(data, dict):
            return {
                k: (
                    redaction_text
                    if k.lower() in sensitive_keys_lower
                    else DataSanitizer.redact(v, sensitive_keys, redaction_text)
                )
                for k, v in data.items()
            }
        if isinstance(data, list):
            return [
                DataSanitizer.redact(item, sensitive_keys, redaction_text)
                for item in data
            ]
        # Return primitives unchanged
        return data

    @staticmethod
    def redact_settings_snapshot(
        snapshot: Any,
        sensitive_keys: Set[str] | None = None,
        redaction_text: str = REDACTION_TEXT,
    ) -> Any:
        """Redact secret values in a settings snapshot while preserving metadata.

        A settings snapshot from ``SettingsManager.get_all_settings()`` has the
        nested-with-metadata shape ``{dotted_key: {"value": ..., "ui_element":
        ..., "type": ..., ...}}``. The ordinary ``redact()`` method does not
        catch secrets in this shape: the outer dotted key (e.g.
        ``"llm.openai.api_key"``) is not in the sensitive-name set (only the
        suffix ``"api_key"`` is), and the inner key ``"value"`` is not
        sensitive — so the secret survives unredacted.
        ``redact_settings_snapshot`` handles the shape correctly:

        - Replaces ``entry["value"]`` with ``redaction_text`` when the entry
          is sensitive (``ui_element == "password"`` OR the last dotted
          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``, ``""``, ``[]``, ``{}`` 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. mixed
        snapshots that contain bare values) are passed through untouched —
        this is intentional so the helper is safe to call on any dict
        without crashing.

        Args:
            snapshot: A settings snapshot dict.
            sensitive_keys: Override the default set of sensitive name
                suffixes. Defaults to ``DataSanitizer.DEFAULT_SENSITIVE_KEYS``.
            redaction_text: Replacement string for redacted values.

        Returns:
            New dict with secret values replaced.

        Example:
            >>> snap = {"llm.openai.api_key": {"value": "sk-x", "ui_element": "password"}}
            >>> DataSanitizer.redact_settings_snapshot(snap)
            {'llm.openai.api_key': {'value': '[REDACTED]', 'ui_element': 'password'}}
        """
        if not isinstance(snapshot, dict):
            return snapshot

        out: dict = {}
        for key, entry in snapshot.items():
            if not isinstance(entry, dict) or "value" not in entry:
                out[key] = entry
                continue
            new_entry = dict(entry)  # shallow copy preserves metadata
            # Delegate the per-value rule to redact_value so the snapshot,
            # the singular GET and the bulk GET can never mask differently.
            new_entry["value"] = DataSanitizer.redact_value(
                key,
                entry.get("ui_element"),
                entry.get("value"),
                sensitive_keys,
                redaction_text,
            )
            out[key] = new_entry
        return out


# Convenience functions for direct use
def sanitize_data(data: Any, sensitive_keys: Set[str] | None = None) -> Any:
    """
    Remove sensitive keys from data structures.

    Convenience function that calls DataSanitizer.sanitize().

    Args:
        data: The data structure to sanitize
        sensitive_keys: Optional set of sensitive key names

    Returns:
        Sanitized copy of the data
    """
    return DataSanitizer.sanitize(data, sensitive_keys)


def redact_data(
    data: Any,
    sensitive_keys: Set[str] | None = None,
    redaction_text: str = REDACTION_TEXT,
) -> Any:
    """
    Redact (replace) sensitive values in data structures.

    Convenience function that calls DataSanitizer.redact().

    Args:
        data: The data structure to redact
        sensitive_keys: Optional set of sensitive key names
        redaction_text: Text to replace sensitive values with

    Returns:
        Copy of the data with sensitive values redacted
    """
    return DataSanitizer.redact(data, sensitive_keys, redaction_text)


def filter_research_metadata(research_meta: Any) -> dict:
    """Filter research_meta to only safe fields for history list API responses.

    Uses an allowlist approach to prevent leaking settings_snapshot
    (which contains API keys, passwords, tokens) to the frontend.
    History list consumers only need is_news_search from metadata.

    Args:
        research_meta: Raw research metadata (dict, JSON string, or None)

    Returns:
        dict with only safe fields extracted (currently: is_news_search)
    """
    try:
        meta = research_meta or {}
        if isinstance(meta, str):
            meta = json.loads(meta)
        if not isinstance(meta, dict):
            return {"is_news_search": False}
        return {
            "is_news_search": bool(meta.get("is_news_search", False)),
        }
    except (json.JSONDecodeError, TypeError, AttributeError):
        return {"is_news_search": False}


def strip_settings_snapshot(research_meta: Any) -> dict:
    """Remove private fields from research_meta for API responses.

    settings_snapshot contains all application settings including API keys.
    Legacy error strings can contain absolute report paths because older
    writers persisted str(exception). Preserve the error field's shape for
    clients, but allow only known authored messages through. Keep other
    metadata fields that the frontend needs (phase, error_type,
    processed_query, mode, duration, etc.).

    Args:
        research_meta: Raw research metadata (dict, JSON string, or None)

    Returns:
        Copy of the dict with settings_snapshot removed and error masked
    """
    try:
        meta = research_meta or {}
        if isinstance(meta, str):
            meta = json.loads(meta)
        if not isinstance(meta, dict):
            return {}
        public = {k: v for k, v in meta.items() if k != "settings_snapshot"}
        if public.get("error"):
            public["error"] = (
                get_known_research_error_message(public["error"])
                or PUBLIC_RESEARCH_ERROR
            )
        return public
    except (json.JSONDecodeError, TypeError, AttributeError):
        return {}