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.

    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

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