Picture a small product company in Nagpur. Their signup service has run quietly for two years. One Tuesday afternoon the CPU on every application server climbs to 100 percent, requests pile up, and the load balancer starts marking healthy machines as dead. Nothing was deployed that day. No traffic spike either. The on-call engineer, Vikram, finally finds a single request stuck in a loop: a user, or a bot, had pasted a long string of letters into the “company name” field.

The culprit is a validation pattern written long ago. It says, roughly: one or more letters, repeated one or more times, followed by a digit. It passes every test the team ever wrote. Then it meets thirty letters and no digit, and it does not come back.

How the engine thinks

Most popular regex engines, including those in Java, Python, JavaScript, .NET, PHP and Perl, work by trial and error. They try a path through the pattern. If it fails, they back up to the last point where they had a choice and try the next option. This is called backtracking. For ordinary patterns it is fast, and it is what makes features like backreferences possible.

Now look at Vikram’s pattern again. “One or more letters” inside “one or more times” gives the engine a choice that is really a puzzle: how to split a run of letters into groups. With three letters it could take them as one group of three, or 2 and 1, or 1 and 2, or 1, 1 and 1. That is four ways. In general a run of n letters can be split in 2 to the power of (n minus 1) ways.

When the string ends with a digit, the engine succeeds on its first attempt and nobody notices. When there is no digit, every single split fails, and the engine tries them all before giving up. For 30 letters that is 536,870,912 splits. Add one more letter and the work doubles. Twenty letters is instant; thirty can pin a core for a long time; forty is effectively forever.

That is why testing never caught it. Valid input is the fast case. Only a near-miss, long and just wrong enough to fail late, triggers the explosion.

Why this is a security issue too

Anyone who can send input to such a pattern can stall your server with a request a few dozen bytes long. This is called ReDoS, regular expression denial of service. It is not theoretical. In July 2019 Cloudflare had a global outage that it publicly attributed to a regular expression in a firewall rule that backtracked heavily and consumed CPU across its network. Big company, careful engineers, one pattern.

What Vikram’s team can do

The cheapest fix is to look at the length first. A company name does not need to be 5,000 characters. Reject oversized input before any pattern touches it, and the exponential case never gets room to grow.

The real fix is to remove the ambiguity. “One or more letters, repeated” means the same as “one or more letters”. Nested repetition over the same characters is almost always a mistake; collapsing it removes the puzzle altogether. When two alternatives in a pattern can match the same text, such as an alternation where both branches accept a letter, the same risk appears. Rewrite so that at each point only one choice can succeed.

Some engines offer help. Possessive quantifiers and atomic groups tell the engine not to go back once it has matched a part, which cuts off the retries. Check whether your language supports them; support differs.

And some engines avoid the problem by design. RE2 from Google, Go’s standard regexp package and Rust’s regex crate use techniques that guarantee running time proportional to input length. The price is that they leave out features that need backtracking, such as backreferences. For validating untrusted input, that is a good trade. Newer versions of some mainstream runtimes have also added safeguards or optional non-backtracking modes, so check what your platform’s current version offers instead of assuming.

Looking for it before Tuesday

Review any pattern that is applied to user-controlled text and contains repetition inside repetition, or repetition followed by something that can fail late. Add a test with a long, almost-valid string and give it a time limit. Several static analysis tools can flag risky patterns too.

Also ask whether you need a regex at all. Splitting an email at the at-sign and checking a few simple conditions is easier to read, easier to bound, and often better than a monster pattern copied from a forum. The only truly reliable check for an email address is sending a message to it.

Figures and names in the examples are illustrative.

Leave a Reply

Trending