A single copy-pasted validation regex can pin a Node.js API server at 100% CPU with just one 35-byte request. Because JavaScript's V8 regex engine uses backtracking NFA execution, ambiguous quantifiers can explode into exponential work on non-matching input—a class of bug known as ReDoS (Regular Expression Denial of Service) .
1. Why Valid Tests Pass While One Bad Input Freezes the Server
Consider this common email validation mistake:
// Vulnerable: nested quantifier ([a-zA-Z0-9._-]+)+ const badEmail = /^ ( [ a - zA - Z0 - 9 . _ - ] + ) + @ [ a - zA - Z0 - 9 . - ] + \ . [ a - zA - Z ] { 2 , } $ / ;
// Matches instantly on valid input, stalls exponentially on missing '@' badEmail . test ( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!" ) ;
Enter fullscreen mode
Exit fullscreen mode
On [email protected] , the engine succeeds on its first pass, so unit tests stay green.…