← スキル一覧に戻る

redos-anti-pattern
by igbuend
A repository of security related skills - like secure code review and pentesting - for Claude and other AI.
⭐ 2🍴 1📅 2026年1月24日
SKILL.md
name: "redos-anti-pattern" description: "Security anti-pattern for Regular Expression Denial of Service (CWE-1333). Use when generating or reviewing code that uses regex for input validation, parsing, or pattern matching. Detects catastrophic backtracking patterns with nested quantifiers."
ReDoS (Regular Expression Denial of Service) Anti-Pattern
Severity: High
Summary
Poorly written regex patterns take extremely long to evaluate malicious input, causing applications to hang and consume 100% CPU from a single request. Caused by catastrophic backtracking in patterns with nested quantifiers ((a+)+) or overlapping alternations.
The Anti-Pattern
The anti-pattern is regex with exponential-time complexity for input validation. Small input length increases cause exponential computation time growth.
BAD Code Example
// VULNERABLE: Nested quantifiers cause catastrophic backtracking.
// Validates string of 'a's followed by 'b'.
// `(a+)+` is the "evil" pattern creating catastrophic backtracking.
const VULNERABLE_REGEX = /^(a+)+b$/;
function validateString(input) {
console.time('Regex Execution');
const result = VULNERABLE_REGEX.test(input);
console.timeEnd('Regex Execution');
return result;
}
// Normal: validateString("aaab"); // -> true, < 1ms
// Attack: string that almost matches
const malicious_input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaab"; // 30 'a's + 'b'
// `(a+)+` matches 'a's in exponential ways.
// "aaa" → (a)(a)(a), (aa)(a), (a)(aa), (aaa)
// Engine tries all combinations.
// 30 'a's → over 1 billion backtracking steps, freezing process.
validateString(malicious_input); // Hangs for very long time.
GOOD Code Example
// SECURE: Linear-time regex or add controls.
// Option 1 (Best): Remove nested quantifier.
// Functionally identical, linear-time complexity.
const SAFE_REGEX = /^a+b$/;
function validateStringSafe(input) {
console.time('Regex Execution');
// Fails almost instantly for malicious input.
const result = SAFE_REGEX.test(input);
console.timeEnd('Regex Execution');
return result;
}
// Option 2: Input length limit (defense-in-depth).
const MAX_LENGTH = 50;
function validateStringWithLimit(input) {
if (input.length > MAX_LENGTH) {
throw new Error("Input exceeds maximum length.");
}
// Prefer safe regex, but this provides fallback.
return VULNERABLE_REGEX.test(input);
}
// Option 3: Use ReDoS-safe engine (Google RE2)
// Guarantees linear-time, avoids catastrophic backtracking.
Detection
- Scan for "evil" regex patterns: The most common red flags are nested quantifiers. Look for patterns like:
(a+)+(a*)*(a|a)+(a?)*
- Look for alternations with overlapping patterns:
(a|b)*is safe, but(a|ab)*is not, becauseabcan be matched in two different ways. - Use static analysis tools: There are many linters and security scanners that are specifically designed to detect vulnerable regular expressions in your code (e.g.,
safe-regexfor Node.js). - Test with "almost matching" strings: To test a regex, create a long string that matches the repeating part of the pattern but fails at the very end. If the execution time increases dramatically with the length of the string, it is likely vulnerable.
Prevention
- Avoid nested quantifiers: Most important rule. Rewrite
(a+)+asa+. - Avoid overlapping alternations: Use
(a|b)not(a|ab)within repeated groups. - Limit input length: Validate input length before complex regex. Caps execution time (crude but effective defense).
- Use timeouts: Regex match timeouts prevent indefinite freezing (doesn't fix underlying vulnerability).
- Use ReDoS-safe engines: Google RE2 guarantees linear-time, immune to catastrophic backtracking.
Related Security Patterns & Anti-Patterns
- Missing Input Validation Anti-Pattern: Failing to limit input length is a form of missing validation that makes ReDoS attacks possible.
- Denial of Service (DoS): ReDoS is a specific type of application-layer DoS attack.
References
スコア
総合スコア
70/100
リポジトリの品質指標に基づく評価
✓SKILL.md
SKILL.mdファイルが含まれている
+20
✓LICENSE
ライセンスが設定されている
+10
✓説明文
100文字以上の説明がある
+10
○人気
GitHub Stars 100以上
0/15
○最近の活動
3ヶ月以内に更新がある
0/10
○フォーク
10回以上フォークされている
0/5
✓Issue管理
オープンIssueが50未満
+5
✓言語
プログラミング言語が設定されている
+5
○タグ
1つ以上のタグが設定されている
0/5
レビュー
💬
レビュー機能は近日公開予定です