Skip to content

Commit 4ccac86

Browse files
committed
fix: executing CLI routes via HTTP request
1 parent 1eb0f6e commit 4ccac86

3 files changed

Lines changed: 26 additions & 5 deletions

File tree

phpstan-baseline.neon.dist

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,6 @@ parameters:
115115
count: 1
116116
path: system/CodeIgniter.php
117117

118-
-
119-
message: "#^Dead catch \\- CodeIgniter\\\\Exceptions\\\\PageNotFoundException is never thrown in the try block\\.$#"
120-
count: 1
121-
path: system/CodeIgniter.php
122-
123118
-
124119
message: "#^Property Config\\\\App\\:\\:\\$appTimezone \\(string\\) on left side of \\?\\? is not nullable\\.$#"
125120
count: 1

system/CodeIgniter.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,12 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon
299299

300300
$this->spoofRequestMethod();
301301

302+
if ($this->request instanceof IncomingRequest && $this->request->getMethod() === 'cli') {
303+
$this->response->setStatusCode(405)->setBody('Method Not Allowed');
304+
305+
return $this->sendResponse();
306+
}
307+
302308
Events::trigger('pre_system');
303309

304310
// Check for a cached page. Execution will stop
@@ -352,6 +358,7 @@ public function useSafeOutput(bool $safe = true)
352358
/**
353359
* Handles the main request logic and fires the controller.
354360
*
361+
* @throws PageNotFoundException
355362
* @throws RedirectException
356363
*
357364
* @return mixed|RequestInterface|ResponseInterface

tests/system/CodeIgniterTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,4 +425,23 @@ public function testRunDefaultRoute()
425425

426426
$this->assertStringContainsString('Welcome to CodeIgniter', $output);
427427
}
428+
429+
public function testRunCLIRoute()
430+
{
431+
$_SERVER['argv'] = ['index.php', 'cli'];
432+
$_SERVER['argc'] = 2;
433+
434+
$_SERVER['REQUEST_URI'] = '/cli';
435+
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
436+
$_SERVER['REQUEST_METHOD'] = 'CLI';
437+
438+
$routes = Services::routes();
439+
$routes->cli('cli', '\Tests\Support\Controllers\Popcorn::index');
440+
441+
ob_start();
442+
$this->codeigniter->useSafeOutput(true)->run();
443+
$output = ob_get_clean();
444+
445+
$this->assertStringContainsString('Method Not Allowed', $output);
446+
}
428447
}

0 commit comments

Comments
 (0)