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


---

# 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/2-java/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.
