Skip to content

The agent that grows with you

fix(redact): keep dotted config-key scans linear past the keyword pre-gate

kshitijk4poorfe0cfdf99c18
diff --git a/agent/redact.py b/agent/redact.py
index eb41ef05510..50e263cee39 100644
--- a/agent/redact.py
+++ b/agent/redact.py
@@ -204,7 +204,14 @@ _ENV_LOOKUP_VALUE_RE = re.compile(
# ``(?:[A-Za-z0-9_\-]+\.)+`` (exponential backtracking on long dotted runs).
# The ``*`` runs bordering {_SECRET_CFG_NAMES} must stay backtrackable
# (secret words are matchable by the class, e.g. ``app.api.key=…``).
+# The lookbehind anchors each attempt to the start of a key run: without it,
+# ``re.sub`` retries the backtrackable ``*`` prefix at every byte of a long
+# non-matching dotted run, making the sub quadratic whenever the text contains
+# a secret keyword anywhere (the ``_CFG_SECRET_WORD_RE`` pre-gate only skips
+# secret-free text). Match set is unchanged — any match starting mid-run
+# implies a leftmost match starting at the run start (#99255).
_CFG_DOTTED_RE = re.compile(
+ rf"(?<![A-Za-z0-9_.\-])"
rf"([A-Za-z0-9_\-]++\.[A-Za-z0-9_.\-]*{_SECRET_CFG_NAMES}[A-Za-z0-9_.\-]*+"
rf"|[A-Za-z0-9_.\-]*{_SECRET_CFG_NAMES}[A-Za-z0-9_.\-]*\.[A-Za-z0-9_.\-]++)"
rf"={_CFG_VALUE}",
diff --git a/tests/agent/test_redact.py b/tests/agent/test_redact.py
index 2e508629d9d..e51dfdf12bd 100644
--- a/tests/agent/test_redact.py
+++ b/tests/agent/test_redact.py
@@ -690,6 +690,23 @@ class TestConfigKeyRedosResistance:
assert redact_sensitive_text(text, force=True) == text
assert time.perf_counter() - t0 < 2.0
+ def test_dotted_cfg_scan_stays_linear_with_keyword_elsewhere(self):
+ """_CFG_DOTTED_RE must stay linear once the pre-gate passes.
+
+ The ``_CFG_SECRET_WORD_RE`` pre-gate only skips secret-FREE text, so a
+ payload that contains a real secret assignment AND a long opaque
+ dotted run still reaches the backtrackable ``*`` prefix. Without the
+ run-start lookbehind the sub retries that prefix from every byte of
+ the run (quadratic while holding the GIL).
+ """
+ import time
+
+ text = "password=hunter2\n" + "a." * 15_000 + "=value"
+ t0 = time.perf_counter()
+ result = redact_sensitive_text(text, force=True)
+ assert "hunter2" not in result
+ 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