FastRoute icon indicating copy to clipboard operation
FastRoute copied to clipboard

preg_match errors are ignored

Open Lumaraf opened this issue 7 years ago • 0 comments

If routes contain complicated (and inefficient) patterns for parameters the pcre.backtrack_limit or other limits can be reached when checking for matching routes. FastRoute should check if preg_match returned false and abort routing with an error instead of silently treating this as not matching routes.

The following example shows that on PHP7 with default pcre.backtrack_limit the second route will not match on paths with more than 14 characters.

$dispatcher = \FastRoute\simpleDispatcher(function (\FastRoute\RouteCollector $r) {
    $r->addRoute(['GET'], '/{p:(?:a?a?)*}/complicated', 'complicated_pattern');
    $r->addRoute(['GET'], '/{p:a+}', 'a');
});
for ($n = 1; ; $n++) {
    $routeInfo = $dispatcher->dispatch('GET', '/' . str_repeat('a', $n));
    if ($routeInfo[0] !== \FastRoute\Dispatcher::FOUND) {
        var_dump($n, $routeInfo);
        break;
    }
}

Lumaraf avatar Jun 13 '18 09:06 Lumaraf