> For the complete documentation index, see [llms.txt](https://docs.sec1.io/user-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sec1.io/user-docs/4-sast/2-java/unsanitized-user-input-in-regular-expression.md).

# Unsanitized User Input in Regular Expression

## Overview

* **Rule ID**: `java_lang_regex_using_user_input`
* **Applicable Languages**: Java
* **Weakness ID**: CWE-1287

## Description

Building regular expressions from user input can result in a vulnerability called Regular Expression Denial of Service (ReDoS). This occurs because some regular expressions can have exponential time complexity. If attackers exploit this, they can trigger significant CPU usage spikes, thereby denying service to legitimate users.

## Remediation Guidelines

* **Avoid** using user input directly in regular expressions whenever possible to prevent attackers from exploiting complex patterns that cause excessive CPU consumption.

  ```java
  Pattern pattern = Pattern.compile(request.getParameter("dangerousRegex"));

  ```
* If user input must be used in a regular expression, **sanitize or validate** it to ensure it does not contain patterns that could lead to ReDoS.

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