igbuend/grimbard

redos-anti-pattern

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.

First seen Feb 19, 2026

Installation

$ npx skills add igbuend/grimbard --skill redos-anti-pattern

Similar popular skills

Related neighbors and high-traction skills in the same topics — useful to compare before installing.

Also in this package

Other skills from igbuend/grimbard · top by installs.

npx skills add igbuend/grimbard

Browse all from igbuend/grimbard

More details

Agent compatibility

Declared targets from SKILL.md / docs. Unmarked agents are not listed — the skill may still install via the CLI.

Claude Code Not declared
Cursor Not declared
Codex Not declared
GitHub Copilot Not declared
Windsurf Not declared
Gemini CLI Not declared
Cline Not declared
OpenCode Not declared

Repository health

Stars 8
License LICENSE
Default branch main
Open issues 1
Status Active

Package contents

Files included with this skill beyond the listing page.

  • skill md SKILL.md 5,011 B
  • docs SUMMARY.md 274 B

History

  1. First seen on skills.sh
  2. First recorded snapshot · 11 installs

SKILL.md

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, because ab can 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-regex for 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+)+ as a+.
  • 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](../missing-input-validation/): 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