The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
perf(redact): eliminate exponential backtracking in config-key patterns
carlotestor13ad903a3c6a
diff --git a/agent/redact.py b/agent/redact.pyindex b8b9be8912f..ea70246a907 100644--- a/agent/redact.py+++ b/agent/redact.py@@ -171,9 +171,13 @@ _ENV_LOOKUP_VALUE_RE = re.compile(r"^(?:os\.(?:getenv|environ)|process\.env|\$ENV\{)")# Namespaced (dotted) key: the secret word may sit anywhere in a dotted path.+# NOTE(perf): possessive quantifiers (py3.11+) replace the nested quantifier+# ``(?:[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=…``)._CFG_DOTTED_RE = re.compile(- 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"([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}",re.IGNORECASE,)@@ -192,8 +196,10 @@ _CFG_ANCHORED_RE = re.compile(# is masked by _AUTH_HEADER_RE); ``auth_token``/``auth-token`` still match via# the ``token`` keyword. Quoted values defer to _JSON_FIELD_RE via the lookahead._YAML_CFG_NAMES = r"(?:api[ _.\-]?key|token|secret|passwd|password|credential)"+# NOTE(perf): possessive quantifiers wherever the successor is disjoint; the+# leading ``[A-Za-z0-9_.\-]*`` stays backtrackable (see _CFG_DOTTED_RE note)._YAML_ASSIGN_RE = re.compile(- rf"(^[ \t]*[A-Za-z0-9_.\-]*{_YAML_CFG_NAMES}[A-Za-z0-9_.\-]*)(:[ \t]*)(?!['\"])([^\s&]+)",+ rf"(^[ \t]*+[A-Za-z0-9_.\-]*{_YAML_CFG_NAMES}[A-Za-z0-9_.\-]*+)(:[ \t]*+)(?!['\"])([^\s&]++)",re.IGNORECASE | re.MULTILINE,)diff --git a/tests/agent/test_redact.py b/tests/agent/test_redact.pyindex 2ea405f051a..3d40be4e3f4 100644--- a/tests/agent/test_redact.py+++ b/tests/agent/test_redact.py@@ -545,6 +545,31 @@ class TestLowercaseDottedConfigKeys:+class TestConfigKeyRedosResistance:+ """The dotted-key patterns must not backtrack exponentially (ReDoS).++ Before the possessive-quantifier rewrite, a non-matching run of ~40+ dotted segments took ~30ms and doubled every ~4 segments; 100 segments+ would effectively hang the redactor (it runs on every log line).+ """++ def test_long_dotted_run_completes_fast(self):+ import time++ # 100 dotted segments with no '=' — worst case for the old pattern.+ text = ".".join(["segment"] * 100) + " end"+ t0 = time.perf_counter()+ assert redact_sensitive_text(text) == text+ assert time.perf_counter() - t0 < 1.0++ def test_long_dotted_secret_still_redacted(self):+ # Possessive quantifiers must not change matching behavior.+ text = ".".join(["seg"] * 50) + ".password=Sup3rS3cret!"+ result = redact_sensitive_text(text)+ assert "Sup3rS3cret!" not in result+ assert ".password=" in result++class TestXaiToken:KEY = "xai-ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstu"