diff --git a/Tests/AbstractWebApplicationTest.php b/Tests/AbstractWebApplicationTest.php index 198d1303..288cbae3 100644 --- a/Tests/AbstractWebApplicationTest.php +++ b/Tests/AbstractWebApplicationTest.php @@ -1407,7 +1407,7 @@ public function testGetBody() } /** - * @testdox Tests that the application correcty detects the request URI based on the injected data + * @testdox Tests that the application correctly detects the request URI based on the injected data * * @param string|null $https Value for $_SERVER['HTTPS'] or null to not set it * @param string $phpSelf Value for $_SERVER['PHP_SELF'] @@ -1453,6 +1453,34 @@ public function testDetectRequestUri( ); } + /** + * @testdox Tests that the application throws an exception when there isn't a valid HTTP host. + * + * @param string|null $https Value for $_SERVER['HTTPS'] or null to not set it + * @param string $phpSelf Value for $_SERVER['PHP_SELF'] + * @param string $requestUri Value for $_SERVER['REQUEST_URI'] + * @param string $httpHost Value for $_SERVER['HTTP_HOST'] + * @param string $scriptName Value for $_SERVER['SCRIPT_NAME'] + * @param string $queryString Value for $_SERVER['QUERY_STRING'] + * @param string $expects Expected full URI string + * + * @covers \Joomla\Application\AbstractWebApplication + * + * @backupGlobals enabled + */ + public function testDetectRequestUriException() + { + $this->expectException(\InvalidArgumentException::class); + $mockInput = new Input([]); + + $_SERVER['PHP_SELF'] = 'somthing/framework.php'; + $_SERVER['HTTP_HOST'] = ''; + $_SERVER['SCRIPT_NAME'] = 'somthing/framework.php'; + + $object = $this->getMockForAbstractClass(AbstractWebApplication::class, [$mockInput]); + + TestHelper::invoke($object, 'detectRequestUri'); + } /** * @testdox Tests the system URIs are correctly loaded when a URI is set in the application configuration * diff --git a/src/AbstractWebApplication.php b/src/AbstractWebApplication.php index a1637bc3..6145408b 100644 --- a/src/AbstractWebApplication.php +++ b/src/AbstractWebApplication.php @@ -793,9 +793,20 @@ protected function checkHeadersSent() * @return string The requested URI * * @since 1.0.0 + * @throws \InvalidArgumentException */ protected function detectRequestUri() { + /** + * If we've arrived here via a CLI application (which shouldn't happen in our web app) then we can use this as + * an opportunity to bail out + */ + $httpHost = $this->input->server->getString('HTTP_HOST'); + + if ($httpHost === null) { + throw new \InvalidArgumentException('Found an empty hostname when parsing the request'); + } + // First we need to detect the URI scheme. $scheme = $this->isSslConnection() ? 'https://' : 'http://'; @@ -808,7 +819,7 @@ protected function detectRequestUri() $phpSelf = $this->input->server->getString('PHP_SELF', ''); $requestUri = $this->input->server->getString('REQUEST_URI', ''); - $uri = $scheme . $this->input->server->getString('HTTP_HOST'); + $uri = $scheme . $httpHost; if (!empty($phpSelf) && !empty($requestUri)) { // If PHP_SELF and REQUEST_URI are both populated then we will assume "Apache Mode".