~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
"""Sanitize raw strings before writing them to log output.
``data_sanitizer.py`` handles dict-key redaction (e.g. stripping API keys
from structured data by key name). This module handles different
concerns:
* :func:`strip_control_chars` / :func:`sanitize_for_log` \u2014 make a single
string value safe to include in a log line by removing non-printable
characters and truncating to a reasonable length.
:func:`redact_and_bound_for_log` redacts credential shapes before it
truncates, for a value whose cut could otherwise hide a credential from
the log patcher's redaction.
* :func:`redact_secrets` \u2014 scrub known sensitive *values* (API keys,
passwords, session tokens) from an arbitrary string before it is
logged, returned in an error message, or persisted.
* :func:`sanitize_error_details` \u2014 recurse a structured ``details`` value
and redact credential *shapes* from its string leaves before it is
serialized to a client. (Value-shape based; contrast
``data_sanitizer.DataSanitizer``, which redacts by key *name*.)
Threat model of the log-record redaction (:func:`redact_log_record`):
exception classes and the code that logs are application code, and are
trusted; the *data* inside exceptions -- messages, ``args``, fields and
notes built from upstream responses, URLs or user input -- and bound
``extra`` values are not. Credential-shaped data in exception text,
``args``, notes and ``extra`` (known key and token shapes, URL userinfo,
sensitive ``name=value`` pairs, and ``extra`` values under sensitive key
names) is redacted before any sink renders it, and every record is
picklable by an ``enqueue=True`` sink. Redaction matches shapes, not
meaning: a secret in another form passes through, such as an arbitrary
application-specific ``name: value`` pair with a colon, or a bare password
or token with no label and no known shape. Nothing is claimed about an
exception class or object deliberately written to defeat the redactor:
metaclass, descriptor, ``__class__`` or ``__dict__`` hooks, a ``__str__``
with side effects on other exceptions, a class registered in
:mod:`builtins` under its own name, and the like. Such code can leak a
secret in many other ways anyway. The checks below that reject such
classes (rebuilding the exception from its checked text) are defence in
depth, not a complete barrier; where a docstring says a check or a step
"runs no code", that holds for exception classes and objects that do not
hook those mechanisms.
"""
import builtins
import collections.abc
import contextlib
import contextvars
import copyreg
import bisect
import dataclasses
import functools
import itertools
import operator
import re
import sys
import types
from importlib.machinery import ModuleSpec
from multiprocessing.reduction import ForkingPickler
from typing import Any, Callable, Iterable, NamedTuple, Optional, Union
from .data_sanitizer import (
DataSanitizer,
REDACTION_TEXT,
_EXEMPT_QUALIFIER_LEAF,
)
# Strip C0/C1 control characters and dangerous Unicode format characters,
# but preserve visible Unicode (accented, CJK, emoji, etc.)
_UNSAFE_CHAR_RE = re.compile(
r"[\x00-\x1f\x7f-\x9f" # C0/C1 control chars
r"\u061c" # Arabic letter mark
r"\u200b-\u200f" # Zero-width chars + LTR/RTL marks
r"\u202a-\u202e" # Embedding/override (incl. RLO)
r"\u2028\u2029" # Line/paragraph separators — forced breaks in rendered
# HTML per CSS Text, so a log line carrying one can forge what looks
# like a separate entry even though re and str.splitlines() ignore them
r"\u2060-\u2064" # Word joiner + math invisible operators
r"\u2066-\u2069" # Isolate chars
r"\u206a-\u206f" # Digit shape controls
r"\ufeff" # BOM / zero-width no-break space
r"]"
)
# Default minimum length for a value to be considered a redactable secret.
# Values shorter than this are skipped because a literal ``str.replace`` on
# a short string would produce false positives in normal message content
# (e.g. redacting the 3-char string ``key`` would scrub the word "key"
# everywhere it appears).
_MIN_SECRET_LENGTH = 8
# Replacement token written in place of any redacted secret.
_REDACTION_TOKEN = "***REDACTED***" # noqa: S105 # gitleaks:allow
def strip_control_chars(value: str) -> str:
"""Remove control and format characters from *value*, preserving visible Unicode."""
return _UNSAFE_CHAR_RE.sub("", value)
def sanitize_log_record(record) -> None:
"""loguru patcher stripping control characters from a record's message.
Control-character strip only. The web and MCP processes both install
:func:`redact_log_record` instead, which does this strip as part of its
message redaction and also redacts credentials.
"""
record["message"] = strip_control_chars(record["message"])
def sanitize_for_log(value: str, max_length: int = 50) -> str:
"""Return a log-safe version of *value*.
* Control and format characters are stripped; valid Unicode is preserved.
* The result is truncated to *max_length* characters.
"""
cleaned = strip_control_chars(value)
if len(cleaned) > max_length:
cleaned = (
cleaned[: max_length - 3] + "..."
if max_length > 3
else cleaned[:max_length]
)
return cleaned
def redact_secrets(
message: str,
*secrets: Optional[str],
min_length: int = _MIN_SECRET_LENGTH,
replacement: str = _REDACTION_TOKEN,
) -> str:
"""Replace each occurrence of any *secret* in *message* with *replacement*.
Use this before writing a string to a log sink, returning it in an
error response, or persisting it \u2014 when the string may have been
constructed from upstream exception messages, URLs, or other
sources that could contain a value the caller already knows is
sensitive.
Each *secret* is matched as a literal substring (``str.replace``).
The function does not normalize encodings: if a secret appears
URL-encoded or otherwise transformed in *message*, the transformed
form is NOT redacted unless the caller also passes that
transformed form.
When multiple secrets are passed, they are applied in descending
length order so a shorter secret that happens to be a substring of
a longer one cannot consume part of the longer match. Example:
given secrets ``"abc12345"`` and ``"sk-abc12345"``, the longer one
is replaced first.
Args:
message: The string to scrub (a ``str`` subclass is scrubbed, and
returned, as a plain ``str``). Returned unchanged if it is
empty or a falsy non-string.
*secrets: Zero or more candidate secret values. ``None`` and
values shorter than *min_length* are silently skipped \u2014 the
caller is responsible for noticing missing config.
min_length: Minimum secret length to redact. Values shorter than
this are skipped to avoid corrupting normal message content
(a 1- or 2-character secret would match too aggressively).
Defaults to 8. Real API keys and session tokens are
typically 16+ characters.
replacement: String written in place of each redacted secret.
Defaults to ``"***REDACTED***"``.
Returns:
*message* with every occurrence of each qualifying secret
replaced.
See ``tests/security/test_log_sanitizer.py::TestRedactSecrets`` for
worked examples (doctest examples are constrained because the
repository's gitleaks rule flags token-shaped literals in
docstrings; ``.gitleaks.toml`` exempts only one audited historical
example value in this file, while other credential literals and
token-shaped values remain scanned).
"""
message = _plain_text(message)
if not message:
return message
# Longest-first prevents a shorter overlapping secret from
# truncating a longer one once the replacement token is in place.
ordered = sorted(
(s for s in secrets if s and len(s) >= min_length),
key=len,
reverse=True,
)
for secret in ordered:
message = message.replace(secret, replacement)
return message
# Pre-compiled regex patterns for common credential formats found in HTTP
# library exception messages. Used by sanitize_error_message().
#
# Order matters: the URL-credentials pattern must run BEFORE the URL-param
# pattern. Otherwise an input like ``?api-key=https://user:pass@host`` gets
# its ``https`` consumed by the param replacement, the credentials pattern
# no longer matches, and ``user:pass`` leaks.
_CREDENTIAL_PATTERNS: list[tuple[re.Pattern[str], str]] = [
# Bearer tokens
(re.compile(r"Bearer\s+[A-Za-z0-9\-._~+/]+=*"), "Bearer [REDACTED]"),
# Authorization header WITH an explicit scheme. The scheme word is a
# strong anchor that rules out prose, so redact the credential on length
# alone (>=8 chars) regardless of its shape — this catches even an
# all-alphabetic Basic/Digest value. The scheme is preserved for
# debuggability. (A short prose word after a scheme is rare and would
# only be over-redacted, never leaked.)
(
re.compile(
r"(?i)(authorization\s*[:=]\s*)"
r"(basic|bearer|digest|negotiate|apikey|token)\s+"
r"[A-Za-z0-9\-._~+/]{8,}=*"
),
r"\1\2 [REDACTED]",
),
# Authorization header WITHOUT a scheme — here the value could be prose
# ("Authorization: required"), so require a *token-shaped* value (>=8
# chars containing a digit/+///=/_) to catch a raw token while leaving
# all-alphabetic prose intact.
(
re.compile(
r"(?i)(authorization\s*[:=]\s*)"
r"(?=[A-Za-z0-9\-._~+/]*[0-9+/=_])"
r"[A-Za-z0-9\-._~+/]{8,}=*"
),
r"\1[REDACTED]",
),
# x-api-key header — the label is a strong anchor (it doesn't appear in
# ordinary prose), so redact any sufficiently long value (>=16 chars)
# regardless of shape. Short values like "invalid"/"missing" stay intact.
(
re.compile(r"(?i)(x-api-key\s*[:=]\s*)[A-Za-z0-9\-._~+/]{16,}=*"),
r"\1[REDACTED]",
),
# URL credentials (user:pass@host) in ANY URL scheme. Userinfo in a URL is
# always a credential, so this is not restricted to http(s): it also covers
# URL-form database connection strings — including SQLAlchemy's
# ``dialect+driver`` form (``postgresql+psycopg2://``, ``mysql+pymysql://``,
# ``mongodb+srv://``) and password-only DSNs (``redis://:pass@host``) —
# which are the most common credential-bearing strings in a raw DB/driver
# exception message. (Key=value DSNs like pyodbc's ``Server=...;Pwd=...``
# have no ``://`` and are out of scope for a userinfo regex.)
#
# The scheme is matched case-insensitively (URL schemes are case-insensitive
# per RFC 3986). NOTE: do NOT re-add a leading ``\b`` here — the scheme's
# first char is a word char, so ``\b`` fails to anchor when the URL is glued
# to a preceding word char (``Xhttps://user:pass@``) and the credential then
# leaks. The ``://`` literal plus the leading-letter requirement are already
# strong anchors against prose. The trailing ``@`` is required and ``/`` is
# excluded from the userinfo, so a credential-less DSN with a port
# (``postgresql://host:5432/db``) does not match.
(
re.compile(
r"([A-Za-z][A-Za-z0-9+.\-]{1,31}://)([^:\s/@]*):([^@\s/]+)@"
),
r"\1[REDACTED]:[REDACTED]@",
),
# Credential-bearing URL query parameters (?api_key=..., &access_token=...).
# Specific multi-word names precede the short catch-alls so the full
# parameter name is matched (e.g. ``secret_key`` not just ``secret``).
(
re.compile(
r"(?i)([?&])("
r"api[_-]?key|access[_-]?token|refresh[_-]?token|id[_-]?token|"
r"auth[_-]?token|session[_-]?token|secret[_-]?key|bearer[_-]?token|"
r"subscription[_-]?key|client[_-]?secret|api[_-]?secret|"
r"app[_-]?secret|private[_-]?key|"
r"key|token|secret|password|passwd|pwd"
r")=([^&\s#]+)"
),
r"\1\2=[REDACTED]",
),
# Common API key prefixes (sk-*, pk-*) — includes hyphens for modern
# formats like sk-proj-... and sk-ant-api03-...
(re.compile(r"\b(sk-[A-Za-z0-9\-]{20,})\b"), "[REDACTED_KEY]"),
(re.compile(r"\b(pk-[A-Za-z0-9\-]{20,})\b"), "[REDACTED_KEY]"),
# Google API keys (AIza...) — match generously to cover length variants
# while the 20-char floor avoids short false positives.
(re.compile(r"\bAIza[0-9A-Za-z\-_]{20,}\b"), "[REDACTED_KEY]"),
# Distinctive provider token prefixes. These mirror the canonical,
# actively-maintained gitleaks ruleset (https://github.com/gitleaks/
# gitleaks, config/gitleaks.toml) — refresh from there when new token
# formats appear. They are prefix-anchored (very low false-positive risk
# in prose); the dual-scrub redact_secrets(known_literal) path remains the
# backstop for arbitrary/unknown secret shapes. See
# docs/developing/credential-scrubbing.md for the maintenance process.
# GitHub tokens: ghp_/gho_/ghu_/ghs_/ghr_ + fine-grained PATs.
(re.compile(r"\bgh[pousr]_[A-Za-z0-9]{36,}\b"), "[REDACTED_KEY]"),
(re.compile(r"\bgithub_pat_[A-Za-z0-9_]{30,}\b"), "[REDACTED_KEY]"),
# AWS access key IDs (AKIA/ASIA/ABIA/ACCA/A3T...).
# Accepted false positive (over-redaction is the safe failure): a
# contiguous 20-char all-caps word starting with one of these prefixes
# is redacted. Mirrors gitleaks exactly.
(
re.compile(r"\b(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}\b"),
"[REDACTED_KEY]",
),
# Slack tokens: xox[baeprs]-* (bot/user/app/refresh/config-refresh/...)
# and app-level xapp-* tokens. The ``xapp``/``xox`` prefixes are generic
# enough to appear in hyphenated identifiers, so (unlike the distinctive
# GitHub/AWS prefixes) we additionally require the long numeric workspace
# ID that every real Slack token carries (gitleaks expects ``[0-9]{10,13}``
# segments). This keeps real tokens redacted while leaving prose such as
# ``xapp-release-notes-2026`` intact.
#
# Linear-time form (these patterns run on every log record through
# redact_log_message): the naive ``\bxox[baeprs]-(?=[A-Za-z0-9-]*...)``
# lets EVERY ``xoxb-`` inside one ``[A-Za-z0-9-]`` run start an attempt
# whose lookahead rescans the rest of the run, so ``"xoxb-" * n`` is
# O(n^2) (measured: 10 KB 0.3 s, 20 KB 1.2 s). Every start inside the
# same run sees a suffix of what the run's leftmost ``\bxox`` start sees
# (same run end, same later digits, fewer characters), so if any start
# in a run matches, the leftmost one does. Attempts therefore begin only
# at a run start (the lookbehind) and commit, atomically, to the first
# ``\bxox*-`` in the run; the run prefix before it is captured and kept.
# Same matches as the naive form, one scan per run.
(
re.compile(
r"(?<![A-Za-z0-9-])(?>([A-Za-z0-9-]*?)\bxox[baeprs]-)"
r"(?=[A-Za-z0-9-]*[0-9]{9,})[A-Za-z0-9-]{10,}\b"
),
r"\1[REDACTED_KEY]",
),
(
re.compile(
r"(?<![A-Za-z0-9-])(?>([A-Za-z0-9-]*?)\bxapp-)"
r"(?=[A-Za-z0-9-]*[0-9]{9,})[A-Za-z0-9-]{10,}\b"
),
r"\1[REDACTED_KEY]",
),
# Google OAuth access tokens (ya29...).
(re.compile(r"\bya29\.[A-Za-z0-9_\-]{20,}"), "[REDACTED_KEY]"),
# JSON Web Tokens (three base64url segments). The two literal dots make
# this distinctive enough to avoid prose false positives. Accepted FP:
# a 3-part dotted identifier whose segments start with "eyJ" and are
# >=8 base64url chars (e.g. "eyJsonParser.eyJsonReader.eyJsonX") is
# redacted — over-redaction, not a leak. ``/`` is intentionally omitted
# (RFC 7515 JWTs are base64url); ``Bearer``/``Authorization`` paths
# already catch standard-base64 JWTs.
#
# Linear-time form, for the reason given on the Slack patterns above:
# ``.`` is outside the segment class, so a segment always runs to the end
# of its ``[A-Za-z0-9_+-]`` run, and the naive ``\beyJ...`` retried (and
# rescanned) at every ``eyJ`` inside one run — ``"eyJ-" * n`` was O(n^2)
# (320 KB: ~60 s). Only the run's leftmost ``\beyJ`` can matter (it
# has the longest first segment and the same run end), so attempts
# start at run starts and commit to it; the segments are possessive
# because backtracking inside a run can never reach a ``.``.
(
re.compile(
r"(?<![A-Za-z0-9_+\-])(?>([A-Za-z0-9_+\-]*?)\beyJ)"
r"[A-Za-z0-9_+\-]{8,}+\.[A-Za-z0-9_+\-]{8,}+\.[A-Za-z0-9_+\-]+"
),
r"\1[REDACTED_KEY]",
),
]
# Query string of any URL appearing in a message bound for logs/DB, e.g. the
# "for url: https://host/path?..." suffix `requests.Response.raise_for_status`
# appends to HTTPError. Only credential-*shaped* query params are redacted by
# _CREDENTIAL_PATTERNS above; an arbitrary parameter (a search engine's `term=`,
# an `id=`, a filter) carries no credential shape yet may still be user-supplied
# text (e.g. the user's raw search query) that should not reach a log sink.
# Scoped to scrub_error() only (see below) — NOT part of sanitize_error_message,
# since that function also backs sanitize_error_for_client() — and, through it,
# sanitize_error_for_agent(), which is that helper at a 500-char cap — where
# preserving a failed request's own query string is useful for the caller
# debugging it.
#
# Scheme: RFC 3986 §3.1 is one ALPHA followed by zero or more
# ALPHA/DIGIT/+/-/. — the *lower* bound must stay 0 so a valid
# single-character scheme (``x://...``) still matches, but the
# *continuation* is capped at 31 chars (matches the sibling URL-credential
# pattern above). An unbounded ``*`` here is a measured quadratic ReDoS:
# on a message containing a long ALPHA/DIGIT/+/-/. run with no following
# "://" (e.g. a stringified response body reaching str(error)), the engine
# retries the "://" match at every run offset, backtracking the whole
# remaining run each time — O(n^2). Verified locally: unbounded, a 40k-char
# run takes ~1.5s (quadratic: ~4x per input doubling); bounded, the same
# input is back to microseconds. See test_scrub_error_is_linear_time_on_a_
# long_scheme_like_run.
#
# URL body (authority + path, up to the "?"): excludes whitespace, "?" and
# "#" as before, plus the double quote and angle brackets, which are never
# valid unencoded inside a URL (an HTTP client percent-encodes them), and is
# capped at 4096 chars. Both are needed for the same reason the scheme
# continuation is bounded: without them, ``[^\s?#]+`` followed by ``\?`` is a
# second quadratic path — on a whitespace-free run dense in "://" (a
# minified JSON list of URLs, a stringified response body) the engine
# consumes to the end of the run at every "://" and backtracks the whole
# remainder looking for a "?" that never comes. Measured on the uncapped
# body: ~1s at 36k chars, ~4s at 72k, ~16s at 144k (4x per doubling), against
# milliseconds before the query pass existed. Excluding the quote makes the
# common JSON shape short per URL, and the cap bounds every other shape. A
# URL whose scheme+authority+path exceeds 4096 chars has its query left
# alone — no engine in this codebase builds one, and over-length is not a
# leak vector the way a redaction gap is. See
# test_scrub_error_is_linear_time_on_a_dense_url_run.
#
# Query stop-set: whitespace plus the wrappers a URL appears inside in
# prose/logs (a markdown link's ``)``, a quote) so the redaction doesn't
# consume past the URL's actual end and corrupt the surrounding message
# (e.g. eating a markdown link's closing paren, or swallowing a second,
# unspaced adjacent URL's own leading scheme once the first URL's wrapper is
# reached). ``]`` and ``}`` are deliberately NOT in the stop-set: this pass
# runs after sanitize_error_message, whose credential-shaped redaction emits
# ``[REDACTED]`` / ``[REDACTED_KEY]`` in place of a value, and a stop-set
# containing ``]`` halted the strip at that marker — so every parameter
# AFTER a credential-shaped one (Google PSE's ``key=...&cx=...&q=<query>``,
# ScaleSERP's ``api_key=...&q=<query>``) survived into the log while looking
# scrubbed. A URL inside square brackets is now over-redacted up to the
# closing bracket, which is the safe direction. Requires >=1 char (``+``,
# not ``*``): a bare trailing "?" with nothing after it is not a query at
# all, so a sentence-final "...page? yes" must not be rewritten into
# "...page?<redacted> yes", which would imply a query existed.
_URL_QUERY_STRING_RE = re.compile(
r"([A-Za-z][A-Za-z0-9+.\-]{0,31}://[^\s?#\"<>]{1,4096})\?([^\s)\"']+)"
)
_URL_QUERY_MARKER = "<redacted>"
_URL_QUERY_WRAPPERS = {"<": ">", "`": "`"}
def _restore_query_wrapper(match: "re.Match[str]") -> str:
"""Keep only a final Markdown wrapper character, never query content."""
url, query = match.group(1), match.group(2)
opener = match.string[match.start() - 1] if match.start() else ""
closer = _URL_QUERY_WRAPPERS.get(opener)
if (
closer is not None
and len(query) > 1
and query.endswith(closer)
and query != _URL_QUERY_MARKER
):
return f"{url}?{_URL_QUERY_MARKER}{closer}"
return f"{url}?{_URL_QUERY_MARKER}"
def _query_needs_credential_guards(message: str, glued: str) -> bool:
"""Whether query removal must preserve later credential readings."""
return "=" in message and (
_has_name_assignment_hint(message) or _has_name_assignment_hint(glued)
)
def _redact_url_query_strings(message: str) -> str:
"""Replace the query component of any URL in *message* with a fixed
marker, preserving the scheme/host/path so the endpoint stays legible.
A quoted value after a query assignment's ``=`` belongs to that query.
The regex stops at its opening quote; consume the whole value before
replacing the query, or a zero-width separator before the assignment
could leave the quoted secret visible without its credential name.
"""
if "?" not in message or "://" not in message:
return message
queries = list(_URL_QUERY_STRING_RE.finditer(message))
if not queries:
return message
guards: list[list[tuple[int, int, int]]] = []
glued_query_ends: dict[int, int] = {}
glued = strip_control_chars(message)
split_offsets = _glued_offsets(message) if glued != message else None
if _query_needs_credential_guards(message, glued):
if glued == message:
to_text: Callable[[int], int] = int
else:
glued_starts, text_starts = _glued_offsets(message)
def to_text(offset: int) -> int:
run = bisect.bisect_right(glued_starts, offset) - 1
return text_starts[run] + offset - glued_starts[run]
guards = _log_patcher_spans(message, glued, to_text)
if glued != message:
glued_query_ends = {
to_text(query.start(2) - 1): to_text(query.end() - 1) + 1
for query in _URL_QUERY_STRING_RE.finditer(glued)
}
pieces: list[str] = []
done = 0
for match in queries:
if match.start() < done:
continue
query_end = match.end()
end = _cover_query_cut_credentials(
match.start(2) - 1, query_end, guards, query_end
)
if end > query_end:
# A name split by a control can sit inside a longer query in
# the stripped view. Consume that whole query so the suffix
# cannot be mistaken for URL userinfo on the next scrub round.
end = _cover_query_cut_credentials(
match.start(2) - 1,
max(end, glued_query_ends.get(match.start(2) - 1, end)),
guards,
query_end,
)
if (
end == match.end()
and match.group(2).endswith("=")
and end < len(message)
):
quote = message[end]
if quote in ('"', "'"):
end += 1
while end < len(message):
if message[end] == "\\":
end += 2
elif message[end] == quote:
end += 1
break
else:
end += 1
end = min(end, len(message))
# Extending through a quoted query value can consume the
# name of a later credential. Include its complete value
# so the log patcher is not left an unlabelled suffix.
end = _cover_query_cut_credentials(
match.start(2) - 1, end, guards, query_end
)
if split_offsets is not None:
while True:
extended = _cover_query_cut_credentials(
match.start(2) - 1,
_extend_over_split_credential(
glued,
*split_offsets,
match.start(2) - 1,
end,
),
guards,
query_end,
)
if extended == end:
break
end = extended
pieces.append(message[done : match.start()])
pieces.append(_restore_query_wrapper(match))
if end > query_end:
pieces.extend(
char
for char in message[query_end:end]
if _UNSAFE_CHAR_RE.fullmatch(char)
)
done = end
if not pieces:
return message
pieces.append(message[done:])
return "".join(pieces)
_MAX_SCRUB_ROUNDS = 5
def _scrub_to_fixed_point(scrub_once: Callable[[str], str], text: str) -> str:
"""Repeat redaction until stable, with a bound on adversarial input."""
for _ in range(_MAX_SCRUB_ROUNDS):
scrubbed = scrub_once(text)
if scrubbed == text:
break
text = scrubbed
return text
def sanitize_error_message(message: str) -> str:
"""Remove or mask API keys, tokens, and secrets from *message* using
pattern matching for common credential formats.
Use this as a first scrub pass on exception messages before logging,
followed by :func:`redact_secrets` with known literal values (the
"dual-scrub" pattern).
Handles:
* Bearer tokens (``Bearer sk-...``)
* ``Authorization:`` (any scheme) and ``x-api-key:`` headers
* URL query parameters (``?api_key=``, ``?access_token=``,
``?refresh_token=``, ``?subscription-key=``, ``?secret=``, ...)
* URL-embedded credentials (``https://user:pass@host``)
* Well-known token prefixes — ``sk-``/``pk-``, Google ``AIza``/``ya29.``,
GitHub ``ghp_``/``github_pat_``, AWS ``AKIA``/``ASIA``, Slack ``xox*-``,
and JWTs (``eyJ….….…``). See ``docs/developing/credential-scrubbing.md``.
"""
message = _plain_text(message)
return _scrub_to_fixed_point(_sanitize_error_message_once, message)
def _sanitize_error_message_once(message: str) -> str:
if not message:
return message
for pattern, replacement in _CREDENTIAL_PATTERNS:
message = pattern.sub(replacement, message)
return message
# A match found in the stripped view: start, end, replacement, and whether
# the replacement is a fixed string holding none of the matched text.
_GluedMatch = tuple[int, int, str, bool]
def _glued_offsets(text: str) -> tuple[list[int], list[int]]:
"""Map the text :func:`strip_control_chars` leaves back onto *text*.
Returns the start of each run of kept characters in the stripped text
and in *text*, in step, so a stripped offset ``i`` in run ``k`` is
``text_starts[k] + i - glued_starts[k]`` in *text*.
"""
glued_starts: list[int] = []
text_starts: list[int] = []
glued = 0
pos = 0
for unsafe in _UNSAFE_CHAR_RE.finditer(text):
if unsafe.start() > pos:
glued_starts.append(glued)
text_starts.append(pos)
glued += unsafe.start() - pos
pos = unsafe.end()
if pos < len(text):
glued_starts.append(glued)
text_starts.append(pos)
return glued_starts, text_starts
def _log_patcher_spans(
text: str, glued: str, to_text: Callable[[int], int]
) -> list[list[tuple[int, int, int]]]:
"""Credentials in *text* that the log patcher's bare passes redact.
Every pass in :data:`_LOG_PATCHER_BARE_PASSES`, the list the patcher
itself applies (``name=value`` assignments and ``?name=value``
parameters), found both with the original separators and in the
stripped view (mapped back), as ``(start, value_start, end)``: one
sorted, non-overlapping list per pass and view.
"""
lists = []
for pattern, _, name_group, value_after, _ in _LOG_PATCHER_BARE_PASSES:
def span(
m: "re.Match[str]",
name: int = name_group,
value: int = value_after,
) -> tuple[int, int, int]:
return m.start(name), m.end(value), m.end()
lists.append([span(m) for m in pattern.finditer(text)])
lists.append(
[
(to_text(start), to_text(value), to_text(end - 1) + 1)
for start, value, end in map(span, pattern.finditer(glued))
]
)
# Later passes can read a value differently after an inner name
# has been redacted. Guard those actual readings as well as the
# direct matches above; removing the outer name must also remove
# every value that any of the log pipelines associates with it.
for spans in _pipeline_credential_spans(text):
lists.extend(([], spans))
return lists
def _fit_around_credentials(
start: int, end: int, guards: list[list[tuple[int, int, int]]]
) -> Optional[tuple[int, int, bool]]:
"""Fit the span ``[start, end)`` so the log patcher can still redact
every credential it touches.
A guard is a credential the patcher redacts (see
:func:`_log_patcher_spans`); the patcher needs its name to find its
value. A span that covers the whole value, or starts inside the value,
leaves nothing of the value visible without its name and is kept. A
span that starts before the guard and ends inside it is cut back to
end where the guard starts. A span that starts in the name and ends
before the value does is dropped (``None``). Returns the new span and
whether it changed.
"""
lo, hi = start, end
changed = True
while changed: # cutting a span can uncover another partial overlap
changed = False
for spans in guards:
# Non-overlapping and sorted, so the ends are sorted too.
i = bisect.bisect_right(spans, lo, key=lambda span: span[2])
while i < len(spans) and spans[i][0] < hi:
g_start, value_start, g_end = spans[i]
i += 1
if lo >= value_start or hi >= g_end:
continue
if lo >= g_start:
return None
hi = g_start
changed = True
return lo, hi, (lo, hi) != (start, end)
def _cover_cut_credentials(
start: int, end: int, guards: list[list[tuple[int, int, int]]]
) -> int:
"""End of the span ``[start, end)``, extended to the end of every
credential in *guards* (see :func:`_log_patcher_spans`) whose name it
takes without the whole of its value, so the patcher is never left that
value without its name."""
changed = True
while changed:
changed = False
for spans in guards:
i = bisect.bisect_right(spans, start, key=lambda span: span[2])
while i < len(spans) and spans[i][0] < end:
g_start, value_start, g_end = spans[i]
i += 1
if start < value_start and end < g_end:
end = g_end
changed = True
return end
def _cover_query_cut_credentials(
start: int,
end: int,
guards: list[list[tuple[int, int, int]]],
query_end: int,
) -> int:
"""Cover credentials cut by a query, retaining intact later assignments.
A glued-view match can begin in the final query character and extend
across a line break into a complete assignment. If the original-view
patcher already covers that whole assignment after *query_end*, leave
the line and its credential untouched. Span lists alternate original
and glued views, so only overlapping spans need to be checked.
"""
def preserved_in_original(value_start: int, value_end: int) -> bool:
for originals in guards[::2]:
j = bisect.bisect_left(
originals, value_start, key=lambda span: span[1]
)
if (
j < len(originals)
and originals[j][0] >= query_end
and originals[j][1] == value_start
and originals[j][2] >= value_end
):
return True
return False
changed = True
while changed:
changed = False
for view, spans in enumerate(guards):
i = bisect.bisect_right(spans, start, key=lambda span: span[2])
while i < len(spans) and spans[i][0] < end:
g_start, value_start, g_end = spans[i]
i += 1
if start >= value_start or end >= g_end:
continue
if view % 2 and preserved_in_original(value_start, g_end):
continue
end = g_end
changed = True
return end
# What the log patcher's bare passes take as an unquoted value: the run of
# non-whitespace after the ``=`` (a stop inside it only makes it shorter).
_NON_SPACE_RUN_RE = re.compile(r"\S*")
def _reveals_log_patcher_name(covered: str, replacement: str) -> bool:
"""True when *replacement*, written in place of *covered*, shows the log
patcher a ``name=`` or ``?name=`` it does not see in *covered*.
The stripped view can join a name split by an invisible character
(``authorizatio<U+200B>n=...``), and a credential pattern's replacement
keeps it (``authorization=[REDACTED]``). The patcher then takes the run
of text after the replacement as that name's value, up to the next
whitespace, and that run can be the first half of another name split by
a line break (``'api_`` of ``'api_\\nkey=...``), which it would no
longer recognise. :func:`_splice_across_controls` therefore takes that
run into the redaction, and so the credential it starts.
"""
return any(
bare.unanchored.search(replacement)
and not bare.unanchored.search(covered)
for bare in _LOG_PATCHER_BARE_PASSES
)
# How many line breaks (or other stripped characters) at the end of a span
# :func:`_extend_over_split_credential` looks at, and how far before the
# first of them and past the span it searches at a time. Bounds its cost
# on adversarial input.
_MAX_PROBED_CROSSINGS = 64
_PROBE_WINDOW = 128
def _extend_over_split_credential(
glued: str,
glued_starts: list[int],
text_starts: list[int],
start: int,
end: int,
) -> int:
"""End of the span ``[start, end)`` of *text*, extended so it does not
take only the start of a credential that runs on past it.
In the stripped view a span can run on through a line break into the
next line and take the start of a credential there: a name, or a
``Bearer``/``Authorization:`` anchor split by a stripped character
(``Beare\\rr``), whose value lies past the span. Without that start
the log patcher no longer recognises the value. So when a credential
pattern the patcher uses matches across the span's end (as a scan from
before the span finds it, or starting right after a stripped character
inside the span) and would still redact something, the span is extended
to the end of that match, and the value is redacted with it. *glued_starts* and *text_starts* are from :func:`_glued_offsets`.
"""
probes = [
*_CREDENTIAL_PATTERNS,
*(
(bare.unanchored, bare.replacement)
for bare in _LOG_PATCHER_BARE_PASSES
),
]
anchored = {
bare.unanchored: bare.pattern for bare in _LOG_PATCHER_BARE_PASSES
}
def to_glued(offset: int) -> int:
"""Stripped-view offset of the first kept character at or after
*offset* in *text*."""
run = bisect.bisect_right(text_starts, offset) - 1
if run < 0:
return 0
run_len = (
glued_starts[run + 1] if run + 1 < len(glued_starts) else len(glued)
) - glued_starts[run]
return glued_starts[run] + min(offset - text_starts[run], run_len)
def to_text_end(glued_end: int) -> int:
"""Offset in *text* just after the kept character before
*glued_end*."""
run = bisect.bisect_right(glued_starts, glued_end - 1) - 1
return text_starts[run] + glued_end - glued_starts[run]
# Bound chains that repeatedly expose another credential. If the work
# limit is reached, redact the remaining tail instead of leaving a value
# behind after consuming its label.
extensions_left = 16
while True:
if extensions_left == 0:
return to_text_end(len(glued))
extensions_left -= 1
# Runs of kept text that begin inside the span follow a stripped
# character there.
first = bisect.bisect_right(text_starts, start)
last = bisect.bisect_left(text_starts, end)
runs = range(max(first, last - _MAX_PROBED_CROSSINGS), last)
if not runs:
return end
crossings = [glued_starts[run] for run in runs]
glued_end = to_glued(end)
window_end = min(len(glued), glued_end + _PROBE_WINDOW)
reach = glued_end
def runs_on(m: Optional["re.Match[str]"], template: str) -> int:
"""End of *m* if it runs past the span and would still redact
something, else 0. A match cut off by the window is redone
without it, so the whole value is covered."""
if (
m is None
or m.end() <= glued_end
or m.expand(template) == m.group()
):
return 0
if m.end() >= window_end:
whole = m.re.match(glued, m.start())
if whole is not None:
return max(m.end(), whole.end())
return m.end()
window_start = max(0, crossings[0] - _PROBE_WINDOW)
for pattern, template in probes:
# A match as the patcher finds it, scanning from before the
# span, that straddles the span's end.
natural = anchored.get(pattern, pattern)
for m in natural.finditer(glued, window_start, window_end):
if m.start() >= glued_end:
break
reach = max(reach, runs_on(m, template))
if "://" in pattern.pattern:
continue # a URL scheme has no fixed start to anchor at
# A match starting right after a stripped character: the
# patcher may see it there once what precedes it is redacted.
for crossing in crossings:
m = pattern.match(glued, crossing, window_end)
reach = max(reach, runs_on(m, template))
if reach == glued_end:
return end
end = to_text_end(reach)
def _splice_across_controls(
text: str, glued: str, matches: Iterable[_GluedMatch]
) -> str:
"""Apply redactions found in *glued* (``strip_control_chars(text)``)
to *text* itself.
Each match is mapped back to the span of *text* it covers, then fitted
around any credential the log patcher would redact (see
:func:`_fit_around_credentials`) and extended over any credential it
would otherwise cut into (see :func:`_extend_over_split_credential`),
and over the whole of any credential that extension takes the name of
(see :func:`_cover_cut_credentials`). A replacement that would show the
patcher a name it does not see in the span (a split name the stripped
view joined, see :func:`_reveals_log_patcher_name`) is extended over the
run of text the patcher would take as that name's value. These rules
keep the patcher from being left a value without its name in the
shapes they model; they are not a proof for every input. A span that
had to change gets a plain
``[REDACTED]`` unless its replacement holds no matched text. The
whitespace control characters inside a replaced span (line breaks,
tabs, U+2028, ...) are written back after the replacement, so no two
lines are joined. A span whose stripped text already equals its
replacement is left alone.
"""
glued_starts, text_starts = _glued_offsets(text)
def to_text(offset: int) -> int:
run = bisect.bisect_right(glued_starts, offset) - 1
return text_starts[run] + offset - glued_starts[run]
guards = _log_patcher_spans(text, glued, to_text)
pieces: list[str] = []
done = 0
for g_start, g_end, replacement, literal in matches:
if g_end <= g_start:
continue
fitted = _fit_around_credentials(
to_text(g_start), to_text(g_end - 1) + 1, guards
)
if fitted is None:
continue
start, end, shrunk = fitted
if shrunk and not literal:
# A shape replacement retains its anchor (header name, URL
# scheme, ...). Cutting its match short and replacing that
# anchor with a plain marker would expose the original value.
# Keep the whole match; the extension below also conceals any
# overlapping assignment whose name this span would take.
end = to_text(g_end - 1) + 1
if start < done:
continue # inside a span an earlier match was extended over
if not (shrunk and not literal) and _reveals_log_patcher_name(
text[start:end], replacement
):
run_end = _NON_SPACE_RUN_RE.match(text, end).end()
if run_end != end:
end, shrunk = run_end, True
while True:
# Extending can take the name of a credential the patcher sees
# in *text*; the span then takes its whole value as well.
extended = _cover_cut_credentials(
start,
_extend_over_split_credential(
glued, glued_starts, text_starts, start, end
),
guards,
)
if extended == end:
break
end, shrunk = extended, True
if shrunk and not literal:
replacement = REDACTION_TEXT
covered = text[start:end]
if strip_control_chars(covered) == replacement:
continue
kept = "".join(
c for c in covered if c.isspace() and _UNSAFE_CHAR_RE.match(c)
)
pieces.append(text[done:start])
pieces.append(replacement + kept)
done = end
pieces.append(text[done:])
return "".join(pieces)
# Longest text :func:`_scrub_across_controls` scans. On adversarial input
# (a line break inside every ``Bearer``, ``Authorization:`` or URL) it costs
# up to about 55 microseconds per character, against about 0.4 for the
# plain passes, so at this bound about 0.2 s per round. Longer text gets the
# plain passes only, as before that view existed.
_GLUED_VIEW_MAX_CHARS = 4096
def _scrub_across_controls(text: str, secrets: list) -> str:
"""Re-run :func:`scrub_error`'s passes on *text* with control and
invisible format characters removed, applying what they find to
*text* itself (see :func:`_splice_across_controls`).
Text without such characters is returned as it is: the plain passes
already saw it exactly as this view would. So is text longer than
``_GLUED_VIEW_MAX_CHARS``, which bounds the cost of this view; such text
keeps only the plain passes, and the log patcher still strips control
characters and redacts again before it is logged.
"""
if len(text) > _GLUED_VIEW_MAX_CHARS or not _UNSAFE_CHAR_RE.search(text):
return text
glued = strip_control_chars(text)
def apply(find: Callable[[str], Iterable[_GluedMatch]]) -> None:
nonlocal text, glued
matches = list(find(glued))
if matches:
spliced = _splice_across_controls(text, glued, matches)
if spliced != text:
text = spliced
glued = strip_control_chars(text)
for pattern, template in _CREDENTIAL_PATTERNS:
apply(
lambda glued, p=pattern, t=template: (
(m.start(), m.end(), m.expand(t), "\\" not in t)
for m in p.finditer(glued)
)
)
for secret in sorted(
(s for s in secrets if s and len(s) >= _MIN_SECRET_LENGTH),
key=len,
reverse=True,
):
apply(lambda glued, s=secret: _literal_matches(glued, s))
def query_matches(glued: str) -> Iterable[_GluedMatch]:
for m in _URL_QUERY_STRING_RE.finditer(glued):
# Only the query is replaced; the URL before it keeps its text.
yield (
m.start(2) - 1,
m.end(),
_restore_query_wrapper(m)[len(m.group(1)) :],
True,
)
apply(query_matches)
return text
def _literal_matches(glued: str, secret: str) -> Iterable[_GluedMatch]:
"""Non-overlapping occurrences of *secret*, as ``str.replace`` finds them."""
pos = glued.find(secret)
while pos != -1:
yield (pos, pos + len(secret), _REDACTION_TOKEN, True)
pos = glued.find(secret, pos + len(secret))
def scrub_error(error: Union[BaseException, str], *secrets: Any) -> str:
"""Return a log/DB-safe rendering of *error* (the "triple-scrub").
Composes the scrub passes every catch site needs:
:func:`sanitize_error_message` (catches credential *shapes* — Bearer
tokens, URL-embedded credentials, ``sk-``/``pk-`` keys), then
:func:`redact_secrets` with the caller's known literal secret values,
then a blanket strip of any URL's query string (``?...`` →
``?<redacted>``) so a non-credential-shaped parameter — e.g. a search
engine's ``term=`` — cannot carry the caller's raw query text into
logs via a library-formatted message such as
``requests``' ``"... for url: https://host/path?term=<query>"``. This
last pass is specific to ``scrub_error``, not :func:`sanitize_error_message`
(which also backs the client-facing :func:`sanitize_error_for_client` and,
through it, the agent-facing :func:`sanitize_error_for_agent` — where
preserving a failed request's own query string is useful for the caller
debugging it).
Use this at every catch site that logs or persists an exception so
the passes can never drift apart per-site.
``BaseSearchEngine._scrub_error`` delegates here, resolving its
engine's ``_secret_attrs`` into the *secrets* arguments.
Query removal needs additional credential-boundary analysis when a
URL query and a sensitive assignment occur together. Such text is cut
at the log redactor's whitespace-safe 32 KiB bound after shape and
known-literal redaction, before that expensive analysis; the omitted
tail is replaced by a marker outside the fixed-point rounds. Other
long diagnostics retain their full-text behavior.
Defensive by design: this runs inside ``except`` blocks, so it must
never raise. ``str(error)`` is guarded (a custom exception whose
``__str__`` raises won't crash the handler) and each secret is coerced
to ``str`` (a misconfigured non-string secret, e.g. an int from
settings, won't trip ``redact_secrets``' ``len()`` check).
Args:
error: An exception or a pre-built message string.
*secrets: Known literal secret values to redact. ``None`` and
falsy values are silently skipped.
Returns:
The scrubbed message, safe for production log sinks.
"""
try:
message = str(error)
except Exception:
message = "<unprintable {}>".format(type(error).__name__)
# Coerce truthy non-str secrets to str; keep None/falsy as-is
# (redact_secrets filters those out). Guarded per secret: a
# pathological value whose __bool__/__str__ raises cannot be
# literal-matched anyway, so it is skipped rather than allowed to
# crash the except handler this runs in.
safe_secrets = []
for v in secrets:
try:
safe_secrets.append(v and str(v))
except Exception:
continue
omitted = 0
def scrub_once(text: str) -> str:
nonlocal omitted
text = redact_secrets(sanitize_error_message(text), *safe_secrets)
if (
len(text) > _LOG_REDACTION_MAX_CHARS
and "?" in text
and "://" in text
and _query_needs_credential_guards(text, strip_control_chars(text))
and _URL_QUERY_STRING_RE.search(text) is not None
):
# Shape and known-literal redaction must precede the cut: a
# known secret containing spaces could straddle its boundary.
# Guarded query removal tracks every character through several
# readings; limit that work before creating the origin arrays.
# Ordinary long diagnostics keep their existing full-text path.
head, _ = _truncate_for_log_redaction(text)
omitted += len(text) - len(head)
text = head
text = _redact_url_query_strings(text)
return _scrub_across_controls(text, safe_secrets)
result = _scrub_to_fixed_point(scrub_once, message)
if omitted:
# Keep generated metadata outside subsequent redaction rounds.
result += f" [... {omitted} characters omitted from log output]"
return result
def sanitize_error_for_client(message: str, max_length: int = 200) -> str:
"""Make an exception-derived string safe to return to an HTTP client.
Composes :func:`sanitize_error_message` (credential redaction) and
:func:`sanitize_for_log` (control-char strip + length cap). Credential
scrubbing runs FIRST, on the full untruncated string, so a secret near
the ``max_length`` boundary cannot be split by truncation and slip past
the regexes.
Scope, because it is easy to over-trust: this removes credential
*shapes*. It does NOT remove server filesystem paths, SQL text, provider
endpoints or dependency internals. Use it on a message an author already
knows to be safe apart from a possible embedded secret — not as a filter
that makes an arbitrary exception safe to show.
Where an exception can carry those other kinds of detail, do not pass its
text through here at all. Two worked examples, and they are NOT the same
technique:
* ``web/routers/rag.py``'s ``_format_test_embedding_error`` withholds the
text entirely — it uses the exception's module and class only to SELECT
one of a fixed set of messages, and interpolates neither.
* ``web/routers/zotero.py``'s ``_zotero_error_response`` forwards text,
but only text that is already one of the package's author-written
constants: ``client_safe_zotero_message`` looks ``str(exc)`` up in
``CLIENT_SAFE_ZOTERO_MESSAGES`` and returns the module constant it
found (or the caller's own fallback literal), so the value in the
response never comes from the exception object.
Keep the detail server-side. ``logger.exception`` is the default, but it
is not unconditional: loguru's ``diagnose`` renders every frame-local in
an attached traceback, so in a handler whose frames hold credentials (an
API key, the SQLCipher password) log without the traceback instead —
``logger.warning(scrub_error(exc, *known_secrets))``. See the Zotero sync
handler in ``research_library/zotero/sync_service.py`` and the
credential-frame handlers in ``web/queue/processor_v2.py``. Only the
stderr sink can have ``diagnose`` on, and only behind two explicit
opt-ins (``utilities/log_utils.py``), but that is exactly the
configuration an operator debugging such a failure runs.
"""
scrubbed = _scrub_to_fixed_point(
lambda text: strip_control_chars(_sanitize_error_message_once(text)),
message,
)
return sanitize_for_log(scrubbed, max_length=max_length)
# Larger cap than ``sanitize_error_for_client``'s HTTP-client default: these
# strings feed the agent's reasoning AND the ErrorReporter pattern map, where
# over-aggressive truncation drops the categorizable error signal. Credential
# scrubbing still runs first on the full untruncated string (#4633).
_AGENT_ERROR_MAX_LEN = 500
def sanitize_error_for_agent(message: str) -> str:
"""Scrub an exception-derived string that is bound for an LLM / agent.
A preset over :func:`sanitize_error_for_client`: it removes credential
*shapes* and control characters and caps the result at 500 characters.
It delegates rather than re-composing those passes, so the agent path
cannot drift away from the client path — the cap is the only
difference between the two.
Why 500 and not the client helper's 200-char default: a tool error
feeds the model's reasoning loop and the ``ErrorReporter`` pattern map,
and the classification signal often sits deep in a provider message (a
rate-limit phrase such as ``"429 Too Many Requests"`` behind a long
upstream prefix). A 200-char cap removes that signal before anything
downstream can act on it.
Scope, because it is easy to over-trust: this removes credential
*shapes*. It does NOT remove server filesystem paths, SQL text,
provider endpoints or dependency internals. Use it on a message the
caller already knows to be safe apart from a possible embedded secret
— not as a filter that makes an arbitrary exception safe to show.
Where an exception can carry those other kinds of detail, do not pass
its text through here at all: select one of a fixed set of messages
from the exception's type, or forward only author-written constants,
and keep the detail server-side in the logs.
"Agent" is not a routing boundary. Agent-facing text does reach HTTP
responses: a strategy's ``formatted_findings`` travels through
``api/research_functions.py`` to ``web/routers/api_v1.py``, whose
boundary re-applies the same credential-shape pass. Choosing between
this helper and :func:`sanitize_error_for_client` picks a cap; it does
not decide where the string ends up.
"""
return sanitize_error_for_client(message, max_length=_AGENT_ERROR_MAX_LEN)
#: Characters that delimit every secret-bearing part of a URL: path ``/``
#: (and ``\\``), query ``?``, fragment ``#``, userinfo ``@`` and path
#: parameters ``;``. A string with none of them can hold at most a host.
_URL_PART_DELIMITERS = frozenset("/\\?#@;")
#: Placeholder logged instead of a failure reason that may embed a URL.
FAILURE_REASON_WITHHELD = "<withheld: may embed a URL>"
def failure_reason_for_log(reason: Any) -> str:
"""Return a download failure reason in a form that is safe to log.
The reason strings that ``DownloadService.download_resource`` and
``download_as_text`` return are usually fixed categories
(``"egress_policy_denied:..."``, ``"No compatible downloader
available"``), but their exception paths return
``sanitize_error_for_client(str(e))``. That removes credential
*shapes* only: request and urllib3 exception texts keep the URL's path
and query (``with url: /p?X-Amz-Signature=...``), so presigned tokens
survive it. The exception path logs its own cause (exception type and
redacted URL) where it is caught, so a log line further up needs only
the category.
Fails closed on shape: a reason containing any URL-part delimiter is
replaced by :data:`FAILURE_REASON_WITHHELD`; anything else is returned
unchanged. The test is on shape, not origin, so a fixed reason that
happens to contain a delimiter (``"Unexpected content type: text/html
- expected PDF"``) is withheld too; that loss of detail is accepted
rather than trying to tell a fixed reason from exception text. The
result is for logs only: callers keep passing the original reason to
their response or database.
"""
if reason is None:
return "None"
if not isinstance(reason, str):
return f"<{type(reason).__name__}>"
if any(char in _URL_PART_DELIMITERS for char in reason):
return FAILURE_REASON_WITHHELD
return reason
def sanitize_error_details(value: Any) -> Any:
"""Recursively redact credential *shapes* from the string leaves of a
structured ``details`` value (``dict`` / ``list`` / ``tuple`` / dataclass).
Intended for the ``details`` payload of an exception ``to_dict()`` that is
serialized to a client (e.g. ``NewsAPIException`` / ``WebAPIException``).
This is the *value-shape* counterpart to :class:`data_sanitizer.DataSanitizer`,
which redacts by *key name*; use this one when the concern is a credential
embedded anywhere in the text, regardless of the key it sits under.
Behaviour by node type:
* ``dict`` — recurse into values; ``str`` keys are also run through
:func:`sanitize_error_message` (a credential used *as* a key would
otherwise ship verbatim as a JSON key). A key whose redacted form is
already taken (two URLs with different passwords, or a redacted key
equal to a later literal key) gets a ``#2``/``#3`` suffix instead of
overwriting that entry; the first key in iteration order keeps the
unsuffixed name.
* ``list`` / ``tuple`` — both rebuilt as a plain ``list``: the payload is
about to be JSON-serialized (a tuple already becomes a JSON array), and
rebuilding via ``type(value)(<generator>)`` would raise on a namedtuple /
tuple subclass whose constructor is not ``(iterable) -> instance`` —
inside a Flask error handler that degrades a structured error into a bare
500.
* **dataclass instance** — converted via ``dataclasses.asdict`` and recursed.
Flask's default JSON provider serializes a dataclass (via ``asdict``), so
without this a credential in a dataclass field would ship un-redacted.
* ``str`` leaf — redacted via :func:`sanitize_error_message` (credential-shape
redaction only — no length cap or control-char strip, so structured values
such as IDs survive).
* anything else (ints, bools, ``None``, and any other object) — passed
through untouched. Note some passthrough types (``set``/``frozenset``,
``bytes``, a non-``str``-mixin ``Enum``) are not JSON-serializable, so a
credential inside them fails *closed* at ``jsonify`` (generic 500) rather
than leaking.
Redaction is shape-based, so it never removes a value by key name — a benign
``{"query": "reset my password"}`` is unchanged. Containers are rebuilt fresh
(the input is not mutated); passthrough leaf objects are returned by
reference.
"""
if isinstance(value, dict):
out: dict = {}
for k, v in value.items():
safe_key = sanitize_error_message(k) if isinstance(k, str) else k
out[_unique_log_key(out, safe_key)] = sanitize_error_details(v)
return out
if isinstance(value, (list, tuple)):
return [sanitize_error_details(v) for v in value]
# dataclass *instance* (not the class itself) — Flask serializes it via
# asdict(), so recurse into its fields to redact any credential-shaped one.
if dataclasses.is_dataclass(value) and not isinstance(value, type):
return sanitize_error_details(dataclasses.asdict(value))
if isinstance(value, str):
return sanitize_error_message(value)
return value
# ---------------------------------------------------------------------------
# Log-sink credential redaction (the backstop applied by the loguru patcher)
# ---------------------------------------------------------------------------
#
# ``utilities/log_utils.py`` installs :func:`redact_log_message` /
# :func:`redact_log_extra` as a global loguru *patcher* so every record is
# scrubbed once, before it reaches ANY sink — stderr, the encrypted per-user
# DB, the browser progress stream, and (when enabled) the persistent,
# unencrypted ``<LDR_DATA_DIR>/logs/*.log`` file.
#
# The sanctioned per-call-site pattern is ``logger.error(scrub_error(msg,
# secret))`` (see :func:`scrub_error`). This sink-level pass is the BACKSTOP
# for the recurring class of bug where a call site forgets it: a credential
# that rides the message as a bare ``field=value`` pair, or that is bound as
# a structured ``extra`` value under a key the name-based checks miss. Both
# the message string and the structured extras are scrubbed here.
# Settings redaction's default secret names, plus credentials that appear only
# in logs. Routing fields remain explicitly exempt below. This is a set of
# exact names. Message text also recognises environment-variable and dotted
# setting prefixes for these names (``OPENAI_API_KEY``, ``db_password``,
# ``llm.openai.api_key``); structured ``extra`` keys use the same suffix rule
# (see ``_is_sensitive_log_key``). The later quoted-key pass also handles
# JSON and Python-repr credential keys (see ``_QUOTED_KEY_PREFIX``).
LOG_SENSITIVE_KEYS: frozenset = frozenset(
DataSanitizer.DEFAULT_SENSITIVE_KEYS
) | frozenset(
{
"passwd",
"pwd",
"user_password",
"encryption_key",
"sqlcipher_key",
"derived_key",
"salt",
"authorization",
"id_token",
"subscription_key",
}
)
# ``extra`` keys the log sinks read for routing / behaviour. They are never
# redacted, or the DB sink loses per-user attribution
# (``database_sink``/``_get_research_id`` read ``username``/``research_id``)
# and the frontend filter loses its ``policy_audit`` guard. None of these
# hold a secret.
_LOG_STRUCTURAL_EXTRA_KEYS: frozenset = frozenset(
{
"research_id",
"username",
"policy_audit",
}
)
# Keep query and bare-assignment credential names in agreement, but retain
# their distinct value boundaries so harmless query parameters survive.
_LOG_SENSITIVE_NAME_PATTERN = "|".join(
re.escape(name).replace("_", r"[_-]?")
for name in sorted(LOG_SENSITIVE_KEYS, key=lambda name: (-len(name), name))
)
# A following assignment can use the same prefixed or dotted credential
# names as the log patcher. The settings flag remains exempt: its value is
# not a credential and must not be used to shorten a preceding redaction.
_STOP_SENSITIVE_NAME_PATTERN = "".join(
(
r"(?!(?:[a-z0-9]+[._])*requires[_-]api[_-]key\s*=)",
r"(?:[a-z0-9]+[._])*(?:",
_LOG_SENSITIVE_NAME_PATTERN,
r")",
)
)
_SENSITIVE_QUERY_RE = re.compile(
r"(?i)([?&](?:" + _LOG_SENSITIVE_NAME_PATTERN + r")=)([^&\s#]+)"
)
# The name is a sensitive name, optionally preceded by any number of
# ``<alnum>_`` / ``<alnum>.`` segments: the same suffix rule ``extra`` keys
# get, applied to env-var (``OPENAI_API_KEY``), prefixed (``db_password``)
# and dotted setting (``llm.openai.api_key``) spellings. The whole name must
# start a word (not after ``\w``, ``.``, ``-``, or a query's ``?``/``&``),
# so ``mytoken=`` and ``x-token=`` stay readable, and it must END at the
# ``=``: ``max_tokens=`` is not ``_token``. After ``?``/``&``,
# ``_SENSITIVE_QUERY_RE`` redacts the exact names only: a prefixed name in
# a query string (``&google_api_key=x``) is not redacted, nor is any name
# after ``-`` (``--db_password=x``, ``--password=x``). The prefix
# segments cannot contain a separator, so each start position scans one
# identifier once -- linear, like the rest of the pass.
#
# Quoted values may contain spaces and escaped quotes. An unfinished quote
# consumes the remaining message. Unquoted values end at whitespace, not at
# punctuation that may be part of a password -- except that ONE trailing
# ``,`` or ``;`` directly before whitespace or the end of the text is left
# as the list delimiter it almost always is (``max token = 4096, temp``).
# An unquoted value also ends where another credential assignment starts,
# but only where that assignment, redacted in its own right, covers
# everything the longer value would have covered and leaves nothing after
# it that a later pass would have redacted. Stripping control characters
# glues such an assignment onto the value before it
# (``token = [REDACTED]\nap<U+200B>i_key = x`` becomes
# ``token = [REDACTED]api_key = x``); without the stop, the value of
# ``token`` would run on and take the name ``api_key``, leaving ``x``
# visible. The next assignment qualifies in these shapes only:
#
# * ``name=`` with no whitespace in its match: an unquoted value, or a
# quoted value without whitespace that closes or runs to the end of the
# text. Its match ends at the same whitespace the longer value would
# have, which must be one ``strip_control_chars`` keeps (or the end of
# the text): at a stripped line break the patcher's second pass would
# glue the next line onto the visible name.
# * a quoted value that never closes: its match takes the rest of the text.
# * whitespace around the ``=`` or inside a quoted value
# (``token=x;password="p q" r``): its match then runs on past the
# whitespace that ended the longer value, over text the scan after it
# would otherwise read. So that text must hold nothing such a scan, or a
# later pass, could redact differently: no character
# ``strip_control_chars`` removes (a later pass would glue it to its
# neighbours), no ``name=`` inside a quoted value (the value would close on
# that name's opening quote, leaving the rest of its value visible), and
# in an unquoted value, or in the text glued after a closing quote up to
# the next whitespace, no ``name=`` and no ``Bearer``, ``Authorization:``
# or ``x-api-key:`` (anchors whose value lies past that whitespace), which
# must again be kept whitespace or the end of the text.
#
# Anything else is not a stop, and the longer value runs on as before.
_DOUBLE_QUOTED_VALUE = r"\"(?:\\.|[^\"\\])*"
_SINGLE_QUOTED_VALUE = r"'(?:\\.|[^'\\])*"
# A character ``strip_control_chars`` keeps, and kept whitespace.
_KEPT = r"(?!{})".format(_UNSAFE_CHAR_RE.pattern)
_KEPT_SPACE = _KEPT + r"\s"
_ENDS_AT_KEPT_SPACE = r"(?={}|\Z)".format(_KEPT_SPACE)
# How far past a candidate stop its lookahead reads to the end of a run of
# non-whitespace. Each candidate in one run reads to the same end, so the
# bound keeps a run dense with credential names linear; a value that runs
# on longer is not a stop.
_STOP_LOOKAHEAD_CHARS = 256
_GAP = r"(?:\s|{})*".format(_UNSAFE_CHAR_RE.pattern)
# An assignment as a pass reads it, before or after that strip: a credential
# name where an assignment can start, then ``=``, with any whitespace or
# character ``strip_control_chars`` removes between them.
_NAME_THEN_EQUALS = (
r"(?<![\w.?&-])(?:" + _STOP_SENSITIVE_NAME_PATTERN + r")" + _GAP + r"="
)
# The same, or the anchor of a credential shape whose value may lie past
# whitespace (``Authorization: Basic ...``, ``Bearer ...``, ``x-api-key: ...``).
_NAME_OR_SHAPE_ANCHOR = (
_NAME_THEN_EQUALS + r"|bearer|(?:authorization|x-api-key)" + _GAP + r"[:=]"
)
# A run of non-whitespace up to kept whitespace or the end of the text,
# holding no assignment or credential-shape anchor.
_PLAIN_RUN = (
r"(?:(?!"
+ _NAME_OR_SHAPE_ANCHOR
+ r")"
+ _KEPT
+ r"\S){0,"
+ str(_STOP_LOOKAHEAD_CHARS)
+ r"}"
+ _ENDS_AT_KEPT_SPACE
)
def _spread_quoted_value(quote: str) -> str:
"""A quoted value whose match may reach past whitespace (see above)."""
return (
quote
+ r"(?:(?!"
+ _NAME_THEN_EQUALS
+ r")"
+ _KEPT
+ r"(?:\\"
+ _KEPT
+ r"\S|[^"
+ quote
+ r"\\]))*(?:"
+ quote
+ r"(?="
+ _PLAIN_RUN
+ r")|\\?\Z)"
)
def _space_before_closing(quote: str) -> str:
"""Lookahead: whitespace inside the quoted value that starts here."""
return r"(?=" + quote + r"(?:\\\S|[^" + quote + r"\\\s])*\s)"
# An unquoted value that may reach past whitespace (see above).
_SPREAD_UNQUOTED_VALUE = r"(?=[^\s\"'])" + _PLAIN_RUN
_NEXT_ASSIGNMENT = (
r"(?<![\w.?&-])(?:" + _STOP_SENSITIVE_NAME_PATTERN + r")"
# ``name=`` with no whitespace in its match.
r"(?:=(?:\"(?:\\[^\s]|[^\"\\\s])*(?:\"|\\?\Z)"
r"|'(?:\\[^\s]|[^'\\\s])*(?:'|\\?\Z)"
r"|[^\s\"']\S{0,"
+ str(_STOP_LOOKAHEAD_CHARS)
+ r"})"
+ _ENDS_AT_KEPT_SPACE
# A quoted value that never closes: its match takes the rest of the text.
+ r"|\s*=\s*(?:"
+ _DOUBLE_QUOTED_VALUE
+ r"|"
+ _SINGLE_QUOTED_VALUE
+ r")\\?$"
# Whitespace around the ``=``.
+ r"|(?:(?:"
+ _KEPT_SPACE
+ r")+=(?:"
+ _KEPT_SPACE
+ r")*|=(?:"
+ _KEPT_SPACE
+ r")+)(?:"
+ _SPREAD_UNQUOTED_VALUE
+ r"|"
+ _spread_quoted_value('"')
+ r"|"
+ _spread_quoted_value("'")
+ r")"
# Whitespace inside a quoted value.
+ r"|="
+ _space_before_closing('"')
+ _spread_quoted_value('"')
+ r"|="
+ _space_before_closing("'")
+ _spread_quoted_value("'")
+ r")"
)
_ASSIGNMENT_SEPARATOR_PATTERN = r"(\s*=\s*)"
_ASSIGNMENT_VALUE_PATTERN = (
r"(?:\"(?:\\.|[^\"\\])*(?:\"|\\?$)|'(?:\\.|[^'\\])*(?:'|\\?$)"
r"|\S+?(?=[,;]?(?:\s|$)))"
)
_STOP_ASSIGNMENT_VALUE_PATTERN = (
r"(?:\"(?:\\.|[^\"\\])*(?:\"|\\?$)|'(?:\\.|[^'\\])*(?:'|\\?$)"
r"|(?:(?!"
+ _NEXT_ASSIGNMENT
+ r")\S)+?(?=(?:"
+ _NEXT_ASSIGNMENT
+ r"|[,;]?(?:\s|$))))"
)
_ASSIGNMENT_REPLACEMENT = r"\1\2" + REDACTION_TEXT
_ASSIGNMENT_REPLACEMENT_PARTS = (1, 2, REDACTION_TEXT)
# The exact names (``password``, ``client_secret``, ``api-key``). Every
# pass of the log redaction runs this before the prefixed-name pattern
# below, which skips these names. Run after it, the prefixed pattern only
# replaces more of the text: a prefixed value that swallows the start of an
# exact-name assignment (``OPENAI_API_KEY=abc,password = x``) no longer
# hides it. Across the two passes of :func:`_redact_log_text`'s
# "restricted first pass" pipeline the same holds through
# :data:`_FIRST_PASS_SENSITIVE_ASSIGNMENT_RE`.
_BARE_SENSITIVE_ASSIGNMENT_RE = re.compile(
r"(?i)(?<![\w.?&-])("
+ _LOG_SENSITIVE_NAME_PATTERN
+ r")"
+ _ASSIGNMENT_SEPARATOR_PATTERN
+ _ASSIGNMENT_VALUE_PATTERN
)
_STOP_BARE_SENSITIVE_ASSIGNMENT_RE = re.compile(
r"(?i)(?<![\w.?&-])("
+ _LOG_SENSITIVE_NAME_PATTERN
+ r")"
+ _ASSIGNMENT_SEPARATOR_PATTERN
+ _STOP_ASSIGNMENT_VALUE_PATTERN
)
# Names the prefixed pattern must never match, checked by a lookahead
# BEFORE anything is consumed, so the regex scans on inside their values:
# - an exact name: the bare pass has already replaced its value, and
# matching it again would let the unquoted rule run that value on into a
# glued name (``client_secret='x'llm.api_key='y z'``) and hide it;
# - the one leaf settings redaction exempts (``requires_api_key``, a
# boolean flag; see ``data_sanitizer._has_non_secret_qualifier``),
# following a ``.`` or starting the name, as that leaf is computed there.
# A match that consumed the flag's value and handed it back unchanged
# would make ``re.sub`` resume after that value, so an assignment inside
# it (``requires_api_key=true,password=x``, or an unterminated quote
# running to the end of the text) would never be scanned.
_EXEMPT_ASSIGNMENT_NAME_PATTERN = "".join(
(
r"(?:(?:(?:[a-z0-9]+[._])*[a-z0-9]+\.)?",
re.escape(_EXEMPT_QUALIFIER_LEAF).replace("_", "[_-]"),
r")",
)
)
_NOT_PREFIXED_ASSIGNMENT_NAME_PATTERN = (
r"(?!(?:"
+ _EXEMPT_ASSIGNMENT_NAME_PATTERN
+ r"|(?:"
+ _LOG_SENSITIVE_NAME_PATTERN
+ r"))\s*=)"
)
_PREFIXED_ASSIGNMENT_NAME_PATTERN = (
# ``(?=[a-z0-9])`` first: rejects a non-name position before the
# lookahead's long alternation runs (a run of spaces stays cheap).
r"(?i)(?<![\w.?&-])(?=[a-z0-9])"
+ _NOT_PREFIXED_ASSIGNMENT_NAME_PATTERN
+ r"((?:[a-z0-9]+[._])*(?:"
+ _LOG_SENSITIVE_NAME_PATTERN
+ r"))"
+ _ASSIGNMENT_SEPARATOR_PATTERN
)
_SENSITIVE_ASSIGNMENT_RE = re.compile(
_PREFIXED_ASSIGNMENT_NAME_PATTERN + _ASSIGNMENT_VALUE_PATTERN
)
_STOP_SENSITIVE_ASSIGNMENT_RE = re.compile(
_PREFIXED_ASSIGNMENT_NAME_PATTERN + _STOP_ASSIGNMENT_VALUE_PATTERN
)
# Exact and prefixed names in ONE pattern, so whichever assignment starts
# first wins. Two readings of it run, each as a pipeline of
# :func:`_redact_log_text`:
# - "next-line names" (:data:`_LEFTMOST_SENSITIVE_ASSIGNMENT_RE`, first
# pass only, on the original separators; also a pipeline of
# :func:`redact_log_message`): the exempt flag is skipped by
# the lookahead, and a name at the start of the next line followed by
# ``=`` is never taken as a value: ``DB_PASSWORD=\nOPENAI_API_KEY = x``
# is an empty ``DB_PASSWORD`` and an ``OPENAI_API_KEY`` whose value is
# ``x``. Read otherwise, the name is the value and `` = x`` is left
# unredacted. "The next line" is any run of whitespace and
# :func:`strip_control_chars` characters between the ``=`` and the name
# that holds at least one of the latter (``\n``, ``\r\n``, ``\x1c``,
# U+2028, ...): the indentation of a pretty-printed dump
# (``DB_PASSWORD =\n OPENAI_API_KEY = x``) and an invisible character
# before the name (``\n<U+200B>OPENAI_API_KEY``) included. A name after
# only spaces on the same line (``password= api_key = x``) is still read
# as the value.
# - "flag kept whole" (:data:`_START_ORDER_SENSITIVE_ASSIGNMENT_RE`, both
# passes): the exempt flag's assignment is matched like any other and
# kept as it is, value included, so nothing in its value starts an
# assignment (``requires_api_key=a,db_password\n=x;token=y``: the value
# ``a,db_password`` is the flag's, and ``token=y`` is still found).
_ANY_SENSITIVE_NAME_PATTERN = (
r"(?=[a-z0-9])(?!"
+ _EXEMPT_ASSIGNMENT_NAME_PATTERN
+ r"\s*=)(?:[a-z0-9]+[._])*(?:"
+ _LOG_SENSITIVE_NAME_PATTERN
+ r")"
)
# Whitespace that :func:`strip_control_chars` keeps: a space, U+00A0,
# U+3000, ... -- not a tab or a line break, which it removes.
_KEPT_WHITESPACE_PATTERN = "".join(
(r"(?:(?!", _UNSAFE_CHAR_RE.pattern, r")\s)")
)
# The next-line guard of :data:`_LEFTMOST_SENSITIVE_ASSIGNMENT_RE`, checked
# right after the ``=``: kept whitespace, one stripped character, then any
# mix of whitespace and stripped characters (ONE character class --
# ``_UNSAFE_CHAR_RE`` is a single class, whose inside is reused -- so a
# long run is scanned once, not re-split), then a name and ``=``. The parts
# before the name are disjoint or a single class, so the check is linear in
# the run it scans.
_NEXT_LINE_NAME_GUARD_PATTERN = (
r"(?!"
+ _KEPT_WHITESPACE_PATTERN
+ r"*"
+ _UNSAFE_CHAR_RE.pattern
+ r"[\s"
+ _UNSAFE_CHAR_RE.pattern[1:-1]
+ r"]*"
+ _ANY_SENSITIVE_NAME_PATTERN
+ r"\s*=)"
)
_LEFTMOST_SENSITIVE_ASSIGNMENT_RE = re.compile(
r"(?i)(?<![\w.?&-])("
+ _ANY_SENSITIVE_NAME_PATTERN
+ r")(\s*="
+ _NEXT_LINE_NAME_GUARD_PATTERN
+ r"\s*)"
+ _ASSIGNMENT_VALUE_PATTERN
)
_START_ORDER_SENSITIVE_ASSIGNMENT_RE = re.compile(
r"(?i)(?<![\w.?&-])((?:[a-z0-9]+[._])*(?:"
+ _LOG_SENSITIVE_NAME_PATTERN
+ r"))"
+ _ASSIGNMENT_SEPARATOR_PATTERN
+ _ASSIGNMENT_VALUE_PATTERN
)
# Run the guarded stop as one additional reading, leaving the established
# pipelines intact. Their union keeps every value the old readings masked
# while the stop reading can preserve and redact a glued next name.
_STOP_START_ORDER_SENSITIVE_ASSIGNMENT_RE = re.compile(
r"(?i)(?<![\w.?&-])((?:[a-z0-9]+[._])*(?:"
+ _LOG_SENSITIVE_NAME_PATTERN
+ r"))"
+ _ASSIGNMENT_SEPARATOR_PATTERN
+ _STOP_ASSIGNMENT_VALUE_PATTERN
)
_EXEMPT_ASSIGNMENT_NAME_RE = re.compile(
r"(?i)" + _EXEMPT_ASSIGNMENT_NAME_PATTERN + r"\Z"
)
def _start_order_replacement_parts(match: "re.Match[str]") -> tuple:
"""Replacement for :data:`_START_ORDER_SENSITIVE_ASSIGNMENT_RE`, as parts.
The exempt flag's whole assignment is kept (group 0); any other keeps
its name and separator and redacts the value.
"""
if _EXEMPT_ASSIGNMENT_NAME_RE.match(match.group(1)):
return (0,)
return _ASSIGNMENT_REPLACEMENT_PARTS
def _redact_start_order_match(match: "re.Match[str]") -> str:
"""Replacement for :data:`_START_ORDER_SENSITIVE_ASSIGNMENT_RE`."""
return _join_replacement_parts(match, _start_order_replacement_parts)
# The prefixed pattern for the first pass of the "restricted" pipeline of
# :func:`_redact_log_text`. It must not change how that pipeline's second
# pass parses the text around the value it replaces, or that pass -- whose
# bare-name half is exactly what the bare names alone would redact -- could
# match less. So it only takes a value the second pass could never read
# differently:
# - no ``=`` and not followed by ``\s*=``, so no name inside it (one that
# only becomes recognisable once invisible characters are stripped,
# ``pass<U+200B>word = x``) is lost;
# - followed by whitespace that survives :func:`strip_control_chars` (or by
# the end of the text), so the stripped text never glues the replacement
# onto the next token and makes a name there newly recognisable, whose
# value could then swallow the next one;
# - unquoted with no quote character in it, or quoted with no quote
# character or backslash inside the quotes -- and a quoted value keeps
# its quotes (``"[REDACTED]"``) -- so a quote the second pass opens
# earlier still closes where it would have. The second pass then
# redacts the kept quotes along with the value.
# Any other value is left for the second pass, which can MISS it: on the
# stripped text a name the newline separated is glued to the line before
# (``-\nDB_PASSWORD=x`` reads ``-DB_PASSWORD=x``, a name after ``-``), or
# the value before it runs on into it. That is why this pipeline never
# runs alone: the others read the first kind on the original separators,
# where the newline still separates the name. The second kind is missed by
# every reading -- each reads the quoted value's start as part of the value
# before it, and none redacts the rest -- when this pattern cannot take it
# either: a prefixed name's quoted value followed by a line break, a tab
# or another stripped character (``OPENAI_API_KEY=abc,DB_PASSWORD="a b c"``
# and ``\n`` leaves `` b c"``), by any other non-whitespace character
# (``")``) or by whitespace and ``=`` (``" = x``); one whose run-on value
# belongs to an exact name, which this pipeline's bare-name step has
# already redacted to the end of the quote's first word
# (``token=abc,DB_PASSWORD="a b c"``); or a quoted assignment glued on
# with ``&``, ``?`` or ``-`` (``OPENAI_API_KEY=abc&DB_PASSWORD="a b c"``,
# also with an exact name), which no reading takes as a quoted value.
_FIRST_PASS_SENSITIVE_ASSIGNMENT_RE = re.compile(
_PREFIXED_ASSIGNMENT_NAME_PATTERN
+ r"(?:([\"'])[^\"'\\=]*(\3|\Z)|[^\s=\"']+?)"
+ r"(?=[,;]?(?:"
+ _KEPT_WHITESPACE_PATTERN
+ r"|\Z))(?!\s*=)"
)
def _first_pass_replacement_parts(match: "re.Match[str]") -> tuple:
"""Replacement for :data:`_FIRST_PASS_SENSITIVE_ASSIGNMENT_RE`, as parts.
The name, the separator and any quotes are kept (group numbers); only
the value becomes :data:`REDACTION_TEXT` (a literal). See
:func:`_substitute_tracked` for the part format.
"""
parts: list = [1, 2]
if match.group(3):
parts.append(3)
parts.append(REDACTION_TEXT)
if match.group(4):
parts.append(4)
return tuple(parts)
def _join_replacement_parts(match: "re.Match[str]", parts_for) -> str:
"""The replacement text *parts_for* gives for *match*."""
return "".join(
part if isinstance(part, str) else match.group(part)
for part in parts_for(match)
)
def _redact_first_pass_match(match: "re.Match[str]") -> str:
"""Replacement for :data:`_FIRST_PASS_SENSITIVE_ASSIGNMENT_RE`."""
return _join_replacement_parts(match, _first_pass_replacement_parts)
def redact_sensitive_assignments(message: str) -> str:
"""Redact known credential assignments with quoted or unquoted values.
Complements :func:`sanitize_error_message` (which only redacts a
``field=value`` when it sits in a URL query string, i.e. preceded by
``?``/``&``). The field name (``api_key=``, ``password=``, ``token=``,
``OPENAI_API_KEY=``, ``llm.openai.api_key=``, ...) is preserved; only
its value is replaced. Returns *message*
unchanged when it is empty or contains no sensitive assignment.
"""
message = _plain_text(message)
return _scrub_to_fixed_point(_redact_sensitive_assignments_once, message)
def _redact_sensitive_assignments_once(message: str) -> str:
if not message:
return message
bare = _STOP_BARE_SENSITIVE_ASSIGNMENT_RE.sub(
_ASSIGNMENT_REPLACEMENT, message
)
return _STOP_SENSITIVE_ASSIGNMENT_RE.sub(_ASSIGNMENT_REPLACEMENT, bare)
# Upper bound on how much of ONE string the log-path redaction scans. The
# patcher runs synchronously in the logging thread on every record (message,
# every ``extra`` string leaf, every rendered exception field), so its cost
# must not grow with whatever a call site hands it -- fetched page text, a
# response body, a long traceback string. Text past the bound is NOT passed
# through unscanned (that would ship any secret in it); it is dropped and
# replaced by a marker giving the omitted length, in every sink (stderr,
# file, DB, frontend). 32 KiB keeps a fetched page (the fetch tool logs up
# to 10,000 characters) intact. On adversarial 32 KiB input the passes of
# :func:`_redact_log_text` measured up to ~280 ms together on an idle
# machine and about twice that on a loaded one, and those of
# :func:`redact_log_message` up to ~240 ms idle (``pwd="a"b=c `` repeated):
# a text whose pipelines disagree on every line (that one,
# ``db_password = a, `` repeated, or ``DB_PASSWORD=x`` lines) runs all of
# them with origins tracked, several times the cost of one pipeline. A
# text with no credential name followed by ``=`` skips the name steps (see
# :func:`_result_without_name_assignments`): about 10-50 ms at 32 KiB.
# The cost is linear in the length scanned (8x the time for 8x the text,
# measured from 4 KiB to 32 KiB), so it is the same for a 1 MB or a 10 MB
# string; the cut itself is one scan of at most 32 KiB (under 0.3 ms).
_LOG_REDACTION_MAX_CHARS = 32_768
# The run at the end of a truncated head, matched reversed: non-whitespace
# plus every character ``strip_control_chars`` removes (``\n``, ``\t``,
# ``\x1c``, U+0085, U+2028, ...). ``_redact_log_text`` strips those between
# its passes, which rejoins a token they split, so they are not a boundary.
# Built from ``_UNSAFE_CHAR_RE`` itself so the two cannot drift apart.
_LOG_TRAILING_RUN_RE = re.compile(r"(?:\S|" + _UNSAFE_CHAR_RE.pattern + r")*")
# Digits bounded (ASCII, at most 19) so a forged marker cannot make
# ``int()`` exceed its digit limit; the real count is far below that.
_LOG_OMISSION_MARKER_RE = re.compile(
r" \[\.\.\. ([0-9]{1,19}) characters omitted from log output\]\Z"
)
def _truncate_for_log_redaction(
message: str, max_chars: int = _LOG_REDACTION_MAX_CHARS
) -> tuple[str, str]:
"""Split *message* into the part to scan and a marker for the rest.
*max_chars* is the bound (the patcher's ``_LOG_REDACTION_MAX_CHARS``
unless a caller that shows only a short prefix of the result passes a
smaller one, see :func:`redact_and_bound_for_log`); the rule below is
the same for any bound.
The cut only lands at a boundary: a whitespace character that
:func:`strip_control_chars` keeps (a space, U+00A0, U+3000, ...). A
control or separator whitespace character (``\n``, ``\t``, ``\x1c``,
U+0085, U+2028, ...) is not one, because :func:`_redact_log_text`
strips it between its passes and so joins the text on either side.
Unless the character at the bound is a boundary, the cut moves back to
just after the last boundary before the bound, dropping the whole run
the bound falls in -- however long, so a string with no boundary in its
first *max_chars* characters keeps nothing but the
marker. A partial run could be a credential fragment its shape pattern
no longer recognises (a key cut below its minimum length, URL userinfo
cut before its ``@``, a password cut at a ``&``). A credential token
contains no boundary character -- one split by a stripped control
character is a single run here, as it is to the second pass -- so the
head keeps at most a label that the passes redact or that holds no
secret (``password = ``, ``Authorization: Basic ``, or an unterminated
quote, which the assignment pass redacts to the end of the head, so a
quoted value with spaces in it is covered too).
"""
if len(message) <= max_chars:
return message, ""
cut = max_chars
at_bound = message[cut]
if not at_bound.isspace() or _UNSAFE_CHAR_RE.match(at_bound):
# ``\S`` is exactly ``not str.isspace()``; matching the reversed head
# measures the trailing run in one linear scan.
run = _LOG_TRAILING_RUN_RE.match(message[cut - 1 :: -1])
cut -= run.end()
omitted = len(message) - cut
return message[:cut], f" [... {omitted} characters omitted from log output]"
def log_length_before_truncation(message: str) -> int:
"""Length *message* had before the log-path length bound cut it.
For a string ending in the marker :func:`redact_log_message` appends,
the kept text plus the omitted count; otherwise ``len(message)``. Both
are measured after redaction, which may change the length. Never
raises: a marker the pattern does not accept (logged text can forge
one) yields ``len(message)``.
"""
marker = _LOG_OMISSION_MARKER_RE.search(message)
if marker is None:
return len(message)
try:
return marker.start() + int(marker.group(1))
except ValueError:
return len(message)
def redact_log_message(message: str) -> str:
"""Full credential scrub for a log *message* string.
Runs the credential-*shape* patterns :func:`sanitize_error_message`
runs (its ``_CREDENTIAL_PATTERNS``: URL userinfo, ``?param=``
credentials, ``Bearer``/``x-api-key`` headers, well-known key prefixes,
JWTs) -- the shape redaction the sanctioned :func:`scrub_error` applies,
run as a backstop on every record -- then ``?name=``/``&name=`` query
parameters for every :data:`LOG_SENSITIVE_KEYS` name, then
``name=value`` assignments of those names, exact or prefixed (the
names :func:`redact_sensitive_assignments` recognises), in each reading
of :data:`_LOG_MESSAGE_PIPELINES`; what any reading redacts is
redacted. Control characters are kept (exception text is rendered
with its newlines). Non-sensitive text up to
``_LOG_REDACTION_MAX_CHARS`` characters is returned unchanged; a longer
string is cut there (see :func:`_truncate_for_log_redaction`) and the
remainder replaced by an omitted-length marker, so the cost per string
is bounded.
"""
message = _plain_text(message)
if not message:
return _exact_str(message)
head, marker = _truncate_for_log_redaction(message)
return _scrub_to_fixed_point(_redact_bounded_log_text, head) + marker
# One substitution of a redaction pass: a pattern and its replacement,
# given as the string or callable ``re.sub`` takes and as PARTS -- a list
# (or a callable giving one per match) of group numbers, which keep that
# group's text, and literal strings, which are inserted. The parts let
# :func:`_substitute_tracked` follow every kept character back to the
# position it came from.
_TEMPLATE_GROUP_RE = re.compile(r"\\([0-9])")
def _template_parts(template: str) -> tuple:
"""Parts of a ``re.sub`` template that uses only ``\\1``-style groups."""
if "\\" in _TEMPLATE_GROUP_RE.sub("", template):
raise ValueError(f"unsupported replacement template: {template!r}")
parts: list = []
for index, piece in enumerate(_TEMPLATE_GROUP_RE.split(template)):
if index % 2:
parts.append(int(piece))
elif piece:
parts.append(piece)
return tuple(parts)
def _step(pattern: "re.Pattern[str]", replacement: Any, parts: Any = None):
if parts is None:
parts = _template_parts(replacement)
return pattern, replacement, parts
# The credential-shape and query-parameter steps every pass starts with.
_SHAPE_STEPS = tuple(
_step(pattern, replacement) for pattern, replacement in _CREDENTIAL_PATTERNS
) + (_step(_SENSITIVE_QUERY_RE, r"\1" + REDACTION_TEXT),)
_BARE_STEP = _step(_BARE_SENSITIVE_ASSIGNMENT_RE, _ASSIGNMENT_REPLACEMENT)
_PREFIXED_STEP = _step(_SENSITIVE_ASSIGNMENT_RE, _ASSIGNMENT_REPLACEMENT)
# The name steps of :func:`redact_log_message`'s pass before the glued
# tails were closed (below). The bare names run before the prefixed ones
# (see :data:`_BARE_SENSITIVE_ASSIGNMENT_RE`); the prefixed ones run twice,
# because a quoted value glued to the next text (``OPENAI_API_KEY="x"tail``)
# leaves ``[REDACTED]tail``, which a second scan reads as one unquoted value.
# An exact name's tail (``password="x"tail``) was left.
_NAME_STEPS_BEFORE_CLOSING = (_BARE_STEP, _PREFIXED_STEP, _PREFIXED_STEP)
# A quoted value glued to the next text leaves that text behind when it is
# redacted: ``password="x"tail`` becomes ``password=[REDACTED]tail``. This
# step reads such an assignment again, as a scan for names would (the same
# names, word start and separator; not the exempt flag), but only where the
# value starts with ``[REDACTED]``, and redacts the tail up to the
# whitespace (or ``,``/``;`` before it) where an unquoted value ends --
# unless the tail holds an ``=`` or is followed by one, i.e. is itself the
# name of an assignment (``passwd=""apikey =""tail``: ``apikey`` stays,
# its own ``tail`` goes). So, unlike another scan for names, it never reads
# a value on into the name of an assignment whose own tail it has not yet
# redacted.
_GLUED_TAIL_RE = re.compile(
r"(?i)(?<![\w.?&-])("
+ _ANY_SENSITIVE_NAME_PATTERN
+ r")"
+ _ASSIGNMENT_SEPARATOR_PATTERN
+ re.escape(REDACTION_TEXT)
+ r"(?![,;]?(?:\s|$))[^\s=]+?(?=[,;]?(?:\s|$))(?!\s*=)"
)
_GLUED_TAIL_STEP = _step(_GLUED_TAIL_RE, _ASSIGNMENT_REPLACEMENT)
# The "exact names first" name steps: the bare names, the prefixed ones,
# the glued tails, then both name scans once more, the bare names last
# (see :data:`_BARE_SENSITIVE_ASSIGNMENT_RE`). The last two scans redact a
# tail the tail step leaves (one holding an ``=``: ``password="x"a=b``) as
# part of an unquoted value. Running these steps again on their result
# changes nothing (seeded fuzz: 0 of 50,000 messages).
_FULL_PASS_NAME_STEPS = (
_BARE_STEP,
_PREFIXED_STEP,
_GLUED_TAIL_STEP,
_PREFIXED_STEP,
_BARE_STEP,
)
_FULL_PASS_STEPS = _SHAPE_STEPS + _FULL_PASS_NAME_STEPS
_RESTRICTED_FIRST_PASS_STEPS = (
_BARE_STEP,
_step(
_FIRST_PASS_SENSITIVE_ASSIGNMENT_RE,
_redact_first_pass_match,
_first_pass_replacement_parts,
),
)
_LEFTMOST_STEP = _step(
_LEFTMOST_SENSITIVE_ASSIGNMENT_RE, _ASSIGNMENT_REPLACEMENT
)
# The "next-line names" steps, closed the same way, so
# ``DB_PASSWORD=\nOPENAI_API_KEY = "x"tail`` does not leave ``tail`` -- which
# the second pass, reading ``OPENAI_API_KEY`` as ``DB_PASSWORD``'s value on
# the stripped text, would not redact either.
_LEFTMOST_STEPS = (_LEFTMOST_STEP, _GLUED_TAIL_STEP, _LEFTMOST_STEP)
_START_ORDER_STEPS = (
_step(
_START_ORDER_SENSITIVE_ASSIGNMENT_RE,
_redact_start_order_match,
_start_order_replacement_parts,
),
)
_STOP_START_ORDER_STEPS = (
_step(
_STOP_START_ORDER_SENSITIVE_ASSIGNMENT_RE,
_redact_start_order_match,
_start_order_replacement_parts,
),
)
# The pipelines of :func:`_redact_log_text`: the name steps each runs after
# :data:`_SHAPE_STEPS`, in its first pass (on the original text) and
# in its second (on the text with control characters stripped). The first
# pipeline's text is the one kept; the others only add redactions to it.
_LOG_TEXT_PIPELINES = (
# "exact names first": :func:`redact_log_message`'s name steps, on the
# original separators and again on the stripped text. Its result is
# the one kept.
(_FULL_PASS_NAME_STEPS, _FULL_PASS_NAME_STEPS),
# "restricted first pass": a first pass that never changes how the
# second reads the text around a value it replaces (see
# :data:`_FIRST_PASS_SENSITIVE_ASSIGNMENT_RE`).
(_RESTRICTED_FIRST_PASS_STEPS, _FULL_PASS_NAME_STEPS),
# "next-line names": names in start order, in the first pass (see
# :data:`_LEFTMOST_SENSITIVE_ASSIGNMENT_RE`).
(_LEFTMOST_STEPS, _FULL_PASS_NAME_STEPS),
# "flag kept whole": names in start order, in both passes (see
# :data:`_START_ORDER_SENSITIVE_ASSIGNMENT_RE`).
(_START_ORDER_STEPS, _START_ORDER_STEPS),
# "exact names only": the redaction as it was before the prefix rule
# (with the trailing ``,``/``;`` rule of the value pattern), so this
# one never removes less than the exact names alone would.
((_BARE_STEP,), (_BARE_STEP,)),
# "exact names first" and "next-line names" with their first pass as
# it was before the glued tails were closed. Closing them changes the
# text the later steps of that pass read, and removes text before the
# strip, which can join what follows onto a different neighbour and
# change how the second pass reads it; with these readings kept,
# closing never shows anything that leaving it open would hide.
(_NAME_STEPS_BEFORE_CLOSING, _FULL_PASS_NAME_STEPS),
((_LEFTMOST_STEP,), _FULL_PASS_NAME_STEPS),
# Preserve names that an earlier value would otherwise swallow.
(_STOP_START_ORDER_STEPS, _STOP_START_ORDER_STEPS),
)
# The pipelines of :func:`redact_log_message`, which also renders exception
# text (``str()``, notes, ``SyntaxError`` fields) and so keeps every
# control character: the first passes of :data:`_LOG_TEXT_PIPELINES`, in
# that order, on the text as it is -- one pass each, no second pass
# (``None``) -- except that the "restricted first pass" reading, which
# leaves values for its second pass, runs the "exact names first" steps
# after it on the same text, and the unclosed "next-line names" reading is
# left out: its closed steps start with it and only run more steps after
# it on the same text, so they only redact more. So an empty
# ``DB_PASSWORD=`` at the end of a line does not take the name starting the
# next line as its value, and ``OPENAI_API_KEY=abc,DB_PASSWORD="a b c"``
# followed by the end of the text or by a space (with no ``=`` after it)
# does not leave `` b c"`` readable, in an exception's text as in a log
# message (followed by a line break, or by `` = x``, it does in both; see
# :data:`_FIRST_PASS_SENSITIVE_ASSIGNMENT_RE`).
_LOG_MESSAGE_PIPELINES = (
(_FULL_PASS_NAME_STEPS, None),
(_NAME_STEPS_BEFORE_CLOSING, None),
(_RESTRICTED_FIRST_PASS_STEPS + _FULL_PASS_NAME_STEPS, None),
(_LEFTMOST_STEPS, None),
(_START_ORDER_STEPS, None),
((_BARE_STEP,), None),
(_STOP_START_ORDER_STEPS, None),
)
class _BarePass(NamedTuple):
"""A name-based log pass and its unanchored variant for glued text."""
pattern: "re.Pattern[str]"
replacement: str
name_group: int
value_after_group: int
unanchored: "re.Pattern[str]"
def _without_name_boundary(pattern: "re.Pattern[str]") -> "re.Pattern[str]":
return re.compile(pattern.pattern.replace(r"(?<![\w.?&-])", "", 1))
def _log_patcher_bare_passes() -> tuple[_BarePass, ...]:
"""Find the query and name patterns in every live log pipeline.
The control-splice guard must follow new name passes automatically;
otherwise a splice could consume a name and expose its value to a sink.
All current name passes capture the name and separator as groups 1/2.
The replacement here is used only to detect whether a prospective match
would hide anything, so a conservative value marker is sufficient for
passes with conditional replacement callbacks.
"""
passes = [
_BarePass(
_SENSITIVE_QUERY_RE,
r"\1" + REDACTION_TEXT,
1,
1,
_SENSITIVE_QUERY_RE,
)
]
seen = {_SENSITIVE_QUERY_RE}
for pipelines in (_LOG_TEXT_PIPELINES, _LOG_MESSAGE_PIPELINES):
for first, second in pipelines:
for steps in (first, second or ()):
for pattern, _replacement, _parts in steps:
if pattern in seen:
continue
seen.add(pattern)
passes.append(
_BarePass(
pattern,
_ASSIGNMENT_REPLACEMENT,
1,
2,
_without_name_boundary(pattern),
)
)
return tuple(passes)
_LOG_PATCHER_BARE_PASSES = _log_patcher_bare_passes()
_LOG_PATCHER_BARE_GROUPS = {
bare.pattern: (bare.name_group, bare.value_after_group)
for bare in _LOG_PATCHER_BARE_PASSES
}
def _run_steps(text: str, steps) -> str:
for pattern, replacement, _parts in steps:
text = pattern.sub(replacement, text)
return text
def _substitute_tracked(
pattern: "re.Pattern[str]", parts: Any, text: str, origins: list
) -> tuple[str, list]:
"""``pattern.sub`` on *text*, carrying each character's origin along.
*origins* gives, for every character of *text*, the position in the
original string it came from, or ``-1`` for an inserted one. The result
is the same text ``re.sub`` gives with the matching replacement, and its
origins: a kept group keeps its characters' origins, a literal part is
inserted (``-1``).
"""
pieces: list = []
# Accumulate directly to avoid retaining one list per match fragment,
# which can trigger repeated garbage collections on dense credentials.
merged: list = []
position = 0
for match in pattern.finditer(text):
start = match.start()
pieces.append(text[position:start])
merged.extend(origins[position:start])
for part in parts(match) if callable(parts) else parts:
if isinstance(part, str):
pieces.append(part)
merged.extend([-1] * len(part))
continue
group_start, group_end = match.span(part)
if group_start < 0:
continue
pieces.append(text[group_start:group_end])
merged.extend(origins[group_start:group_end])
position = match.end()
if not pieces:
return text, origins
pieces.append(text[position:])
merged.extend(origins[position:])
return "".join(pieces), merged
def _strip_control_chars_tracked(text: str, origins: list) -> tuple[str, list]:
""":func:`strip_control_chars`, carrying origins as
:func:`_substitute_tracked` does."""
kept: list = []
kept_origins: list = []
position = 0
for match in _UNSAFE_CHAR_RE.finditer(text):
kept.append(text[position : match.start()])
kept_origins.extend(origins[position : match.start()])
position = match.end()
if not kept:
return text, origins
kept.append(text[position:])
kept_origins.extend(origins[position:])
return "".join(kept), kept_origins
def _redact_bounded_log_text(text: str) -> str:
""":func:`redact_log_message`'s pass, on text already within the bound.
Runs the pipelines of :data:`_LOG_MESSAGE_PIPELINES` and shows nothing
that any of them leaves out (see :func:`_redact_by_pipelines`).
"""
return _redact_by_pipelines(text, _LOG_MESSAGE_PIPELINES)
def _redact_log_text(value: str) -> str:
"""Redact, strip control characters, then redact again.
The first pass sees the original separators, so ``failure\ntoken=x`` does
not glue into an unrecognisable ``failuretoken=x``; the second catches a
name that only becomes recognisable once invisible characters are gone
(``api_<U+200B>key=x``). Used for the message and every ``extra`` string.
The length bound is applied once, up front, so the second pass never
re-truncates the first pass's output.
Several pipelines run these passes, differing only in how they redact
names (see :data:`_LOG_TEXT_PIPELINES`), because no single order is
safe on its own: a replacement made in the first pass changes how the
second pass, on the stripped text, splits the text around it, and a
value left for the second pass can be missed there
(``-\nDB_PASSWORD=x`` is stripped to ``-DB_PASSWORD=x``, a name after
``-``). The result shows nothing that any pipeline's result leaves
out (see :func:`_redact_by_pipelines`).
"""
value = _plain_text(value)
if not value:
return _exact_str(value)
head, marker = _truncate_for_log_redaction(value)
return (
_scrub_to_fixed_point(
lambda text: _redact_by_pipelines(text, _LOG_TEXT_PIPELINES), head
)
+ marker
)
# A credential name directly followed by ``\s*=``. Every name step's
# pattern (:data:`_BARE_SENSITIVE_ASSIGNMENT_RE`,
# :data:`_SENSITIVE_ASSIGNMENT_RE`, :data:`_GLUED_TAIL_RE`,
# :data:`_LEFTMOST_SENSITIVE_ASSIGNMENT_RE`,
# :data:`_START_ORDER_SENSITIVE_ASSIGNMENT_RE`,
# :data:`_FIRST_PASS_SENSITIVE_ASSIGNMENT_RE`) ends its name with an
# alternative of :data:`_LOG_SENSITIVE_NAME_PATTERN` followed by
# ``\s*=``, under the same ``(?i)``, so a text this does not match is
# left unchanged by every name step.
_NAME_ASSIGNMENT_HINT_RE = re.compile(
"".join((r"(?i)(?:", _LOG_SENSITIVE_NAME_PATTERN, r")\s*="))
)
def _has_name_assignment_hint(text: str) -> bool:
# No ``=`` at all is the common case, and a substring test is far
# cheaper than the case-insensitive scan over every name.
return "=" in text and _NAME_ASSIGNMENT_HINT_RE.search(text) is not None
def _result_without_name_assignments(
shaped: str, pipelines: tuple
) -> Optional[str]:
"""What every pipeline gives when no name step can match, else ``None``.
*shaped* is the text after :data:`_SHAPE_STEPS`, where every first
pass starts. When neither it nor (for pipelines with a second pass)
the second pass's text after its shape steps holds a credential name
followed by ``=`` (:data:`_NAME_ASSIGNMENT_HINT_RE`), every name step
is a no-op and every pipeline gives the same text, which is returned
-- the text :func:`_redact_by_pipelines` would return, without running
the name steps of each pipeline (most log records). ``None`` means the
pipelines must run.
"""
if _has_name_assignment_hint(shaped):
return None
second_passes = {second is None for _first, second in pipelines}
if second_passes == {True}:
return shaped
if second_passes != {False}:
return None
second_text = _run_steps(strip_control_chars(shaped), _SHAPE_STEPS)
if _has_name_assignment_hint(second_text):
return None
return second_text
def _redact_by_pipelines(head: str, pipelines: tuple) -> str:
"""*head* as every pipeline of *pipelines* redacts it, together.
Runs them untracked first. When the text holds no credential name
followed by ``=`` (before or after control characters are stripped),
no name step can match and the shape-redacted text is returned
directly (see :func:`_result_without_name_assignments`). In the usual
remaining case they all give the same text, which is returned;
otherwise :func:`_redact_log_text_union` reruns them with origins
tracked.
"""
run = _PipelineRun(head, tracked=False, pipelines=pipelines)
without_names = _result_without_name_assignments(run.shaped, pipelines)
if without_names is not None:
return without_names
indexes = range(len(pipelines))
if len({run.first_pass(index) for index in indexes}) == 1:
# The first passes agree, so the pipelines differ at most in the
# name steps of the second.
results = {run.result(index)[0] for index in indexes}
if len(results) == 1:
return results.pop()
return _redact_log_text_union(head, run.unmatched, pipelines)
class _PipelineRun:
"""The pipelines of *pipelines* (by default :data:`_LOG_TEXT_PIPELINES`)
on one text.
Each pipeline is a pair: the name steps of its first pass and those of
its second, or ``None`` for a pipeline with no second pass (no control
characters stripped). Steps several pipelines share on the same text
(the shape steps of the first pass, the bare names, a second pass
reached from the same first pass) run once. *tracked* carries each
character's origin along (see :func:`_substitute_tracked`); otherwise
origins are ``None``. An untracked run records each step that matched
nothing on a text (:attr:`unmatched`); a tracked run given that set
skips those steps, which leave both the text and its origins unchanged.
"""
def __init__(
self,
head: str,
*,
tracked: bool,
unmatched: Optional[set] = None,
pipelines: Optional[tuple] = None,
credential_spans: Optional[list] = None,
origins: Optional[list] = None,
) -> None:
self._tracked = tracked
self._pipelines = (
_LOG_TEXT_PIPELINES if pipelines is None else pipelines
)
self._memo: dict = {}
self.unmatched: set = set() if unmatched is None else unmatched
self._credential_spans = credential_spans
if tracked and origins is None:
origins = list(range(len(head)))
self._shared = self._run((head, origins), _SHAPE_STEPS)
@property
def shaped(self) -> str:
"""The text after :data:`_SHAPE_STEPS`, where every first pass
starts."""
return self._shared[0]
def _cached(self, key: tuple, state: tuple, compute) -> tuple:
# Keyed by text; reused only for equal origins, so two pipelines
# that reach the same text from different characters stay apart.
entries = self._memo.setdefault(key, [])
for origins, result in entries:
if origins is state[1] or origins == state[1]:
return result
result = compute()
entries.append((state[1], result))
return result
def _substitute(self, step: tuple, state: tuple) -> tuple:
pattern, replacement, parts = step
if self._tracked:
if (state[0], step) in self.unmatched:
return state
if self._credential_spans is not None:
self._collect_credential_spans(step, state)
return _substitute_tracked(pattern, parts, *state)
text, count = pattern.subn(replacement, state[0])
if not count:
self.unmatched.add((state[0], step))
return text, None
def _collect_credential_spans(self, step: tuple, state: tuple) -> None:
collected = self._credential_spans
if collected is None:
return
pattern, replacement, _parts = step
groups = _LOG_PATCHER_BARE_GROUPS.get(pattern)
if groups is None:
return
name_group, value_after = groups
text, origins = state
spans = []
for match in pattern.finditer(text):
rendered = (
replacement(match)
if callable(replacement)
else match.expand(replacement)
)
if rendered == match.group():
continue
name = [
offset
for offset in origins[
match.start(name_group) : match.end(value_after)
]
if offset >= 0
]
value = [
offset
for offset in origins[match.end(value_after) : match.end()]
if offset >= 0
]
if name and value:
spans.append((min(name), min(value), max(value) + 1))
if spans:
collected.append(spans)
def _run(self, state: tuple, steps: tuple) -> tuple:
for step in steps:
# Reuse identical steps reached through different pipelines.
# _cached also checks origins before reusing a tracked result.
state = self._cached(
(state[0], step),
state,
lambda: self._substitute(step, state),
)
return state
def first_pass(self, index: int) -> tuple:
return self._run(self._shared, self._pipelines[index][0])
def result(self, index: int) -> tuple:
state = self.first_pass(index)
second = self._pipelines[index][1]
if second is None:
return state
if self._tracked:
stripped = self._cached(
(state[0], "strip"),
state,
lambda: _strip_control_chars_tracked(*state),
)
else:
stripped = (strip_control_chars(state[0]), None)
return self._run(stripped, _SHAPE_STEPS + second)
_LOG_TEXT_PIPELINE_INDEXES = range(len(_LOG_TEXT_PIPELINES))
def _pipeline_credential_spans(text: str) -> list:
"""Map later name/value readings back to the original input."""
spans: list = []
run = _PipelineRun(text, tracked=True, credential_spans=spans)
for index in _LOG_TEXT_PIPELINE_INDEXES:
run.result(index)
# A plain query replacement can expose a name that the original URL
# made part of another value. Cover the later log-pipeline readings
# after that replacement too, before extending across line breaks or
# quoted values would remove the newly exposed name.
pieces: list[str] = []
origins: list[int] = []
position = 0
for match in _URL_QUERY_STRING_RE.finditer(text):
pieces.append(text[position : match.start()])
origins.extend(range(position, match.start()))
replacement = _restore_query_wrapper(match)
pieces.append(replacement)
origins.extend(range(match.start(), match.start(2)))
origins.extend([-1] * len(_URL_QUERY_MARKER))
if len(replacement) > len(match.group(1)) + 1 + len(_URL_QUERY_MARKER):
origins.append(match.end() - 1)
position = match.end()
if pieces:
pieces.append(text[position:])
origins.extend(range(position, len(text)))
projected = "".join(pieces)
if projected != text:
run = _PipelineRun(
projected,
tracked=True,
credential_spans=spans,
origins=origins,
)
for index in _LOG_TEXT_PIPELINE_INDEXES:
run.result(index)
return spans
def _redact_log_text_union(
head: str, unmatched: set, pipelines: Optional[tuple] = None
) -> str:
"""Every pipeline of *pipelines* (by default :data:`_LOG_TEXT_PIPELINES`),
with origins tracked.
Each character of a pipeline's result is followed back to the position
of *head* it came from (see :func:`_substitute_tracked`). A position
that some pipeline's result no longer holds was redacted (or stripped)
there; the first pipeline's result is returned with every run of such
characters replaced by one ``[REDACTED]``. A pipeline whose result is
the same text as the first's adds nothing and is skipped. *unmatched*
is :attr:`_PipelineRun.unmatched` of an untracked run on *head*.
"""
run = _PipelineRun(
head, tracked=True, unmatched=unmatched, pipelines=pipelines
)
kept_text, kept_origins = run.result(0)
removed = bytearray(len(head))
for index in range(1, len(run._pipelines)):
text, origins = run.result(index)
if text == kept_text:
# Prints exactly what the kept text prints.
continue
survived = bytearray(len(head))
for origin in origins:
if origin >= 0:
survived[origin] = 1
for origin in kept_origins:
if origin >= 0 and not survived[origin]:
removed[origin] = 1
out: list = []
in_run = False
width = len(REDACTION_TEXT)
for position, (character, origin) in enumerate(
zip(kept_text, kept_origins)
):
if origin >= 0 and removed[origin]:
# A run next to a replacement the kept pipeline made already
# reads ``[REDACTED]``; do not double it.
if not in_run and not "".join(out[-width:]).endswith(
REDACTION_TEXT
):
out.append(REDACTION_TEXT)
in_run = True
continue
if (
in_run
and out[-1] is REDACTION_TEXT
and kept_text.startswith(REDACTION_TEXT, position)
and max(kept_origins[position : position + width]) < 0
):
out.pop()
in_run = False
out.append(character)
return "".join(out)
def _type_is_one_of(kind: Any, types: tuple) -> bool:
"""True when *kind* is one of *types* (by identity).
``kind in types`` compares with ``==``, which a metaclass ``__eq__`` of
an application class can answer ``True`` for ``str`` or ``list``.
"""
return any(kind is candidate for candidate in types)
# Credential shapes the log-record path redacts on top of the passes above.
# They run ONLY as the last step of :func:`redact_log_record`'s message and
# ``extra`` string redaction (:func:`_redact_log_record_text`), its rendered
# exception fields (:func:`_redact_exception_text`), and the exception prefix
# of a database log row (``utilities.log_utils._exception_context``), whose
# output goes straight to the sinks: no pass of
# ``sanitize_error_message``, ``scrub_error``, the client helpers or
# :func:`redact_log_message` ever runs over what these patterns write. Those
# functions do not apply them, so their output is what it was before these
# patterns existed. These patterns only replace part of the text they are
# given with a ``[REDACTED]`` marker; whatever the earlier passes redacted
# stays redacted, so where they run they can hide more, never less.
#
# Their value boundaries are conservative, and what may follow a value is an
# allowlist. A value stops before a credential label (``_VALUE_STOP``; that
# label is then matched on its own) and before a URL scheme, and it is
# redacted only when what follows it ends a value in its syntax: one of the
# structural followers listed below for its kind, after optional whitespace
# that is not a line break, or a line break, or the end of the text
# (``_cuts_a_value``, ``_API_KEY_FOLLOWER_RE``, ``_DSN_HOST_RE``). Anything
# else -- a word after a space, another quote, a backslash, an opening
# bracket, or a symbol a secret may contain (``&``, ``!``, ``$``, ``/``,
# ``.``, ``@`` ...) -- means the value may go on past the match, so the
# text is left exactly as the earlier passes left it: replacing only part
# of a value would print the rest of it behind a marker that makes the
# line look redacted. A
# quoted value holding the other kind of quote, an unquoted value that
# starts with a string prefix before a quote (``_UNQUOTED_PREFIX_STOP``) and
# one that leaves a bracket it opened unclosed are left as they were too.
#
# What no allowlist can tell apart: a secret written raw into a quoted or
# unquoted slot (JSON and a repr escape a quote inside a string; a
# hand-built ``f"{{'password': '{pw}'}}"`` does not) that itself holds a
# quote or a value end followed by a structural follower. ``'ab',cd'`` or
# ``{'password': ab,cd}`` has exactly the shape of the value ``'ab'`` (or
# ``ab``) followed by another item, so the part after the follower prints
# behind the marker. The same holds for a DSN password that holds an '@'
# followed by what reads as a host and its end (``_DSN_HOST_RE``).
#
# The structural followers, by the kind of value they follow. After a
# quoted value's closing quote (a JSON or repr string, a quoted parameter):
# ``,`` the next item of an object, a list, a call or a parameter list;
# ``}``, ``]`` and ``)`` the end of an object, a list, a call or a tuple;
# ``:`` the separator after an item in a ``name: value`` listing
# (``f"{key!r}: {value!r}: {reason}"``); ``;`` the next parameter or cookie
# attribute (``password="..."; Path=/``); ``&`` the next query or form
# parameter; ``#`` a URL fragment or a trailing comment.
_QUOTED_VALUE_FOLLOWERS = ",}]):;&#"
# After an unquoted value of a quoted key (JSON or a repr, where an unquoted
# value is a number, a literal, a name or a call): ``,`` the next item, and
# ``}`` or ``]`` the end of the object or list. Nothing else ends such a
# value: ``&`` and ``#`` never do in JSON or a repr, the run takes ``;``,
# ``:`` and ``)`` in (a call ends at its own ``)``), and a word after a
# space means the value may be several words (``Basic dXNl...``,
# ``correct horse battery staple``).
_KEY_VALUE_FOLLOWERS = ",}]"
# After an unquoted ``;``/``,`` parameter value (matrix parameters, joined
# lists, call arguments): ``,`` and ``;`` the next parameter, and ``)``,
# ``]`` and ``}`` the end of the call, list or object the parameters are
# in. ``&`` and ``#`` belong to ``?``/``&`` query syntax (its separator
# and the fragment), not to these parameters, so a value that stops at one
# may go on past it (``;key=ab&cd``), and a word after a space means the
# value may be several words.
_PARAM_VALUE_FOLLOWERS = ",;)]}"
# After an ``Api-Key:`` header value (a run of the header pattern's token
# class, below): ``,``, ``;``, ``)``, ``]`` and ``}`` end the list, call or
# object the header sits in, and a word after a space means the value may
# be several words, as above. A quote that closes the string the header
# sits in counts too, when one of ``_QUOTED_VALUE_FOLLOWERS``, a line break
# or the end of the text follows it (``['Api-Key: ...', ...]``), and so
# does ``://`` straight after the run: a URL glued to the key, whose scheme
# letters the run took in.
_API_KEY_VALUE_FOLLOWERS = ",;)]}"
def _followers_pattern(followers: str) -> str:
"""Matches at the end of a value only when the value ends there:
optional whitespace other than a line break, then one of *followers*,
a line break (``\\n``, ``\\r``) or the end of the text."""
return r"[^\S\r\n]*+(?:[" + re.escape(followers) + r"\r\n]|\Z)"
_QUOTED_VALUE_FOLLOWER_RE = re.compile(
_followers_pattern(_QUOTED_VALUE_FOLLOWERS)
)
_KEY_VALUE_FOLLOWER_RE = re.compile(_followers_pattern(_KEY_VALUE_FOLLOWERS))
_PARAM_VALUE_FOLLOWER_RE = re.compile(
_followers_pattern(_PARAM_VALUE_FOLLOWERS)
)
_API_KEY_FOLLOWER_RE = re.compile(
"://|"
+ _followers_pattern(_API_KEY_VALUE_FOLLOWERS)
+ r"|[^\S\r\n]*+[\"'](?="
+ _followers_pattern(_QUOTED_VALUE_FOLLOWERS)
+ ")"
)
# After the '@' that ends a would-be DSN's userinfo: a host (a name or an
# IPv4 address, an IPv6 literal in brackets, or a ``${VAR}`` placeholder)
# followed by what ends a host: whitespace, ``:`` before a port, ``/``,
# ``?``, ``#``, a quote, ``<``, ``>``, ``,``, ``;``, ``)``, ``]``, ``}``, a
# backslash or the end of the text. Anything else after the '@' (another
# '@', a symbol, a space) means the password may hold an unescaped '@' and
# go on past it.
_DSN_HOST_RE = re.compile(
r"(?:[A-Za-z0-9][A-Za-z0-9.\-_~%]*+|\[[0-9A-Za-z:.%]++\]|\$\{\w++\})"
r"(?=[\s:/?#\"'<>,;)\]}\\]|\Z)"
)
# Credential parameter names for the ``;``/``,`` parameter pattern below:
# the same names the ``?``/``&`` query-parameter pattern above redacts.
_CREDENTIAL_PARAM_NAMES = (
r"api[_-]?key|access[_-]?token|refresh[_-]?token|id[_-]?token|"
r"auth[_-]?token|session[_-]?token|secret[_-]?key|bearer[_-]?token|"
r"subscription[_-]?key|client[_-]?secret|api[_-]?secret|"
r"app[_-]?secret|private[_-]?key|"
r"key|token|secret|password|passwd|pwd"
)
# A credential label: a ``name=`` that ``redact_sensitive_assignments``
# redacts (a ``LOG_SENSITIVE_KEYS`` name, optional whitespace before the
# ``=``) or that the parameter pattern below redacts
# (``_CREDENTIAL_PARAM_NAMES``).
_CREDENTIAL_LABEL = (
r"(?:(?i:" + _LOG_SENSITIVE_NAME_PATTERN + r")\s*="
r"|(?i:" + _CREDENTIAL_PARAM_NAMES + r")=)"
)
# Checked before each character of a value matched below. A credential
# label is found only after a character outside ``[\w.-]`` (the assignment
# pass's left boundary; the ``,``/``;`` anchors of the parameter pattern),
# so the value stops before such a character when a label follows it: the
# label and its anchor stay as they were, legible, and the label's own
# value is matched separately.
_LABEL_STOP = "".join((r"(?![^\w.\-]", _CREDENTIAL_LABEL, r")"))
# At the first character of a value: no label there either.
_NO_LABEL = "".join((r"(?!", _CREDENTIAL_LABEL, r")"))
# Also checked before each character: a value stops before a URL scheme
# that starts a run of scheme characters (a letter, then letters, digits,
# ``+``, ``.`` or ``-`` reaching ``://`` within 32 characters), so a URL
# after a value stays legible; the URL-credential patterns have already
# redacted its userinfo.
_SCHEME_STOP = r"(?!(?<![A-Za-z0-9+.\-])[A-Za-z][A-Za-z0-9+.\-]{0,31}://)"
_VALUE_STOP = _LABEL_STOP + _SCHEME_STOP
# A name the assignment pass redacts, at the start of its ``name=``.
_ASSIGNMENT_NAME = "".join((r"(?:(?i:", _LOG_SENSITIVE_NAME_PATTERN, r")\s*=)"))
# Quoted keys (JSON, Python repr) name a credential with the names an
# ``extra`` key is redacted under: ``LOG_SENSITIVE_KEYS``, through
# ``_LOG_SENSITIVE_NAME_PATTERN`` (``_`` also matching ``-`` or nothing), so
# the message text and the ``extra`` keys cannot drift apart. As for an
# ``extra`` key (``_is_sensitive_log_key``: the last dotted segment, equal
# to a name or ending in ``_`` and the name), the name may follow any
# prefix that ends in a ``_``, ``.`` or ``-`` (``_QUOTED_KEY_PREFIX``):
# ``"_api_key"``, ``"db__password"``, ``"csrf_token"``, the request-header
# dicts the search engines send (``'X-API-KEY'``, ``'x-api-key'``,
# ``'X-Api-Key'``, ``'X-Subscription-Token'``), settings dumps
# (``"openai_api_key"``, ``"Ocp-Apim-Subscription-Key"``), the project's
# dotted settings keys (``"llm.openai.api_key"``) and its environment
# variables (``"LDR_SEARCH_ENGINE_WEB_TAVILY_API_KEY"``). A ``-`` before the
# name counts too, which an ``extra`` key does not need. The name must end
# the key, so ``"secret_name"`` or ``"password_hint"`` is not a credential
# key; a name glued to a camelCase prefix (``"dbPassword"``) is not one
# either, as for an ``extra`` key. Any key that ends in one is, so
# ``"show_password"`` or ``"use_secret"`` is redacted unless its value is a
# bare literal (over-redaction only); ``"requires_api_key"``, which
# ``_is_sensitive_log_key`` exempts, is redacted too when its value is not
# a bare literal.
#
# The cost stays linear in the key: the prefix cannot contain a quote, a
# backslash or a newline, so the scan from one opening quote ends before
# the next one starts; the prefix is lazy, so a name is tried once at each
# position, and the atomic group commits to the first position whose name
# reaches the closing quote (every decomposition ends at that same quote),
# so the value is matched once per key.
_QUOTED_CREDENTIAL_KEY_NAMES = _LOG_SENSITIVE_NAME_PATTERN
_QUOTED_KEY_PREFIX = r"(?:[^\"'\n\\]*?[_.\-])?"
# An unquoted value that is a whole JSON/Python literal (``true``,
# ``False``, ``None``, ``null``) is not a secret; with prefixed names
# (``"requires_api_key": True``) it is common, so it is left legible.
_UNQUOTED_LITERAL_STOP = r"(?!(?:true|false|none|null)(?![^\s,}\]&#\"'\\]))"
# A label inside a would-be DSN password, with the assignment pass's own
# left boundary.
_LABEL_IN_USERINFO_RE = re.compile(r"(?<![\w.?&-])" + _CREDENTIAL_LABEL)
# A quoted value for the parameter and quoted-key patterns below: to its
# closing quote (or the end of the text), with no credential label or URL
# scheme inside, and no quote character of the other kind (escaped or
# not). ``_VALUE_STOP`` is checked before every character, the one after a
# backslash included: a backslash escape is two characters, and checking
# only before the backslash would let an escaped separator (a backslash and
# a comma) hide a quoted label's ``password =`` after it, so the value would
# run on to that label's own opening quote and take the label with it.
_QUOTED_VALUE = "".join(
(
r"\"",
_NO_LABEL,
r"(?:",
_VALUE_STOP,
r"(?:\\",
_VALUE_STOP,
r"[^'\n]|[^\"'\\]))*(?:\"|\\?$)",
r"|'",
_NO_LABEL,
r"(?:",
_VALUE_STOP,
r"(?:\\",
_VALUE_STOP,
r"[^\"\n]|[^'\"\\]))*(?:'|\\?$)",
)
)
# A quoted value as above, after an optional Python string prefix
# (``b'...'``, ``rb"..."``, ``f'...'``), so a bytes value or a bytes-keyed
# repr (``{b'password': b'...'}``) is redacted whole.
# ``_redacted_value`` keeps the prefix.
_PREFIXED_QUOTED_VALUE = "".join(
(r"(?:[bruf]{1,2}(?=[\"']))?(?:", _QUOTED_VALUE, r")")
)
# At the first character of an unquoted value: not a string prefix before a
# quote. Matched as an unquoted run, ``b'secret'`` would stop at the quote,
# so the marker would replace only the ``b``; it is matched whole by
# ``_PREFIXED_QUOTED_VALUE`` instead, or left as it is when that refuses it.
_UNQUOTED_PREFIX_STOP = r"(?![bruf]{1,2}[\"'])"
# An unquoted value of a quoted key that is written as an ``Authorization``
# header's is: a scheme word (the schemes ``sanitize_error_message``'s
# ``Authorization`` pattern names) and one credential token. It is taken as
# one value, whatever the key, so the marker replaces both
# (``{'Authorization': Basic dXNl...}``). Anything but a structural
# follower after the token (a second token, the ``username=...,
# response=...`` list of a ``Digest`` value) leaves the text as it was.
_SCHEME_CREDENTIAL = (
r"(?:basic|bearer|digest|negotiate|apikey|token)"
r"[^\S\r\n]++[A-Za-z0-9\-._~+/]++=*"
)
# Brackets an unquoted value may open and the ones that close them.
_VALUE_BRACKETS = (("(", ")"), ("[", "]"), ("{", "}"))
def _cuts_a_value(
text: str,
start: int,
end: int,
unquoted_followers: "re.Pattern[str]",
) -> bool:
"""True unless ``text[start:end]``, a value matched below, ends where
its syntax ends a value, so that replacing it cannot print the rest of
the value behind a ``[REDACTED]`` marker that makes the line look
redacted. The callers then return the text unchanged.
An allowlist: the value is taken as whole only when what follows it
matches ``_QUOTED_VALUE_FOLLOWER_RE`` (after a quoted value's closing
quote, or its end when it runs to the end of the text) or
*unquoted_followers* (after an unquoted run): a structural follower for
that kind of value, after optional whitespace that is not a line
break, or a line break, or the end of the text. Any other character
refuses it: a quote (``'''secret'''``, whose first two quotes are an
empty string, or a quote that opens a string belonging to the value,
``SecretStr('...')``), a backslash, a word after a space, or a symbol a
secret may hold (``'Pa'$$w0rd'``, ``hunter2&rest``). An unquoted value
that leaves a bracket it opened unclosed is not whole either, whatever
follows it: a nested container or a call stops at its first separator
(``(1, '...')``, ``{0: '...'}``, ``Foo(a, '...')``), so the rest of it
is outside the match.
"""
value = text[start:end]
if _redacted_value(value) != "[REDACTED]":
# A quoted value, string prefix and all (``b'...'``).
return _QUOTED_VALUE_FOLLOWER_RE.match(text, end) is None
if unquoted_followers.match(text, end) is None:
return True
return any(
value.count(opener) > value.count(closer)
for opener, closer in _VALUE_BRACKETS
)
def _redacted_value(value: str) -> str:
"""``[REDACTED]`` in place of a value matched by the patterns below.
A quoted value keeps its quotes (its closing quote only when it has
one) and its string prefix (``b'[REDACTED]'``), so the text after it
is still delimited as it was.
"""
stripped = value.lstrip("bBrRuUfF")
prefix = value[: len(value) - len(stripped)]
quote = stripped[:1]
if quote not in ('"', "'") or len(prefix) > 2:
return "[REDACTED]"
body = stripped[1:]
closed = False
if body.endswith(quote):
inner = body[:-1]
closed = (len(inner) - len(inner.rstrip("\\"))) % 2 == 0
return prefix + quote + "[REDACTED]" + (quote if closed else "")
def _redact_slash_dsn(match: "re.Match[str]") -> str:
"""Replacement for the slash-DSN pattern below.
A password run that holds a credential label (``/password=...``,
``;token=...``) is not userinfo but a path running into that label's
value, up to an '@' inside it. Redacting it would remove the label the
parameter pattern below needs and print the value's tail, so the text
is returned unchanged for it. Checked here rather than inside the
regex: a per-character label lookahead made the capped scan several
times slower.
The '@' is taken as the end of the userinfo only when a host follows
it and ends where a host ends (``_DSN_HOST_RE``). Anything else, such
as a second '@' (``u:pa/ss@word@host``), a symbol or a space after it,
means the password may hold an unescaped '@', so which '@' ends the
userinfo cannot be told: the text is returned unchanged rather than
printing the password's tail behind the marker.
"""
if _LABEL_IN_USERINFO_RE.search(match.string, match.start(4), match.end(4)):
return match.group(0)
if _DSN_HOST_RE.match(match.string, match.end()) is None:
return match.group(0)
return match.group(1) + match.group(2) + "[REDACTED]:[REDACTED]@"
def _redact_api_key_header(match: "re.Match[str]") -> str:
"""Replacement for the ``Api-Key:`` header pattern below.
The text is returned unchanged unless the value ends where a header
value ends (``_API_KEY_FOLLOWER_RE``).
"""
if _API_KEY_FOLLOWER_RE.match(match.string, match.end()) is None:
return match.group(0)
return match.group(1) + "[REDACTED]"
def _redact_credential_param(match: "re.Match[str]") -> str:
"""Replacement for the ``;``/``,`` credential-parameter pattern below.
The text is returned unchanged unless the matched value ends where a
value ends (:func:`_cuts_a_value`, with ``_PARAM_VALUE_FOLLOWERS`` after
an unquoted value).
"""
group = 3 if match.group(3) is not None else 5
if _cuts_a_value(
match.string,
match.start(group),
match.end(group),
_PARAM_VALUE_FOLLOWER_RE,
):
return match.group(0)
return (
match.group(1)
+ (match.group(2) or match.group(4))
+ "="
+ _redacted_value(match.group(group))
)
def _redact_quoted_key(match: "re.Match[str]") -> str:
"""Replacement for the quoted-credential-key pattern below.
The text is returned unchanged unless the matched value ends where a
value ends (:func:`_cuts_a_value`, with ``_KEY_VALUE_FOLLOWERS`` after
an unquoted value).
"""
if _cuts_a_value(
match.string, match.start(4), match.end(4), _KEY_VALUE_FOLLOWER_RE
):
return match.group(0)
return (
match.group(1)
+ match.group(2)
+ match.group(1)
+ match.group(3)
+ _redacted_value(match.group(4))
)
_ADDITIONAL_CREDENTIAL_PATTERNS: list[
tuple[re.Pattern[str], Union[str, Callable[["re.Match[str]"], str]]]
] = [
# Slash-bearing DSN passwords — base64/base64-ish generated secrets
# pasted into a connection string routinely contain an unescaped '/',
# which the URL-credentials pattern in ``_CREDENTIAL_PATTERNS`` refuses
# by design. Admitting the slash makes one URL shape look like
# userinfo: ``host:port/path...@...``
# (``https://api.example.com:443/users/[email protected]``), where the
# "user" is really the host and the "password" a port followed by a
# path. A password that is a run of digits followed by ``/``, ``?`` or
# ``#`` (or that starts with one of them) is that shape, so it is
# refused; the user part also refuses ``[``/``]`` so an IPv6 literal
# host is never read as userinfo. Any host is accepted after the '@'
# (``@postgres:5432``, ``@localhost``, ``@10.0.0.5`` are the usual
# container DSNs). Known gap: a real password that is digits followed
# by '/' is left alone.
#
# Neither group crosses a JSON, list or query boundary: the user refuses
# ``"'<>,`` and ``?#&=;``, the password ``"'<>,`` and ``?#&``. A
# match whose password holds a credential label (``/password=``,
# ``;token=``, ``=api_key=``), or whose '@' is not followed by a host
# and a host's end (``_DSN_HOST_RE``; ``u:pa/ss@word@host``, where the
# password may hold an unescaped '@'), is left alone
# (``_redact_slash_dsn``). Known gap: a slash-bearing password that
# also contains one of those characters is not redacted by this
# pattern, nor one that contains an '@' unless what follows that '@'
# reads as a host and its end (``u:pa/ss@word/x@host``), which cannot
# be told from a DSN followed by more text, so the rest of such a
# password prints behind the marker.
#
# Both groups are length-capped: an uncapped password class that may
# run through '/' made matching quadratic on a whitespace-free run of
# ``scheme://:`` with no '@' (seconds of GIL-holding CPU at ~30k
# chars). The caps bound the work per start position, which keeps the
# scan linear; a longer user or password is not redacted by this
# pattern.
#
# Each ``://`` is also tried once, not once per scheme start: a run of
# scheme characters is entered only at its first character (the
# lookbehind), a lazy prefix (group 1, put back by the replacement)
# skips to the leftmost position where a scheme reaches the ``://`` --
# where an unanchored search would have started -- and the atomic group
# commits to that scheme. Every later start before the same ``://``
# would retry the identical user/password tail, so dropping them changes
# no result; without this, a 32-letter scheme repeated with ``://:``
# ran up to 31 capped tails per ``://`` (about 6x the cost of the
# URL-credentials pattern on the same run). The user and password
# quantifiers are possessive: the character that must follow each is
# outside its class, so giving characters back can never succeed.
(
re.compile(
r"(?<![A-Za-z0-9+.\-])"
r"(?>([A-Za-z0-9+.\-]*?)([A-Za-z][A-Za-z0-9+.\-]{1,31}://))"
r"([^:\s/@\[\]\"'<>,?#&=;]{0,128}+):(?![0-9]*[/?#])"
r"([^@\s\"'<>,?&#]{1,256}+)@"
),
_redact_slash_dsn,
),
# API-key header spellings the ``x-api-key`` pattern in
# ``_CREDENTIAL_PATTERNS`` misses — the unprefixed ``Api-Key:`` form is
# what Anthropic, Azure Cognitive Services and several search APIs
# send, plus ``x-api_key``/``x-apikey``. Like the ``x-api-key``
# pattern, the label is a strong anchor, so any value of >=16
# characters is redacted regardless of shape; short values like
# "invalid"/"missing" stay intact. The unprefixed forms match only the
# ``:`` header-dump shape — an ``=`` assignment is prose (``api_key=...``
# mid-sentence is not redacted by this pattern). The value stops where
# a credential label begins (``KEY/client_secret=...``) or a URL scheme
# starts a run (``api_key: postgresql+asyncpg://...``), and is not
# redacted when fewer than 16 characters come before that. It is
# redacted only when it ends where a value ends
# (``_API_KEY_FOLLOWER_RE``): one of ``_API_KEY_VALUE_FOLLOWERS``, a
# line break or the end of the text, after optional whitespace; a
# quote that closes the string the header sits in; or ``://`` glued to
# it. A key followed by a word after a space, by ``&``, ``!``, ``:``,
# a ``/`` before a label or any other character its class lacks is
# left as the earlier passes left it.
#
# Known gap: where a key glued straight to a scheme (``KEYhttps://``)
# ends and the scheme begins cannot be told apart. When the run of
# scheme characters starts at a letter within 32 characters of
# ``://``, the value stops there, so the key prints, all of it, since
# what follows the stop is a letter and not a header value's end. A run
# that starts with a digit or is longer than that is taken whole,
# scheme included.
(
re.compile(
r"(?i)((?:x-api[_-]?key\s*[:=]|api[_-]?key\s*:)\s*)"
+ _NO_LABEL
+ r"(?:"
+ _VALUE_STOP
+ r"[A-Za-z0-9\-._~+/]){16,}+=*"
),
_redact_api_key_header,
),
# Credential parameters after ``;`` (matrix parameters, ``;token=...``)
# or ``,`` (joined lists, ``,password=...``), with the ``?``/``&``
# parameter pattern's names. The value is a quoted string, optionally
# string-prefixed (``_PREFIXED_QUOTED_VALUE``), or an unquoted run that
# does not start with a string prefix before a quote
# (``_UNQUOTED_PREFIX_STOP``). For a name the assignment
# pass also redacts (``password``, ``token``, ``api_key``, ...) an
# unquoted run must reach whitespace (or the end of the text) without a
# ``&``, ``#``, quote, backslash, label or scheme on the way; any other
# value of such a name is left as the earlier passes left it. Any other
# name (``key``) takes a run that stops at whitespace, ``&``, ``#``, a
# quote, a backslash or a ``,``/``;`` starting a credential label
# (``,key=abc,password="example secret"`` redacts both). Either is
# redacted only when what follows it ends a value (``_cuts_a_value``):
# one of ``_QUOTED_VALUE_FOLLOWERS`` after a quoted value, one of
# ``_PARAM_VALUE_FOLLOWERS`` after an unquoted one, after optional
# whitespace, or a line break or the end of the text; so a run that
# stops at a space before a word (``;key=abc def``), at a quote, at a
# backslash, or at a label after any character but ``,``/``;`` is not
# redacted, nor an unquoted value that leaves a bracket it opened
# unclosed (``,key=[b'...']``). Such a value is left as the earlier
# passes left it.
(
re.compile(
r"(?i)([,;])(?:"
r"(?=" + _ASSIGNMENT_NAME + r")(" + _CREDENTIAL_PARAM_NAMES + r")="
r"("
+ _PREFIXED_QUOTED_VALUE
+ r"|(?![\"'])"
+ _UNQUOTED_PREFIX_STOP
+ _NO_LABEL
+ r"(?:"
+ _VALUE_STOP
+ r"[^\s&#\"'\\])++(?!\S))"
r"|(?!" + _ASSIGNMENT_NAME + r")(" + _CREDENTIAL_PARAM_NAMES + r")="
r"("
+ _PREFIXED_QUOTED_VALUE
+ r"|"
+ _UNQUOTED_PREFIX_STOP
+ _NO_LABEL
+ r"(?:[,;]"
+ _NO_LABEL
+ r"|"
+ _VALUE_STOP
+ r"[^\s,;&#\"'\\])++)"
r")"
),
_redact_credential_param,
),
# Quoted credential keys in JSON or a Python repr ("password": "...",
# {'api-key': '...'}, {'X-API-KEY': '...'}, "llm.openai.api_key": ...).
# The value is a quoted string, optionally string-prefixed
# (``_PREFIXED_QUOTED_VALUE``: ``b'...'``), a scheme word and one
# credential token (``_SCHEME_CREDENTIAL``: ``Basic dXNl...``), or an
# unquoted run (to whitespace, ``,``, ``}``, ``]``, ``&``, ``#``, a
# quote, a backslash, a credential label or a scheme); an unquoted run
# never starts with a quote or ``[``, so an already-redacted
# ``[REDACTED]`` value is left as it is, nor with a string prefix before
# a quote (``_UNQUOTED_PREFIX_STOP``), and is never a bare
# ``true``/``false``/``None``/``null``. It is redacted only when what
# follows it ends a value (``_cuts_a_value``): one of
# ``_QUOTED_VALUE_FOLLOWERS`` after a quoted value, one of
# ``_KEY_VALUE_FOLLOWERS`` (``,``, ``}``, ``]``) after an unquoted one,
# after optional whitespace, or a line break or the end of the text; an
# unquoted value that leaves a bracket it opened unclosed (``(b'...',)``,
# ``bytearray(b'...')``, ``{0: '...'}``) is not whole either. Any other
# value is left as the earlier passes left it: ``'''...'''``,
# ``'Pa'$$w0rd'``, ``hunter2&rest``, ``correct horse battery staple``,
# ``ApiKey(key=...)``.
(
re.compile(
r"(?i)([\"'])(?>("
+ _QUOTED_KEY_PREFIX
+ r"(?:"
+ _QUOTED_CREDENTIAL_KEY_NAMES
+ r"))\1)(\s*:\s*)"
r"("
+ _PREFIXED_QUOTED_VALUE
+ r"|"
+ _SCHEME_CREDENTIAL
+ r"|(?![\"'\[])"
+ _UNQUOTED_PREFIX_STOP
+ _UNQUOTED_LITERAL_STOP
+ _NO_LABEL
+ r"(?:"
+ _VALUE_STOP
+ r"[^\s,}\]&#\"'\\])+)"
),
_redact_quoted_key,
),
]
def _redact_additional_shapes(message: str) -> str:
"""Apply ``_ADDITIONAL_CREDENTIAL_PATTERNS`` in order, as plain substitutions.
Used as the final redaction step for record text, rendered exception
fields, and the database log row's exception prefix. Callers bound the
text before applying these patterns.
"""
if not message:
return message
for pattern, replacement in _ADDITIONAL_CREDENTIAL_PATTERNS:
message = pattern.sub(replacement, message)
return message
def _redact_log_record_text(value: str) -> str:
""":func:`_redact_log_text`, then ``_ADDITIONAL_CREDENTIAL_PATTERNS``.
For the log record's message and its ``extra`` string leaves, whose
redacted text goes straight to the sinks. The text is cut to the bound
first, :func:`_redact_log_text` redacts that head, and the added
patterns run on its output, so they see what its passes left and can
only replace more of it; the omission marker is appended after them.
"""
value = _plain_text(value)
if not value:
return _exact_str(value)
# Cut once here so the added patterns see a bounded string and the
# omission marker stays outside their reach. ``head`` is within the
# bound, so ``_redact_log_text`` cuts nothing more and appends no
# marker of its own: its output is exactly its two passes over
# ``head``. Calling it (rather than repeating its passes) keeps any
# change to it in force on the log-record path.
head, marker = _truncate_for_log_redaction(value)
return _redact_additional_shapes(_redact_log_text(head)) + marker
def redact_for_log(value: str) -> str:
"""Run the log patcher's credential redaction on *value* alone.
The same two passes and the same ``_LOG_REDACTION_MAX_CHARS`` bound the
loguru patcher applies to every record (:func:`_redact_log_text`), for a
caller that is about to shorten *value* before it reaches a log line.
Shortening first can hide a credential from the patcher: a cut that drops
``?key=`` or the ``@`` after URL userinfo, but keeps part of the secret,
leaves text no credential pattern matches. Redacting first means any cut
afterwards only removes text the patcher already judged safe or already
replaced. Cost is bounded the same way as the patcher's.
"""
return _redact_log_text(value)
# How much of one value :func:`redact_and_bound_for_log` scans: at least
# this many characters, or four times the output cap if that is more, and
# never more than the patcher's own ``_LOG_REDACTION_MAX_CHARS``. The
# result keeps at most ``max_length`` characters, so scanning the full
# 32 KiB made one call cost ~50-100 ms on adversarial input (a run of
# spaces, or of ``https://a:b@ ``) for a 256-character result; 1,024
# characters cost a few milliseconds.
_LOG_BOUNDED_REDACTION_MIN_CHARS = 1_024
def bounded_redaction_chars(max_length: int) -> int:
"""How many characters of a value to redact for a *max_length* result."""
return min(
_LOG_REDACTION_MAX_CHARS,
max(_LOG_BOUNDED_REDACTION_MIN_CHARS, 4 * max_length),
)
def redact_head_for_log(value: str, max_chars: int) -> str:
""":func:`redact_for_log` on at most *max_chars* characters of *value*.
For a caller that shows only a short prefix of the result. The value
is cut first by the patcher's own rule
(:func:`_truncate_for_log_redaction` with *max_chars* as the bound:
only at a whitespace boundary, dropping the whole run the bound falls
in), then the kept head is redacted, and the rest is replaced by the
omitted-length marker. The patcher's own argument applies: a
credential token holds no boundary character, so the head never keeps
part of a token whose label or ``@`` was cut off, and text past the
cut is never shown. The cost is bounded by *max_chars*.
"""
if not value:
return value
head, marker = _truncate_for_log_redaction(value, max_chars)
return redact_for_log(head) + marker
def redact_and_bound_for_log(value: str, max_length: int) -> str:
""":func:`redact_for_log`, then :func:`sanitize_for_log` (strip + cap).
Use instead of plain ``sanitize_for_log`` for a value that a call site
previously logged in full, where the patcher saw (and redacted) the whole
value: truncating first would let it see less than it did before.
Only the first :func:`bounded_redaction_chars` characters (at least
1,024) are redacted, cut by the patcher's whitespace-boundary rule (see
:func:`redact_head_for_log`); the result keeps at most *max_length* of
them. A value whose first whitespace-free run reaches past that bound
is shown as the omitted-length marker only.
"""
return sanitize_for_log(
redact_head_for_log(value, bounded_redaction_chars(max_length)),
max_length,
)
def _is_sensitive_log_key(key: Any) -> bool:
"""True when *key* names a secret under settings redaction's rules.
Delegates to ``DataSanitizer.is_sensitive_setting`` with
:data:`LOG_SENSITIVE_KEYS`: the last dotted segment matches a name exactly
or as an underscore-delimited suffix (``openai_api_key``,
``db_password``), after invisible characters are removed from it. Message
text also recognizes prefixed and dotted assignments, and the additional
shape pass recognizes quoted credential keys. A ``str`` subclass key is
checked by its plain characters: its own ``rsplit``, ``lower`` or
``__eq__`` could otherwise hide a name. A key longer than
``_LOG_REDACTION_MAX_CHARS`` is treated as sensitive without scanning it,
keeping the cost per key bounded and masking its value.
"""
if not isinstance(key, str):
return False
if len(key) > _LOG_REDACTION_MAX_CHARS:
return True
return DataSanitizer.is_sensitive_setting(
_exact_str(key), sensitive_keys=LOG_SENSITIVE_KEYS
)
def _is_structural_extra_key(key: Any) -> bool:
"""True when *key* is one of the routing keys, as an exact ``str``.
A ``str`` subclass can hash and compare equal to ``"username"`` while
holding (and rendering) other text, so only an exact ``str`` passes
unredacted as a routing key.
"""
return type(key) is str and key in _LOG_STRUCTURAL_EXTRA_KEYS
def _is_empty_log_value(value: Any) -> bool:
"""``None`` or an empty str/list/dict (left readable, like DataSanitizer).
Type-checked rather than ``value in (None, "", [], {})`` so an object with
a raising ``__eq__`` cannot abort the redaction of the whole record.
Exact types only: a subclass can override ``__len__`` (or iterate items
its storage does not hold) to look empty under a sensitive key, so a
subclass is never "empty" and is masked like any other value.
"""
if value is None:
return True
return _type_is_one_of(type(value), (str, list, dict)) and len(value) == 0
# Nesting bound for ``extra`` values: a container at this depth or deeper
# (a value bound directly in ``extra`` is depth 1) becomes a placeholder.
_LOG_EXTRA_MAX_DEPTH = 32
def _scrub_log_key(key: Any) -> Any:
"""Scrub a dict key in ``extra`` the way a string value is scrubbed.
A ``str`` key is redacted (:func:`_redact_log_text`). ``None``, ``bool``,
``int`` and ``float`` keys cannot carry a credential and pass through. A
key whose text is fixed by its type (``bytes``, or a tuple or frozenset
of such values, see :func:`_is_stable_text_value`) is kept as is unless
its ``str()`` redacts differently, in which case the redacted text
replaces it. Any other key (an object with its own ``__str__`` or
``__repr__``, a ``str`` subclass) is always replaced by its checked,
redacted text: the sinks render the key again, and an object may render
a secret the second time, or (when its ``str()`` raised here) render
text that was never checked at all.
"""
if isinstance(key, str):
return _redact_log_text(key)
if key is None or _type_is_one_of(type(key), (bool, int, float)):
return key
try:
text = _exact_str(str(key))
printable = True
except Exception:
text = _unprintable_placeholder(key)
printable = False
redacted = _redact_log_text(text)
if printable and redacted == text and _is_stable_text_value(key):
return key
return redacted
def _unique_log_key(out: dict, key: Any, reserved: Any = ()) -> Any:
"""*key*, or ``key#2``, ``key#3``, ... when it is already taken.
Redaction can map two different keys to the same text (two URLs with
different passwords, or ``"a\\nb"`` and ``"ab"``); the second must not
overwrite the first. *reserved* holds keys that may not be produced at
all (the routing keys of the top-level ``extra``).
Keys are taken in the input's iteration order, so the first key to
produce a given text keeps it unsuffixed and later ones are suffixed; a
reserved key is never produced, even for the first key that redacts to
it (the caller keeps a routing key itself under its own name, whether
it comes before or after a key that redacts to it).
"""
if key not in out and key not in reserved:
return key
base = key if isinstance(key, str) else _safe_log_str(key)
n = 2
while f"{base}#{n}" in out or f"{base}#{n}" in reserved:
n += 1
return f"{base}#{n}"
def _scrub_log_bytes(value: Any) -> Any:
"""Scrub a ``bytes``/``bytearray`` ``extra`` leaf.
Only the first ``4 * _LOG_REDACTION_MAX_CHARS + 4`` bytes are decoded
(UTF-8, undecodable bytes become U+FFFD, which cannot join a credential
name the way a ``\\xNN`` escape's hex digits would), so the cost does not
grow with the value. A value that decodes to at most
``_LOG_REDACTION_MAX_CHARS`` characters is returned unchanged (a
``bytearray`` or ``bytes`` subclass as a plain copy of itself) unless
:func:`_redact_log_text` redacts something in it, in which case the
redacted text replaces it. A longer value is always replaced by its
redacted, truncated text, ending in a marker that gives its total size
in bytes.
"""
if type(value) is not bytes:
# A ``bytearray`` (or a ``bytes`` subclass) can change or render
# differently after the check; check and return a plain copy (read
# through the buffer, so a ``__bytes__`` override is not called).
value = (
bytearray(memoryview(value))
if isinstance(value, bytearray)
else bytes(memoryview(value))
)
limit = 4 * _LOG_REDACTION_MAX_CHARS + 4
text = bytes(value[:limit]).decode("utf-8", "replace")
head, marker = _truncate_for_log_redaction(text)
if not marker:
# Compared with the control-stripped text rather than ``text``:
# ``_redact_log_text`` always strips control characters, so only a
# difference beyond that strip is a redaction (``api_<U+001F>key=``
# included), while benign bytes holding a newline stay bytes.
redacted = _redact_log_text(text)
if redacted == strip_control_chars(text):
return value
return redacted
return (
_redact_log_text(head)
+ f" [... {len(value)} bytes in total, the rest omitted from log output]"
)
def _scrub_log_leaf(value: Any, _depth: int = 0, _active: Any = None) -> Any:
"""Recursively scrub credential shapes from an ``extra`` value.
Redacts by key *name* (a value under a sensitive key is masked whole)
AND by value *shape* (a credential embedded in a string leaf under any
key is scrubbed) — the two halves that a name-only or shape-only check
would each miss. Dict keys are scrubbed too (:func:`_scrub_log_key`);
sets and tuples come back as lists; a ``bytes``/``bytearray`` leaf is
handled by :func:`_scrub_log_bytes`. Other non-container leaves pass
through.
A container that contains itself (directly or further down) is rendered
as ``"<cycle>"`` and a container at depth :data:`_LOG_EXTRA_MAX_DEPTH`
or deeper as ``"<nested too deep>"``, so neither raises
``RecursionError`` and trips :func:`redact_log_record`'s fail-closed
fallback for the whole record.
"""
if isinstance(value, str):
return _redact_log_record_text(value)
if isinstance(value, (bytes, bytearray)):
return _scrub_log_bytes(value)
if not isinstance(value, (dict, list, tuple, set, frozenset)):
return value
if _depth >= _LOG_EXTRA_MAX_DEPTH:
return "<nested too deep>"
if _active is None:
_active = set()
marker = id(value)
if marker in _active:
return "<cycle>"
_active.add(marker)
try:
if isinstance(value, dict):
out: dict = {}
for k, v in value.items():
out[_unique_log_key(out, _scrub_log_key(k))] = (
REDACTION_TEXT
if _is_sensitive_log_key(k) and not _is_empty_log_value(v)
else _scrub_log_leaf(v, _depth + 1, _active)
)
return out
return [_scrub_log_leaf(v, _depth + 1, _active) for v in value]
finally:
_active.discard(marker)
def redact_log_extra(extra: Any) -> Any:
"""Redact secrets from a loguru record's structured ``extra`` mapping.
* Keys the sinks depend on for routing (``research_id``, ``username``,
``policy_audit``) pass through untouched -- as exact ``str`` keys only
(:func:`_is_structural_extra_key`); a ``str`` subclass key is checked
by its plain characters like any other key.
* A value under a sensitive key name (:data:`LOG_SENSITIVE_KEYS`, exact
or as an ``_``-delimited suffix such as ``openai_api_key``) is masked
whole (empty values are left readable, mirroring ``DataSanitizer``).
* Other keys are scrubbed for credential shapes like string values; a
key whose scrubbed form is a routing key or an earlier key gets a
``#2``/``#3`` suffix instead of overwriting it.
* Every other value is recursively scrubbed for credential *shapes*, so
a token/URL/query bound under an innocuous key (the family of bug the
name-based checks miss) is still caught. String values get the same
control-character strip as the message, between two redaction passes,
and then the same ``_ADDITIONAL_CREDENTIAL_PATTERNS``
(:func:`_redact_log_record_text`).
Returns a new dict; the input is not mutated. A non-dict ``extra`` is
returned unchanged.
"""
if not isinstance(extra, dict):
return extra
out: dict = {}
# ``extra`` itself counts as an enclosing container, so a value that
# refers back to the mapping is reported as a cycle at the first level.
active = {id(extra)}
for key, value in extra.items():
if _is_structural_extra_key(key):
out[key] = value
continue
safe_key = _unique_log_key(
out, _scrub_log_key(key), _LOG_STRUCTURAL_EXTRA_KEYS
)
if _is_sensitive_log_key(key) and not _is_empty_log_value(value):
out[safe_key] = REDACTION_TEXT
else:
out[safe_key] = _scrub_log_leaf(value, 1, active)
return out
def _unprintable_placeholder(obj: Any) -> str:
"""Stand-in text for an object whose ``str()`` raised or was not a str."""
return f"<unprintable {_type_name(type(obj))}>"
def _safe_log_str(obj: Any) -> str:
"""``str(obj)``, or a placeholder when that raises.
``str()`` raises ``TypeError`` itself when ``__str__`` returns a
non-str, so one guard covers both a raising and a mistyped ``__str__``.
Only the one node's text is replaced; the rest of the exception graph
and its traceback are still rendered. A ``__str__`` may return a ``str``
subclass, which can render differently later (its own ``__str__``,
``__format__`` or ``__repr__``); the result is always an exact ``str``.
"""
try:
return _exact_str(str(obj))
except Exception:
return _unprintable_placeholder(obj)
def _exact_str(text: str) -> str:
"""*text* as an exact ``str`` (a copy when it is a ``str`` subclass)."""
return text if type(text) is str else str.__str__(text)
def _plain_text(value: Any) -> Any:
"""*value* as an exact ``str`` when it is a ``str``; anything else as is.
Called before a ``not value`` shortcut: a ``str`` subclass can override
``__bool__`` or ``__len__`` to look empty while holding text, and would
then be returned unredacted. The copy's truth value and length are its
real characters'.
"""
return _exact_str(value) if isinstance(value, str) else value
# Line breaks and tabs that exception text keeps: ``\t``, ``\n`` and ``\r``
# are never drawn as nothing in a sink -- terminal, log file, and the DB log
# shown in the browser -- (a tab, a line break, or for ``\r`` a line break
# or, in a terminal, a return to the start of the line), so a credential
# name they split is not shown as one unbroken name, and a multi-line
# message keeps its lines.
_EXCEPTION_KEPT_BREAKS = "\t\n\r"
# Every other character ``strip_control_chars`` removes: zero-width and bidi
# format characters, and all other C0/C1 controls. That deliberately
# includes the ones ``str.isspace()`` calls whitespace -- ``\x1c``-``\x1f``,
# U+0085, ``\v``, ``\f``, U+2028 and U+2029. Terminals draw ``\x1c``-``\x1f``
# as nothing, browsers draw U+0085 and ``\v`` as nothing, and ``\f``,
# U+2028 and U+2029 are a line break in some sinks and nothing in others.
# Only a character that is a visible separator in every sink may stay, since
# any of these can hide a credential name (``api_<U+200B>key=``,
# ``tok<U+001F>en=``) or split URL userinfo exactly like a zero-width space.
# Derived from ``_UNSAFE_CHAR_RE`` so the two cannot drift apart.
_EXCEPTION_INVISIBLE_RE = re.compile(
"["
+ "".join(
re.escape(char)
for char in map(
chr,
[
*range(0xA0),
0x61C,
*range(0x2000, 0x2070),
0xFEFF,
],
)
if _UNSAFE_CHAR_RE.match(char) and char not in _EXCEPTION_KEPT_BREAKS
)
+ "]"
)
def _redact_exception_text(value: str) -> str:
"""Redact one rendered exception field (value, group message or note).
The first pass sees the text as loguru will render it. The second pass
runs on that output with every character :func:`strip_control_chars`
removes, except ``\t``, ``\n`` and ``\r``, removed
(:data:`_EXCEPTION_INVISIBLE_RE`), catching a credential name or URL
those characters split; its result is used only when it redacts
something more. Tabs and line breaks are kept in both passes, so a
multi-line exception message keeps its lines, and a value the first
pass redacted at a line end is not glued onto the next line's text.
When removing those characters leaves exactly the original text (none
in it, and the first pass changed nothing), the second pass would scan
that same text again and return the first pass's result, so it is
skipped: clean text, the common case, costs one pass. The additional
credential shapes run on the selected redacted text before the omission
marker is appended.
"""
value = _plain_text(value)
if not value:
return _exact_str(value)
head, marker = _truncate_for_log_redaction(value)
first = _redact_bounded_log_text(head)
visible = _EXCEPTION_INVISIBLE_RE.sub("", first)
if visible == head:
# ``_redact_bounded_log_text(visible)`` is ``first`` itself, which
# the line below would return either way.
result = first
else:
second = _redact_bounded_log_text(visible)
result = first if second == visible else second
return _exact_str(_redact_additional_shapes(result) + marker)
def _rendered_exception_notes(node: Any) -> tuple[list, bool]:
"""The notes the traceback module renders for *node*, and an exactness flag.
Mirrors ``TracebackException.format_exception_only`` on Python 3.12 to
3.14 (which loguru calls): a ``__notes__`` that is a
``collections.abc.Sequence`` other than ``str``/``bytes`` is iterated
and each item rendered with ``str()``; any other value except ``None``
(a ``str``, a ``set``, an arbitrary object) is rendered as one note,
its ``repr()``. The flag is true only for a ``list`` or ``tuple`` of
exact ``str`` objects, which renders as exactly the returned texts as
long as reading ``__notes__`` again returns the same object; a
``__notes__`` property or ``__getattr__`` on the class is caught by
:func:`_exception_text_is_stable` instead.
Anything else (a ``UserList``, a note object, a non-sequence) is
evaluated again when the traceback is rendered and may produce
different text from what was checked, so the caller rebuilds the node
with the checked texts. Raises when getting or iterating ``__notes__``
raises.
"""
notes = getattr(node, "__notes__", None)
if notes is None:
return [], True
if isinstance(notes, collections.abc.Sequence) and not isinstance(
notes, (str, bytes)
):
items = list(notes)
exact = _type_is_one_of(type(notes), (list, tuple)) and all(
type(item) is str for item in items
)
return [_safe_log_str(item) for item in items], exact
try:
text = repr(notes)
except Exception:
text = None
if not isinstance(text, str):
text = _unprintable_placeholder(notes)
return [text], False
# Nodes visited per record when rebuilding an exception graph (causes,
# contexts and group members together); the rest become placeholders.
_LOG_EXCEPTION_NODE_BUDGET = 200
# Values whose ``str()`` and ``repr()`` are fixed by their type: an
# exception holding only these (in ``args`` or in a field its builtin base
# renders) renders the same text every time it is rendered.
_STABLE_TEXT_TYPES = (str, int, float, complex, bool, type(None), bytes)
# Attributes the sinks read or call on an exception they render: its text,
# chained exceptions and notes, and a group's message and members (the
# traceback module), its truth value (loguru's ``if exc_value:``, which
# calls ``__bool__`` or ``__len__``), ``__class__`` (the ``isinstance``
# group check falls back to it), a chained exception's ``__traceback__``,
# and the pickling hooks (an ``enqueue=True`` sink pickles the record,
# exception included, after rendering it and before the next sink renders
# it; ``BaseException.__reduce__`` calls neither ``__getstate__`` nor
# ``__getnewargs__``/``__getnewargs_ex__`` today, but a kept class defines
# no pickling hook at all). A non-builtin class anywhere in the exception
# type's MRO (a mixin listed after a builtin base included) that defines
# one of these (a
# ``__str__`` override, a ``__notes__`` or ``__cause__`` property, a
# ``__getattr__`` that supplies ``__notes__``, a ``__len__`` or
# ``__reduce_ex__`` that rewrites ``args``) can render, or make a later
# sink render, different text from what the redactor checked.
_RENDER_ATTRIBUTES = frozenset(
{
"__str__",
"__getattribute__",
"__getattr__",
"args",
"__notes__",
"__cause__",
"__context__",
"__suppress_context__",
"message",
"exceptions",
"__bool__",
"__len__",
"__class__",
"__traceback__",
"__reduce__",
"__reduce_ex__",
"__getstate__",
"__getnewargs__",
"__getnewargs_ex__",
}
)
# Attributes read on the exception's *type*: the traceback module renders
# ``__qualname__`` and ``__module__``, pickling looks the class up by them,
# and the stability check itself reads ``__mro__`` and ``__dict__``. A
# non-builtin metaclass whose class dict holds anything under one of these
# names (a property, a function, a callable object that is not a
# descriptor) can run its code or make the attribute read something other
# than the type's own value -- an entry under ``__name__`` or
# ``__qualname__``, even a plain string, shadows the type's own. The one
# exception is a plain ``str`` under ``__module__``, which every class
# statement puts in a metaclass's dict: it is inert, because the class's
# own ``__module__`` entry is found first.
_METACLASS_RENDER_ATTRIBUTES = frozenset(
{
"__getattribute__",
"__getattr__",
"__name__",
"__qualname__",
"__module__",
"__mro__",
"__dict__",
}
)
# ``type``'s own descriptors for the attributes above: reading through them
# returns the type's real MRO (the order attribute lookup uses), class dict
# and names, without running a metaclass's code.
_TYPE_MRO = vars(type)["__mro__"]
_TYPE_DICT = vars(type)["__dict__"]
_TYPE_NAME = vars(type)["__name__"]
_TYPE_QUALNAME = vars(type)["__qualname__"]
_TYPE_MODULE = vars(type)["__module__"]
# Fields a builtin exception renders besides ``args`` (in ``str()`` or in
# the traceback module's extra lines), by the builtin base that renders
# them. An exception that is an instance of the base must hold only
# :data:`_STABLE_TEXT_TYPES` there, and no non-builtin class in the
# exception type's MRO may redefine them. ``SyntaxError._metadata``
# (Python 3.13+) holds the source the traceback module's keyword-typo
# suggestion reads its replacement line from. ``AttributeError.obj`` is not
# listed: it feeds only the "Did you mean" suggestion, and an exception for
# which the traceback module would compute one is never kept (see
# :func:`_exception_text_is_stable`).
_RENDERED_FIELDS = (
(OSError, ("errno", "strerror", "filename", "filename2")),
(
SyntaxError,
(
"msg",
"filename",
"lineno",
"offset",
"text",
"end_lineno",
"end_offset",
"print_file_and_line",
"_metadata",
),
),
(UnicodeError, ("encoding", "object", "start", "end", "reason")),
(ImportError, ("msg", "name", "path", "name_from")),
(NameError, ("name",)),
(AttributeError, ("name",)),
)
# ``BaseException``'s ``__dict__`` descriptor: the instance dict, read
# without going through the instance's (possibly overridden) attribute
# lookup.
_BASE_EXCEPTION_DICT = vars(BaseException)["__dict__"]
# Answers computed during the current decision phase of
# :func:`redact_log_exception` (``None`` outside one), by the check and the
# identity of what it was asked about (:func:`_once_per_phase`).
_DECISION_MEMO: contextvars.ContextVar = contextvars.ContextVar(
"ldr_log_decision_memo", default=None
)
def _once_per_phase(check: Any) -> Any:
"""*check*, answered once per decision phase for the same subjects.
The checks wrapped with this (whether pickling a class runs code, a
class's rendering hooks and names, the reducers registered for a type,
``sys.modules``'s keys) depend only on the class or object they are
asked about and on global tables: class and metaclass dicts, MROs, the
picklers' dispatch tables, ``sys.modules`` and module namespaces. A
decision phase (:func:`_decision_phase`) runs no application code for
exception classes that do not hook the machinery those checks read
through, so nothing changes any of these between two nodes' checks
(another thread could, which the threat model in the module docstring
does not cover either way), and the first answer for a subject holds for
the rest of the phase: a group of many members of one class pays for
its class's checks once, not per member. The subjects are compared by
identity (``id()``, so no ``__hash__`` or ``__eq__`` of theirs runs)
and held by the memo, so an id cannot be reused while it lives. Outside
a phase, every call computes its answer. A check that raises is not
remembered.
"""
@functools.wraps(check)
def memoized(*subjects: Any) -> Any:
memo = _DECISION_MEMO.get()
if memo is None:
return check(*subjects)
key = (check, *map(id, subjects))
entry = memo.get(key)
if entry is None:
entry = memo[key] = (subjects, check(*subjects))
return entry[1]
return memoized
@contextlib.contextmanager
def _decision_phase():
"""Scope of one record's decisions, and of :func:`_once_per_phase`'s memo.
Entered by :func:`redact_log_exception` only after every step that can
run application code has run (the read phase,
:func:`_plan_redacted_exception`), so an answer remembered in it cannot
go stale through code the redaction itself runs.
"""
token = _DECISION_MEMO.set({})
try:
yield
finally:
_DECISION_MEMO.reset(token)
def _is_builtin_class(kind: Any) -> bool:
"""True when *kind* is the builtin class of that name (not a lookalike).
The name is read through ``type``'s own descriptor, so a metaclass's
``__name__`` is never called.
"""
try:
name = _TYPE_NAME.__get__(kind)
return type(name) is str and getattr(builtins, name, None) is kind
except Exception:
return False
def _type_name(kind: Any, default: str = "object") -> str:
"""*kind*'s own ``__name__``, redacted; *default* if it is not a type.
Read through ``type``'s descriptor (a metaclass's ``__name__`` property
is never called, and cannot return one name now and another later) and
redacted like exception text, since a class can be created under any
name (``type("token=...", (Exception,), {})``). Used wherever the
redactor writes a type's name into text a sink renders.
"""
try:
name = _exact_str(_TYPE_NAME.__get__(kind))
return _redact_name_text(name)
except Exception:
return default
# Longest name :func:`_redact_name_text` remembers; a longer one (a class
# can be created under any name) is redacted on every call.
_NAME_CACHE_MAX_LEN = 256
def _redact_name_text(name: str) -> str:
""":func:`_redact_exception_text` of the exact ``str`` *name*, remembered.
Class, module and qualified names are redacted on every record that
renders or keeps an exception, and the same few recur. Redaction is a
function of the text alone (fixed patterns, no state), so a remembered
result cannot go stale; names longer than :data:`_NAME_CACHE_MAX_LEN`
are not remembered, which bounds the memory the cache holds.
"""
if len(name) > _NAME_CACHE_MAX_LEN:
return _redact_exception_text(name)
return _redact_short_name(name)
@functools.lru_cache(maxsize=1024)
def _redact_short_name(name: str) -> str:
""":func:`_redact_exception_text` of *name* (:func:`_redact_name_text`)."""
return _redact_exception_text(name)
def type_name_for_log(kind: Any, default: str = "Exception") -> str:
"""Public form of :func:`_type_name` for sinks that print a type name.
The DB sink's row prefix (``utilities.log_utils._exception_context``)
names ``record["exception"].type``, which the patcher leaves as the
original class unless :func:`_record_type_is_safe` rejects it; this
reads and redacts that name the way the patcher's own rebuilt text does,
so the two agree.
"""
return _type_name(kind, default)
def _rendered_block(header: str, notes: list) -> str:
"""The text a sink renders for one exception: its header line, then notes.
``TracebackException.format_exception_only`` writes the header line
(``TypeName: text``, see :func:`_rendered_header_tails`) and then each
note on lines of its own, so a credential label at the end of one and
its value at the start of the next are adjacent text.
"""
return "\n".join([header, *notes])
def _syntax_error_header_text(node: Any, location: bool = True) -> str:
"""The text a ``SyntaxError``'s header line shows after ``TypeName: ``.
``TracebackException._format_syntax_error`` prints ``msg`` (or
``<no detail available>`` when it is empty), followed by `` (filename)``
only when ``lineno`` is ``None`` and ``filename`` is not; the file and
line number otherwise go on the ``File ...`` line above it. That is not
``str(node)``, which adds `` (basename, line N)``: a suffix the sinks do
not print between ``msg`` and the notes on the following lines. On
Python 3.13+ the keyword-typo suggestion can replace an ``invalid
syntax`` message, or one mentioning a forgotten comma, with the fixed
text ``invalid syntax. Did you mean 'kw'?`` (``kw`` a Python keyword).
With *location* false, the `` (filename)`` suffix is left out.
"""
msg = node.msg
if msg is None or (_is_stable_text_value(msg) and not msg):
text = "<no detail available>"
else:
text = _safe_log_str(msg)
if location and node.lineno is None and node.filename is not None:
text += f" ({_safe_log_str(node.filename)})"
return text
def _rendered_header_tails(node: Any, text: str) -> tuple:
"""What the traceback module prints after the type name for *node*.
The header line is ``TypeName: text`` (``format_exception_only``), but
only ``TypeName`` when *text* (the node's ``str()``) is empty. A
``SyntaxError`` (the class or a subclass, as the traceback module tests
it) shows :func:`_syntax_error_header_text` instead of *text*; its form
without the `` (filename)`` suffix is returned too, so that the suffix
the sinks print between ``msg`` and the notes is not what keeps a label
ending ``msg`` apart from its value in a note. The first item is the
line the sinks print.
"""
if issubclass(type(node), SyntaxError):
return (
f": {_syntax_error_header_text(node)}",
f": {_syntax_error_header_text(node, location=False)}",
)
return (f": {text}" if text else "",)
def _named_text(kind: Any, text: str) -> str:
"""``TypeName: text`` for a rebuilt node's text, redacted.
Only ``TypeName`` when *text* is empty, as the traceback module renders
an exception whose ``str()`` is empty: ``TypeName: `` would put text
the original rendering did not have between the name and the notes on
the following lines.
"""
name = _type_name(kind)
return _redact_exception_text(f"{name}: {text}" if text else name)
def _type_header_changed(
kind: Any, text: str, notes: list = (), tails: tuple = ()
) -> bool:
"""True when a sink's rendered ``TypeName: text`` block redacts differently.
The traceback module renders ``qualname`` and the header line's rest
(*tails*, :func:`_rendered_header_tails`), with ``module.`` in front
outside ``__main__`` and ``builtins``, followed by the notes, one per
line (:func:`_rendered_block`); the DB row renders ``name: text``. Each
header form (:func:`_rendered_type_prefixes`) is checked with the notes
after it.
The name, the text and each note can be clean while the joined block is
not: a credential pattern can span them (a class named
``InvalidAuthorization`` and the text ``abc123...`` render as an
``Authorization: abc123...`` header; a message ending in ``password=``
and a note holding the value render as ``password=`` and the value on
the next line). The names are read through ``type``'s own descriptors;
a name that is not an exact ``str`` counts as a change.
"""
prefixes = _rendered_type_prefixes(kind)
if prefixes is None:
return True
tails = dict.fromkeys((f": {text}", *tails))
for prefix in prefixes:
for header_tail in tails:
block = _rendered_block(prefix + header_tail, notes)
if _redact_exception_text(block) != block:
return True
return False
@_once_per_phase
def _rendered_type_prefixes(kind: Any) -> Optional[tuple]:
"""The forms of *kind*'s name a sink renders before ``: text``.
``name`` (the DB row), ``qualname``, and ``module.qualname`` (the
traceback module, except for a class of ``builtins`` or ``__main__``,
which it renders as ``qualname`` alone), without repeats; ``None`` when
a name is not an exact ``str``. The names are read through ``type``'s
own descriptors, so no metaclass code runs.
"""
name = _TYPE_NAME.__get__(kind)
qualname = _TYPE_QUALNAME.__get__(kind)
module = _TYPE_MODULE.__get__(kind)
if not all(type(part) is str for part in (name, qualname, module)):
return None
prefixes = [name, qualname]
if module not in ("builtins", "__main__"):
prefixes.append(f"{module}.{qualname}")
return tuple(dict.fromkeys(prefixes))
def _notes_fitting_header(header: str, notes: list) -> list:
"""*notes*, redacted together with *header* as the sinks render them.
*header* is a rebuilt node's own clean header line (``TypeName: text``,
see :func:`_rendered_header_tails`) and each note is clean on its own;
a credential can still span a line break between them
(:func:`_rendered_block`). When redacting the joined block
changes it but leaves the header line as it is, the redacted rest is
returned as one note (one note holding line breaks renders as the same
lines as several notes). Otherwise a placeholder note is returned, or
no notes when even that does not render cleanly after the header.
"""
if not notes:
return notes
block = _rendered_block(header, notes)
redacted = _redact_exception_text(block)
if redacted == block:
return notes
start = header + "\n"
if redacted.startswith(start):
rest = [redacted[len(start) :]]
block = _rendered_block(header, rest)
if _redact_exception_text(block) == block:
return rest
placeholder = ["<exception notes omitted: they joined into a credential>"]
block = _rendered_block(header, placeholder)
if _redact_exception_text(block) == block:
return placeholder
return []
def _record_type_is_safe(kind: Any) -> bool:
"""True when a record may keep *kind* as its exception's ``type``.
An ``enqueue=True`` sink pickles the whole record before queueing it,
and loguru's ``RecordException.__reduce__`` pickles ``type`` by
reference outside the guard it puts around ``value``. A class pickle
cannot find by its name (one defined inside a function, one made with
``type()``) makes the put raise, and loguru's error handler then writes
``str(record)`` and the pickling traceback to ``sys.stderr``; that
traceback's ``__context__`` is the exception being logged, unredacted,
with its whole chain. The repr of the type in ``str(record)`` shows its
module and qualified name, so these must also be clean.
Trying the pickle here (:func:`_pickles`) is not enough on its own: it
runs once, and the sink pickles again later. So *kind* is kept only
when pickling it by reference runs no code that could answer
differently the second time (:func:`_pickled_by_reference_runs_no_code`,
the check every exception kept in the rendered graph passes too), and
only then is the pickle tried.
"""
try:
if not _pickled_by_reference_runs_no_code(kind):
return False
if not _type_names_are_clean(kind):
return False
qualname = _TYPE_QUALNAME.__get__(kind)
module = _TYPE_MODULE.__get__(kind)
text = "{}.{}".format(module, qualname)
if _redact_name_text(text) != text:
return False
except Exception:
return False
return _pickles(kind)
@_once_per_phase
def _type_names_are_clean(kind: Any) -> bool:
"""True when *kind*'s names are exact ``str`` that redaction leaves as is.
The names are ``__name__``, ``__qualname__`` and ``__module__``, read
through ``type``'s own descriptors, so no metaclass code runs.
"""
for read in (_TYPE_NAME, _TYPE_QUALNAME, _TYPE_MODULE):
text = read.__get__(kind)
if type(text) is not str or _redact_name_text(text) != text:
return False
return True
# Type attributes pickling a class, or an instance of it, reads or calls:
# the class's ``__module__``, ``__qualname__`` and ``__name__`` (looked up
# with ``getattr``, so through the metaclass's ``__getattribute__``,
# ``__getattr__`` and data descriptors); for a class nested in another
# (``Outer.Inner``), the ``__get__`` that ``getattr(Outer, "Inner")`` calls
# when ``Inner``'s metaclass defines one; ``__hash__`` and ``__eq__``, with
# which the pickler's dispatch-table lookup of an instance's class hashes
# and compares it; and ``__reduce__`` (and ``__reduce_ex__``), which
# ``object.__reduce_ex__`` reads on the instance's class to see whether it
# is overridden.
_PICKLE_METACLASS_ATTRIBUTES = _METACLASS_RENDER_ATTRIBUTES | {
"__get__",
"__hash__",
"__eq__",
"__reduce__",
"__reduce_ex__",
}
@_once_per_phase
def _pickled_by_reference_runs_no_code(kind: Any) -> bool:
"""True when pickling *kind*, or an instance of it, runs none of its code.
An ``enqueue=True`` sink pickles the record's ``type`` and, through
loguru's ``RecordException.__reduce__`` (``pickle.dumps(value)``), the
exception and everything its ``args`` and instance dict hold (a group's
members, through its ``args``), after that sink rendered the record and
before the next sink renders it. Pickling an object of class *kind*
looks *kind* up in the picklers' dispatch tables and saves it by module
and qualified name; code run on the way (a metaclass hook, a reducer
registered for *kind*, an enclosing class's metaclass, the module's
``__spec__``) could change what a later sink renders, or succeed in a
trial pickle and fail in the sink's. So every exception kept in the
rendered graph (:func:`_exception_text_is_stable`), and the record's
``type`` (:func:`_record_type_is_safe`), must pass this one check:
* *kind* is a class whose metaclass's own metaclass is ``type`` (which
hashes and dispatches the metaclass when *kind* itself is pickled);
* no non-builtin metaclass of *kind* defines
:data:`_PICKLE_METACLASS_ATTRIBUTES` (:func:`_metaclass_is_inert`;
a ``__module__`` property, say, that names the real module on the
first read and another on the next);
* no reducer is registered for *kind* or its metaclass
(:func:`_has_registered_reducer`); and
* pickle's lookup of *kind* by its module and qualified name runs no
code (:func:`_pickle_lookup_runs_no_code`): it finds *kind*, or
pickle refuses a class defined in a function before looking it up
(that pickle then fails, which loguru's ``__reduce__`` guards for a
value, and :func:`_record_type_is_safe` rejects for the ``type``).
The instance side (the exception's own ``__reduce_ex__``, ``args`` and
instance dict) is checked by :func:`_exception_text_is_stable`.
Everything is read through the builtin descriptors, so this check runs
no code of *kind*'s either.
"""
if not issubclass(type(kind), type):
return False
meta = type(kind)
if type(meta) is not type:
return False
if not _metaclass_is_inert(kind, _PICKLE_METACLASS_ATTRIBUTES):
return False
if _has_registered_reducer(kind) or _has_registered_reducer(meta):
return False
qualname = _TYPE_QUALNAME.__get__(kind)
module = _TYPE_MODULE.__get__(kind)
if type(qualname) is not str or type(module) is not str:
return False
return _pickle_lookup_runs_no_code(kind, module, qualname)
# The reducer ``copyreg`` registers for ``complex``: the only stock entry
# for a type :func:`_is_plain_data` and :func:`_is_stable_text_value`
# accept (the pickler saves the others without consulting the tables).
_STOCK_COMPLEX_REDUCER = getattr(copyreg, "pickle_complex", None)
# Metaclass attributes a dict lookup runs on a key it compares.
_DICT_KEY_ATTRIBUTES = frozenset({"__hash__", "__eq__"})
@_once_per_phase
def _has_registered_reducer(kind: Any, stock: Any = None) -> bool:
"""True when a pickler may call a reducer other than *stock* for *kind*.
``pickle.dumps`` (which loguru pickles an exception with) looks the
exact class of every object it saves, other than its builtin fast-path
types (``str``, ``int``, ``list`` ...), up in ``copyreg.dispatch_table``,
and the ``ForkingPickler`` the queue uses looks it up there and in its
own ``_extra_reducers``; a reducer found there runs. A table that is not
an exact ``dict``, or a key that is not a class whose metaclasses leave
``__hash__`` and ``__eq__`` alone (one that could claim to equal *kind*
and run its own code while the lookup compares it), counts as a
reducer too. Keys are compared by identity, so nothing here runs code.
"""
try:
# What ``_metaclass_is_inert`` answers for every key whose metaclass
# is ``type`` itself, the usual case (see its fast path).
plain_classes_inert = _plain_metaclass_is_builtin()
tables = (
copyreg.dispatch_table,
ForkingPickler._copyreg_dispatch_table,
ForkingPickler._extra_reducers,
)
for table in tables:
if type(table) is not dict:
return True
for key, reducer in dict.items(table):
if key is kind:
if reducer is not stock:
return True
elif type(key) is type:
# A plain class: ``_metaclass_is_inert``'s fast path.
if not plain_classes_inert:
if not _metaclass_is_inert(key, _DICT_KEY_ATTRIBUTES):
return True
elif not (
issubclass(type(key), type)
and _metaclass_is_inert(key, _DICT_KEY_ATTRIBUTES)
):
return True
except Exception:
return True
return False
def _pickle_lookup_runs_no_code(kind: Any, module: str, qualname: str) -> bool:
"""True when pickle's lookup of *kind* by name runs no code.
That is, pickle refuses the name before looking anything up (a class
defined in a function, whose qualified name has a ``<locals>`` part),
or it finds *kind* and runs no code on the way. Pickle saves a class as
its module and qualified name: it imports the module
(:func:`_module_import_runs_no_code`) and checks that following the
name (``getattr`` for each part of ``qualname``) gives back the class. Each ``getattr`` runs the code of the object it is called on: a
module that is an instance of a ``ModuleType`` subclass, or an
enclosing class whose metaclass defines ``__getattribute__``,
``__getattr__``, ``__get__`` or an attribute of the part's name, could
find *kind* when :func:`_pickles` tries it and fail when the
``enqueue=True`` sink pickles it, or change what a later sink renders.
So the module must already be in ``sys.modules`` as an exact
``ModuleType``, each part must be found as the attribute lookup finds it
(no entry of that name in the lookup object's type's MRO, then the
module's dict or the first dict of the enclosing class's MRO holding
the name), through dicts whose keys are all exact ``str`` (a key of
another type could compare equal to the part by running its own code),
each enclosing class's metaclasses must be inert
(:func:`_metaclass_is_inert`), and the last part must be *kind* itself.
Everything is read through the builtin descriptors, so nothing here runs
code either.
"""
parts = qualname.split(".")
if "<locals>" in parts:
# Pickle refuses a class defined in a function by its qualified
# name alone, before it imports or looks anything up: no code runs
# (and the pickle fails, which :func:`_pickles` reports).
return True
if not _module_import_runs_no_code(module):
return False
owner = sys.modules.get(module)
if type(owner) is not types.ModuleType:
return False
for index, part in enumerate(parts):
if index and not (
issubclass(type(owner), type)
and _metaclass_is_inert(owner, _PICKLE_METACLASS_ATTRIBUTES)
):
return False
# Any entry under ``part`` in the type of ``owner`` (the module
# type, or the enclosing class's metaclass) is found before
# ``owner``'s own: a data descriptor there wins.
for cls in _TYPE_MRO.__get__(type(owner)):
namespace = _TYPE_DICT.__get__(cls)
if not _plain_names(namespace) or part in namespace:
return False
if index:
namespaces = [
_TYPE_DICT.__get__(cls) for cls in _TYPE_MRO.__get__(owner)
]
else:
namespaces = [_MODULE_DICT.__get__(owner)]
for namespace in namespaces:
if not _plain_names(namespace):
return False
if part in namespace:
owner = namespace[part]
break
else:
return False
return owner is kind
# ``ModuleType``'s ``__dict__`` descriptor: a module's own namespace.
_MODULE_DICT = vars(types.ModuleType)["__dict__"]
# ``ModuleSpec``'s ``__dict__`` descriptor: a spec's instance attributes.
_MODULE_SPEC_DICT = vars(ModuleSpec)["__dict__"]
# Attributes the import system reads on a module's ``__spec__``.
_MODULE_SPEC_ATTRIBUTES = frozenset(
{"_initializing", "__getattribute__", "__getattr__"}
)
def _module_spec_read_is_plain(kind: Any) -> bool:
"""True when reading ``__spec__`` on a module of class *kind* runs no code.
*kind* is ``ModuleType`` or a subclass whose own classes (other than
``ModuleType`` and ``object``) define neither ``__getattribute__`` nor
an entry named ``__spec__``, so the read finds the module's own
``__spec__`` entry through ``ModuleType``'s lookup.
"""
if not issubclass(kind, types.ModuleType):
return False
for cls in _TYPE_MRO.__get__(kind):
if cls is types.ModuleType or cls is object:
continue
namespace = _TYPE_DICT.__get__(cls)
if not _plain_names(namespace):
return False
if "__getattribute__" in namespace or "__spec__" in namespace:
return False
return True
@_once_per_phase
def _sys_modules_names_are_plain(modules: dict) -> bool:
""":func:`_plain_names` of *modules* (``sys.modules``), once per record.
:func:`_module_import_runs_no_code` runs for every class kept in the
rendered graph, and ``sys.modules`` holds thousands of entries in the
web process, so scanning it for each one made a large exception group
cost tens of milliseconds on the logging thread. Inside a decision
phase it is scanned once (:func:`_once_per_phase`); it is scanned again
if ``sys.modules`` is no longer the dict that was scanned.
"""
return _plain_names(modules)
def _module_import_runs_no_code(module: str) -> bool:
"""True when pickle's import of the imported module *module* runs no code.
Pickle imports the module of every class it saves by reference. For a
module already in ``sys.modules`` the import system still reads the
module's ``__spec__`` and that spec's ``_initializing`` flag (and its
truth value), and for a dotted name it does the same for the top-level
package: a ``__spec__`` object with its own ``__getattr__`` or an
``_initializing`` property, or a module without ``__spec__`` whose
``__getattr__`` then supplies one, runs code there. So each of those
modules must be in ``sys.modules`` (whose keys must all be exact
``str``) as a module whose class reads ``__spec__`` from its namespace
(:func:`_module_spec_read_is_plain`; a lazy-loading ``ModuleType``
subclass with only a ``__getattr__`` for missing names does), and that
namespace (exact ``str`` keys only) must hold ``__spec__`` as ``None``
or an exact ``ModuleSpec`` whose class leaves those attributes alone
and whose ``_initializing``, if set, is an exact ``bool``. The module
a class is then looked up in must also be an exact ``ModuleType``
(:func:`_pickle_lookup_runs_no_code`). ``sys.modules``'s keys are
scanned once per record, not once per call
(:func:`_sys_modules_names_are_plain`).
"""
modules = sys.modules
if type(modules) is not dict or not _sys_modules_names_are_plain(modules):
return False
for name in {module, module.partition(".")[0]}:
owner = modules.get(name)
if not _module_spec_read_is_plain(type(owner)):
return False
namespace = _MODULE_DICT.__get__(owner)
if not _plain_names(namespace) or "__spec__" not in namespace:
return False
spec = namespace["__spec__"]
if spec is None:
continue
if type(spec) is not ModuleSpec:
return False
for cls in _TYPE_MRO.__get__(ModuleSpec):
if _MODULE_SPEC_ATTRIBUTES.intersection(_TYPE_DICT.__get__(cls)):
# ``object`` defines ``__getattribute__``: builtin.
if not _is_builtin_class(cls):
return False
state = _MODULE_SPEC_DICT.__get__(spec)
# An exact ``dict``: a subclass's ``__iter__``, ``__contains__`` or
# ``__getitem__`` would run here.
if type(state) is not dict or not _plain_names(state):
return False
if (
"_initializing" in state
and type(state["_initializing"]) is not bool
):
return False
return True
def _picklable_extra(extra: Any) -> Any:
"""*extra*, with every value that is not plain data replaced.
An ``enqueue=True`` sink pickles the whole record before queueing it.
One value it cannot pickle (a lock, an open file, an instance of a class
defined in a function) makes the put raise, and loguru's error handler
then writes ``str(record)`` and the pickling traceback to
``sys.stderr``; that traceback's ``__context__`` is the exception being
handled when the log call was made (whether or not the call logged it),
unredacted. Trying the pickle here would not settle it: an object's own
pickling code (``__reduce_ex__``, ``__getstate__``) can succeed now and
raise when the sink pickles it. So every value that is not plain data
(:func:`_is_plain_data`: an exact ``str``, ``int``, ``float``,
``complex`` (while ``copyreg``'s own reducer pickles it), ``bool``,
``None``, ``bytes`` or ``bytearray``, or an exact ``list``, ``tuple``,
``dict``, ``set`` or ``frozenset`` of plain data),
whose pickling runs builtin code only, is replaced, for every sink, by
``<unpicklable TypeName>``. If a key is not plain data, only the routing
keys (:data:`_LOG_STRUCTURAL_EXTRA_KEYS`) are kept, and ``{}`` if their
values are not plain data either.
"""
if _is_plain_data(extra):
return extra
try:
out = {
key: value
if _is_plain_data(value)
else f"<unpicklable {_type_name(type(value))}>"
for key, value in dict.items(extra)
}
except Exception:
out = None
if out is None or not _is_plain_data(out):
out = _structural_extra_only(extra)
return out if _is_plain_data(out) else {}
# How deep :func:`_is_plain_data` follows nested containers; a deeper one is
# not counted as plain data. ``redact_log_extra`` already replaces a
# container nested :data:`_LOG_EXTRA_MAX_DEPTH` deep, except under a
# routing key, which it passes through as is.
_PLAIN_DATA_MAX_DEPTH = _LOG_EXTRA_MAX_DEPTH + 2
_PLAIN_DATA_SCALARS = (
str,
int,
float,
complex,
bool,
type(None),
bytes,
bytearray,
)
_PLAIN_DATA_CONTAINERS = (list, tuple, set, frozenset)
def _is_plain_data(value: Any, _depth: int = 0, _seen: Any = None) -> bool:
"""True when pickling *value* runs builtin code only.
*value* is an instance of exactly one of :data:`_PLAIN_DATA_SCALARS`, or
an exact ``list``, ``tuple``, ``set``, ``frozenset`` or ``dict`` whose
items (keys and values) are plain data, nested at most
:data:`_PLAIN_DATA_MAX_DEPTH` deep (a ``complex`` only while
:func:`_complex_pickles_as_stock`). Types are compared by identity
(:func:`_type_is_one_of`), so no subclass and no metaclass ``__eq__``
counts, and nothing here runs code of *value*'s own. A container that
contains itself is not counted as plain data; one reached again by
another path is checked once (so shared sub-containers cost linear,
not exponential, time).
"""
kind = type(value)
if _type_is_one_of(kind, _PLAIN_DATA_SCALARS):
return kind is not complex or _complex_pickles_as_stock()
if _depth >= _PLAIN_DATA_MAX_DEPTH:
return False
if kind is not dict and not _type_is_one_of(kind, _PLAIN_DATA_CONTAINERS):
return False
if _seen is None:
_seen = {}
marker = id(value)
if marker in _seen:
# ``False`` while it is still being checked: a cycle.
return _seen[marker]
_seen[marker] = False
if kind is dict:
result = all(
_is_plain_data(key, _depth + 1, _seen)
and _is_plain_data(item, _depth + 1, _seen)
for key, item in dict.items(value)
)
else:
result = all(_is_plain_data(item, _depth + 1, _seen) for item in value)
_seen[marker] = result
return result
def _complex_pickles_as_stock() -> bool:
"""True when pickling a ``complex`` calls ``copyreg``'s own reducer only.
The pickler saves ``str``, ``int``, ``float``, ``bool``, ``None``,
``bytes``, ``bytearray`` and the exact builtin containers itself, but
looks ``complex`` up in its dispatch tables, where a reducer registered
in place of ``copyreg``'s would run (:func:`_has_registered_reducer`).
"""
return _STOCK_COMPLEX_REDUCER is not None and not _has_registered_reducer(
complex, _STOCK_COMPLEX_REDUCER
)
def _pickles(obj: Any) -> bool:
"""True when *obj* pickles the way an ``enqueue=True`` sink pickles it.
loguru queues the record with ``multiprocessing.SimpleQueue.put``, which
serializes with :class:`multiprocessing.reduction.ForkingPickler`. That
pickler has its own reducers on top of :mod:`pickle`'s (registered, for
example, for a ``ctypes`` type once ``multiprocessing.sharedctypes`` has
created a value of it), so ``pickle.dumps`` succeeding does not mean
the put will.
"""
try:
ForkingPickler.dumps(obj)
except Exception:
return False
return True
def _is_stable_text_value(value: Any, _depth: int = 0) -> bool:
"""True when *value* renders the same text every time.
An exact :data:`_STABLE_TEXT_TYPES` instance, or a ``tuple`` or
``frozenset`` (exact types) of such values. A subclass of ``str`` or
``int``, or any other object, may render differently on each call. A
``complex`` counts only while the reducer pickle calls for it is
``copyreg``'s own (:func:`_complex_pickles_as_stock`): it is the one
such type the pickler dispatches through ``copyreg.dispatch_table``.
"""
kind = type(value)
if _type_is_one_of(kind, _STABLE_TEXT_TYPES):
return kind is not complex or _complex_pickles_as_stock()
if _type_is_one_of(kind, (tuple, frozenset)) and _depth < 4:
return all(_is_stable_text_value(item, _depth + 1) for item in value)
return False
def _plain_names(namespace: Any) -> bool:
"""True when every key of the class dict *namespace* is an exact ``str``.
``type(name) is str`` for every key, run in C (``map`` and
``operator.is_``) rather than as a generator: ``sys.modules`` is checked
once per record and holds thousands of keys in the web process.
"""
return all(map(operator.is_, map(type, namespace), itertools.repeat(str)))
def _plain_metaclass_is_builtin() -> bool:
"""True when :func:`_is_builtin_class` holds for ``type`` and ``object``.
They are the whole MRO of ``type``, the metaclass of a class statement
without one, so :func:`_metaclass_is_inert` holds for every class whose
metaclass is exactly ``type`` (it skips builtin classes).
"""
return (
getattr(builtins, "type", None) is type
and getattr(builtins, "object", None) is object
)
def _metaclass_is_inert(kind: Any, attributes: frozenset) -> bool:
"""True when no non-builtin metaclass of *kind* defines *attributes*.
Every metaclass in ``type(kind)``'s MRO that is not a builtin class must
have a class dict whose keys are all exact ``str`` (a subclass with its
own ``__hash__``/``__eq__`` could hide from the check) and that holds
none of *attributes* -- except a plain ``str`` under ``__module__``,
which every class statement puts in a metaclass's dict and which is
inert, because the class's own ``__module__`` entry is found first. The
MRO and class dicts are read through ``type``'s own descriptors, so no
metaclass code runs here.
"""
if type(kind) is type and _plain_metaclass_is_builtin():
# The common case, answered without the loop: ``type``'s MRO is
# ``type`` and ``object``, both builtin, so the loop skips both.
return True
for meta in _TYPE_MRO.__get__(type(kind)):
if _is_builtin_class(meta):
continue
namespace = _TYPE_DICT.__get__(meta)
if not _plain_names(namespace):
return False
for name in attributes.intersection(namespace):
if name == "__module__" and type(namespace[name]) is str:
continue
return False
return True
def _group_args_are_its_members(node: Any) -> bool:
"""True when an exception group's ``args`` are its message and members.
An ``enqueue=True`` sink pickles a group through its ``args`` (the
message and the container it was built from), not its ``exceptions``,
after that sink rendered it and before the next sink renders it. The
container is the caller's own object: a ``list`` subclass whose
``__reduce_ex__`` rewrites a member's ``args`` would change the text
the later sinks render, and a list appended to after the group was
built would pickle objects never checked. So ``args`` must be an exact
2-tuple of the message (an exact ``str`` equal to ``message``) and an
exact ``list`` or ``tuple`` holding exactly the group's members, the
same objects in the same order; the members themselves are checked as
nodes of their own.
"""
args = node.args
if type(args) is not tuple or len(args) != 2:
return False
message, members = args
if type(message) is not str or message != _exact_str(node.message):
return False
if not _type_is_one_of(type(members), (list, tuple)):
return False
exceptions = node.exceptions
return len(members) == len(exceptions) and all(
member is exception for member, exception in zip(members, exceptions)
)
def _exception_text_is_stable(node: Any, is_group: bool) -> bool:
"""True when rendering *node* again yields exactly the text checked.
The redactor reads an exception's text once and keeps the original
object only when that text was clean, but the sinks render the object
again (loguru's ``format_exception_only``, the DB row's ``str()``) and
run its code while doing so. Keeping it is safe only when no code the
exception's own classes define runs then, and the values the builtin
rendering reads cannot render other text. That holds when:
* every non-builtin class in ``type(node).__mro__`` (including a mixin
listed after a builtin base) leaves the rendering attributes alone
(:data:`_RENDER_ATTRIBUTES` and the base's :data:`_RENDERED_FIELDS`);
* the type is one that pickling by reference runs no code of
(:func:`_pickled_by_reference_runs_no_code`, the check the record's
``type`` passes too): an ``enqueue=True`` sink pickles every kept
node an exception's ``args`` reach (a group's members) after it
rendered the record and before the next sink renders it, and the
check is applied to every kept node, so one node can be kept or
rebuilt only one way wherever it appears in the graph. Among other
things, no non-builtin metaclass of the type defines the type
attributes the sinks, the pickler and this check read
(:data:`_PICKLE_METACLASS_ATTRIBUTES`, which include
:data:`_METACLASS_RENDER_ATTRIBUTES`; the MRO, class dicts and names
are read here through ``type``'s own descriptors, so a metaclass
cannot hide a class or override from this check), and no class or
module pickle looks it up through runs code;
* the type's own ``__name__``, ``__qualname__`` and ``__module__``
(which the traceback module and the DB row render) are plain ``str``
that redaction leaves unchanged;
* the values the builtin rendering reads (``args``, a group's
``message``, the base's fields) and the instance attributes an
``enqueue=True`` sink pickles are of :data:`_STABLE_TEXT_TYPES`
(``__notes__`` aside, which the caller checks), and a group's
``args``, which that sink pickles in place of its ``exceptions``,
hold nothing but its message and a plain container of exactly its
members (:func:`_group_args_are_its_members`); and
* the traceback module computes no "Did you mean" suggestion for it (an
``AttributeError`` or ``NameError`` with a ``str`` ``name``, an
``ImportError`` with a ``str`` ``name_from``): that runs code of
other objects -- ``obj.__dir__()``, importing the named module and
calling its ``__dir__``, and, for a renderer that passes the
traceback (loguru does not), ``hasattr`` on the failing frame's
``self`` -- which can change the exception after its text was
checked. A rebuilt node has no ``name``, so its traceback shows no
suggestion.
Otherwise (a ``__str__`` that returns benign text first and a secret
later, a ``__notes__`` property, a ``__len__`` that rewrites ``args``,
an argument object with its own ``__str__``, a class nested in one
whose metaclass hooks attribute lookup) the caller rebuilds the node
from the text it checked. Code that changes the exception from outside
its rendering and pickling is not covered: another thread; the class's
own construction code (``__init__``, ``__new__``, ``__setattr__``, a
metaclass ``__call__``), which runs when an ``enqueue=True`` sink
unpickles its own copy in its writer thread; a reducer registered for
a builtin exception type, which also runs for the rebuilt nodes; and
patches to the pickling or import machinery itself. Nor is a class or
object written to defeat this check (see the module docstring's threat
model): the reads here are made through builtin descriptors and exact
type tests (an instance ``__dict__`` must be an exact ``dict``, a
``name`` an exact ``str``) as defence in depth, not as a guarantee.
"""
kind = type(node)
# Everything that depends on the class alone, checked once per class in
# a decision phase.
if not _exception_class_is_stable(kind):
return False
# ``type(...) is str``, not ``isinstance``: the latter falls back to a
# non-``str`` value's ``__class__``, which runs its code. Any value
# other than an exact ``str`` or ``None`` fails the field check below.
if issubclass(kind, ImportError):
if type(getattr(node, "name_from", None)) is str:
return False
if issubclass(kind, (NameError, AttributeError)):
if type(getattr(node, "name", None)) is str:
return False
# Read through ``BaseException``'s own descriptor: the attribute lookup
# is guarded (:func:`_exception_class_is_stable`), and this is the dict
# pickling copies. It must be an exact ``dict``
# (``exc.__dict__ = SomeDictSubclass()`` is accepted), read with
# ``dict``'s own methods, so no ``__len__`` or ``items`` of the node's
# runs here or in the pickler.
state = _BASE_EXCEPTION_DICT.__get__(node)
if type(state) is not dict:
return False
if state:
for name, value in dict.items(state):
if type(name) is not str:
return False
if name != "__notes__" and not _is_stable_text_value(value):
return False
if is_group:
if type(node.message) is not str:
return False
if not _group_args_are_its_members(node):
return False
elif not _is_stable_text_value(node.args):
return False
return all(
_is_stable_text_value(getattr(node, name, None))
for name in _rendered_fields(kind)
)
def _rendered_fields(kind: Any) -> list:
"""The :data:`_RENDERED_FIELDS` of the builtin bases *kind* derives from."""
# ``issubclass`` on the type, not ``isinstance`` on the node: the latter
# falls back to the node's ``__class__``, which runs its code.
return [
name
for base, names in _RENDERED_FIELDS
if issubclass(kind, base)
for name in names
]
@_once_per_phase
def _exception_class_is_stable(kind: Any) -> bool:
"""The class-level conditions of :func:`_exception_text_is_stable`.
No non-builtin class in *kind*'s MRO defines a rendering attribute,
pickling *kind* by reference runs no code, and its names are clean.
These depend on the class alone, so a decision phase checks them once
per class (:func:`_once_per_phase`).
"""
guarded = _RENDER_ATTRIBUTES.union(_rendered_fields(kind))
# Every non-builtin class in the MRO counts, not only those before the
# first builtin: in ``class E(ValueError, Mixin)`` the order is E,
# ValueError, Exception, BaseException, Mixin, object, and the builtins
# do not define ``__str__``, ``__notes__``, ``__getattr__`` or
# ``__getattribute__`` themselves, so Python still finds Mixin's.
# The MRO and class dicts are read through ``type``'s own descriptors:
# ``kind.__mro__`` and ``vars(cls)`` go through the metaclass, which
# could report builtin bases only, or an empty dict.
# A class dict key that is not an exact ``str`` (a subclass with its
# own ``__hash__``/``__eq__``) could hide from the name checks.
for cls in _TYPE_MRO.__get__(kind):
if _is_builtin_class(cls):
continue
namespace = _TYPE_DICT.__get__(cls)
if not _plain_names(namespace) or guarded.intersection(namespace):
return False
# The sinks read the type's names and an ``enqueue=True`` sink pickles
# the kept node, so its class must be one that rendering and pickling
# by reference run no code of (a superset of the metaclass attributes
# rendering alone reads).
if not _pickled_by_reference_runs_no_code(kind):
return False
return _type_names_are_clean(kind)
# A ``SyntaxError``'s fields the interpreter fills, by the exact type it
# fills them with (or ``None``). Only a node whose fields all have these
# types is kept; anything else (a ``str`` line number, ``bytes`` source
# text, a ``str`` offset) is rendered differently by each Python version's
# traceback module, and on Python 3.12 a ``bytes`` text or non-``int``
# offset makes it raise, so loguru's error handler prints the context of
# that error -- the exception being handled -- unredacted to stderr. The
# ``int`` fields must also be in the accepted ranges
# (:func:`_syntax_error_fields_out_of_range`).
_SYNTAX_ERROR_FIELD_TYPES = (
("msg", str),
("filename", str),
("text", str),
("lineno", int),
("offset", int),
("end_lineno", int),
("end_offset", int),
)
# The largest line number a kept ``SyntaxError`` may carry: the interpreter
# fills ``lineno`` and ``end_lineno`` from a C ``int``. A larger one is not
# an interpreter value, and one past ``sys.get_int_max_str_digits()`` digits
# makes the ``File ...`` line raise ``ValueError`` when it is printed.
_SYNTAX_ERROR_MAX_LINENO = 2**31 - 1
# How far past the UTF-8 length of ``text`` (plus one) a kept
# ``SyntaxError``'s ``offset`` and ``end_offset`` may run, in columns. The
# interpreter counts columns in UTF-8 bytes of the reported line, not in
# characters: the tokenizer passes raw byte offsets, and the compiler and
# symtable stages (errors raised when a file is compiled or imported) report
# byte offsets too. Those stages also report the ``end_offset`` of a span
# that ends on a later line as a column of that later line, which ``text``
# does not hold and which can be longer than it. This slack covers such a
# later line of up to this many bytes; a span ending further right than that
# is not kept (the caller rebuilds the node). It also bounds the caret line
# Python 3.12 draws (``'^' * (end_offset - offset)``), so a value like
# ``10**9`` is rejected before anything is rendered
# (:func:`_syntax_error_fields_out_of_range`).
_SYNTAX_ERROR_SPAN_END_SLACK = 2**16
# How much longer than the text it prints verbatim (``msg``, ``filename``
# and ``text``, each counted three times) plus its caret line (at most its
# largest offset, when ``text`` is a ``str``) a kept ``SyntaxError``'s
# rendering may be, in characters. On Python 3.13+ the keyword-typo
# suggestion may replace ``msg`` and the source line with ones from at most
# 1024 characters of source. Anything longer is not a rendering of these
# fields, and the caller rebuilds the node
# (:func:`_syntax_error_renders_bounded`).
_SYNTAX_ERROR_RENDER_SLACK = 4096
def _syntax_error_fields_out_of_range(node: Any) -> bool:
"""True when a ``SyntaxError``'s ``int`` fields are not interpreter values.
Called only for fields of the canonical types
(:data:`_SYNTAX_ERROR_FIELD_TYPES`) and a ``text`` of at most
``_LOG_REDACTION_MAX_CHARS`` characters, so encoding it is bounded. The traceback module does
arithmetic on the offsets to draw the caret line (``'^' * (end - start)``
on Python 3.12, and on 3.14 too when ``text`` is empty), so an offset
near ``2**63`` makes it raise ``OverflowError`` or ``MemoryError``, and a
moderate one (``10**9``) makes every sink build a caret line that long.
Loguru's error handler then prints the context of that error -- the
exception being handled -- unredacted to stderr. The interpreter
reports ``lineno`` and ``end_lineno`` between ``0`` and
:data:`_SYNTAX_ERROR_MAX_LINENO`. It reports ``offset`` and
``end_offset`` as ``-1`` (unknown) or as columns counted in
UTF-8 bytes, not characters, so on a line with non-ASCII text they can
exceed ``len(text) + 1``; and for an error raised when a file is
compiled or imported, an ``end_offset`` on a later line than
``lineno`` is a column of that later line, not of ``text``. The
accepted range is therefore ``-1`` to the UTF-8 length of ``text``
plus ``1`` plus :data:`_SYNTAX_ERROR_SPAN_END_SLACK`, or up to
:data:`_SYNTAX_ERROR_MAX_LINENO` when ``text`` is ``None`` (the offsets
are then not drawn). Anything outside these ranges makes the caller
rebuild the node, which drops these fields; that includes a real error
whose span ends more than :data:`_SYNTAX_ERROR_SPAN_END_SLACK` bytes
into a later line.
"""
for value in (node.lineno, node.end_lineno):
if value is not None and not 0 <= value <= _SYNTAX_ERROR_MAX_LINENO:
return True
limit = (
_SYNTAX_ERROR_MAX_LINENO
if node.text is None
else len(node.text.encode("utf-8", "surrogatepass"))
+ 1
+ _SYNTAX_ERROR_SPAN_END_SLACK
)
for value in (node.offset, node.end_offset):
if value is not None and not -1 <= value <= limit:
return True
return False
def _syntax_error_renders_bounded(node: Any) -> bool:
"""True when the traceback module renders *node*'s lines within bounds.
Runs ``traceback.format_exception_only`` -- what loguru's formatter
calls -- on a detached builtin ``SyntaxError`` carrying *node*'s
rendered fields (``msg``, ``filename``, ``lineno``, ``offset``,
``text``, ``end_lineno``, ``end_offset`` and ``_metadata``), not on
*node* itself, so no chained exception is formatted and no frame's
source is read. Called only for a node that is about to be kept, whose
fields have passed :func:`_syntax_error_details_changed` (canonical
types, in range, and a keyword-typo source that is an exact ``str``, so
the rendering reads no file). False when that rendering raises
(``OverflowError``, ``MemoryError``, ``ValueError`` or any other
``Exception`` a sink would hit, whose error handler prints the
exception being handled unredacted) or is longer than
:data:`_SYNTAX_ERROR_RENDER_SLACK` allows; the caller then rebuilds the
node. ``KeyboardInterrupt`` and ``SystemExit`` propagate.
"""
import traceback
try:
probe = SyntaxError()
verbatim = 0
for name, _kind in _SYNTAX_ERROR_FIELD_TYPES:
value = getattr(node, name)
setattr(probe, name, value)
if type(value) is str:
verbatim += len(value)
metadata = getattr(node, "_metadata", None)
if metadata is not None:
probe._metadata = metadata
size = sum(len(line) for line in traceback.format_exception_only(probe))
# Python 3.12 draws ``end_offset - offset`` carets, unclamped.
caret = 0
if type(node.text) is str:
caret = max(0, node.offset or 0, node.end_offset or 0)
except Exception:
return False
return size <= 3 * verbatim + caret + _SYNTAX_ERROR_RENDER_SLACK
def _syntax_error_location_block(node: Any) -> str:
"""The ``File ...`` and source lines the traceback prints for *node*.
``TracebackException._format_syntax_error`` prints
`` File "<filename>", line <lineno>`` (only when ``lineno`` is not
``None``; ``<string>`` for an empty or missing file name) and, on the
next line, the source ``text`` without its trailing newlines and
leading spaces, so a label ending one line and a value starting the
next are adjacent text. Called only for fields of the canonical types
(:data:`_SYNTAX_ERROR_FIELD_TYPES`).
"""
lines = []
if node.lineno is not None:
lines.append(
f' File "{node.filename or "<string>"}", line {node.lineno}'
)
if node.text is not None:
lines.append(" " + node.text.rstrip("\n").lstrip(" \n\f"))
return "\n".join(lines)
def _syntax_error_details_changed(node: Any) -> bool:
"""True when a ``SyntaxError``'s extra traceback lines need redacting.
The traceback module renders ``filename``, ``lineno``, ``text`` and
``msg`` outside ``str(node)``. A field that is not ``None`` or of the
exact type the interpreter fills it with
(:data:`_SYNTAX_ERROR_FIELD_TYPES`: ``str`` for ``msg``, ``filename``
and ``text``, ``int`` -- not ``bool`` -- for ``lineno``, ``offset``,
``end_lineno`` and ``end_offset``) makes the caller rebuild the node,
and so does a ``text`` longer than ``_LOG_REDACTION_MAX_CHARS`` (which
redaction would cut) or an ``int`` field outside the accepted ranges
(:func:`_syntax_error_fields_out_of_range`). Otherwise each of ``msg``, ``filename``, ``lineno`` and ``text`` is
checked on its own, and the ``File ...`` line joined with the source
line below it (:func:`_syntax_error_location_block`). On Python 3.13+
the keyword-typo suggestion may also replace the shown line with one
from the source in ``_metadata``, or from the file ``filename`` names
when that source is ``None``; that happens only when ``msg`` is
``"invalid syntax"`` or mentions a forgotten comma. Any of these that
redacts differently, a source that is not an exact ``str``, or a file
the sink would read makes the caller rebuild the node, which drops
these details.
"""
for name, kind in _SYNTAX_ERROR_FIELD_TYPES:
value = getattr(node, name, None)
if value is not None and type(value) is not kind:
return True
# Redaction cuts a longer ``text`` and appends a marker, so it changes
# anyway; returning here also bounds what the range check encodes.
if node.text is not None and len(node.text) > _LOG_REDACTION_MAX_CHARS:
return True
if _syntax_error_fields_out_of_range(node):
return True
for value in (node.msg, node.filename, node.lineno, node.text):
if value is None:
continue
text = _safe_log_str(value)
if _redact_exception_text(text) != text:
return True
block = _syntax_error_location_block(node)
if _redact_exception_text(block) != block:
return True
metadata = getattr(node, "_metadata", None)
if metadata is None:
return False
# Plain characters: a ``str`` subclass's own ``__ne__`` or
# ``__contains__`` could answer differently here than in the sink.
msg = _plain_text(node.msg)
if not isinstance(node.text, str) or (
isinstance(msg, str)
and msg != "invalid syntax"
and "Perhaps you forgot a comma" not in msg
):
return False
source = (
metadata[2] if type(metadata) is tuple and len(metadata) == 3 else None
)
if type(source) is not str or len(source) > _LOG_REDACTION_MAX_CHARS:
return True
return any(
_redact_exception_text(text) != text
for text in (
source,
*source.splitlines(),
*(line.lstrip() for line in source.splitlines()),
)
)
def redact_log_exception(record) -> None:
"""Give sinks a scrubbed exception graph without changing live exceptions.
Loguru renders exception values, notes and group messages independently
of record["message"]. Rebuild changed nodes using builtin exceptions only;
custom constructors, copy hooks and group derive methods are never called.
Custom non-group types retain their name in a RuntimeError message; group
subclasses become builtin groups. Application-specific attributes are not
copied to log output. Sensitive SyntaxError source details are omitted,
as are those of a SyntaxError whose fields are not of the types the
interpreter fills them with (:data:`_SYNTAX_ERROR_FIELD_TYPES`), not in
the accepted ranges (:func:`_syntax_error_fields_out_of_range`), or whose
rendering by the traceback module raises or runs long
(:func:`_syntax_error_renders_bounded`).
Tracebacks and safe notes are retained. Cycles and traversal limits become
explicit placeholders instead of links back to unsanitized objects. An
exception, cause, context, group member or note whose ``str()`` fails is
rendered as ``<unprintable TypeName>`` rather than aborting the rebuild.
An exception is kept as the original object only when its text was clean
and no code its classes define, and no "Did you mean" suggestion, runs
when the sinks render it again (:func:`_exception_text_is_stable` lists
the conditions and what they do not cover) and its type's name joined
with its text and notes (the ``TypeName: text`` header line, as the
sinks render it, and the notes on the lines after it) is clean too;
otherwise the sinks get a rebuilt node carrying exactly the text that
was checked. The record's ``type`` stays the original class only when an
``enqueue=True`` sink can pickle it and its names are clean
(:func:`_record_type_is_safe`); otherwise the top node is rebuilt even
when it could be kept, and ``type`` becomes the rebuilt node's builtin
type, so that sink never fails and dumps the unredacted original. A
record with a ``type`` but no value (``None``) gets the same check: a
``type`` it rejects becomes ``RuntimeError``, with a value naming the
original class (``RuntimeError: TypeName``).
Application code the redaction itself runs (an exception's or a note
object's ``__str__``, a ``__notes__`` or ``__cause__`` property) runs
only while the graph is read (:func:`_plan_redacted_exception`), for
every node, before anything is decided: such code can change any
object, including one already read. Every decision -- which nodes are
kept, and whether the record's ``type`` is -- is made afterwards, on
the objects as they are then, by code that runs no application code
for exception classes that do not hook the type, descriptor and
instance-dict machinery those checks read through
(:func:`_build_redacted_exception`), so for them nothing changes
between those checks and the sinks. A class written to defeat this
is outside the threat model (see the module docstring).
"""
exc = record.get("exception") if record else None
if not exc:
return
value = getattr(exc, "value", None)
if value is None and isinstance(exc, tuple) and len(exc) >= 2:
value = exc[1]
kind = getattr(exc, "type", None)
if kind is None and isinstance(exc, tuple) and len(exc) >= 1:
kind = exc[0]
if value is None:
# No value to redact (``logger.opt(exception=(Type, None, tb))``),
# but the sink still pickles ``type``: one it cannot keep is
# replaced by a ``RuntimeError`` naming it, as the traceback module
# renders a type with no value.
if kind is None:
return
with _decision_phase():
kind_is_safe = _record_type_is_safe(kind)
if kind_is_safe:
return
new_value = RuntimeError(_named_text(kind, ""))
traceback = getattr(exc, "traceback", None)
if traceback is None and isinstance(exc, tuple) and len(exc) >= 3:
traceback = exc[2]
new_value.__traceback__ = traceback
_replace_record_exception(record, exc, RuntimeError, new_value)
return
plans: dict = {}
root = _plan_redacted_exception(
value, plans, set(), [_LOG_EXCEPTION_NODE_BUDGET]
)
# Every step that can run application code has run. Everything below
# reads the objects as they are now and runs builtin code only, so no
# code can change a node, or the record's type, after it was checked.
# Checks that depend on a class alone (its pickling, rendering hooks
# and names, and ``sys.modules``'s keys) are answered once per class
# for all of them (:func:`_once_per_phase`).
with _decision_phase():
kind_is_safe = _record_type_is_safe(kind)
# A top node whose type cannot stay in the record
# (:func:`_record_type_is_safe`) is rebuilt even when its text is
# clean, so that its builtin type replaces it.
new_value = _build_redacted_exception(
root, plans, {}, force=None if kind_is_safe else root
)
if new_value is value:
return
# ``type`` stays the original class when it is safe to keep (the DB
# row's prefix names it); otherwise it becomes the rebuilt value's
# builtin type.
new_kind = kind if kind_is_safe else type(new_value)
_replace_record_exception(record, exc, new_kind, new_value)
def _replace_record_exception(
record: Any, exc: Any, kind: Any, value: Any
) -> None:
"""Set *record*'s exception to *exc* with *kind* and *value* replaced."""
if hasattr(exc, "_replace"):
record["exception"] = exc._replace(type=kind, value=value)
elif isinstance(exc, tuple) and len(exc) >= 3:
record["exception"] = (kind, value, exc[2])
else:
record["exception"] = None
def _exception_redaction_failed(exc: Any) -> Any:
"""Replacement for a record exception whose redaction raised.
Keeps the traceback (frames and source lines carry no exception text)
and replaces the value with a fixed placeholder, so the record neither
ships unredacted exception text nor silently loses where the error
happened. ``None`` when even that cannot be built.
"""
try:
traceback = getattr(exc, "traceback", None)
kind = getattr(exc, "type", None)
name = _type_name(kind, "Exception")
placeholder = RuntimeError(
f"{name}: <exception text omitted: redaction failed>"
)
placeholder.__traceback__ = traceback
if not _record_type_is_safe(kind):
kind = RuntimeError
return exc._replace(type=kind, value=placeholder, traceback=traceback)
except Exception:
return None
# ``BaseException``'s own ``__traceback__`` descriptor: the traceback a node
# carries, read without running anything its class defines.
_BASE_EXCEPTION_TRACEBACK = vars(BaseException)["__traceback__"]
@dataclasses.dataclass(eq=False)
class _ExceptionNodePlan:
"""What was read from one exception node and redacted, before any decision.
:func:`_plan_redacted_exception` fills one per node; it is the pass
that runs every step that can run application code.
:func:`_build_redacted_exception` then decides, from this and from the
node as it is by then, whether the original can be kept, and otherwise
rebuilds it from the texts recorded here. A child is referred to by the
``id()`` of the original (a key of the plans, which hold the originals,
so the ids stay unique), by a placeholder text, or by ``None``.
"""
node: Any
kind: Any
is_group: bool
cause: Any
cause_ref: Any
context: Any
context_ref: Any
suppress_context: Any
subs: tuple
sub_refs: list
traceback: Any
rendered_message: str
message: str
safe_message: str
note_texts: list
safe_notes: list
# The texts read here were clean, as one would render them: printable,
# unchanged by redaction, with notes that are a list or tuple of exact
# ``str``. A node is kept only when this holds and it is unchanged.
text_clean: bool
def _plan_redacted_exception(
node: Any, plans: dict, in_progress: set, budget: list
) -> Any:
"""Read and redact the exception graph under *node*, deciding nothing.
Every step of the redaction that can run application code runs here,
for every node, before anything is decided: an exception's or a note
object's ``str()``, a ``__notes__``, ``__cause__``, ``__context__``,
``exceptions`` or ``message`` that a class overrides, a group's
message. That code can change any object, a node already read here
included, so :func:`_build_redacted_exception` makes every decision
afterwards, against the objects as they are by then. Each node's texts
are read once and recorded (:class:`_ExceptionNodePlan`), and a node
that is rebuilt carries exactly those texts.
Returns the reference to *node*'s plan: its ``id()``, a placeholder
text (the traversal budget ran out, which bounds both depth and group
width, or the node is its own ancestor), or ``None``.
"""
if node is None:
return None
if budget[0] <= 0:
return "<exception traversal limit reached>"
# Cached children still consume work and space in a group's output.
budget[0] -= 1
key = id(node)
if key in plans:
return key
if key in in_progress:
return "<exception cycle omitted>"
in_progress.add(key)
try:
cause = node.__cause__
cause_ref = _plan_redacted_exception(cause, plans, in_progress, budget)
suppress_context = node.__suppress_context__
# Not only when there is no cause: the sinks render each exception
# once, so a cause already rendered elsewhere in the graph (shared
# by two group members) is skipped, and an unsuppressed context is
# rendered in its place.
context = node.__context__ if not suppress_context else None
context_ref = _plan_redacted_exception(
context, plans, in_progress, budget
)
is_group = isinstance(node, BaseExceptionGroup)
subs = tuple(node.exceptions) if is_group else ()
sub_refs: list = []
for child in subs:
if budget[0] <= 0:
sub_refs.append("<remaining exceptions omitted>")
break
sub_refs.append(
_plan_redacted_exception(child, plans, in_progress, budget)
)
# The text is read once here and, if the node is rebuilt, the
# rebuilt node carries exactly what was checked. A ``str()`` that
# raised (the placeholder below is not what a sink would render)
# rules out keeping the original.
try:
rendered_message = _exact_str(str(node))
printable = True
except Exception:
rendered_message = _unprintable_placeholder(node)
printable = False
if is_group:
try:
message = node.message
except Exception:
message = None
if isinstance(message, str):
# A ``str`` subclass compares and renders on its own terms;
# check, compare and rebuild with its plain characters.
message = _exact_str(message)
else:
message = _unprintable_placeholder(node)
elif printable and issubclass(type(node), SyntaxError):
# The header line shows ``msg``, not ``str(node)`` with its
# `` (file, line N)`` suffix (:func:`_syntax_error_header_text`):
# a rebuilt node carries ``msg`` alone (the file and line are
# details it drops), redacted as one with the notes printed
# right after it.
try:
message = _syntax_error_header_text(node, location=False)
except Exception:
message = rendered_message
else:
message = rendered_message
safe_message = _redact_exception_text(message)
# A group renders ``str(node)`` (message plus sub-exception count),
# and a ``SyntaxError``'s ``str()`` (which the DB row renders) is
# not its header text; for any other exception ``str(node)`` is
# ``message`` itself, so there this check would only repeat
# ``safe_message != message`` below: the guard only saves that
# second redaction pass (dropping it is an equivalent mutant, not a
# missing check).
rendered_changed = (
is_group or message is not rendered_message
) and _redact_exception_text(rendered_message) != rendered_message
# A ``__notes__`` property that raises: the traceback module reports
# it with the error's repr ("Ignored error getting __notes__: ..."),
# which is not redacted, so the node is rebuilt with a placeholder
# note instead (the placeholder alone marks the node as changed).
# The same placeholder covers notes that raise while being iterated.
notes_failed = False
try:
note_texts, notes_exact = _rendered_exception_notes(node)
except Exception:
notes_failed = True
note_texts, notes_exact = [], True
safe_notes = [_redact_exception_text(note) for note in note_texts]
if notes_failed:
safe_notes = ["<exception notes omitted: __notes__ raised>"]
plans[key] = _ExceptionNodePlan(
node=node,
kind=type(node),
is_group=is_group,
cause=cause,
cause_ref=cause_ref,
context=context,
context_ref=context_ref,
suppress_context=suppress_context,
subs=subs,
sub_refs=sub_refs,
traceback=_BASE_EXCEPTION_TRACEBACK.__get__(node),
rendered_message=rendered_message,
message=message,
safe_message=safe_message,
note_texts=note_texts,
safe_notes=safe_notes,
text_clean=(
printable
and not notes_failed
and notes_exact
and safe_message == message
and not rendered_changed
and safe_notes == note_texts
),
)
return key
finally:
in_progress.discard(key)
def _build_redacted_exception(
ref: Any, plans: dict, built: dict, force: Any = None
) -> Any:
"""The graph the sinks get for plan *ref*: originals kept, the rest rebuilt.
Runs after :func:`_plan_redacted_exception` has read the whole graph,
and runs no application code itself for exception classes and objects
that do not hook the machinery it reads through (see the module
docstring's threat model), so for those nothing can change a node
between the decision to keep it and the sinks rendering it. A node
retains identity only when its recorded text was clean, it is still
the node that text was read from (:func:`_node_is_kept_as_planned`),
and every chained exception and member it links to is kept too; any
other node is rebuilt from the texts recorded for it, so the text,
notes and chained exceptions a sink renders for it are the ones checked
(a node for which the traceback module would compute a "Did you mean"
suggestion is rebuilt, so no suggestion is rendered for it). Rebuilt
nodes are builtin exceptions (a class counts as builtin when
:mod:`builtins` holds it under its name): application constructors,
copy hooks and group derive methods are never called. Omitted branches (traversal
budget, cycles) become placeholders. *force* is the reference of a node
rebuilt even when it could be kept (see :func:`redact_log_exception`);
its chained exceptions and members are not forced.
"""
if ref is None:
return None
if type(ref) is str:
return RuntimeError(ref)
if ref in built:
return built[ref]
plan = plans[ref]
new_cause = _build_redacted_exception(plan.cause_ref, plans, built)
new_context = _build_redacted_exception(plan.context_ref, plans, built)
new_subs = [
_build_redacted_exception(sub_ref, plans, built)
for sub_ref in plan.sub_refs
]
keep = (
ref != force
and new_cause is plan.cause
and new_context is plan.context
and len(new_subs) == len(plan.subs)
and all(new is old for new, old in zip(new_subs, plan.subs))
and _node_is_kept_as_planned(plan)
)
if keep:
new_node = plan.node
else:
new_node = _rebuilt_exception(plan, new_cause, new_context, new_subs)
built[ref] = new_node
return new_node
def _exact_note_texts(node: Any) -> Optional[list]:
"""*node*'s notes when they are a ``list`` or ``tuple`` of exact ``str``.
``[]`` for no notes, ``None`` for notes of any other form. Called only
for a node :func:`_exception_text_is_stable` accepts, for which the
lookup and the iteration run builtin code only.
"""
notes = getattr(node, "__notes__", None)
if notes is None:
return []
if not _type_is_one_of(type(notes), (list, tuple)):
return None
items = list(notes)
if not all(type(item) is str for item in items):
return None
return items
def _node_is_kept_as_planned(plan: _ExceptionNodePlan) -> bool:
"""True when *plan*'s original node may go to the sinks as it is now.
Its recorded texts were clean, and it is still what they were read
from: the same class; its rendering and pickling still run no code of
its own classes (:func:`_exception_text_is_stable`, checked first, by
code that runs none of the node's); and its ``str()``, notes, chained
exceptions, suppress flag, a group's message and members, a
``SyntaxError``'s rendered details, and its type's name joined with
its text and notes (:func:`_type_header_changed`) are as recorded and
clean. Application code that ran while the graph was read (a parent's
or a note object's ``__str__``, say) may have changed any of these
after the node itself was read. Once the stability check passes, every
read and call below runs builtin code only, so nothing changes between
this check and the sinks -- for classes and objects that do not hook
the machinery these checks read through; the module docstring's threat
model says why one that does is not covered.
"""
if not plan.text_clean:
return False
node = plan.node
try:
if type(node) is not plan.kind:
return False
if not _exception_text_is_stable(node, plan.is_group):
return False
if node.__cause__ is not plan.cause:
return False
suppress_context = node.__suppress_context__
if suppress_context is not plan.suppress_context:
return False
context = None if suppress_context else node.__context__
if context is not plan.context:
return False
if plan.is_group:
exceptions = node.exceptions
if len(exceptions) != len(plan.subs) or not all(
live is old for live, old in zip(exceptions, plan.subs)
):
return False
if node.message != plan.message:
return False
rendered = str(node)
if type(rendered) is not str or rendered != plan.rendered_message:
return False
if _exact_note_texts(node) != plan.note_texts:
return False
is_syntax_error = issubclass(plan.kind, SyntaxError)
if is_syntax_error:
if _syntax_error_header_text(node, location=False) != plan.message:
return False
if _syntax_error_details_changed(node):
return False
# The sinks render the type's name, the text and the notes joined,
# so a credential can span them; checked last, as it costs one more
# redaction pass per name form (:func:`_rendered_type_prefixes`: one
# for a builtin class, two for one defined at module level, three
# for a nested one) and header form (two for an empty text, up to
# three for a ``SyntaxError``, one otherwise).
if _type_header_changed(
plan.kind,
rendered,
plan.note_texts,
_rendered_header_tails(node, rendered),
):
return False
# Rendered as the sinks will, last: the caret line is drawn from the
# offsets, and a rendering that raises or runs long makes the sinks'
# error handler print the exception being handled.
if is_syntax_error and not _syntax_error_renders_bounded(node):
return False
except Exception:
return False
return True
def _rebuilt_exception(
plan: _ExceptionNodePlan, new_cause: Any, new_context: Any, new_subs: list
) -> BaseException:
"""A builtin exception carrying the texts recorded in *plan*."""
kind = plan.kind
safe_message = plan.safe_message
if plan.is_group:
# The builtin constructor rebuilds the read-only message and
# members, without trusting a subclass's derive implementation.
# The type's name is read through ``type``'s descriptor, and
# redacted (:func:`_type_name`).
if not _is_builtin_class(kind):
# Keep a group subclass's name visible in the rebuilt group.
safe_message = _redact_exception_text(
f"{_type_name(kind)}: {safe_message}"
)
new_node = BaseExceptionGroup(safe_message, new_subs)
elif _is_builtin_class(kind):
try:
new_node = kind(safe_message)
except Exception:
new_node = RuntimeError(_named_text(kind, safe_message))
else:
new_node = RuntimeError(_named_text(kind, safe_message))
new_node.__traceback__ = plan.traceback
new_node.__cause__ = new_cause
new_node.__context__ = new_context
new_node.__suppress_context__ = plan.suppress_context
safe_notes = plan.safe_notes
if safe_notes:
# Each note was redacted on its own; a credential can still span
# the rebuilt header line and the notes.
header = (
_TYPE_NAME.__get__(type(new_node))
+ _rendered_header_tails(new_node, _exact_str(str(new_node)))[0]
)
safe_notes = _notes_fitting_header(header, safe_notes)
if safe_notes:
new_node.__notes__ = safe_notes
return new_node
def redact_log_record(record) -> None:
"""loguru patcher: control-char strip + credential redaction of a record.
Scrubs the message and bound ``extra`` values (:func:`_redact_log_record_text`,
:func:`redact_log_extra`) and the rendered exception graph
(:func:`redact_log_exception`), so a credential that reaches a record
without going through ``scrub_error`` first -- bare ``field=value`` in
the message, a secret bound via ``logger.bind()``, or one embedded in an
exception's text -- is caught before any sink renders it. Kept here,
rather than in ``log_utils``, because that module imports the web stack
at module scope and the MCP subprocess (``mcp/server.py``'s
``configure_mcp_logging``) must not pull that in: this is the patcher
both processes install (the web process through
``utilities.log_utils._sanitize_record``).
Fail-closed per field: a field that fails to redact is replaced rather
than passed through as-is. Routing keys in ``extra``
(:data:`_LOG_STRUCTURAL_EXTRA_KEYS`) are preserved so a failed field
does not change the destination of a database log record. An exception
that fails to redact keeps its traceback with a placeholder value.
The result is always picklable: an ``enqueue=True`` sink pickles the
record, and a pickling failure makes loguru print the original exception
to stderr. Non-plain ``extra`` values and unsafe exception types are
replaced before reaching a sink. Kept exception nodes are checked for
pickling hooks that could change later rendering, within the threat
model in the module docstring.
The message and every ``extra`` string leaf use
:func:`_redact_log_record_text`, which adds the unprefixed ``Api-Key:``
header, slash-bearing DSN password, separator-delimited credential
parameter, and quoted credential-key patterns after the ordinary
redaction passes. Exception values use :func:`redact_log_exception`,
which applies the patterns to rendered fields too; the database row's
exception prefix applies them separately. Application code invoked while
reading an exception graph runs before the keep decisions and
picklability checks.
"""
try:
# Preserve credential boundaries that control stripping may remove,
# then catch names that become recognizable after normalization.
record["message"] = _redact_log_record_text(record["message"])
except Exception:
record["message"] = "<log message redacted: sanitization error>"
extra = record.get("extra")
if extra:
try:
extra = redact_log_extra(extra)
except Exception:
extra = _structural_extra_only(extra)
record["extra"] = extra
else:
extra = None
exc = record.get("exception")
if exc:
try:
redact_log_exception(record)
except Exception:
record["exception"] = _exception_redaction_failed(exc)
if extra is not None:
# Last, after the exception's redaction, which runs application code
# (:func:`redact_log_exception`) that could change a value passed
# through above (a routing key's); this check runs none.
record["extra"] = _picklable_extra(extra)
def _structural_extra_only(extra: Any) -> dict:
"""The routing keys of *extra* alone; ``{}`` if even that fails.
Taken from the keys *extra* holds, by :func:`_is_structural_extra_key`,
rather than looked up by name: ``extra["username"]`` would also find a
``str`` subclass key that only compares equal to ``"username"``.
"""
try:
return {
key: value
for key, value in dict.items(extra)
if _is_structural_extra_key(key)
}
except Exception:
return {}