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): strip the whole URL query after a redacted credential param (#6567)

LearningCircuit9c923f0c5cb0
diff --git a/changelog.d/6549.security.md b/changelog.d/6549.security.md
new file mode 100644
index 0000000000..31e6bfde5e
--- /dev/null
+++ b/changelog.d/6549.security.md
@@ -0,0 +1,8 @@
+`scrub_error()` now strips the whole query string of a URL even when an
+earlier credential-shaped parameter was already replaced by `[REDACTED]`.
+The strip used to halt at that marker's closing bracket, so every parameter
+after a credential-shaped one (Google PSE's `key=…&cx=…&q=<query>`,
+ScaleSERP's `api_key=…&q=<query>`) still reached the log while the URL
+looked scrubbed (#6549). The URL-body part of the pattern is also bounded
+now, closing a quadratic slowdown on whitespace-free text dense in `://`,
+such as a stringified response body embedded in an exception message.
diff --git a/src/local_deep_research/security/log_sanitizer.py b/src/local_deep_research/security/log_sanitizer.py
index 26f2953b6d..c2dd73eb62 100644
--- a/src/local_deep_research/security/log_sanitizer.py
+++ b/src/local_deep_research/security/log_sanitizer.py
@@ -306,17 +306,41 @@ _CREDENTIAL_PATTERNS: list[tuple[re.Pattern[str], str]] = [
# input is back to microseconds. See test_scrub_error_is_linear_time_on_a_
# long_scheme_like_run.
#
-# Query stop-set: whitespace plus the common wrappers a URL appears inside
-# in prose/logs (a markdown link's ``)``, a bracketed/braced reference, or
-# 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). 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 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?#]+)\?[^\s)\]\}\"']+"
+ r"([A-Za-z][A-Za-z0-9+.\-]{0,31}://[^\s?#\"<>]{1,4096})\?[^\s)\"']+"
)
diff --git a/tests/security/test_log_sanitizer.py b/tests/security/test_log_sanitizer.py
index 3c5c07d083..6fe684d32a 100644
--- a/tests/security/test_log_sanitizer.py
+++ b/tests/security/test_log_sanitizer.py
@@ -648,6 +648,89 @@ class TestScrubErrorUrlQueryRedaction:
msg = "try https://h/p? yes"
assert scrub_error(Exception(msg)) == msg
+ def test_params_after_a_redacted_credential_param_are_stripped(self):
+ # Regression for #6549. The query strip runs after
+ # sanitize_error_message, which replaces a credential-shaped value
+ # with "[REDACTED]". With "]" in the query stop-set the strip halted
+ # at that marker, so everything after the credential parameter —
+ # here Google PSE's real order: key, cx, q — reached the log while
+ # the URL looked scrubbed:
+ # ...customsearch/v1?<redacted>]&cx=0123456789&q=does+my+employer+know
+ probe = "does+my+employer+know+i+have+multiple+sclerosis"
+ msg = (
+ "403 Client Error: Forbidden for url: "
+ "https://www.googleapis.com/customsearch/v1"
+ f"?key=AIzaSyA1234567890abcdefghijklmnopqrstuv&cx=0123456789&q={probe}&num=10"
+ )
+ result = scrub_error(Exception(msg))
+ assert probe not in result
+ assert "cx=" not in result
+ assert "num=10" not in result
+ assert "AIzaSyA" not in result
+ assert "https://www.googleapis.com/customsearch/v1?<redacted>" in result
+ # And nothing of the marker's closing bracket leaks through either.
+ assert "]" not in result
+ assert "403 Client Error: Forbidden" in result
+
+ def test_params_after_a_redacted_token_param_are_stripped(self):
+ # Same gap via the token-shaped path, which emits "[REDACTED_KEY]".
+ msg = (
+ "err https://api.x.com/search?token=eyJhbGciOiJIUzI1NiJ9.abc.def"
+ "&q=my+secret+query fail"
+ )
+ result = scrub_error(Exception(msg))
+ assert "my+secret+query" not in result
+ assert "eyJhbGciOiJIUzI1NiJ9" not in result
+ assert result == "err https://api.x.com/search?<redacted> fail"
+
+ def test_bracketed_url_over_redacts_rather_than_leaks(self):
+ # Consequence of taking "]" out of the stop-set: a URL written inside
+ # square brackets loses its closing bracket to the redaction. That is
+ # the safe direction (over-redaction, never a leak) and the price of
+ # not halting at "[REDACTED]" — pinned so it is a decision, not an
+ # accident.
+ result = scrub_error(Exception("see [https://h/p?q=1] end"))
+ assert "q=1" not in result
+ assert result == "see [https://h/p?<redacted> end"
+
+ @pytest.mark.timeout(5)
+ def test_scrub_error_is_linear_time_on_a_dense_url_run(self):
+ # Regression for #6549. The scheme-continuation bound (next test)
+ # covers one quadratic path; the URL body was the other. With an
+ # unbounded ``[^\s?#]+`` followed by ``\?``, a whitespace-free run
+ # dense in "://" (a minified JSON list of URLs — the shape a
+ # stringified response body has) made the engine consume to the end
+ # of the run at every "://" and backtrack looking for a "?" that
+ # never comes: measured ~1s at 36k chars, ~4s at 72k, ~16s at 144k
+ # (4x per doubling), against milliseconds before the query pass.
+ # Excluding the quote from the URL body and capping it restores
+ # linear time (locally: 200k chars in ~30ms).
+ run = '"https://a.com/x",' * 11_111 # ~200k chars, no "?" anywhere
+ t0 = time.perf_counter()
+ result = scrub_error(Exception(run))
+ elapsed = time.perf_counter() - t0
+ assert result == run # no query string, nothing to redact
+ assert elapsed < 2.0, (
+ f"scrub_error took {elapsed:.2f}s on a 200k-char dense-URL run "
+ f"(budget: 2.0s) — the URL-body quantifier may have regressed to "
+ f"unbounded, reintroducing quadratic ReDoS."
+ )
+
+ @pytest.mark.timeout(5)
+ def test_scrub_error_is_bounded_on_a_run_of_bare_schemes(self):
+ # The worst remaining shape for the capped body: no quote to stop
+ # at, "://" at every fourth char, no "?" — each start position now
+ # scans at most 4096 chars instead of the remainder of the run, so
+ # the cost is linear in the input rather than quadratic.
+ run = "x://" * 20_000 # 80k chars
+ t0 = time.perf_counter()
+ scrub_error(Exception(run))
+ elapsed = time.perf_counter() - t0
+ assert elapsed < 2.0, (
+ f"scrub_error took {elapsed:.2f}s on an 80k-char run of bare "
+ f"schemes (budget: 2.0s) — the URL-body cap may have been removed."
+ )
+
@pytest.mark.timeout(5)
def test_scrub_error_is_linear_time_on_a_long_scheme_like_run(self):
# @pytest.mark.timeout is a hard backstop, not just the assertion