Skip to content

Linear regex algorithm #51659

Closed
Closed

Description

@francoatmega

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

  1. added
    feature request
    on Feb 4, 2024
  2. rluvaton commented on Feb 5, 2024

    @rluvaton
    Member

    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

  3. aduh95 commented on Feb 5, 2024

    @aduh95
    Contributor

    @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:

    • why/how is it better to have it in core rather than in userland?
    • who's going to maintain it?
  4. Aurele-Barriere commented on Feb 6, 2024

    @Aurele-Barriere

    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 l flag 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/l with the l flag, 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?

  5. francoatmega commented on Feb 6, 2024

    @francoatmega
    Author

    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

  6. ericcornelissen commented on Aug 17, 2025

    @ericcornelissen

    I have two questions regarding --enable-experimental-regexp-engine

    1. In v24, it appears to have been removed from the docs and CLI --help text but it still works, is that intentional?
    2. How could libraries prepare for/support this option? From what I can tell you can't use the l flag 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...
  7. ericcornelissen commented on May 1, 2026

    @ericcornelissen

    How could libraries prepare for/support this option? From what I can tell you can't use the l flag 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.

    I created a package to help library authors leverage the experimental engine with a "drop-in"1 replacement of RegExp that 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

    1. 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. ↩

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions