fix: API index answers 400 for a list in sort or direction instead of a 500 (#8427)

This commit is contained in:
Harsh Singh authored and GitHub committed 2026-11-27 08:40:13 -05:00
1 parent b5e6456b15
commit fa28222141
7 files changed
+76

No files matched your search

+58
View File
@@ -0,0 +1,58 @@
<?php
// Cake's Paginator uses the request's sort as an array key and lowercases its direction, so /roles, /users,
// /logs and /events with a page or limit, called with sort[]=x or direction[]=x (or the named parameter
// sort[]:x), died with a TypeError, a 500. AppController::requireScalarSort() makes it a 400, and each
// paginating index() calls it before paginate().
// php tests/php/test_api_paginate_sort_list.php [repo root]
$root = isset($argv[1]) ? rtrim($argv[1], '/') : __DIR__.'/../..';
class BadRequestException extends Exception {}
$controllers = $root.'/web/api/app/Controller/';
$app = file_get_contents($controllers.'AppController.php');
$passes = 0;
$failures = 0;
function check($name, $got, $expected) {
global $passes, $failures;
if ($got === $expected) { $passes++; echo "ok - $name\n"; }
else { $failures++; echo "not ok - $name (got ".var_export($got, true)." expected ".var_export($expected, true).")\n"; }
}
$found = preg_match('/^ protected function requireScalarSort\(\) \{.*?^ \}\n/ms', $app, $m);
check('AppController::requireScalarSort() exists', $found, 1);
if ($found) {
eval('class Harness { public $request; '.$m[0].' public function call() { try { $this->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);
?>
+2
View File
@@ -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;
+12
View File
@@ -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');
}
}
}
}
}
@@ -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);
@@ -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'));
}
@@ -39,6 +39,7 @@ class RolesController extends AppController {
throw new UnauthorizedException(__('Insufficient Privileges'));
return;
}
$this->requireScalarSort();
$roles = $this->Paginator->paginate('Role');
$this->set(array(
@@ -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'));