# Unsanitized user input in regular expression

## Overview

* **Rule ID**: `javascript_lang_regex_using_user_input`
* **Applicable Languages**: Javascript
* **Weakness ID**: CWE-1287

## Description

Creating regular expressions from user input can result in a vulnerability called Regular Expression Denial of Service (ReDoS). This problem occurs because certain regular expressions can be executed with exponential time complexity. When attackers take advantage of this, it can considerably deplete CPU resources, thereby causing a denial of service.

## Remediation Guidelines

* **Avoid** using user-provided data directly in regular expressions. This prevents attackers from exploiting the ReDoS vulnerability to cause a denial of service.

  ```javascript
  new RegExp(`abc${req.params.untrusted}`, 'i'); // insecure
  ```
* **Ensure to sanitize** or validate all user input if it must be used in a regular expression, to confirm it does not include patterns that could cause ReDoS attacks.
* **Consider applying timeouts** or other restrictions on regex operations to mitigate possible ReDoS attacks when user input is involved.

## References

* [**OWASP ReDoS attacks explained**](https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS)
* [**CWE-1287: Improper Validation of Specified Type of Input**](https://cwe.mitre.org/data/definitions/1287.html)

## Configuration

To omit this rule during a scan, and to provide you with continuous 24/7 code-level scanning, you can employ our [**SAST TOOL**](https://scopy.sec1.io/login)
