From e6601c70541bd34932c75a316bb596f2f03dcd42 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Sun, 10 May 2026 23:55:59 +0100 Subject: [PATCH] Harden request URL handling in bootstrap and stabilize Theme API not-found test assertions (#3511) * Initial plan * fix: harden index URL handling and relax brittle theme API error assertions Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/1a498284-9424-42d6-8e7f-dfe061a51478 Co-authored-by: admdly <2807620+admdly@users.noreply.github.com> * chore: address review nits in index and theme admin test Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/1a498284-9424-42d6-8e7f-dfe061a51478 Co-authored-by: admdly <2807620+admdly@users.noreply.github.com> * fix: keep url fallback type-safe when parse_url fails Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/1a498284-9424-42d6-8e7f-dfe061a51478 Co-authored-by: admdly <2807620+admdly@users.noreply.github.com> * test: clarify 740 error code reference in theme admin test Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/1a498284-9424-42d6-8e7f-dfe061a51478 Co-authored-by: admdly <2807620+admdly@users.noreply.github.com> * fix: add explicit null fallback for parsed request path Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/1a498284-9424-42d6-8e7f-dfe061a51478 Co-authored-by: admdly <2807620+admdly@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: admdly <2807620+admdly@users.noreply.github.com> --- src/index.php | 14 ++++++++++---- tests/Modules/Theme/AdminTest.php | 6 +++--- 2 files changed, 13 insertions(+), 7 deletions(-) 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.'); } }