← Java / Spring · Contents

JAVA-062 / Advanced

Regular expression denial of service (ReDoS)

A04:2021 Insecure Design · CWE-1333 Inefficient Regex Complexity

Why is this a problem?

Regular expressions with nested quantifiers occupy CPU on long input.

How to fix it

java
if (input.length() > 500) throw new IllegalArgumentException();
input.matches("^\\w+(?:\\s\\w+)*$");

Further reading