Repository navigation
Description
What is the problem this feature will solve?
Recently, we have witnessed a surge in ReDoS (Regular Expression Denial of Service) issues, and the root cause behind these occurrences lies in the regex engine we employ for matching patterns. There is a library called re2, which serves as a wrapper for Google's re2 engine. This library resolves all regex patterns in linear time, albeit lacking support for backreferences and look-arounds.
Upon researching this matter, it becomes evident that prominent projects are grappling with this problem. Languages such as Rust and Go have successfully mitigated these issues long ago. Considering the impact of ReDoS on Node.js, particularly its tendency to stall the event loop, it seems prudent to integrate this functionality into the Node.js codebase.
For further insights into the severity of ReDoS vulnerabilities, you may refer to the following resources:
https://snyk.io/blog/redos-vulnerabilities-in-npm-spikes-by-143-and-xss-continues-to-grow/
https://swtch.com/~rsc/regexp/regexp1.html
What is the feature you are proposing to solve the problem?
Implementing a regex algorithm that operates in linear time, such as the Thompson implementation detailed in this link, is a worthwhile endeavor.
What alternatives have you considered?
Several projects, for instance, address this issue:
- safe-regex: Identifies potentially catastrophic exponential-time regular expressions.
- recheck: A reliable ReDoS checker, ensuring trustworthiness in pattern matching.
- re2: A swift and secure alternative to backtracking regular expression engines.
- redos-killer: A tool in development designed to eliminate ReDoS without causing friction.
Activity
rluvaton commentedon Feb 5, 2024
Since RegExp is part of ECMAScript and therefore the JS engine should implement this and not NodeJS, you can open issue in the V8 issue tracking
aduh95 commentedon Feb 5, 2024
@rluvaton I don't think the OP is asking for changes in
RegExp– or if that's the case, indeed this repo is not the right place to discuss language changes (a TC39 proposal would be way to go I guess). My understanding is that the OP is requesting a new API.As with other requests for adding more APIs to core, it won't happen until someone opens a PR with an implementation and documentation. In order for such PR to land, we'd need to answer those questions:
Aurele-Barriere commentedon Feb 6, 2024
Have you looked into using the "Experimental" regex engine in V8, available in Node?
It has been described here:
https://v8.dev/blog/non-backtracking-regexp
It uses exactly the Thompson NFA simulation algorithm described in the blog post you linked.
You can start node with
node --enable-experimental-regexp-engine.Then, a new Regex flag is available,
l.Any regex with the
lflag will be executed using the linear Experimental engine.For instance, if I define
const re = /(a*)*b/,Using the usual backtracking algorithm on a string of "a"s (for instance
"a".repeat(1000).match(re)), it takes forever.But if I define instead
const re = /(a*)*b/lwith thelflag, then"a".repeat(1000).match(re)finishes almost immediately.As you said, this engine lacks support for backreferences and lookarounds.
However, a recent commit adds support for lookbehinds without capture groups inside:
https://chromium-review.googlesource.com/c/v8/v8/+/5093860
Is this Experimental engine what you are looking for?
francoatmega commentedon Feb 6, 2024
Yeah, it's exactly this, thanks for sharing I didn't knew that. I will take a look and experiment this!
Hope soon this can be integrated to main code base.
#redosNevermore
ericcornelissen commentedon Aug 17, 2025
I have two questions regarding
--enable-experimental-regexp-engine--helptext but it still works, is that intentional?lflag if the engine isn't enabled. Hence, as a library author you can't use it because you don't control whether your users will use the engine. Yet, many ReDoS vulnerabilities are introduced through libraries...ericcornelissen commentedon May 1, 2026
I created a package to help library authors leverage the experimental engine with a "drop-in"1 replacement of
RegExpthat automatically instantiates linear-time regular expressions whenever possible.If anyone wants to try it out and let me know your experience, please see the npm package and repository.
Footnotes
the only catch is that you can still write regular expressions that aren't compatible with the experimental engine and you won't find out unless you use it. ↩
feat(builder): string regex pattern via RE2 (#1, backend)