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): widen credential-scrub patterns (OAuth/Azure/Google keys, auth headers) (#4640)

LearningCircuit7523c2397399
diff --git a/changelog.d/+widen-credential-scrub-patterns.security.md b/changelog.d/+widen-credential-scrub-patterns.security.md
new file mode 100644
index 0000000000..4996ede96a
--- /dev/null
+++ b/changelog.d/+widen-credential-scrub-patterns.security.md
@@ -0,0 +1 @@
+The shared error-message credential scrubber now also redacts OAuth `access_token`/`refresh_token` and other sensitive URL query parameters, Azure `subscription-key`, `Authorization:`/`x-api-key:` headers, and Google (`AIza…`) API keys — strengthening credential redaction for every code path that surfaces error text to clients or logs.
diff --git a/src/local_deep_research/security/log_sanitizer.py b/src/local_deep_research/security/log_sanitizer.py
index 5f14e78475..7c65ff4cfc 100644
--- a/src/local_deep_research/security/log_sanitizer.py
+++ b/src/local_deep_research/security/log_sanitizer.py
@@ -134,15 +134,55 @@ def redact_secrets(
_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)
(
re.compile(r"(https?://)([^:\s]+):([^@\s]+)@"),
r"\1[REDACTED]:[REDACTED]@",
),
- # API keys in URL query strings (?key=..., &api_key=..., etc.)
+ # 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"([?&])(api_key|apikey|api-key|key|token|secret)=([A-Za-z0-9\-._~+/]+)"
+ r"(?i)([?&])("
+ r"api[_-]?key|access[_-]?token|refresh[_-]?token|id[_-]?token|"
+ r"auth[_-]?token|session[_-]?token|secret[_-]?key|"
+ r"subscription[_-]?key|client[_-]?secret|private[_-]?key|"
+ r"key|token|secret|password|passwd|pwd"
+ r")=([^&\s#]+)"
),
r"\1\2=[REDACTED]",
),
@@ -150,6 +190,9 @@ _CREDENTIAL_PATTERNS: list[tuple[re.Pattern[str], str]] = [
# 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]"),
]
@@ -163,9 +206,11 @@ def sanitize_error_message(message: str) -> str:
Handles:
* Bearer tokens (``Bearer sk-...``)
- * URL query parameters (``?api_key=...``, ``?key=...``, ``?api-key=...``)
+ * ``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 key prefixes (``sk-...``, ``pk-...``)
+ * Well-known key prefixes (``sk-...``, ``pk-...``) and Google (``AIza...``)
"""
if not message:
return message
diff --git a/tests/security/test_log_sanitizer.py b/tests/security/test_log_sanitizer.py
index 6601f50afb..1c9c2ced41 100644
--- a/tests/security/test_log_sanitizer.py
+++ b/tests/security/test_log_sanitizer.py
@@ -237,6 +237,90 @@ class TestSanitizeErrorForClient:
assert key not in result
assert "sk-abcdefghij" not in result
+ @pytest.mark.parametrize(
+ "msg, secret",
+ [
+ (
+ "err https://api.x.com?access_token=SECRET_AT_123 fail",
+ "SECRET_AT_123",
+ ),
+ ("https://api.x.com?refresh_token=SECRET_RT_123", "SECRET_RT_123"),
+ (
+ "https://x.cognitive.microsoft.com?subscription-key=SECRET_SUB1",
+ "SECRET_SUB1",
+ ),
+ ("oauth ?client_secret=SECRET_CS_123&grant=x", "SECRET_CS_123"),
+ ("?secret_key=SECRET_SK_123&page=2", "SECRET_SK_123"),
+ (
+ "401 Authorization: Basic SECRETBASICVALUE123 denied",
+ "SECRETBASICVALUE123",
+ ),
+ (
+ "Authorization: token ghp_TOKENVALUE1234567890 x",
+ "ghp_TOKENVALUE1234567890",
+ ),
+ (
+ "req x-api-key: SECRETXAPIKEYVALUE12345 blocked",
+ "SECRETXAPIKEYVALUE12345",
+ ),
+ # Schemeless Authorization carrying a token-shaped raw value.
+ (
+ "Authorization: ghp_RAWTOKEN1234567890 rejected",
+ "ghp_RAWTOKEN1234567890",
+ ),
+ # Anchored (scheme / x-api-key) values redact regardless of shape,
+ # incl. purely-alphabetic ones — the anchor rules out prose.
+ (
+ "Authorization: Basic ABCDEFGHIJKLMNOP denied",
+ "ABCDEFGHIJKLMNOP",
+ ),
+ (
+ "x-api-key: ALPHAONLYKEYVALUEABCDEFGH blocked",
+ "ALPHAONLYKEYVALUEABCDEFGH",
+ ),
+ (
+ "google AIzaSyD-SECRET_GOOGLE_KEY_1234567890 abc",
+ "AIzaSyD-SECRET_GOOGLE_KEY_1234567890",
+ ),
+ # Case-insensitive query params (capitalized names must not bypass).
+ ("https://x.com?API_KEY=SECRET_UP1", "SECRET_UP1"),
+ ("https://x.com?Access_Token=SECRET_MIX1", "SECRET_MIX1"),
+ ],
+ )
+ def test_redacts_additional_credential_formats(self, msg, secret):
+ """OAuth tokens, Azure subscription keys, Authorization/x-api-key
+ headers, Google API keys, and case-variant query params must all be
+ scrubbed (#4633 follow-up)."""
+ assert secret not in sanitize_error_for_client(msg)
+
+ @pytest.mark.parametrize(
+ "msg, preserved",
+ [
+ ("page request ?page=2&limit=10", "page=2"),
+ ("Basic understanding of the topic", "understanding"),
+ ("key concepts explained here", "concepts"),
+ ("fetched https://example.com/path?id=42 ok", "id=42"),
+ # Schemeless "Authorization:" followed by an all-alphabetic word
+ # is prose, not a token — the token-shaped requirement on the
+ # schemeless branch keeps it intact.
+ ("401: Authorization: required for this endpoint", "required"),
+ ("Authorization: denied because user lacks role", "denied"),
+ # Short values below the floors are left intact (x-api-key needs
+ # >=16, the scheme branch needs >=8).
+ ("Response 403: x-api-key: invalid or expired", "invalid"),
+ ("Header x-api-key: missing from request", "missing"),
+ ("Authorization: bearer ab12 failed", "ab12"),
+ ("x-api-key: abc12 next", "abc12"),
+ ],
+ )
+ def test_does_not_over_redact_innocuous_text(self, msg, preserved):
+ """Non-credential query params, schemeless-Authorization prose, and
+ sub-floor values must survive untouched (no false-positive
+ redaction)."""
+ 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,