Regex Missing Timeout

Learn how to prevent Regex Missing Timeout vulnerabilities with real code examples and best practices. Protect your application from denial of service attacks caused by regex patterns.

Tools recognizing this:

Fortify Checkmarx SonarQube Snyk Semgrep CodeQL

What is Regex Missing Timeout and How Does it Work?

Regex Missing Timeout is a security vulnerability that occurs when regular expressions are executed without a timeout limit. This can lead to denial of service attacks through catastrophic backtracking, where a malicious input causes the regex engine to spend excessive time processing.

The vulnerability can be exploited when:

  • Complex regex patterns are used without timeout controls

  • User input can influence the string being matched

  • The regex pattern is susceptible to catastrophic backtracking

  • No execution time limits are set for the regex operation

This guide covers Regex Missing Timeout vulnerabilities, examples, prevention methods, and how to implement proper timeout controls using real-world techniques.

One Simple Regex Missing Timeout Attack Example

Consider this vulnerable regex implementation:

Regex regex = new Regex("^(a+)+b$");
bool isMatch = regex.IsMatch(userInput);

An attacker could provide this input:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaX

This input would cause catastrophic backtracking, potentially freezing the application as the regex engine attempts to process all possible combinations.

Regex Missing Timeout Prevention Methods: How to Fix Your Code

The most efficient way to fix a Regex Missing Timeout issue is by implementing proper timeout controls when executing regular expressions.

Adding timeouts ensures that regex operations cannot run indefinitely, protecting against denial of service attacks. Different programming languages offer various mechanisms to implement these controls.

Code Samples

Vulnerable Code

string pattern = @"^(a+)+b$";
string input = userInput;
Regex regex = new Regex(pattern);
bool isMatch = regex.IsMatch(input);

Fixed Code

string pattern = @"^(a+)+b$";
string input = userInput;
Regex regex = new Regex(pattern, RegexOptions.None, TimeSpan.FromMilliseconds(30000));
bool isMatch = regex.IsMatch(input);

Fix Explanation

The vulnerable code creates a Regex without timeout controls.The fix adds a timeout parameter of 30 seconds using TimeSpan.FromMilliseconds.If the regex operation exceeds the timeout, it throws a RegexMatchTimeoutException.This prevents infinite processing of malicious inputs.

Need more help in preventing Regex Missing Timeout?

Mobb supports fixing many forms of Regex Missing Timeout vulnerabilities, and can mitigate your issues in batch.

Start now for free at app.mobb.ai

We'd love your feedback!

We're excited to hear your thoughts and ideas about fixing vulnerabilities.

Book a meeting or Contact us if you have any corrections, questions or suggestions. Start now for free at https://app.mobb.ai

Last updated

Was this helpful?