The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
feat(plugins): reject ReDoS-shaped patterns at redaction registration
webdevtodayjason50f12e6ad84d
diff --git a/agent/redact.py b/agent/redact.pyindex 4892cfde8be..2512243dc0e 100644--- a/agent/redact.py+++ b/agent/redact.py@@ -1189,6 +1189,65 @@ def _has_top_level_alternation(pattern: str) -> bool:return False+def _has_nested_unbounded_repeat(pattern: str) -> bool:+ """True if an unbounded quantifier applies to a group containing one.++ ``(a+)+``, ``(?:x*)*``, ``(a{2,})+`` — the canonical catastrophic-+ backtracking (ReDoS) shape. Registered patterns run against every log+ line, tool output, and transcript chunk, so a pathological pattern from+ a buggy plugin would stall the host process, not just the plugin.++ Detects structural nesting only; ambiguity between overlapping+ alternation branches (``(a|aa)+``) is not statically detected and+ remains the plugin author's responsibility.+ """++ def _unbounded_quantifier_follows(j: int) -> bool:+ # Is pattern[j:] an unbounded quantifier (* + {m,}) for the atom+ # that just ended at j?+ if j >= len(pattern):+ return False+ if pattern[j] in "*+":+ return True+ if pattern[j] == "{":+ k = pattern.find("}", j)+ body = pattern[j + 1:k] if k != -1 else ""+ # {m,} is open-ended; {m} and {m,n} are bounded.+ return body[:-1].isdigit() and body.endswith(",")+ return False++ # Stack of flags: does the group at this depth contain an unbounded+ # repeat? Index 0 is the top level.+ contains_unbounded = [False]+ i = 0+ while i < len(pattern):+ ch = pattern[i]+ if ch == "\\":+ i += 2+ continue+ if ch == "[":+ i += 1+ if i < len(pattern) and pattern[i] == "]":+ i += 1+ while i < len(pattern) and pattern[i] != "]":+ if pattern[i] == "\\":+ i += 1+ i += 1+ elif ch == "(":+ contains_unbounded.append(False)+ elif ch == ")":+ inner = contains_unbounded.pop() if len(contains_unbounded) > 1 else False+ if inner and _unbounded_quantifier_follows(i + 1):+ return True+ contains_unbounded[-1] = contains_unbounded[-1] or inner+ elif _unbounded_quantifier_follows(i):+ contains_unbounded[-1] = True+ if ch == "{":+ i = pattern.find("}", i) # skip the {m,} body+ i += 1+ return False++_PREFIX_SUBSTRINGS = tuple(_extract_literal_prefix(p) for p in _PREFIX_PATTERNS)@@ -1249,6 +1308,10 @@ def register_redaction_patterns(patterns, source: str = "plugin") -> int:* must not contain a top-level alternation (``ab|.*`` would escapethe literal-prefix guarantee below through its unprefixed branch;grouped alternation after the prefix, ``ab(?:x|y)``, is allowed);+ * must not nest unbounded quantifiers (``(a+)+``-style patterns can+ backtrack catastrophically, and registered patterns run against+ every log line and tool output — see+ ``_has_nested_unbounded_repeat``);* must start with at least 2 literal characters (the pre-screensubstring gate in ``_has_known_prefix_substring`` needs a literalanchor; it also structurally rules out redact-everything patterns@@ -1285,6 +1348,15 @@ def register_redaction_patterns(patterns, source: str = "plugin") -> int:source, pattern,)continue+ if _has_nested_unbounded_repeat(pattern):+ logger.warning(+ "%s: skipping redaction pattern %r — nested unbounded "+ "quantifiers (e.g. '(a+)+') can backtrack catastrophically, "+ "and registered patterns run on every log line and tool "+ "output",+ source, pattern,+ )+ continueif len(_extract_literal_prefix(pattern)) < 2:logger.warning("%s: skipping redaction pattern %r — must start with at "diff --git a/tests/test_redaction_registry.py b/tests/test_redaction_registry.pyindex ddb50eda11b..04c4d394454 100644--- a/tests/test_redaction_registry.py+++ b/tests/test_redaction_registry.py@@ -2,8 +2,8 @@Covers ``agent.redact.register_redaction_patterns`` (validation,dedupe, additive semantics, matcher rebuild), the-``PluginContext.register_redaction_patterns`` wiring, and the bundled-``nvapi-redaction`` reference plugin.+``PluginContext.register_redaction_patterns`` wiring, and a synthetic+plugin written at test time for the ``register()`` end-to-end path.All tests call ``redact_sensitive_text(..., force=True)`` so resultsdon't depend on the HERMES_REDACT_SECRETS environment of the test run,@@ -164,6 +164,28 @@ def test_grouped_alternation_and_literal_pipe_accepted():assert register_redaction_patterns([r"wv[|][A-Za-z0-9]{20,}"], source="test") == 1+def test_nested_unbounded_quantifiers_rejected():+ # (a+)+ backtracks catastrophically; registered patterns run on every+ # log line and tool output, so ReDoS shapes are rejected up front.+ assert register_redaction_patterns([r"ab(a+)+"], source="test") == 0+ assert register_redaction_patterns([r"ab(?:x*)*"], source="test") == 0+ assert register_redaction_patterns([r"ab(x{2,})+"], source="test") == 0+ # Nesting through an intermediate group is still nesting.+ assert register_redaction_patterns([r"ab((x+)y)*"], source="test") == 0+ clean = "nothing here resembles a credential"+ assert redact_sensitive_text(clean, force=True) == clean+++def test_bounded_and_sibling_quantifiers_accepted():+ # A single unbounded quantifier is fine, as are siblings and a+ # bounded quantifier inside an unbounded group.+ assert register_redaction_patterns([r"zr(?:tok)?-[A-Za-z0-9]{20,}"], source="test") == 1+ assert register_redaction_patterns([r"qm-[a-z]+[0-9]+"], source="test") == 1+ assert register_redaction_patterns([r"pv(?:x{1,5}y)+"], source="test") == 1+ # Quantifier chars inside a character class are literals, not repeats.+ assert register_redaction_patterns([r"tw[+*][A-Za-z0-9]{20,}"], source="test") == 1++# ── Plugin register() end-to-end (synthetic, written at test time) ──────