skills/redos-anti-pattern/SKILL.md
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.
npx skillsauth add igbuend/grimbard redos-anti-patternInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Severity: High
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 is regex with exponential-time complexity for input validation. Small input length increases cause exponential computation time growth.
// 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.
// 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.
(a+)+(a*)*(a|a)+(a?)*(a|b)* is safe, but (a|ab)* is not, because ab can be matched in two different ways.safe-regex for Node.js).(a+)+ as a+.(a|b) not (a|ab) within repeated groups.development
Security anti-pattern for Cross-Site Scripting vulnerabilities (CWE-79). Use when generating or reviewing code that renders HTML, handles user input in web pages, uses innerHTML/document.write, or builds dynamic web content. Covers Reflected, Stored, and DOM-based XSS. AI code has 86% XSS failure rate.
development
Security anti-pattern for XPath injection vulnerabilities (CWE-643). Use when generating or reviewing code that queries XML documents, constructs XPath expressions, or handles user input in XML operations. Detects unescaped quotes and special characters in XPath queries.
development
Security anti-pattern for weak password hashing (CWE-327, CWE-759). Use when generating or reviewing code that stores or verifies user passwords. Detects use of MD5, SHA1, SHA256 without salt, or missing password hashing entirely. Recommends bcrypt, Argon2, or scrypt.
development
Security anti-pattern for weak encryption (CWE-326, CWE-327). Use when generating or reviewing code that encrypts data, handles encryption keys, or uses cryptographic modes. Detects DES, ECB mode, static IVs, and custom crypto implementations.