🤖 The AI Prompt That Catches the Regex That Will Eventually Freeze Your Server
A regular expression can pass every test with normal input and still contain catastrophic backtracking – a specific pattern structure that causes execution time to explode exponentially on certain malicious or just unusually-shaped input, effectively freezing whatever thread runs it. This prompt asks an AI assistant to review a regex specifically for that risk before it ships.
đź“‹ The Prompt
Review this regular expression specifically for catastrophic backtracking risk - not general correctness, specifically performance under adversarial input. For the pattern below: 1. Does it contain nested quantifiers or overlapping alternations that could cause exponential backtracking on certain inputs (e.g. (a+)+, (a|a)*, or similar structures)? 2. If a risk exists, describe a SPECIFIC input string that would trigger catastrophic slowdown, so I can verify it myself. 3. Suggest a rewritten version of the pattern that achieves the same matching behavior for legitimate input, without the backtracking risk. 4. Note whether this pattern is ever applied to user-supplied input (untrusted external strings) as opposed to only internal, controlled strings - the risk matters far more in the former case. Pattern: [PASTE THE REGEX HERE] Language/engine it runs in: [e.g. .NET, JavaScript, PCRE]'"'"' Context: [where in the code this pattern is used, and what kind of input it processes]
âś… Why This Prompt Works
- Asking for a SPECIFIC triggering input rather than a general ‘yes this looks risky’ answer makes the finding verifiable – running that exact input against the pattern is a fast, concrete way to confirm the risk is real before spending time rewriting anything.
- Specifying the regex engine and language matters because backtracking behavior genuinely differs between engines (some use different matching algorithms with different worst-case behavior) – a risk assessment that ignores which engine is running the pattern can miss engine-specific nuances entirely.
- Asking specifically whether the pattern processes user-supplied input reframes this from an abstract code-quality question into a concrete security question – a backtracking risk in a pattern that only ever runs against trusted, internally-generated strings is a much lower priority than the same risk in a pattern validating public form input.
⚠️ Where to Draw the Line
- A suggested rewrite needs to be tested against the same set of legitimate inputs the original pattern was meant to handle – a regex that avoids backtracking risk but no longer matches correctly isn’t actually a fix, it’s a different bug.
- This prompt catches a specific, well-known category of regex risk – it isn’t a substitute for a broader security review of anywhere user input reaches the application, just one specific and often-overlooked piece of that larger picture.
A regex that’s correct on every test case you tried can still be one adversarial input away from freezing a thread – that gap is exactly what’s worth a second look before it ships.




