diff --git a/src/index.php b/src/index.php index b2918c1c8..3fd8c8695 100644 --- a/src/index.php +++ b/src/index.php @@ -25,7 +25,7 @@ if ((bool) ($config['debug_and_monitoring']['debug'] ?? false)) { $timeCollector = $debugBar->getCollector('time'); if (!$timeCollector instanceof TimeDataCollector) { - throw new RuntimeException('Time collector not found in debug bar.'); + throw new \RuntimeException('Time collector not found in debug bar.'); } // PDO collector @@ -53,11 +53,17 @@ if ((bool) ($config['debug_and_monitoring']['debug'] ?? false)) { } // Get the request URL -$rawUrl = $_GET['_url'] ?? parse_url((string) ($_SERVER['REQUEST_URI'] ?? '/'), PHP_URL_PATH); +if (isset($_GET['_url']) && is_string($_GET['_url'])) { + $rawUrl = $_GET['_url']; +} else { + $rawUrl = parse_url((string) ($_SERVER['REQUEST_URI'] ?? '/'), PHP_URL_PATH) ?? '/'; +} $url = is_string($rawUrl) ? $rawUrl : '/'; // Validate and normalize URL path before using it in routing logic -if ($url === '' || $url[0] !== '/' || preg_match('/[\x00-\x1F\x7F]/', $url) === 1) { +if ($url === '') { + $url = '/'; +} elseif ($url[0] !== '/' || preg_match('/[\x00-\x1F\x7F]/', $url) === 1) { $url = '/'; } @@ -88,7 +94,7 @@ $timeCollector?->stopMeasure('session_start'); if (strncasecmp((string) $url, ADMIN_PREFIX, strlen(ADMIN_PREFIX)) === 0) { define('ADMIN_AREA', true); - $urlWithoutQueryString = preg_replace('/\?.+/', '', (string) $url); + $urlWithoutQueryString = parse_url((string) $url, PHP_URL_PATH) ?? (string) $url; $adminRelativeUrl = str_replace(ADMIN_PREFIX, '', (string) $urlWithoutQueryString); $appUrl = $adminRelativeUrl; $app = new Box_AppAdmin([], $debugBar); diff --git a/tests/Modules/Theme/AdminTest.php b/tests/Modules/Theme/AdminTest.php index 4150c0c7b..f5234c8c8 100644 --- a/tests/Modules/Theme/AdminTest.php +++ b/tests/Modules/Theme/AdminTest.php @@ -40,12 +40,12 @@ final class AdminTest extends TestCase $result = Request::makeRequest('admin/theme/non_existing_action'); $this->assertSame(404, $result->getStatusCode(), 'Invalid theme action should return HTTP 404.'); $this->assertFalse($result->wasSuccessful(), 'Invalid theme action should not be successful.'); - $this->assertSame(740, $result->getErrorCode(), 'Invalid theme action should return error code 740.'); + // API not-found actions/endpoints are mapped to error code 740 (see src/library/Api/Handler.php). + $expectedNotFoundErrorCode = 740; + $this->assertSame($expectedNotFoundErrorCode, $result->getErrorCode(), 'Invalid theme action should return the API not-found error code.'); $errorMessage = $result->getErrorMessage(); $this->assertIsString($errorMessage, 'Error message should be a string.'); $this->assertNotSame('', trim($errorMessage), 'Error message should not be empty.'); - $this->assertStringContainsString('non_existing_action', $errorMessage, 'Error message should reference the invalid action or endpoint.'); - $this->assertStringContainsString('does not exist', $errorMessage, 'Invalid theme action should report a missing endpoint/action.'); } }