Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(security): scrub provider token prefixes from error text (#4664)

LearningCircuit03330341984a
diff --git a/changelog.d/+scrub-more-credential-prefixes.security.md b/changelog.d/+scrub-more-credential-prefixes.security.md
new file mode 100644
index 0000000000..b2e175f4a9
--- /dev/null
+++ b/changelog.d/+scrub-more-credential-prefixes.security.md
@@ -0,0 +1 @@
+The error-message credential scrubber now also redacts GitHub (`ghp_`/`github_pat_`), AWS (`AKIA`/`ASIA`/…), Slack (`xox*-`/`xapp-`), Google OAuth (`ya29.`) tokens and JWTs — extending coverage to more common credential formats before error text reaches clients or logs.
diff --git a/docs/developing/credential-scrubbing.md b/docs/developing/credential-scrubbing.md
new file mode 100644
index 0000000000..2e69d3551b
--- /dev/null
+++ b/docs/developing/credential-scrubbing.md
@@ -0,0 +1,55 @@
+# Credential scrubbing in error/log text
+
+`local_deep_research.security.log_sanitizer` scrubs credentials out of
+exception/error strings **before** they reach a client (HTTP/SSE/JSON
+responses) or the logs. It is a runtime, defense-in-depth *sanitizer* — not a
+git/CI secret *scanner* (use [gitleaks](https://github.com/gitleaks/gitleaks)
+for that, which this repo already runs in pre-commit/CI).
+
+## The two layers
+
+1. **`sanitize_error_message(text)`** — a regex first-pass over `_CREDENTIAL_PATTERNS`
+ for *common credential shapes* (API-key query params, `Authorization:`/
+ `x-api-key:` headers, `user:pass@host`, and well-known token prefixes:
+ `sk-`/`pk-`, Google `AIza`/`ya29.`, GitHub `ghp_`/`github_pat_`, AWS
+ `AKIA`/`ASIA`, Slack `xox*-`, JWTs). `sanitize_error_for_client()` composes
+ it with control-char stripping + length capping.
+2. **`redact_secrets(text, *known_literals)`** — the **backstop**. When you
+ hold the actual secret value (e.g. the configured API key), pass it here so
+ it is scrubbed regardless of shape. This is the real guarantee for
+ *known* secrets; the regex layer is best-effort for *unknown* ones.
+
+Always pair them ("dual-scrub") on any path that surfaces secret-adjacent text.
+
+## Design constraints (why it is curated, not exhaustive)
+
+This runs on **arbitrary, possibly attacker-influenced** strings at runtime, so:
+
+- **No ReDoS.** Every pattern must scale linearly — prefer prefix-anchored,
+ single-quantifier regexes; avoid nested quantifiers / ambiguous alternations.
+ Spot-check new patterns on a 200k-char adversarial input.
+- **Over-redaction is the safe failure; under-redaction (a leak) is not** —
+ but gratuitous over-redaction harms log readability, so patterns are
+ anchored/length-floored to avoid eating prose.
+- Keep the set **small and high-signal**. Matching gitleaks' full 100+ rules
+ at runtime is overkill and raises false positives; the `redact_secrets`
+ backstop covers the long tail for known secrets.
+
+## Keeping the patterns current
+
+The prefix regexes mirror the **canonical, actively-maintained gitleaks
+ruleset** — gitleaks' own upstream `config/gitleaks.toml`
+(<https://github.com/gitleaks/gitleaks>), **not** this repo's root
+`.gitleaks.toml` (which only configures the gitleaks *scan* run in
+pre-commit/CI). To refresh when a provider introduces a new token format:
+
+1. Find the rule in gitleaks' upstream `config/gitleaks.toml` (or detect-secrets).
+2. Adapt the regex to a prefix-anchored, ReDoS-safe form here; redact to
+ `[REDACTED_KEY]`.
+3. Add a positive (redacts) **and** a negative (prose not over-redacted) case
+ to `tests/security/test_log_sanitizer.py`.
+4. Run `pytest tests/security/` and a 200k-char ReDoS spot-check.
+
+A periodic (e.g. quarterly) glance at gitleaks' changelog for new
+widely-used token prefixes is enough — this layer only needs the formats that
+plausibly appear in *this app's* error/log text.
diff --git a/src/local_deep_research/security/log_sanitizer.py b/src/local_deep_research/security/log_sanitizer.py
index 7c65ff4cfc..c1f0ae908e 100644
--- a/src/local_deep_research/security/log_sanitizer.py
+++ b/src/local_deep_research/security/log_sanitizer.py
@@ -193,6 +193,56 @@ _CREDENTIAL_PATTERNS: list[tuple[re.Pattern[str], str]] = [
# 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.
+ (
+ re.compile(
+ r"\bxox[baeprs]-(?=[A-Za-z0-9-]*[0-9]{9,})[A-Za-z0-9-]{10,}\b"
+ ),
+ "[REDACTED_KEY]",
+ ),
+ (
+ re.compile(r"\bxapp-(?=[A-Za-z0-9-]*[0-9]{9,})[A-Za-z0-9-]{10,}\b"),
+ "[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.
+ (
+ re.compile(
+ r"\beyJ[A-Za-z0-9_+\-]{8,}\.[A-Za-z0-9_+\-]{8,}\.[A-Za-z0-9_+\-]+"
+ ),
+ "[REDACTED_KEY]",
+ ),
]
@@ -210,7 +260,9 @@ def sanitize_error_message(message: str) -> str:
* URL query parameters (``?api_key=``, ``?access_token=``,
``?refresh_token=``, ``?subscription-key=``, ``?secret=``, ...)
* URL-embedded credentials (``https://user:pass@host``)
- * Well-known key prefixes (``sk-...``, ``pk-...``) and Google (``AIza...``)
+ * 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``.
"""
if not message:
return message
diff --git a/tests/security/test_log_sanitizer.py b/tests/security/test_log_sanitizer.py
index 1c9c2ced41..ba33fb291e 100644
--- a/tests/security/test_log_sanitizer.py
+++ b/tests/security/test_log_sanitizer.py
@@ -321,6 +321,72 @@ class TestSanitizeErrorForClient:
assert preserved in result
assert "REDACTED" not in result
+ @pytest.mark.parametrize(
+ "msg, secret",
+ [
+ (
+ "failed ghp_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 here",
+ "ghp_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
+ ),
+ (
+ "github_pat_11ABCDE0aBcDeFgHiJ_kLmNoPqRsTuVwXyZ0123456789AbCdEf x",
+ "github_pat_11ABCDE0aBcDeFgHiJ_kLmNoPqRsTuVwXyZ0123456789AbCdEf",
+ ),
+ ("AWS AKIAIOSFODNN7EXAMPLE rejected", "AKIAIOSFODNN7EXAMPLE"),
+ ("creds ASIAY34FZKBOKMUTVV7A expired", "ASIAY34FZKBOKMUTVV7A"),
+ (
+ # Prefix split ("xox" + ...) so the literal isn't a scannable
+ # Slack-token shape that trips GitHub push protection.
+ "slack " + "xox" + "b-2345678901-abcdefABCDEF123456 failed",
+ "xox" + "b-2345678901-abcdefABCDEF123456",
+ ),
+ (
+ "cfg " + "xox" + "e-1-AB-2345678901-abcdef123456 expired",
+ "xox" + "e-1-AB-2345678901-abcdef123456",
+ ),
+ (
+ "app " + "xa" + "pp-1-A012345-678901234567-abcdef0123 bad",
+ "xa" + "pp-1-A012345-678901234567-abcdef0123",
+ ),
+ (
+ "token ya29.a0AfH6SMBsecretOAUTH_token-12345 expired",
+ "ya29.a0AfH6SMBsecretOAUTH_token-12345",
+ ),
+ (
+ "auth eyJhbGciOiJIUzI1Ni2.eyJzdWIiOiIxMjM0NTY3OD2.dozjgNryP4 bad",
+ # assert the WHOLE JWT is gone, not just one segment
+ "eyJhbGciOiJIUzI1Ni2.eyJzdWIiOiIxMjM0NTY3OD2.dozjgNryP4",
+ ),
+ ],
+ )
+ def test_redacts_provider_token_prefixes(self, msg, secret):
+ """GitHub/AWS/Slack/Google-OAuth tokens and JWTs (canonical gitleaks
+ prefixes) must be scrubbed from error text."""
+ assert secret not in sanitize_error_for_client(msg)
+
+ @pytest.mark.parametrize(
+ "msg, preserved",
+ [
+ ("the AKIApattern doc explains this", "AKIApattern"),
+ ("ghp build pipeline failed", "ghp"),
+ ("see ya29 release notes for details", "ya29"),
+ ("eyJot just an ordinary word here", "eyJot"),
+ # The generic Slack ``xapp``/``xox`` prefixes require a long
+ # numeric workspace ID, so hyphenated prose that merely starts
+ # with them (and carries no 9+ digit run) survives untouched.
+ ("xapp-release-notes-2026 published", "xapp-release-notes-2026"),
+ ("rolling out xapp-config-reload now", "xapp-config-reload"),
+ ("xoxe-release-bundle-2026 attached", "xoxe-release-bundle-2026"),
+ ("module xoxb-internal-doc loaded", "xoxb-internal-doc"),
+ ],
+ )
+ def test_token_prefixes_do_not_over_redact_prose(self, msg, preserved):
+ """The prefix patterns are distinctive/length-floored — ordinary
+ prose that merely starts with a prefix is not redacted."""
+ result = sanitize_error_for_client(msg)
+ assert preserved in result
+ assert "REDACTED" not in result
+
def test_importable_from_security_package(self):
from local_deep_research.security import (
sanitize_error_for_client as exported,