#18850: fix: prevent ReDoS in session filter via safe-regex utilities
channel: discord
size: S
Cluster:
Session Management Enhancements
## Summary
Apply ReDoS protection fixes for session filter from the `fix/redos-session-filter` branch.
## Changes
- Created `src/utils/safe-regex.ts` with ReDoS protection utilities:
- `isDangerousPattern()` - detects potentially dangerous regex patterns
- `safeRegex()` - validates patterns and returns safe regex or null
- `safeRegexTest()` - safely tests strings against patterns with safe fallback
- Updated `src/discord/monitor/exec-approvals.ts`:
- Added import for `safeRegexTest`
- Changed session filter check to use `safeRegexTest()` instead of direct `RegExp.test()`
- This prevents catastrophic backtracking from user-provided regex patterns
## Security Fix
The session filter could previously be exploited with patterns like `(a+)+` that cause exponential backtracking, leading to ReDoS attacks. The new implementation:
1. Detects dangerous pattern structures
2. Limits pattern length
3. Falls back to substring matching for unsafe patterns
4. Prevents unbounded regex execution
Refs: fix/redos-session-filter
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR adds ReDoS protection to the session filter by introducing safe regex utilities and applying them to `exec-approvals.ts`. The change replaces direct `RegExp` construction with `safeRegexTest()` which detects dangerous patterns and falls back to substring matching.
**Key changes:**
- New `src/utils/safe-regex.ts` with ReDoS detection heuristics and safe regex testing
- Updated `src/discord/monitor/exec-approvals.ts` to use `safeRegexTest()` for session filtering (line 363)
**Issues found:**
- The `safeRegexTest()` function has a logic bug: it performs substring matching (`str.includes(pattern)`) *before* checking if the pattern is a regex, which breaks regex metacharacter matching (e.g., `^agent:` would match `"test^agent:foo"` instead of only matching strings starting with `"agent:"`)
<h3>Confidence Score: 2/5</h3>
- This PR has a critical logic bug that breaks regex pattern matching behavior
- The implementation has good security intentions but contains a significant logic error in `safeRegexTest()` that will cause incorrect matching behavior. The function checks substring inclusion before attempting regex matching, which means patterns with regex metacharacters (like `^`, `$`, `.*`, etc.) will match incorrectly. This breaks the expected behavior when users provide regex patterns in `sessionFilter` configuration. Additionally, there are no tests for the new `safe-regex.ts` utilities to catch this type of issue.
- `src/utils/safe-regex.ts` requires a fix to the pattern matching logic, and tests should be added
<sub>Last reviewed commit: 882be46</sub>
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#23670: fix(security): add ReDoS protection for user-controlled regex patterns
by kevinWangSheng · 2026-02-22
90.8%
#18107: fix(security): prevent ReDoS in session filter patterns (CWE-1333)
by brandonwise · 2026-02-16
88.6%
#20204: fix(sessions): allow negative IDs and colon separators in session IDs
by zerone0x · 2026-02-18
74.1%
#16061: fix(sessions): tolerate invalid sessionFile metadata
by haoyifan · 2026-02-14
71.5%
#11774: fix: add guards for undefined mentionRegexes arrays
by ikvyk · 2026-02-08
71.1%
#16928: fix(security): OC-07 redact session history credentials and enforce...
by aether-ai-agent · 2026-02-15
70.7%
#22231: fix(security): redact sensitive data in session transcripts
by novalis133 · 2026-02-20
70.5%
#12296: security: persistence-only secret redaction for session transcripts
by akoscz · 2026-02-09
70.1%
#15744: fix: allow cross-agent session path validation
by scottgl9 · 2026-02-13
69.9%
#16531: fix(sessions): allow WhatsApp-style identifiers in session IDs (#16...
by robbyczgw-cla · 2026-02-14
69.8%