From 8e248f33d2bdd8dda74d5ab992eada6965a01563 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 5 May 2022 09:11:43 +0900 Subject: [PATCH 1/3] fix: limit to subdomain '*' does not work --- system/Router/RouteCollection.php | 6 +++--- tests/system/Router/RouteCollectionTest.php | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/system/Router/RouteCollection.php b/system/Router/RouteCollection.php index abfa5493ae1f..b9f59c1aa14d 100644 --- a/system/Router/RouteCollection.php +++ b/system/Router/RouteCollection.php @@ -1297,7 +1297,7 @@ private function getMethodParams(string $from): string * Compares the subdomain(s) passed in against the current subdomain * on this page request. * - * @param mixed $subdomains + * @param string|string[] $subdomains */ private function checkSubdomains($subdomains): bool { @@ -1330,7 +1330,7 @@ private function checkSubdomains($subdomains): bool * It's especially not perfect since it's possible to register a domain * with a period (.) as part of the domain name. * - * @return mixed + * @return false|string the subdomain */ private function determineCurrentSubdomain() { @@ -1351,7 +1351,7 @@ private function determineCurrentSubdomain() } // Get rid of any domains, which will be the last - unset($host[count($host)]); + unset($host[count($host) - 1]); // Account for .co.uk, .co.nz, etc. domains if (end($host) === 'co') { diff --git a/tests/system/Router/RouteCollectionTest.php b/tests/system/Router/RouteCollectionTest.php index 444d32d9b258..10bb36ec9c08 100644 --- a/tests/system/Router/RouteCollectionTest.php +++ b/tests/system/Router/RouteCollectionTest.php @@ -1156,6 +1156,20 @@ public function testWithDifferentSubdomainMissing() $this->assertSame($expects, $routes->getRoutes()); } + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/5959 + */ + public function testWithNoSubdomainAndDot() + { + $_SERVER['HTTP_HOST'] = 'example.com'; + + $routes = $this->getCollector(); + + $routes->add('/objects/(:alphanum)', 'App::objectsList/$1', ['subdomain' => '*']); + + $this->assertSame([], $routes->getRoutes()); + } + /** * @see https://github.com/codeigniter4/CodeIgniter4/issues/1692 */ From 38d5c109981bb51dc3ba2309ca6152c49fc07f28 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 5 May 2022 09:16:42 +0900 Subject: [PATCH 2/3] fix: incorrect test `example.com` does not match`'subdomain' => '*'`, so the route is not registered. --- tests/system/Router/RouteCollectionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/system/Router/RouteCollectionTest.php b/tests/system/Router/RouteCollectionTest.php index 10bb36ec9c08..a36fcb8de3b2 100644 --- a/tests/system/Router/RouteCollectionTest.php +++ b/tests/system/Router/RouteCollectionTest.php @@ -1462,7 +1462,7 @@ public function testRouteToWithGenericSubdomainNot() $routes->get('i/(:any)', 'App\Controllers\Site\CDoc::item/$1', ['subdomain' => '*', 'as' => 'doc_item']); - $this->assertSame('/i/sth', $routes->reverseRoute('doc_item', 'sth')); + $this->assertFalse($routes->reverseRoute('doc_item', 'sth')); } public function testRouteToWithoutSubdomainMatch() From fbc94c149bcfe3cb905a4124810de6c954507e85 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 6 May 2022 09:55:48 +0900 Subject: [PATCH 3/3] test: fix wrong test --- tests/system/Router/RouteCollectionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/system/Router/RouteCollectionTest.php b/tests/system/Router/RouteCollectionTest.php index a36fcb8de3b2..44747ca35d3a 100644 --- a/tests/system/Router/RouteCollectionTest.php +++ b/tests/system/Router/RouteCollectionTest.php @@ -1128,7 +1128,7 @@ public function testWithDotCoSubdomain() { $routes = $this->getCollector(); - $_SERVER['HTTP_HOST'] = 'example.uk.co'; + $_SERVER['HTTP_HOST'] = 'example.co.uk'; $routes->add('/objects/(:alphanum)', 'Admin::objectsList/$1', ['subdomain' => 'sales']); $routes->add('/objects/(:alphanum)', 'App::objectsList/$1');