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.

    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

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

Last updated