Click to see the query in the CodeQL repository
Constructing a regular expression with unsanitized user input can be dangerous. A malicious user may be able to modify the meaning of the expression, causing it to match unexpected strings and construct large regular expressions by using counted repetitions.
Before embedding user input into a regular expression, escape the input string using a function such as regex::escape to escape meta-characters that have special meaning.
If purposefully supporting user supplied regular expressions, then use RegexBuilder::size_limit to limit the pattern size so that it is no larger than necessary.
The following example constructs a regular expressions from the user input key without escaping it first.
The regular expression is intended to match strings starting with "property" such as "property:foo=bar". However, a malicious user might inject the regular expression ".*^|key" and unexpectedly cause strings such as "key=secret" to match.
If user input is used to construct a regular expression, it should be escaped first. This ensures that malicious users cannot insert characters that have special meanings in regular expressions.
regex crate documentation: Untrusted patterns.
Common Weakness Enumeration: CWE-20.
Common Weakness Enumeration: CWE-74.