fix(redact): keep lowercase assignment scans linear

This commit is contained in:
fangliquanflq
2026-08-31 15:48:34 +08:00
committed by Teknium
parent 3145986c20
commit ba0f5839d4
2 changed files with 18 additions and 1 deletions
+5 -1
View File
@@ -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,
)
+13
View File
@@ -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