Menu

1 / 2

Why One Regex Freezes Your Node.js Event Loop: Detecting Catastrophic Backtracking (ReDoS)

DEV Community: node·Jason Y. (dev_in_the_fog)·13 days ago
#wfjiv4Ru
#devtonodejs#devto#dev#regex#redos
Long article (306 words) — no AI summary yet.
Reading 0:00
15s threshold

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.