Can one regex line freeze your server? How to spot ReDoS
ReDoS sends crafted text to a regex that has repetition inside repetition, so the check tries paths that double with every character. Per OWASP, 16 characters give 65536 paths.

A sign-up form checks the email with a regex you found in an old answer. It works fine for years, until someone sends a long crafted string, and the server hangs on that one line and stops answering anyone. In this article you will see how one line can do that, and how to find it in your code.
What is ReDoS?
ReDoS is a denial of service attack that uses a regex which becomes very slow on crafted text, because the time the check takes grows exponentially with the length of the input.
According to the OWASP page on ReDoS, most regex implementations can reach extreme situations where they work very slowly, and the page calls a pattern that can get stuck on crafted input an "Evil Regex".
How do you recognize a dangerous regex?
A dangerous regex has a repeated group, and inside that group there's another repetition or options that overlap. That shape gives the engine many ways to match the same text.
These are OWASP's own examples. The first two have repetition inside repetition, and the last two have two overlapping options inside a repeated group:
(a+)+$
([a-zA-Z]+)*$
(a|aa)+$
(a|a?)+$Why does one more character double the time?
One more character doubles the time because, when the match fails at the end, the engine goes back and tries every possible way to split the characters between the two repetitions.
The page gives the number: with the pattern ^(a+)+$ and the input aaaaX there are 16 possible paths. With 16 a characters followed by X, there are 65536 paths, and the number doubles with each extra a. So a short string, the size of a username, is enough to hang the request.
The human mistake here is that we test the regex on valid text only, see that it's fast, and assume it's always fast. The slow case only shows up with text that almost matches and fails on the last character.
What do you change in your code today?
You start by limiting the length of the text before it reaches the regex, and by replacing every repetition inside a repetition with a pattern that has no overlapping paths.
Set a length limit on each field before the check. An email or a username doesn't need thousands of characters.
Search your code for repeated groups that contain
+,*or overlapping options, like the examples above, and rewrite them with a single repetition.Test each pattern with a long run of the same character and one character at the end that doesn't match, and time it as you add characters.
Watch out for a regex you build from user input. Use RegExp.escape so the user can't add repetition characters of their own.
And because a stuck request eats your server, also add a timeout to requests that wait so one request can't hold everything behind it.
Where do you start now?
The lesson for us as developers: a regex is a small program, and a program that tries every path gets slow when the paths double. Open every regex that checks user input today, and look for repetition inside repetition.
And if you want someone to check your platform for this kind of flaw, there's Muhassan. Which regex in your code came from an old answer and was never tried on a long string?
FAQ
Is every regex with + or * dangerous?
No. The danger is a repeated group that has repetition or overlapping options inside it, like (a+)+ or (a|aa)+. A simple repetition like [a-z]+ on its own doesn't cause this blow up.
Does the attack only happen on the server?
No. OWASP says an attacker can hang a web browser too, on a computer or possibly on a mobile device, because regular expressions exist in every layer of the web.
How do I test my regex?
Send it a long run of the same character with one character at the end that doesn't match, and time it. If the time grows sharply with each added character, you have a problem.


