diff --git a/tests/php/test_api_paginate_sort_list.php b/tests/php/test_api_paginate_sort_list.php new file mode 100644 index 0000000000..b4ec07744b --- /dev/null +++ b/tests/php/test_api_paginate_sort_list.php @@ -0,0 +1,58 @@ +requireScalarSort(); } catch (BadRequestException $e) { return $e->getMessage(); } return "ok"; } }'); +} + +function verdict($named, $query) { + $h = new Harness(); + $h->request = new stdClass(); + $h->request->params = array('named' => $named); + $h->request->query = $query; + return $h->call(); +} + +if ($found) { + check('no sort is accepted', verdict(array(), array()), 'ok'); + check('a sort and direction are accepted', verdict(array(), array('sort' => 'Name', 'direction' => 'desc')), 'ok'); + check('a named sort and direction are accepted', verdict(array('sort' => 'Name', 'direction' => 'asc'), array()), 'ok'); + check('a list in the query sort is refused', verdict(array(), array('sort' => array('x'))), 'sort must be a single value'); + check('a nested array in the query sort is refused', verdict(array(), array('sort' => array('a' => array('b')))), 'sort must be a single value'); + check('an empty list in the query sort is refused', verdict(array(), array('sort' => array())), 'sort must be a single value'); + check('a list in the query direction is refused', verdict(array(), array('sort' => 'Id', 'direction' => array('x'))), 'direction must be a single value'); + check('a list in the named sort is refused', verdict(array('sort' => array('x')), array()), 'sort must be a single value'); + check('a list in the named direction is refused', verdict(array('direction' => array('x')), array()), 'direction must be a single value'); + +} + +foreach (array('Roles', 'Users', 'Logs', 'Events') as $name) { + $src = file_get_contents($controllers.$name.'Controller.php'); + $call = strpos($src, 'requireScalarSort()'); + $paginate = strpos($src, '->paginate('); + check("$name calls requireScalarSort() before paginate()", $call !== false and $paginate !== false and $call < $paginate, true); +} + +echo "\n$passes passed, $failures failed\n"; +exit($failures ? 1 : 0); +?> diff --git a/tests/php/test_api_users_edit_add.php b/tests/php/test_api_users_edit_add.php index d58e9234d1..d2a7b76818 100644 --- a/tests/php/test_api_users_edit_add.php +++ b/tests/php/test_api_users_edit_add.php @@ -80,6 +80,8 @@ class FakePaginator { function paginate($m) { return array(); } } eval('class UsersHarness { public $request, $response, $User, $UserPreference, $Paginator, $vars; function set($v) { $this->vars = $v; } function loadModel($m) { $this->$m = new FakeModel(); } + // AppController::requireScalarSort() is covered by test_api_paginate_sort_list.php. + function requireScalarSort() {} // Copy of AppController::pinRequestId(). function pinRequestId($model, $id) { $alias = $model->alias; diff --git a/web/api/app/Controller/AppController.php b/web/api/app/Controller/AppController.php index 45cd794af9..57a793626a 100644 --- a/web/api/app/Controller/AppController.php +++ b/web/api/app/Controller/AppController.php @@ -320,4 +320,16 @@ class AppController extends Controller { })); return $row; } + + # Paginator uses sort as an array key and lowercases direction, so a list for either, from the + # query string or a named parameter, is a TypeError. + protected function requireScalarSort() { + foreach (array($this->request->params['named'], $this->request->query) as $params) { + foreach (array('sort', 'direction') as $key) { + if (isset($params[$key]) and !is_string($params[$key])) { + throw new BadRequestException($key.' must be a single value'); + } + } + } + } } diff --git a/web/api/app/Controller/EventsController.php b/web/api/app/Controller/EventsController.php index ea53951116..828637506b 100644 --- a/web/api/app/Controller/EventsController.php +++ b/web/api/app/Controller/EventsController.php @@ -282,6 +282,7 @@ class EventsController extends AppController { $this->Paginator->settings = $settings; if ($this->request->query('limit') or $this->request->query('page')) { + $this->requireScalarSort(); $events = $this->Paginator->paginate('Event'); } else { $events = $this->Event->find('all', $settings); diff --git a/web/api/app/Controller/LogsController.php b/web/api/app/Controller/LogsController.php index d4c6475844..2de2cfe67f 100644 --- a/web/api/app/Controller/LogsController.php +++ b/web/api/app/Controller/LogsController.php @@ -44,6 +44,7 @@ class LogsController extends AppController { $this->Paginator->settings['conditions'] = $this->FilterComponent->buildFilter($this->request->params['named'], array_keys($this->Log->schema())); } + $this->requireScalarSort(); $logs = $this->Paginator->paginate('Log'); $this->set(compact('logs')); } diff --git a/web/api/app/Controller/RolesController.php b/web/api/app/Controller/RolesController.php index 327e722b30..d5328ad1c1 100644 --- a/web/api/app/Controller/RolesController.php +++ b/web/api/app/Controller/RolesController.php @@ -39,6 +39,7 @@ class RolesController extends AppController { throw new UnauthorizedException(__('Insufficient Privileges')); return; } + $this->requireScalarSort(); $roles = $this->Paginator->paginate('Role'); $this->set(array( diff --git a/web/api/app/Controller/UsersController.php b/web/api/app/Controller/UsersController.php index 097881a4f2..297269f672 100644 --- a/web/api/app/Controller/UsersController.php +++ b/web/api/app/Controller/UsersController.php @@ -44,6 +44,7 @@ class UsersController extends AppController { throw new UnauthorizedException(__('Insufficient Privileges')); return; } + $this->requireScalarSort(); $users = $this->Paginator->paginate('User'); $this->set(compact('users'));