Regex Injection
ID |
javascript.regex_injection |
Severity |
critical |
Resource |
Injection |
Language |
JavaScript |
Tags |
CWE:1333, CWE:625, NIST.SP.800-5, OWASP:2004:A9, PCI-DSS:6.5.1 |
Description
Improper neutralization of external input used in a regular expression ('Regex Injection').
Regular Expression (Regex) Injection arises when input from users is directly concatenated into a regex pattern without validation or sanitization. This can allow an attacker to manipulate the pattern used for searching or matching, potentially bypassing validation or security checks.
Rationale
Consider the following code:
function passwordContainsUsername() {
var email = document.forms['login']['email'].value;
var uname = email.split('@')[0];
// This is regex injection, which could be used for Denial of Service and other attacks
var pattern = new RegExp('.*' + uname + '.*', 'i'); // FLAW
var pass = document.forms['login']['password'].value;
return !pattern.test(pass);
}
In this code, the uname
from user input is directly embedded into the regex pattern. If the input contains special regex characters or sequences, it can alter the intended structure of the expression. For example, an input of "test)|(^.*$"
could manipulate the pattern to match virtually any string, circumventing the intended constraints. Also, an attacker may provide in the email input a pattern affected by catastrophic backtracking, which together with a well-chosen password input can cause a Denial of Service condition.
Remediation
To remediate Regular Expression Injection vulnerabilities, you can use the following strategies:
-
Escape User Input: Always escape user input to be included in regex patterns, using the library’s 'regexp quoting' functions.
-
Use Strict Validation: Implement strict validation on input prior to its inclusion in a regex pattern. Ensuring that input only contains expected characters significantly lowers the risk of injection.
-
Predefine Regular Expressions: Avoid dynamically building regex patterns based on user input. Instead, use predefined patterns that safely incorporate user input as part of the matching process.
-
Input Filtering: Apply input filtering to remove known harmful characters or sequences from user input before processing them in regex operations.
-
Security Tools and Static Analysis: Utilize SAST tools to identify regex injection vulnerabilities during the development process. These tools can provide early warnings about unsafe patterns or input concatenations.
By following these measures, you not only prevent Regular Expression Injection but also ensure that your application remains robust and secure against a variety of input-based exploits.
In the example above, the regex pattern should be escaped using the RegExp.escape()
method, so the input is matched literally.
function passwordContainsUsername() {
var email = document.forms['login']['email'].value;
var uname = email.split('@')[0];
var quoted = Regexp.escape(uname); // FLAW
var pattern = new RegExp('.*' + quoted + '.*', 'i');
var pass = document.forms['login']['password'].value;
return !pattern.test(pass);
}
Configuration
The detector has the following configurable parameters:
-
sources
, that indicates the source kinds to check. -
neutralizations
, that indicates the neutralization kinds to check.
Unless you need to change the default behavior, you typically do not need to configure this detector.
References
-
CWE-625 : Permissive Regular Expression.
-
CWE-1333 : Inefficient Regular Expression Complexity.
-
OWASP Top 10 2021 - A03 : Injection.