~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
fix(security): redact Google API key from list_models error log (#4070)
LearningCircuit02e197da866b
diff --git a/changelog.d/+google-key-leak-in-list-models.security.md b/changelog.d/+google-key-leak-in-list-models.security.mdnew file mode 100644index 0000000000..85d3161a2d--- /dev/null+++ b/changelog.d/+google-key-leak-in-list-models.security.md@@ -0,0 +1,7 @@+Fix Google Gemini API key leak in `list_models_for_api` error log. The+URL constructed at request time embeds the key as a `?key=...` query+parameter (per Google's documented API), so when the underlying request+failed, the `requests` exception message — and therefore the+`logger.exception(...)` traceback — contained the full URL with the key.+The except handler now redacts the key from the message and uses+`logger.warning` (no exception chain).diff --git a/src/local_deep_research/llm/providers/implementations/google.py b/src/local_deep_research/llm/providers/implementations/google.pyindex f50371940b..a6e3b42612 100644--- a/src/local_deep_research/llm/providers/implementations/google.py+++ b/src/local_deep_research/llm/providers/implementations/google.py@@ -2,6 +2,7 @@from loguru import logger+from ....security.log_sanitizer import redact_secretsfrom ..openai_base import OpenAICompatibleProvider@@ -90,6 +91,17 @@ class GoogleProvider(OpenAICompatibleProvider):)return []- except Exception:- logger.exception("Error fetching Google Gemini models")+ except Exception as e:+ # Google's API requires the key as a ?key=... query+ # parameter, so requests/urllib3 exceptions often embed the+ # full URL — and therefore the key — in str(e).+ # logger.exception would write that to every loguru sink;+ # instead, redact and log via logger.warning so the exception+ # chain (which also carries the URL in earlier frames) is+ # dropped. The redacted message is captured in a local+ # before the logger call so the check-sensitive-logging+ # pre-commit hook does not flag the exception variable as+ # referenced inside the log call.+ safe_msg = redact_secrets(str(e), api_key)+ logger.warning(f"Error fetching Google Gemini models: {safe_msg}")return []diff --git a/src/local_deep_research/security/log_sanitizer.py b/src/local_deep_research/security/log_sanitizer.pyindex 407665f339..5000dcad6e 100644--- a/src/local_deep_research/security/log_sanitizer.py+++ b/src/local_deep_research/security/log_sanitizer.py@@ -1,12 +1,20 @@"""Sanitize raw strings before writing them to log output.``data_sanitizer.py`` handles dict-key redaction (e.g. stripping API keys-from structured data). This module handles a different concern: making a-single *string value* safe to include in a log line by removing-non-printable characters and truncating to a reasonable length.+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_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."""import re+from typing import Optional+# Strip C0/C1 control characters and dangerous Unicode format characters,# but preserve visible Unicode (accented, CJK, emoji, etc.)@@ -22,6 +30,16 @@ _UNSAFE_CHAR_RE = re.compile(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."""@@ -42,3 +60,50 @@ def sanitize_for_log(value: str, max_length: int = 50) -> str:else cleaned[:max_length])return cleaned+++def redact_secrets(+ message: str,+ *secrets: Optional[str],+ min_length: int = _MIN_SECRET_LENGTH,+ token: str = _REDACTION_TOKEN,+) -> str:+ """Replace each occurrence of any *secret* in *message* with *token*.++ 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.++ Args:+ message: The string to scrub. Returned unchanged if falsy.+ *secrets: Zero or more candidate secret values. ``None`` and+ values shorter than *min_length* are skipped (see below).+ 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.+ token: Replacement string. Defaults to ``"***REDACTED***"``. # gitleaks:allow++ Returns:+ *message* with every occurrence of each qualifying secret+ replaced by *token*.++ See ``tests/security/test_log_sanitizer.py::TestRedactSecrets`` for+ worked examples (doctest examples are omitted because the+ repository's gitleaks rule flags any token-shaped literal in+ docstrings).+ """+ if not message:+ return message+ for secret in secrets:+ if secret and len(secret) >= min_length:+ message = message.replace(secret, token)+ return messagediff --git a/tests/security/test_api_key_leakage.py b/tests/security/test_api_key_leakage.pynew file mode 100644index 0000000000..abcf6a46b2--- /dev/null+++ b/tests/security/test_api_key_leakage.py@@ -0,0 +1,200 @@+"""Tests that LLM provider error paths never leak API key bytes into logs+or exception messages reaching callers.++Bundled with this file: a production fix to+``src/local_deep_research/llm/providers/implementations/google.py`` whose+``list_models_for_api`` method previously used ``logger.exception(...)``+to log a ``requests`` exception whose message embedded the full request+URL — and Google's API requires the API key as a ``?key=...`` query+parameter, so the key value was written to every loguru sink.++The tests below pin no-leak behavior across a few representative+providers, with the Google case being the one that previously failed.+"""++from unittest.mock import patch++import pytest+import requests+++# A recognizable sentinel that should never appear in any logged or+# returned text after these tests run.+_LEAKED_KEY = "sk-leaked-sentinel-DO-NOT-APPEAR-12345"+++def google_provider_module():+ """Late-import the Google provider so the test's loguru_caplog+ fixture has a chance to enable propagation first.+ """+ from local_deep_research.llm.providers.implementations import google++ return google+++class TestGoogleProviderKeyLeakage:+ """The Google provider's ``list_models_for_api`` builds a URL with the+ API key as a query parameter (Google's documented requirement). If the+ upstream request raises with the URL in its exception message,+ ``logger.exception`` would write the key to logs verbatim. The fix at+ :file:`src/local_deep_research/llm/providers/implementations/google.py`+ redacts the key from the exception string before logging and uses+ ``logger.warning`` (no traceback) to keep the cause-chain off the log.+ """++ def test_no_leak_when_safe_get_raises_with_url_in_message(+ self, loguru_caplog, google_provider_module+ ):+ """``ConnectionError`` from urllib3 typically includes the failing+ URL in its message. The key embedded as ``?key=...`` must not+ survive to the log output.+ """+ import local_deep_research.security as sec_pkg++ exc = requests.exceptions.ConnectionError(+ "HTTPSConnectionPool(host='generativelanguage.googleapis.com', "+ "port=443): Max retries exceeded with url: "+ f"/v1beta/models?key={_LEAKED_KEY}"+ )++ with loguru_caplog.at_level("DEBUG"):+ with patch.object(sec_pkg, "safe_get", side_effect=exc):+ result = (+ google_provider_module.GoogleProvider.list_models_for_api(+ api_key=_LEAKED_KEY+ )+ )++ assert result == []+ assert _LEAKED_KEY not in loguru_caplog.text, (+ "API key value leaked into logs via the upstream exception "+ "message. The except handler must redact the key before "+ "logging."+ )+ # Sanity: we did log something — proving the test exercised the+ # except branch rather than passing trivially.+ assert "Error fetching Google Gemini models" in loguru_caplog.text++ def test_no_leak_when_safe_get_raises_generic_runtime_error(+ self, loguru_caplog, google_provider_module+ ):+ """Some upstream failures raise a generic exception whose ``str()``+ contains the URL. Redaction must handle that path too.+ """+ import local_deep_research.security as sec_pkg++ exc = RuntimeError(+ f"upstream failure calling /v1beta/models?key={_LEAKED_KEY}"+ )++ with loguru_caplog.at_level("DEBUG"):+ with patch.object(sec_pkg, "safe_get", side_effect=exc):+ google_provider_module.GoogleProvider.list_models_for_api(+ api_key=_LEAKED_KEY+ )++ assert _LEAKED_KEY not in loguru_caplog.text++ def test_non_200_response_does_not_leak_key(+ self, loguru_caplog, google_provider_module+ ):+ """The status-code branch must also not surface the URL. The+ existing warning at line 88-90 only includes ``response.status_code``+ — verify that contract holds.+ """+ import local_deep_research.security as sec_pkg++ class _Resp:+ status_code = 503+ text = "upstream busy"++ def json(self):+ return {}++ with loguru_caplog.at_level("DEBUG"):+ with patch.object(sec_pkg, "safe_get", return_value=_Resp()):+ result = (+ google_provider_module.GoogleProvider.list_models_for_api(+ api_key=_LEAKED_KEY+ )+ )++ assert result == []+ assert _LEAKED_KEY not in loguru_caplog.text+++class TestCredentialStoreKeyLeakage:+ """Pin no-leak behavior in the credential store base class. The class+ is a small wrapper around a dict; verify ``__repr__``,+ ``__str__``, and any exception paths do not expose stored secrets.+ """++ def test_repr_does_not_expose_stored_passwords(self):+ from local_deep_research.scheduler.background import (+ SchedulerCredentialStore,+ )++ store = SchedulerCredentialStore(ttl_hours=1)+ store.store("alice", _LEAKED_KEY)++ # repr / str must not expose the password+ assert _LEAKED_KEY not in repr(store)+ assert _LEAKED_KEY not in str(store)++ def test_clear_entry_does_not_log_store_state(self, loguru_caplog):+ """``clear_entry`` must be completely silent. The implementation+ in ``credential_store_base.py`` does not call ``logger`` at all+ — this test pins that contract so a future+ ``logger.debug(f"store contents: {self._store}")`` (which would+ expose every stored credential) is caught immediately. Exercises+ both the present-key and missing-key paths and asserts not just+ the leaked sentinel but that *no records were emitted at all*.+ """+ from local_deep_research.scheduler.background import (+ SchedulerCredentialStore,+ )++ store = SchedulerCredentialStore(ttl_hours=1)+ store.store("alice", _LEAKED_KEY)+ store.store("bob", "another-stored-secret-87654321")++ with loguru_caplog.at_level("DEBUG"):+ store.clear_entry("never-stored-user")+ store.clear_entry("alice")++ assert not loguru_caplog.records, (+ "clear_entry must be silent. Got log records: "+ f"{[r.getMessage() for r in loguru_caplog.records]}"+ )+ assert _LEAKED_KEY not in loguru_caplog.text+++class TestOpenAICompatErrorRedaction:+ """The OpenAI-compat error helper at+ ``src/local_deep_research/error_handling/openai_compat_errors.py``+ runs ``_strip_credentials`` on ``base_url`` and appends ``{exc!s}`` to+ the returned friendly message. Verify that an embedded-credential+ base URL is stripped from the final string.+ """++ def test_friendly_error_strips_credentials_from_base_url(self):+ from local_deep_research.error_handling.openai_compat_errors import (+ friendly_openai_compatible_error,+ )++ # Some users embed API keys in the base URL itself+ embedded_url = f"https://user:{_LEAKED_KEY}@host.example.com/v1"+ exc = RuntimeError("upstream failed") # exc!s does NOT contain the key++ result = friendly_openai_compatible_error(+ exc,+ provider="lmstudio",+ base_url=embedded_url,+ model="some-model",+ )++ assert _LEAKED_KEY not in result, (+ "_strip_credentials must remove userinfo from base_url before "+ "the URL is embedded in the friendly message"+ )diff --git a/tests/security/test_log_sanitizer.py b/tests/security/test_log_sanitizer.pyindex fcbaf26422..51e4c64294 100644--- a/tests/security/test_log_sanitizer.py+++ b/tests/security/test_log_sanitizer.py@@ -1,6 +1,9 @@"""Tests for log string sanitization."""+import pytest+from local_deep_research.security.log_sanitizer import (+ redact_secrets,sanitize_for_log,strip_control_chars,)@@ -84,3 +87,78 @@ class TestStripControlChars:def test_empty_string(self):assert strip_control_chars("") == ""+++class TestRedactSecrets:+ """Unit tests for redact_secrets."""++ def test_redacts_single_secret(self):+ result = redact_secrets(+ "call to ?key=sk-abc1234567 failed", "sk-abc1234567"+ )+ assert result == "call to ?key=***REDACTED*** failed"++ def test_redacts_multiple_secrets(self):+ result = redact_secrets(+ "user=alice12345 token=tok-xyz98765",+ "alice12345",+ "tok-xyz98765",+ )+ assert "alice12345" not in result+ assert "tok-xyz98765" not in result+ assert result.count("***REDACTED***") == 2++ def test_redacts_all_occurrences(self):+ result = redact_secrets("X sk-12345678 Y sk-12345678 Z", "sk-12345678")+ assert result == "X ***REDACTED*** Y ***REDACTED*** Z"++ def test_none_secret_ignored(self):+ assert redact_secrets("message stays put", None) == "message stays put"++ def test_empty_secret_ignored(self):+ # Replacing the empty string would insert the token between every+ # character of the message; this is the load-bearing guard.+ assert redact_secrets("message stays put", "") == "message stays put"++ def test_short_secret_below_min_length_ignored(self):+ # 7 characters is below the default min_length of 8.+ result = redact_secrets("password is hunter", "hunter")+ assert result == "password is hunter"++ def test_min_length_parameter_lowers_threshold(self):+ result = redact_secrets("password is hunter", "hunter", min_length=6)+ assert result == "password is ***REDACTED***"++ def test_no_secrets_returns_message_unchanged(self):+ assert redact_secrets("hello world") == "hello world"++ def test_empty_message_returned_unchanged(self):+ assert redact_secrets("", "sk-abc1234567") == ""++ def test_message_without_secret_returned_unchanged(self):+ assert redact_secrets("hello world", "sk-abc1234567") == "hello world"++ def test_custom_token(self):+ result = redact_secrets(+ "key=sk-abc1234567", "sk-abc1234567", token="[KEY]"+ )+ assert result == "key=[KEY]"++ @pytest.mark.parametrize(+ "secret",+ [+ "sk-abc1234567890",+ "AIzaSy-mock-google-key-12345",+ "sk-ant-api03-very-long-anthropic-key-12345",+ ],+ )+ def test_realistic_provider_key_shapes_redacted(self, secret):+ message = f"upstream failed: ?key={secret}&model=x"+ assert secret not in redact_secrets(message, secret)++ def test_literal_substring_match_only(self):+ # Document the contract: URL-encoded or otherwise transformed+ # forms are NOT redacted. Callers must pass each form they need+ # to scrub.+ result = redact_secrets("encoded=%2Bsk-abc12345", "+sk-abc12345")+ assert "%2Bsk-abc12345" in result