Skip to content

Commit b9f4896

Browse files
authored
Fix Leaf\Router base path validation to enforce exact match (#291)
1 parent 53a7297 commit b9f4896

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/Router.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -701,11 +701,23 @@ public static function setBasePath($serverBasePath)
701701
*/
702702
public static function getCurrentUri(): string
703703
{
704-
// Get the current Request URI and remove rewrite base path from it (= allows one to run the router in a sub folder)
705-
$uri = substr(rawurldecode($_SERVER['REQUEST_URI']), strlen(static::getBasePath()));
704+
$basePath = static::getBasePath();
705+
$requestUri = rawurldecode($_SERVER['REQUEST_URI']);
706+
707+
// Early exit If base path doesn't match
708+
if (strncmp($requestUri, $basePath, strlen($basePath)) !== 0) {
709+
if (!static::$notFoundHandler) {
710+
static::$notFoundHandler = function () {
711+
\Leaf\Exception\General::default404();
712+
};
713+
}
714+
static::invoke(static::$notFoundHandler);
715+
}
706716

707-
if (strstr($uri, '?')) {
708-
$uri = substr($uri, 0, strpos($uri, '?'));
717+
// Get the current Request URI and remove rewrite base path from it (= allows one to run the router in a sub folder)
718+
$uri = substr($requestUri, strlen($basePath)) ?: '/';
719+
if (($queryPos = strpos($uri, '?')) !== false) {
720+
$uri = substr($uri, 0, $queryPos);
709721
}
710722

711723
return '/' . trim($uri, '/');

0 commit comments

Comments
 (0)