Skip to content

Commit 74c2ded

Browse files
committed
Fix directory traversal of .ctp files
1 parent 02df9ff commit 74c2ded

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

app/Controller/PagesController.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ class PagesController extends AppController {
4141
* Displays a view
4242
*
4343
* @return void
44+
* @throws ForbiddenException When a directory traversal attempt.
4445
* @throws NotFoundException When the view file could not be found
45-
* or MissingViewException in debug mode.
46+
* or MissingViewException in debug mode.
4647
*/
4748
public function display() {
4849
$path = func_get_args();
@@ -51,6 +52,9 @@ public function display() {
5152
if (!$count) {
5253
return $this->redirect('/');
5354
}
55+
if (in_array('..', $path, true) || in_array('.', $path, true)) {
56+
throw new ForbiddenException();
57+
}
5458
$page = $subpage = $title_for_layout = null;
5559

5660
if (!empty($path[0])) {

lib/Cake/Console/Templates/skel/Controller/PagesController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class PagesController extends AppController {
3232
* Displays a view
3333
*
3434
* @return void
35+
* @throws ForbiddenException When a directory traversal attempt.
3536
* @throws NotFoundException When the view file could not be found
3637
* or MissingViewException in debug mode.
3738
*/
@@ -42,6 +43,9 @@ public function display() {
4243
if (!$count) {
4344
return $this->redirect('/');
4445
}
46+
if (in_array('..', $path, true) || in_array('.', $path, true)) {
47+
throw new ForbiddenException();
48+
}
4549
$page = $subpage = $title_for_layout = null;
4650

4751
if (!empty($path[0])) {

lib/Cake/Test/Case/Controller/PagesControllerTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,21 @@ public function testMissingViewInDebug() {
7575
$Pages = new PagesController(new CakeRequest(null, false), new CakeResponse());
7676
$Pages->display('non_existing_page');
7777
}
78+
79+
/**
80+
* Test directory traversal protection
81+
*
82+
* @expectedException ForbiddenException
83+
* @expectedExceptionCode 403
84+
* @return void
85+
*/
86+
public function testDirectoryTraversalProtection() {
87+
App::build(array(
88+
'View' => array(
89+
CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS
90+
)
91+
));
92+
$Pages = new PagesController(new CakeRequest(null, false), new CakeResponse());
93+
$Pages->display('..', 'Posts', 'index');
94+
}
7895
}

0 commit comments

Comments
 (0)