Skip to content

Commit

Permalink
refactor: move logic to prevent access to initController
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Feb 3, 2022
1 parent 51fee60 commit fa60803
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
7 changes: 0 additions & 7 deletions system/Config/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
* the LICENSE file that was distributed with this source code.
*/

use CodeIgniter\Exceptions\PageNotFoundException;

/*
* System URI Routing
*
Expand All @@ -21,11 +19,6 @@
* already loaded up and ready for us to use.
*/

// Prevent access to initController method
$routes->add('(:any)/initController', static function () {
throw PageNotFoundException::forPageNotFound();
});

// Migrations
$routes->cli('migrations/(:segment)/(:segment)', '\CodeIgniter\Commands\MigrationsCommand::$1/$2');
$routes->cli('migrations/(:segment)', '\CodeIgniter\Commands\MigrationsCommand::$1');
Expand Down
5 changes: 5 additions & 0 deletions system/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,11 @@ public function autoRoute(string $uri)
$this->method = array_shift($segments) ?: $this->method;
}

// Prevent access to initController method
if (strtolower($this->method) === 'initcontroller') {
throw PageNotFoundException::forPageNotFound();
}

if (! empty($segments)) {
$this->params = $segments;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/system/Router/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,16 @@ public function testAutoRouteRejectsMidDot()
$router->autoRoute('Foo.bar');
}

public function testAutoRouteRejectsInitController()
{
$router = new Router($this->collection, $this->request);
$router->setTranslateURIDashes(true);

$this->expectException(PageNotFoundException::class);

$router->autoRoute('home/initController');
}

public function testDetectsLocales()
{
$router = new Router($this->collection, $this->request);
Expand Down

0 comments on commit fa60803

Please sign in to comment.