Skip to content

The agent that grows with you

fix(redact): keep lowercase assignment scans linear

fangliquanflqba0f5839d462
diff --git a/agent/redact.py b/agent/redact.py
index 7fa3c11e1d3..eb41ef05510 100644
--- a/agent/redact.py
+++ b/agent/redact.py
@@ -158,8 +158,12 @@ _ENV_ASSIGN_RE = re.compile(
# Lowercase env names: only underscore-boundary forms (``openai_key=…``,
# ``FAL_KEY=…``, ``db_pw=…``) — NOT bare ``password=``/``token=``/``secret=``,
# which appear in prose, URLs, and form bodies (issue #77484).
+# Anchor each attempt to the start of an identifier run. Without the
+# lookbehind, ``re.sub`` retries the greedy ``[a-z0-9_]+`` prefix at every byte
+# of a long non-matching opaque payload, making strict compaction redaction
+# quadratic while holding the GIL (#99255).
_ENV_ASSIGN_LOWER_RE = re.compile(
- rf"([a-z0-9_]+(?:_|^)(?:key|pass|pw|token|secret|password|passwd|credential|auth)(?=[^a-z0-9_]|$))\s*=\s*(['\"]?)(\S+)\2",
+ rf"(?<![a-z0-9_])([a-z0-9_]+(?:_|^)(?:key|pass|pw|token|secret|password|passwd|credential|auth)(?=[^a-z0-9_]|$))\s*=\s*(['\"]?)(\S+)\2",
re.IGNORECASE,
)
diff --git a/tests/agent/test_redact.py b/tests/agent/test_redact.py
index b52c7c1aed9..2e508629d9d 100644
--- a/tests/agent/test_redact.py
+++ b/tests/agent/test_redact.py
@@ -677,6 +677,19 @@ class TestConfigKeyRedosResistance:
assert "Sup3rS3cret!" not in result
assert ".password=" in result
+ def test_long_opaque_assignment_run_completes_fast(self):
+ """Lowercase env scanning stays linear on compaction payload blobs."""
+ import time
+
+ # A serialized tool payload can contain a long opaque alphanumeric
+ # value followed by '=' without containing a secret-key suffix. The
+ # old unanchored lowercase-env pattern retried its greedy prefix from
+ # every byte, making this quadratic while holding the GIL.
+ text = "a" * 20_000 + "=value"
+ t0 = time.perf_counter()
+ assert redact_sensitive_text(text, force=True) == text
+ assert time.perf_counter() - t0 < 2.0
+
def test_yaml_assign_redos_resistance(self):
"""_YAML_ASSIGN_RE must not backtrack excessively on long inputs."""
import time