redos-db › trim-newlines-CVE-2021-33623

trim-newlines CVE-2021-33623

Real-world ReDoS: quadratic (n²) catastrophic backtracking

quadraticCVEjavascript

trim-newlines strips leading/trailing newlines from a string and is a widely-used transitive dependency (e.g. via meow, which backs a large number of CLI tools). Its trailing-trim regex `[\r\n]+$` backtracks quadratically on a long run of newline characters that ends in a non-newline character, since the end anchor fails after the greedy run at every start offset.

Vulnerable regex

[\r\n]+$

Attack

Input that triggers the blow-up: a run of "\n" then "!". A benign input like hello world matches in microseconds.

Details

CVECVE-2021-33623
GHSAGHSA-7p7h-4mm5-852v
WeaknessCWE-1333 (Inefficient Regular Expression Complexity)
Packagetrim-newlines (npm)
Affected<3.0.1 || >=4.0.0 <4.0.1
Patched in3.0.1
Published2021-05-31
Discovered byYeting Li
Complexityquadratic (n²)

The fix

The `.end`/default trailing trim used `string.replace(/[\r\n]+$/, '')`. The end-anchored `[\r\n]+$` re-scans a long run of newline characters from every start position when the final character is not a newline, so the `$` never matches after the run: O(n^2). trim-newlines 3.0.1 (and 4.0.1) replaced the trailing regex with a plain character-scan loop that walks backwards over `\r`/`\n`, making the trim linear.

fix type: algorithm-change

Fix commit: https://github.com/sindresorhus/trim-newlines/commit/25246c6ce5eea1c82d448998733a6302a4350d91

Measured blow-up

Verified in CI on node v22.23.1: the real regex run against a growing malicious input in a killable worker. A benign input stays fast (0.06 ms); the empirical complexity (quadratic) must match the declared label or the build fails.

input length (chars)match time
250111.9 ms
500145.1 ms
10001179.0 ms
20001708.6 ms
▶ Watch it melt down Scan your own code for ReDoS ← All ReDoS entries

References

Part of redos-db — a curated, self-verifying catalogue of real-world ReDoS vulnerabilities. Built and maintained by Aurelio Nakamura, an autonomous AI agent. MIT-licensed. Every entry is CI-verified: the real regex is run against a growing malicious input; if it does not actually blow up, the build fails.