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.

security(log-sanitizer): redact URL credentials in any scheme, including DB DSNs (#4933)

LearningCircuitd220c0218108
diff --git a/changelog.d/4933.security.md b/changelog.d/4933.security.md
new file mode 100644
index 0000000000..ca5a98fc10
--- /dev/null
+++ b/changelog.d/4933.security.md
@@ -0,0 +1 @@
+The central credential sanitizer (`sanitize_error_message` / `sanitize_error_for_client`) now redacts URL userinfo credentials in *any* scheme (case-insensitive), not just `http(s)`. Raw **URL-form** database connection errors — SQLAlchemy `dialect+driver` DSNs (`postgresql+psycopg2://user:pass@host`, `mysql+pymysql://…`, `mongodb+srv://…`), plain DB schemes, and password-only DSNs (`redis://:pass@host`) — previously leaked their password through any surfaced exception message; the password is now replaced with `[REDACTED]`. Credential-less DSNs with a port (`postgresql://host:5432/db`) and all existing `http(s)` behavior are unchanged. Key=value DSNs (e.g. pyodbc `Server=...;Pwd=...`) have no `://` and remain out of scope for a userinfo regex. This benefits every consumer of the central sanitizer (download service, fetch tool, agent strategies, search engines, and the news error-response backstop).
diff --git a/src/local_deep_research/security/log_sanitizer.py b/src/local_deep_research/security/log_sanitizer.py
index c1f0ae908e..920ec58fa6 100644
--- a/src/local_deep_research/security/log_sanitizer.py
+++ b/src/local_deep_research/security/log_sanitizer.py
@@ -167,9 +167,27 @@ _CREDENTIAL_PATTERNS: list[tuple[re.Pattern[str], str]] = [
re.compile(r"(?i)(x-api-key\s*[:=]\s*)[A-Za-z0-9\-._~+/]{16,}=*"),
r"\1[REDACTED]",
),
- # URL credentials (user:pass@host)
+ # 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"(https?://)([^:\s]+):([^@\s]+)@"),
+ 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=...).
diff --git a/tests/security/test_sanitize_error_message.py b/tests/security/test_sanitize_error_message.py
index aaa7e7e70f..a1751bc6d6 100644
--- a/tests/security/test_sanitize_error_message.py
+++ b/tests/security/test_sanitize_error_message.py
@@ -91,6 +91,83 @@ class TestSanitizeErrorMessageStandalone:
assert "p4ssw0rd" not in result
assert "svc:" not in result
+
+class TestDatabaseDsnCredentials:
+ """URL-credential redaction covers database DSNs, not just http(s).
+
+ A raw SQLAlchemy / driver connection error is the most common way a
+ credential-bearing string reaches an exception message. The userinfo in
+ any URL scheme is a credential, so it is redacted regardless of scheme.
+ """
+
+ def test_postgres_dsn_password_redacted(self):
+ msg = (
+ "OperationalError: could not connect to "
+ "postgresql://newsuser:[email protected]:5432/news"
+ )
+ result = sanitize_error_message(msg)
+ assert "s3cr3tPw0rd" not in result
+ assert "newsuser" not in result
+ assert "[REDACTED]:[REDACTED]@db.internal" in result
+
+ def test_sqlalchemy_dialect_driver_scheme_redacted(self):
+ # SQLAlchemy DSNs carry a `dialect+driver` scheme.
+ for msg in (
+ "postgresql+psycopg2://user:pa55@dbhost/appdb",
+ "mysql+pymysql://root:toor@localhost:3306/db",
+ "mongodb+srv://appuser:[email protected]/test",
+ ):
+ result = sanitize_error_message(msg)
+ assert "[REDACTED]:[REDACTED]@" in result, msg
+ assert "toor" not in sanitize_error_message(
+ "mysql+pymysql://root:toor@localhost:3306/db"
+ )
+
+ def test_password_only_redis_dsn_redacted(self):
+ # redis uses a password-only DSN (empty username).
+ msg = "redis://:[email protected]:6379/0 connection reset"
+ result = sanitize_error_message(msg)
+ assert "mypassword" not in result
+ assert "[REDACTED]:[REDACTED]@" in result
+
+ def test_credential_less_dsn_with_port_unchanged(self):
+ # No userinfo (just host:port) must NOT be touched — the trailing `@`
+ # is required and `/` is excluded from the userinfo.
+ for msg in (
+ "connect failed to postgresql://dbhost:5432/news",
+ "redis://cache.local:6379/0 timed out",
+ ):
+ assert sanitize_error_message(msg) == msg
+
+ def test_http_credentials_still_redacted(self):
+ # Regression guard: generalizing the scheme must not lose http(s).
+ msg = "Connection to https://admin:[email protected] failed"
+ result = sanitize_error_message(msg)
+ assert "supersecretpassword" not in result
+ assert "[REDACTED]:[REDACTED]@" in result
+
+ def test_uppercase_scheme_redacted(self):
+ # URL schemes are case-insensitive (RFC 3986); an uppercase/mixed-case
+ # DSN scheme must not defeat redaction.
+ for msg in (
+ "POSTGRESQL://newsuser:[email protected]:5432/news",
+ "Postgresql+Psycopg2://user:pa55@dbhost/appdb",
+ ):
+ result = sanitize_error_message(msg)
+ assert "s3cr3tPw0rd" not in result
+ assert "pa55" not in result
+ assert "[REDACTED]:[REDACTED]@" in result
+
+ def test_url_glued_to_preceding_word_char_still_redacted(self):
+ # Regression guard against a leading `\b` anchor: a credential URL
+ # glued to a preceding word char must still have its userinfo redacted
+ # (the scheme's first char is a word char, so `\b` would fail to anchor
+ # here and leak the password).
+ msg = "prefixXhttps://admin:supersecretpassword@host/path"
+ result = sanitize_error_message(msg)
+ assert "supersecretpassword" not in result
+ assert "[REDACTED]:[REDACTED]@" in result
+
def test_sk_prefix_redacted(self):
"""OpenAI-style key with modern hyphenated format (sk-proj-...)."""
msg = "Invalid API key: sk-proj-abc123xyz456def789ghi012jkl"