Regex without timeout guard (ARCH7074)
Ce contenu n’est pas encore disponible dans votre langue.
Key php.performance.regex-timeout · Severity info · Category performance
Matching untrusted input with an unbounded regex can lock the CPU through catastrophic backtracking (ReDoS) on pathological patterns. Cap input length or set the PCRE backtracking limit.
Incorrect
Section titled “Incorrect”<?phpfunction matches(string $input): bool{ return (bool) preg_match('/^(\w+)+$/', $input);}Correct
Section titled “Correct”<?phpfunction matches(string $input): bool{ if (strlen($input) > 256) { return false; } ini_set('pcre.backtrack_limit', '100000'); return (bool) preg_match('/^\w+$/', $input);}Bound input length and constrain backtracking via pcre.backtrack_limit to prevent ReDoS.