Skip to content

perf(redact): eliminate exponential backtracking (ReDoS) in config-key patterns - #76083

Merged
kshitijk4poor merged 3 commits into
NousResearch:mainfrom
kshitijk4poor:salvage/68086-redact-possessive-quantifiers
Aug 1, 2026
Merged

perf(redact): eliminate exponential backtracking (ReDoS) in config-key patterns#76083
kshitijk4poor merged 3 commits into
NousResearch:mainfrom
kshitijk4poor:salvage/68086-redact-possessive-quantifiers

Conversation

@kshitijk4poor

Copy link
Copy Markdown
Collaborator

Summary

Eliminates exponential backtracking (ReDoS) in agent/redact.py config-key regexes by replacing nested quantifiers with Python 3.11+ possessive quantifiers.

Changes

  • agent/redact.py: _CFG_DOTTED_RE and _YAML_ASSIGN_RE rewritten with possessive quantifiers (*+, ++) wherever the successor character class is disjoint, preventing catastrophic backtracking on long dotted runs.
  • tests/agent/test_redact.py: 5 ReDoS resistance tests (3 from contributor + 2 added during salvage).

Why

redact_sensitive_text() runs on every log line and transcript chunk. A hostile or unlucky log line containing a long dotted run could pin a core and stall the redaction pipeline. Old pattern was O(2^(n/4)); new is O(n).

Salvage notes

Cherry-picked from #68086 by @carlotestor (authorship preserved). Follow-up commit adds:

  • test_long_dotted_run_with_keyword_completes_fast — the contributor's test used "segment" tokens (no secret keyword), so the keyword pre-gate on main skips _CFG_DOTTED_RE entirely. This new test includes "token" so the regex is directly exercised.
  • test_yaml_assign_redos_resistance + test_yaml_assign_secret_still_redacted_YAML_ASSIGN_RE was modified but had no ReDoS test.
  • Relaxed timing bound from 1.0s to 2.0s for CI robustness.

Validation

Before (old regex) After (possessive)
100 segments, no match ~hours (exponential) 26µs
100 segments + password= 379ms 55µs
Fuzz equivalence (5000+ inputs) 0 divergences

Closes #68086

/cc @carlotestor

carlotestor and others added 3 commits August 1, 2026 15:42
_CFG_DOTTED_RE's nested quantifier (?:[A-Za-z0-9_\-]+\.)+ backtracks
exponentially on long non-matching dotted runs (doubles every ~4
segments). Flatten it and use possessive quantifiers (py3.11+) in
_CFG_DOTTED_RE and _YAML_ASSIGN_RE wherever the successor is disjoint.

Zero behavior change: equivalence fuzz-verified over 120k structured
and random inputs comparing full sub() output including groups. Adds a
ReDoS regression test.
- Relax timing bound from 1.0s to 2.0s (CI machine robustness).
- Add test_long_dotted_run_with_keyword_completes_fast: includes a secret
  keyword so the pre-gate does NOT skip _CFG_DOTTED_RE — directly exercises
  the possessive-quantifier regex, not just the pre-gate.
- Add test_yaml_assign_redos_resistance: _YAML_ASSIGN_RE was modified but
  had no ReDoS test — add 100-line stress input.
- Add test_yaml_assign_secret_still_redacted: verify YAML matching behavior
  preserved with possessive quantifiers.
Required for check-attribution CI on salvage PR NousResearch#76083.
@kshitijk4poor
kshitijk4poor enabled auto-merge (rebase) August 1, 2026 10:27
@kshitijk4poor
kshitijk4poor merged commit 5b3c66a into NousResearch:main Aug 1, 2026
35 checks passed
@alt-glitch alt-glitch added type/perf Performance improvement or optimization P2 Medium — degraded but workaround exists comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint sweeper:risk-security-boundary Sweeper risk: may affect sandboxing, auth, credentials, or sensitive data labels Aug 1, 2026
@kshitijk4poor
kshitijk4poor deleted the salvage/68086-redact-possessive-quantifiers branch August 5, 2026 07:10
louisgreen0726 pushed a commit to louisgreen0726/hermes-agent that referenced this pull request Aug 7, 2026
Required for check-attribution CI on salvage PR NousResearch#76083.
randlee pushed a commit to randlee/hermes-agent that referenced this pull request Aug 11, 2026
Required for check-attribution CI on salvage PR NousResearch#76083.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P2 Medium — degraded but workaround exists sweeper:risk-security-boundary Sweeper risk: may affect sandboxing, auth, credentials, or sensitive data type/perf Performance improvement or optimization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants