Skip to content

Commit 427f2d7

Browse files
authored
Fix Leaf\Router base path validation to enforce exact match (#290)
* Fix Leaf\Router base path validation to enforce exact match * chore: fix styling --------- Co-authored-by: terrybr <[email protected]>
1 parent d09dde2 commit 427f2d7

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
@@ -686,11 +686,23 @@ public static function setBasePath($serverBasePath)
686686
*/
687687
public static function getCurrentUri(): string
688688
{
689-
// Get the current Request URI and remove rewrite base path from it (= allows one to run the router in a sub folder)
690-
$uri = substr(rawurldecode($_SERVER['REQUEST_URI']), strlen(static::getBasePath()));
689+
$basePath = static::getBasePath();
690+
$requestUri = rawurldecode($_SERVER['REQUEST_URI']);
691+
692+
// Early exit If base path doesn't match
693+
if (strncmp($requestUri, $basePath, strlen($basePath)) !== 0) {
694+
if (!static::$notFoundHandler) {
695+
static::$notFoundHandler = function () {
696+
\Leaf\Exception\General::default404();
697+
};
698+
}
699+
static::invoke(static::$notFoundHandler);
700+
}
691701

692-
if (strstr($uri, '?')) {
693-
$uri = substr($uri, 0, strpos($uri, '?'));
702+
// Get the current Request URI and remove rewrite base path from it (= allows one to run the router in a sub folder)
703+
$uri = substr($requestUri, strlen($basePath)) ?: '/';
704+
if (($queryPos = strpos($uri, '?')) !== false) {
705+
$uri = substr($uri, 0, $queryPos);
694706
}
695707

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

0 commit comments

Comments
 (0)