Skip to content

Commit b5c3f18

Browse files
authored
Merge pull request #1850 from MGatner/secure-routable-controller-methods
Secure routable controller methods
2 parents e73d3c3 + a56bfdf commit b5c3f18

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

system/Config/Routes.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,9 @@
5757

5858
// CLI Catchall - uses a _remap to call Commands
5959
$routes->cli('ci(:any)', '\CodeIgniter\CLI\CommandRunner::index/$1');
60+
61+
// Prevent access to initController method
62+
$routes->add('(:any)/initController', function()
63+
{
64+
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
65+
});

system/Controller.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function initController(RequestInterface $request, ResponseInterface $res
138138
*
139139
* @throws \CodeIgniter\HTTP\Exceptions\HTTPException
140140
*/
141-
public function forceHTTPS(int $duration = 31536000)
141+
protected function forceHTTPS(int $duration = 31536000)
142142
{
143143
force_https($duration, $this->request, $this->response);
144144
}
@@ -151,7 +151,7 @@ public function forceHTTPS(int $duration = 31536000)
151151
*
152152
* @param integer $time
153153
*/
154-
public function cachePage(int $time)
154+
protected function cachePage(int $time)
155155
{
156156
CodeIgniter::cache($time);
157157
}
@@ -185,7 +185,7 @@ protected function loadHelpers()
185185
*
186186
* @return boolean
187187
*/
188-
public function validate($rules, array $messages = []): bool
188+
protected function validate($rules, array $messages = []): bool
189189
{
190190
$this->validator = Services::validation();
191191

tests/system/ControllerTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ public function testCachePage()
8787
$this->controller = new Controller();
8888
$this->controller->initController($this->request, $this->response, $this->logger);
8989

90-
$this->assertNull($this->controller->cachePage(10));
90+
$method = $this->getPrivateMethodInvoker($this->controller, 'cachePage');
91+
$this->assertNull($method(10));
9192
}
9293

9394
public function testValidate()
@@ -97,7 +98,8 @@ public function testValidate()
9798
$this->controller->initController($this->request, $this->response, $this->logger);
9899

99100
// and that we can attempt validation, with no rules
100-
$this->assertFalse($this->controller->validate([]));
101+
$method = $this->getPrivateMethodInvoker($this->controller, 'validate');
102+
$this->assertFalse($method([]));
101103
}
102104

103105
//--------------------------------------------------------------------

0 commit comments

Comments
 (0)