mirror of
https://github.com/ZoneMinder/zoneminder.git
synced 2026-11-30 04:04:03 -05:00
This commit is contained in:
1 parent
866400991a
commit
d13098901f
4 files changed
+92
No files matched your search
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
// RolesController edit and UserPreferenceController add/edit saved only for POST
|
||||
// (or PUT) but answered any other method with 200 and an empty or failure payload,
|
||||
// where the other controllers refuse it with allowMethod (#8098, #7886). They now
|
||||
// call allowMethod first, so a GET is refused before anything runs. (Roles add
|
||||
// already answers a GET with 400.)
|
||||
//
|
||||
// The controllers can't be loaded without CakePHP, so each action is taken from
|
||||
// the source and run against a stub request.
|
||||
// Run as: php tests/php/test_api_role_userpref_methods.php [root]
|
||||
|
||||
$failures = 0;
|
||||
function check($name, $got, $want) {
|
||||
global $failures;
|
||||
if ($got === $want) echo "ok - $name\n";
|
||||
else { $failures++; echo "FAIL - $name (got ".var_export($got, true).", want ".var_export($want, true).")\n"; }
|
||||
}
|
||||
|
||||
$root = isset($argv[1]) ? $argv[1] : __DIR__.'/../..';
|
||||
class MethodNotAllowedException extends Exception {}
|
||||
// Thrown by the actions' own checks once past the method; with no method check
|
||||
// a GET reaches them.
|
||||
class BadRequestException extends Exception {}
|
||||
class UnauthorizedException extends Exception {}
|
||||
class NotFoundException extends Exception {}
|
||||
function __($s) { return $s; }
|
||||
class Reached extends Exception {}
|
||||
|
||||
// Any use of the controller beyond the request means the action got past the
|
||||
// method check.
|
||||
class FakeRequest {
|
||||
private $method;
|
||||
public $data = array();
|
||||
function __construct($m) { $this->method = $m; }
|
||||
function is($type) { return in_array($this->method, (array)$type); }
|
||||
function allowMethod() {
|
||||
if (!in_array($this->method, func_get_args())) throw new MethodNotAllowedException();
|
||||
throw new Reached();
|
||||
}
|
||||
function __get($n) { throw new Reached(); }
|
||||
function __call($n, $a) { throw new Reached(); }
|
||||
}
|
||||
|
||||
function extract_method($source, $name) {
|
||||
$start = strpos($source, 'public function '.$name.'(');
|
||||
if ($start === false) return null;
|
||||
$open = strpos($source, '{', $start);
|
||||
$depth = 0;
|
||||
for ($i = $open; $i < strlen($source); $i++) {
|
||||
if ($source[$i] == '{') $depth++;
|
||||
else if ($source[$i] == '}' && --$depth == 0) break;
|
||||
}
|
||||
return substr($source, $start, $i - $start + 1);
|
||||
}
|
||||
|
||||
foreach (array('Roles', 'UserPreference') as $n => $controller) {
|
||||
$src = file_get_contents("$root/web/api/app/Controller/{$controller}Controller.php");
|
||||
$methods = extract_method($src, 'add')."\n".extract_method($src, 'edit');
|
||||
$class = "Fake{$controller}";
|
||||
eval("class $class { public \$request;
|
||||
function __get(\$n) { throw new Reached(); }
|
||||
function __call(\$n, \$a) { throw new Reached(); }
|
||||
$methods }");
|
||||
$actions = array('edit' => array('get', 'delete'));
|
||||
if ($controller == 'UserPreference') $actions = array('add' => array('get', 'put', 'delete')) + $actions;
|
||||
foreach ($actions as $action => $refused) {
|
||||
foreach ($refused as $m) {
|
||||
$c = new $class(); $c->request = new FakeRequest($m);
|
||||
try { $c->$action(1); $got = 'answered'; }
|
||||
catch (MethodNotAllowedException $e) { $got = 'refused'; }
|
||||
catch (Reached $e) { $got = "ran"; }
|
||||
catch (Exception $e) { $got = "answered"; }
|
||||
check("$controller $action refuses ".strtoupper($m), $got, 'refused');
|
||||
}
|
||||
$ok = ($action == 'add') ? 'post' : 'put';
|
||||
$c = new $class(); $c->request = new FakeRequest($ok);
|
||||
try { $c->$action(1); $got = 'answered'; }
|
||||
catch (MethodNotAllowedException $e) { $got = 'refused'; }
|
||||
catch (Reached $e) { $got = 'ran'; }
|
||||
check("$controller $action accepts ".strtoupper($ok), $got, 'ran');
|
||||
}
|
||||
}
|
||||
|
||||
echo $failures ? "\n$failures check(s) FAILED\n" : "\nall checks passed\n";
|
||||
exit($failures ? 1 : 0);
|
||||
@@ -40,6 +40,10 @@ class FakeRequest {
|
||||
public $data = array(); public $method;
|
||||
function __construct($m) { $this->method = $m; }
|
||||
function is($t) { return strtolower($t) == strtolower($this->method); }
|
||||
function allowMethod() {
|
||||
foreach (func_get_args() as $m) if ($this->is($m)) return true;
|
||||
throw new Exception('Method Not Allowed');
|
||||
}
|
||||
}
|
||||
eval('class Harness {
|
||||
public $Role, $request, $response, $set = array();
|
||||
|
||||
@@ -121,6 +121,7 @@ class RolesController extends AppController {
|
||||
* @return void
|
||||
*/
|
||||
public function edit($id = null) {
|
||||
$this->request->allowMethod('post', 'put');
|
||||
$this->Role->id = $id;
|
||||
|
||||
global $user;
|
||||
|
||||
@@ -102,6 +102,7 @@ class UserPreferenceController extends AppController {
|
||||
* @return void
|
||||
*/
|
||||
public function add() {
|
||||
$this->request->allowMethod('post');
|
||||
$data = $this->request->data;
|
||||
if ($this->RequestHandler->requestedWith('json')) {
|
||||
$data = $this->request->input('json_decode', true) ;
|
||||
@@ -146,6 +147,7 @@ class UserPreferenceController extends AppController {
|
||||
* @return void
|
||||
*/
|
||||
public function edit($id = null) {
|
||||
$this->request->allowMethod('post', 'put');
|
||||
if (!$this->UserPreference->exists($id)) {
|
||||
throw new NotFoundException(__('Invalid user_preference'));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user