Skip to content

Commit 08cbc30

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 893f1e1 commit 08cbc30

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
@@ -1132,4 +1132,52 @@ public function testRouteWithEscapedHashMatchesViaRegexBranch(): void
11321132
$this->assertSame('\Docs', $router->controllerName());
11331133
$this->assertSame('anchor', $router->methodName());
11341134
}
1135+
1136+
public function testStaticRouteMatchesUriEndingWithNewline(): void
1137+
{
1138+
$config = config(App::class);
1139+
$permittedURIChars = $config->permittedURIChars;
1140+
$config->permittedURIChars = '';
1141+
1142+
$collection = clone $this->collection;
1143+
$collection->resetRoutes();
1144+
$collection->get('about-us', 'Home::about');
1145+
1146+
$router = new Router($collection, $this->request);
1147+
1148+
try {
1149+
// `handle()` decodes `%0A` to LF, and the `$` anchor also matches
1150+
// before a trailing newline.
1151+
$router->handle('about-us%0A');
1152+
1153+
$this->assertSame('\Home', $router->controllerName());
1154+
$this->assertSame('about', $router->methodName());
1155+
$this->assertSame([], $router->params());
1156+
} finally {
1157+
$config->permittedURIChars = $permittedURIChars;
1158+
}
1159+
}
1160+
1161+
public function testStaticRouteDoesNotMatchUriEndingWithCarriageReturnAndNewline(): void
1162+
{
1163+
$config = config(App::class);
1164+
$permittedURIChars = $config->permittedURIChars;
1165+
$config->permittedURIChars = '';
1166+
1167+
$collection = clone $this->collection;
1168+
$collection->resetRoutes();
1169+
$collection->get('about-us', 'Home::about');
1170+
1171+
$router = new Router($collection, $this->request);
1172+
1173+
try {
1174+
$this->expectException(PageNotFoundException::class);
1175+
1176+
// The `$` anchor does not match before CRLF, so the guard must not
1177+
// be widened to a substring check.
1178+
$router->handle('about-us%0D%0A');
1179+
} finally {
1180+
$config->permittedURIChars = $permittedURIChars;
1181+
}
1182+
}
11351183
}

0 commit comments

Comments
 (0)