Files
zoneminder/tests/php/test_options_api_requires_system_edit.php
Isaac ConnorandClaude Opus 5.5 5784978733 test: check the API tab without the per-user API toggle refs #4908
The API can no longer be disabled per user, so the Options API tab has only
the token revoke checkbox and updateSelected only revokes the tokens of the
users marked. Post tokenUids instead of apiUids, expect one disabled
checkbox for System=View, and check that nothing writes APIEnabled. The
System=View checks that no write runs are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 11:58:25 -05:00

111 lines
4.6 KiB
PHP

<?php
// Security regression test: the API tab of Options renders for System=View, but
// its two POST handlers (updateSelected: revoke the tokens of the users marked;
// revokeAllTokens: bump every Users.TokenMinExpiry) must only run for System=Edit.
// The API can no longer be disabled per user (#4908), so nothing writes APIEnabled.
//
// Includes the real web/skins/classic/views/_options_api.php with the web
// helpers it calls stubbed, and records the SQL it sends to dbQuery().
//
// Run as: php tests/php/test_options_api_requires_system_edit.php
namespace ZM {
$GLOBALS['warnings'] = array();
function Warning($s) { $GLOBALS['warnings'][] = $s; }
class User {
public static function find($a = array(), $b = array()) { return array(new User()); }
public function Id() { return 1; }
public function Username() { return 'admin'; }
}
}
namespace {
define('ZM_OPT_USE_API', '1');
$GLOBALS['perms'] = array();
$GLOBALS['queries'] = array();
function canView($area) { return in_array($GLOBALS['perms'][$area] ?? 'None', array('View', 'Edit'), true); }
function canEdit($area) { return ($GLOBALS['perms'][$area] ?? 'None') === 'Edit'; }
function dbQuery($sql, $params = array()) { $GLOBALS['queries'][] = $sql; return true; }
function translate($s) { return $s; }
function validHtmlStr($s) { return htmlspecialchars($s); }
$failures = 0;
$passes = 0;
function check($name, $got, $want) {
global $failures, $passes;
if ($got === $want) {
$passes++;
echo "ok - $name\n";
} else {
$failures++;
echo "FAIL - $name (got ".var_export($got, true).", want ".var_export($want, true).")\n";
}
}
// The view declares its handler functions, so it can only be included once per
// process: each case runs in a child php process that prints what it saw.
if (isset($argv[1])) {
$case = json_decode($argv[1], true);
$GLOBALS['perms'] = array('System' => $case['perm']);
$_POST = $case['post'];
$_REQUEST = $case['post'];
$view = 'options';
$tab = 'API';
ob_start();
include __DIR__.'/../../web/skins/classic/views/_options_api.php';
$html = ob_get_clean();
echo json_encode(array('queries' => $GLOBALS['queries'], 'warnings' => $GLOBALS['warnings'], 'html' => $html));
exit(0);
}
function render_api_tab($systemPerm, array $post) {
$arg = json_encode(array('perm' => $systemPerm, 'post' => (object)$post));
$out = shell_exec(escapeshellarg(PHP_BINARY).' '.escapeshellarg(__FILE__).' '.escapeshellarg($arg));
$r = json_decode($out, true);
if (!is_array($r)) {
echo "child failed: $out\n";
exit(1);
}
$GLOBALS['queries'] = $r['queries'];
$GLOBALS['warnings'] = $r['warnings'];
return $r['html'];
}
$writes = array(
'updateSelected' => array('updateSelected' => '', 'tokenUids' => array('1')),
'revokeAllTokens' => array('revokeAllTokens' => ''),
);
foreach ($writes as $name => $post) {
$html = render_api_tab('View', $post);
check("System=View $name sends no SQL", $GLOBALS['queries'], array());
check("System=View $name logs a warning", count($GLOBALS['warnings']), 1);
check("System=View $name page still renders", strpos($html, 'contentTable') !== false, true);
render_api_tab('Edit', $post);
check("System=Edit $name revokes tokens", count($GLOBALS['queries']) > 0 &&
strpos($GLOBALS['queries'][0], 'UPDATE `Users` SET `TokenMinExpiry`') === 0, true);
}
$html = render_api_tab('View', array());
check('System=View sees the write buttons disabled',
preg_match_all('/id="(updateSelected|revokeAllTokens)" disabled="disabled"/', $html), 2);
check('System=View sees the per-user revoke checkbox disabled',
preg_match_all('/name="tokenUids\[\]"[^>]*disabled="disabled"/', $html), 1);
check('The API tab has no per-user API enable checkbox', strpos($html, 'apiUids') === false, true);
$html = render_api_tab('Edit', array());
check('System=Edit sees the write buttons enabled',
substr_count($html, 'disabled="disabled"'), 0);
render_api_tab('Edit', array('revokeAllTokens' => '', 'updateSelected' => '', 'tokenUids' => array('1')));
check('System=Edit with both buttons runs both handlers',
count(array_filter($GLOBALS['queries'], function($q) { return strpos($q, 'WHERE `Id`') === false; })) === 1 &&
count(array_filter($GLOBALS['queries'], function($q) { return strpos($q, 'WHERE `Id`') !== false; })) === 1, true);
check('Nothing writes APIEnabled',
count(array_filter($GLOBALS['queries'], function($q) { return strpos($q, 'APIEnabled') !== false; })), 0);
echo "\n$passes passed, $failures failed\n";
exit($failures ? 1 : 0);
}