diff --git a/system/Router/Router.php b/system/Router/Router.php index f56af09c91f6..a72915acda72 100644 --- a/system/Router/Router.php +++ b/system/Router/Router.php @@ -542,6 +542,32 @@ public function autoRoute(string $uri) $this->params = $segments; } + if ($this->collection->getHTTPVerb() !== 'cli') + { + $controller = '\\' . $this->collection->getDefaultNamespace(); + $controller .= $this->directory ? str_replace('/', '\\', $this->directory) : ''; + $controller .= $this->controllerName(); + $controller = strtolower($controller); + $methodName = strtolower($this->methodName()); + + foreach ($this->collection->getRoutes('cli') as $route) + { + if (is_string($route)) + { + $route = strtolower($route); + if (strpos($route, $controller . '::' . $methodName) === 0) + { + throw new PageNotFoundException(); + } + + if ($route === $controller) + { + throw new PageNotFoundException(); + } + } + } + } + // Load the file so that it's available for CodeIgniter. $file = APPPATH . 'Controllers/' . $this->directory . $this->controllerName() . '.php'; if (is_file($file)) diff --git a/tests/_support/Controllers/Hello.php b/tests/_support/Controllers/Hello.php new file mode 100644 index 000000000000..8c7c05f01970 --- /dev/null +++ b/tests/_support/Controllers/Hello.php @@ -0,0 +1,11 @@ +get('0'); } + + public function provideRoutesData() + { + return [ + 'non parameterized cli' => [ + 'hello', + 'Hello::index', + 'Hello', + ], + 'parameterized cli' => [ + 'hello/(:any)', + 'Hello::index/$1', + 'Hello/index/samsonasik', + ], + 'default method index' => [ + 'hello', + 'Hello', + 'Hello', + ], + 'capitalized controller and/or method' => [ + 'hello', + 'Hello', + 'HELLO/INDEX', + ], + ]; + } + + /** + * @dataProvider provideRoutesData + */ + public function testOpenCliRoutesFromHttpGot404($from, $to, $httpGet) + { + $this->expectException(PageNotFoundException::class); + + require_once SUPPORTPATH . 'Controllers/Hello.php'; + + $this->withRoutes([ + [ + 'cli', + $from, + $to, + ], + ]); + + while (\ob_get_level() > 0) + { + \ob_end_flush(); + } + $this->get($httpGet); + } }