Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2704: ensure route registered via $routes->cli() not accessible via web browser even autoroute is true #2707

Merged
merged 12 commits into from
Mar 22, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions system/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,25 @@ 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();
$methodName = $this->methodName();

foreach ($this->collection->getRoutes('cli') as $route)
{
if (is_string($route))
{
if (strpos($route, $controller . '::' . $methodName) === 0)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using strpos is on purpose to handle parameter, eg: controller::method/$1:

$routes->cli('hello/(:any)', 'Commands\Hello::index/$1');

{
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))
Expand Down
11 changes: 11 additions & 0 deletions tests/_support/Controllers/Hello.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php namespace App\Controllers;

use CodeIgniter\Controller;

class Hello extends Controller
{
public function index()
{
return 'Hello';
}
}
21 changes: 21 additions & 0 deletions tests/system/Test/FeatureTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,25 @@ public function testCallZeroAsPathGot404()
}
$this->get('0');
}

public function testOpenCliRoutesFromHttpGot404()
{
$this->expectException(PageNotFoundException::class);
Copy link
Member

@kenjis kenjis Apr 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I add the following test case,
the test passes because there is no App\Controllers\Hello class.

            'parameterized method cli' => [
                'hello/(:segment)',
                'Hello::$1',
                'Hello/index',
            ],

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests pass because there is no App\Controllers\Hello class.


require_once SUPPORTPATH . 'Controllers/Hello.php';

$this->withRoutes([
[
'cli',
'hello',
'Hello::index',
],
]);

while (\ob_get_level() > 0)
{
\ob_end_flush();
}
$this->get('Hello');
}
}