Skip to content

Commit 1dec495

Browse files
committed
fix: keep the regex path for URIs ending with LF
The static fast-path compared the route key with ===, which dropped matches the `$` anchor used to accept for a URI ending in LF, sending those requests to a catch-all or a 404. Fall back to PCRE when the URI ends with LF, and add regression tests for LF and CRLF URIs. Also compare preg_match() against 1, as requested in review.
1 parent aab92a1 commit 1dec495

2 files changed

Lines changed: 54 additions & 4 deletions

File tree

‎system/Router/Router.php‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -450,10 +450,13 @@ protected function checkRoutes(string $uri): bool
450450

451451
$matchedKey = $routeKey;
452452

453-
// `#` is the pattern delimiter used below, so it must be treated as a metacharacter.
453+
// A key without PCRE metacharacters, including the `#` delimiter
454+
// used below, can be compared as a plain string.
454455
$isRegex = strpbrk($routeKey, '()[]?*+{}^$\\.|#') !== false;
455456

456-
if (! $isRegex) {
457+
// `$` also matches before a trailing newline, so the plain
458+
// comparison is only safe when the URI does not end with LF.
459+
if (! $isRegex && ! str_ends_with($uri, "\n")) {
457460
if ($routeKey !== $uri) {
458461
continue;
459462
}
@@ -465,8 +468,7 @@ protected function checkRoutes(string $uri): bool
465468
$routeKey = str_replace('{locale}', '[^/]+', $routeKey);
466469
}
467470

468-
// Does the RegEx match?
469-
if (! preg_match('#^' . $routeKey . '$#u', $uri, $matches)) {
471+
if (preg_match('#^' . $routeKey . '$#u', $uri, $matches) !== 1) {
470472
continue;
471473
}
472474
}

‎tests/system/Router/RouterTest.php‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,4 +1158,52 @@ public function testRouteWithEscapedHashMatchesViaRegexBranch(): void
11581158
$this->assertSame('\Docs', $router->controllerName());
11591159
$this->assertSame('anchor', $router->methodName());
11601160
}
1161+
1162+
public function testStaticRouteMatchesUriEndingWithNewline(): void
1163+
{
1164+
$config = config(App::class);
1165+
$permittedURIChars = $config->permittedURIChars;
1166+
$config->permittedURIChars = '';
1167+
1168+
$collection = clone $this->collection;
1169+
$collection->resetRoutes();
1170+
$collection->get('about-us', 'Home::about');
1171+
1172+
$router = new Router($collection, $this->request);
1173+
1174+
try {
1175+
// `handle()` decodes `%0A` to LF, and the `$` anchor also matches
1176+
// before a trailing newline.
1177+
$router->handle('about-us%0A');
1178+
1179+
$this->assertSame('\Home', $router->controllerName());
1180+
$this->assertSame('about', $router->methodName());
1181+
$this->assertSame([], $router->params());
1182+
} finally {
1183+
$config->permittedURIChars = $permittedURIChars;
1184+
}
1185+
}
1186+
1187+
public function testStaticRouteDoesNotMatchUriEndingWithCarriageReturnAndNewline(): void
1188+
{
1189+
$config = config(App::class);
1190+
$permittedURIChars = $config->permittedURIChars;
1191+
$config->permittedURIChars = '';
1192+
1193+
$collection = clone $this->collection;
1194+
$collection->resetRoutes();
1195+
$collection->get('about-us', 'Home::about');
1196+
1197+
$router = new Router($collection, $this->request);
1198+
1199+
try {
1200+
$this->expectException(PageNotFoundException::class);
1201+
1202+
// The `$` anchor does not match before CRLF, so the guard must not
1203+
// be widened to a substring check.
1204+
$router->handle('about-us%0D%0A');
1205+
} finally {
1206+
$config->permittedURIChars = $permittedURIChars;
1207+
}
1208+
}
11611209
}

0 commit comments

Comments
 (0)