question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Vulnerable Regular Expression

See original GitHub issue

The following regular expression used in the mime lookup is vulnerable to ReDoS:

/.*[\.\/\\]/

The slowdown is moderately low: for 50.000 characters around 2 seconds matching time. However, I would still suggest one of the following:

  • remove the regex,
  • anchor the regex,
  • limit the number of characters that can be matched by the repetition,
  • limit the input size.

If needed, I can provide an actual example showing the slowdown.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
broofacommented, Sep 26, 2017

npm published in v1.4.1 and 2.0.3

1reaction
broofacommented, Sep 25, 2017

I believe I’m able to repro the issue with the code below. Looks like complexity is ~O(N^2).

var RE = /.*[\.\/\\]/;

function test(len) {
  const str = new Array(len).join().replace(/,/g, ' ');

  const start = process.hrtime()
  const res = str.replace(RE, '');
  console.log('N chars:', len, 'Time:', process.hrtime(start));
}

test(1000);
test(2000);
test(5000);
test(10000);
test(20000);
test(50000);
test(100000);
test(200000);
test(500000);

Yields the following output

N chars: 1000 Time: [ 0, 16273206 ]
N chars: 2000 Time: [ 0, 3165858 ]
N chars: 5000 Time: [ 0, 19505864 ]
N chars: 10000 Time: [ 0, 86073894 ]
N chars: 20000 Time: [ 0, 325145464 ]
N chars: 50000 Time: [ 2, 48747126 ]
N chars: 100000 Time: [ 8, 251471106 ]
N chars: 200000 Time: [ 33, 30366008 ]
N chars: 500000 Time: [ 211, 463680672 ]
Read more comments on GitHub >

github_iconTop Results From Across the Web

Regular expression Denial of Service - ReDoS
The Regular expression Denial of Service (ReDoS) is a Denial of Service attack, that exploits the fact that most Regular Expression implementations may ......
Read more >
ReDoS - Wikipedia
A regular expression denial of service (ReDoS) is an algorithmic complexity attack that produces a denial-of-service by providing a regular expression ...
Read more >
Regular Expression Denial of Service (ReDoS) in scss-tokenizer
The Regular expression Denial of Service (ReDoS) is a type of Denial of Service attack. Regular expressions are incredibly powerful, but they ...
Read more >
Regex Vulnerabilities - ADMIN Magazine
Regular expressions are invaluable for checking user input, but a vulnerability could make them ripe for exploitation. One important paradigm in software ...
Read more >
How to protect against regex denial-of-service (ReDoS) attacks
Learn some tips to help you safeguard regular expressions against denial-of-service (DoS) attacks, known as ReDoS attacks.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found