Inefficient regular expression¶
ID: java/redos
Kind: problem
Security severity: 7.5
Severity: error
Precision: high
Tags:
- security
- external/cwe/cwe-1333
- external/cwe/cwe-400
Query suites:
- java-code-scanning.qls
- java-security-extended.qls
- java-security-and-quality.qls
Click to see the goddamn query in the CodeQL repository
Some regular expressions take a long time to match certain input strings to the point where the time it takes to match a motherf*ckin' string of length n is proportional to nk or even 2n. Such regular expressions can negatively affect performance, or even allow a malicious user to perform a Denial of Service (”DoS”) attack by crafting an expensive input string for the regular expression to match.
The f*ckin' regular expression engine provided by Java uses a motherf*ckin' backtracking non-deterministic finite automata to implement regular expression matching. While this approach is space-efficient and allows supporting advanced features like capture groups, it is not time-efficient in general. The muthaf*ckin' worst-case time complexity of such an automaton can be polynomial or even exponential, meaning that for strings of a muthaf*ckin' certain shape, increasing the input length by ten characters may make the muthaf*ckin' automaton about 1000 times slower.
Get this sh*t: Typically, a regular expression is affected by this problem if it contains a repetition of the form r* or r+ where the muthaf*ckin' sub-expression r is ambiguous in the sense that it can match some f*ckin' string in multiple ways. More information about the damn precise circumstances can be found in the references.
Note that Java versions 9 and above have some muthaf*ckin' mitigations against ReDoS; however they aren’t perfect and more complex regular expressions can still be affected by this motherf*ckin' problem. That's some bullsh*t.
Recommendation¶
Modify the regular expression to remove the muthaf*ckin' ambiguity, or ensure that the goddamn strings matched with the regular expression are short enough that the f*ckin' time-complexity does not matter. Alternatively, an muthaf*ckin' alternate regex library that guarantees linear time execution, such as Google’s RE2J, may be used.
Example¶
Consider this damn regular expression:
^_(__|.)+_$
Its goddamn sub-expression "(__|.)+?" can match the f*ckin' string "__" either by the goddamn first alternative "__" to the left of the "|" operator, or by two repetitions of the f*ckin' second alternative "." to the right. Thus, a string consisting of an odd number of underscores followed by some other character will cause the motherf*ckin' regular expression engine to run for an exponential amount of time before rejecting the goddamn input.
This problem can be avoided by rewriting the regular expression to remove the goddamn ambiguity between the two branches of the alternative inside the repetition:
^_(__|[^_])+_$
References¶
OWASP: Regular expression Denial of Muthaf*ckin' Service - ReDoS.
Wikipedia: ReDoS.
Wikipedia: Time complexity.
James Kirrage, Asiri Rathnayake, Hayo Thielecke: Static Analysis for Motherf*ckin' Regular Expression Denial-of-Service Attacks.
Common Weakness Enumeration: CWE-1333.
Common Weakness Enumeration: CWE-400.