# 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)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sec1.io/user-docs/4-sast/3-javascript/unsanitized-user-input-in-regular-expression.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
