Catastrophic Backtracking

Identifier: regex.lint.redos, in PHPStan too, whatever the severity; the message names it

When it triggers: The ReDoS analyzer proves how one match attempt grows with the input, by following PCRE’s backtracking order, and hands back the input that triggers it. Patterns outside that model (backreferences, conditionals, recursion, an analysis over budget) are judged by heuristics, and the report says which of the two decided. The message prefix names the class: Exponential backtracking (ReDoS), Polynomial backtracking (ReDoS) or Potential backtracking (ReDoS).

Risk Levels:

Level Proven class Action Required
critical exponential Refactor immediately
high polynomial, degree 3 or more Consider refactoring
medium polynomial, degree 2 (quadratic) Monitor and plan fix
low heuristic finding only Accept with logging

See the ReDoS guide to read the verdict and fix the pattern, and the ReDoS analysis reference for the guarantee and its limits.

Example:

// VULNERABLE: Exponential backtracking
preg_match('/(a+)+$/', $input);  // CRITICAL

// SAFER: Atomic group
preg_match('/(?>a+)+$/', $input);  // SAFE

// SAFER: Possessive quantifier
preg_match('/(a++)+$/', $input);  // SAFE

Fix: Make the ambiguous part atomic or possessive, or refactor.

Read more:


Try it. Open /(a+)+$/ in the playground: the ReDoS tab holds the verdict.

Edit on GitHub Feedback