Answer#
Regular expression denial of service happens when a pattern such as ^(a+)+$ meets a text such as many a characters followed by !. A backtracking engine tries an exponential number of ways to split the text, and one request can occupy a CPU for hours.
The regex crate documents a different design: its matching is based on finite automata, and searches take time proportional to the size of the pattern and the haystack. The program below applies the classic bad pattern to a 50,001-character text; it finishes and answers false. (The program prints no timings: this thread makes no speed claim beyond the documented guarantee.)
use regex::{Regex, RegexBuilder};
fn main() {
// In a backtracking engine this shape (nested repetition, then a failure at the end) can take
// exponential time. The regex crate does not backtrack: its running time grows with the input.
let re = Regex::new(r"^(a+)+$").unwrap();
let mut hay = "a".repeat(50_000);
hay.push('!');
println!("matches: {}", re.is_match(&hay));
// If users may supply the pattern itself, cap what it may cost to compile.
let user_pattern = r"(\w{50}){50}";
match RegexBuilder::new(user_pattern).size_limit(50_000).build() {
Ok(_) => println!("pattern accepted"),
Err(e) => println!("pattern refused: {e}"),
}
}matches: false
pattern refused: Compiled regex exceeds size limit of 50000 bytes.The second part addresses the remaining risk. If users can supply the pattern itself (a search box with "regex" mode), a large pattern can use much memory to compile. RegexBuilder::size_limit caps the size of the compiled program and returns an error, as shown.
How to do it#
- Prefer the
regexcrate for untrusted text. Read its documentation on performance guarantees for the version you use. - Never build a pattern from user input without escaping it: use
regex::escapewhen the user means literal text. - If users need to write patterns, set
size_limit(anddfa_size_limitif you tune it), and limit the length of the pattern and the haystack. - If you use a crate that supports look-around or back-references (backtracking features, such as
fancy-regex), read its documentation about limits, and add timeouts or backtrack limits. - Treat regular expressions in other languages that your code calls (for example through a database) with the same care.
Common mistakes#
- Believing "Rust means no ReDoS" for every crate. The guarantee belongs to the engine, not to the language.
- Validating e-mail addresses or URLs with one huge pattern. Use a parser (for example the
urlcrate) instead. - Forgetting
escapeand letting a search term such asa.*change the meaning of the query.
Related questions#
Does a size limit make a pattern safe?#
It bounds compilation cost. Combine it with limits on the haystack, and with request timeouts.
Does the regex crate have other limits?#
Yes: a limit on the size of the compiled program and one on the lazy matching cache. They are documented in its builder; read them before accepting patterns from users.
Sources#
regexdocumentation, "Untrusted input": https://docs.rs/regex/1.13.1/regex/#untrusted-inputRegexBuilder::size_limit: https://docs.rs/regex/1.13.1/regex/struct.RegexBuilder.html#method.size_limit- OWASP Regular expression Denial of Service: https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS