fix: alarm ajax reads id and command only when they are sent refs #8011 (#8012)

This commit is contained in:
Jash Ambaliya authored and GitHub committed 2026-11-23 23:05:00 -05:00
1 parent 124827480a
commit f9a39ce99f
2 files changed
+76 -3

No files matched your search

@@ -0,0 +1,73 @@
<?php
// web/ajax/alarm.php must tolerate a request with no parameters: it has to
// reach ajaxError('Insufficient permissions'), not blow up on an undefined
// array key from $_REQUEST['id'] / $_REQUEST['command'].
//
// Run as: php tests/php/test_ajax_alarm_missing_params.php [repo_root]
namespace ZM {
class Monitor {
public function __construct($id) {}
public function Id() { return 0; }
public function canEdit() { return false; }
}
}
namespace {
class StopRequest extends Exception {}
$root = $argv[1] ?? __DIR__.'/../..';
$failures = 0;
function check($name, $got, $want) {
global $failures;
if ($got === $want) {
echo "ok - $name\n";
} else {
$failures++;
echo "FAIL - $name\n got: ".var_export($got, true)."\n want: ".var_export($want, true)."\n";
}
}
function validCardinal($v) { return (int)$v; }
function validJsStr($v) { return (string)$v; }
function getZmuCommand($args) { return 'true'; }
function ajaxResponse($data = array()) { throw new StopRequest('ok'); }
function ajaxError($m) { throw new StopRequest($m); }
// alarm.php does require_once('includes/Monitor.php'); give it an empty
// stub file in a temp dir first on the include path so the ZM\Monitor class
// defined above is the one used.
$tmp = sys_get_temp_dir().'/zm_alarm_test_'.getmypid();
@mkdir($tmp.'/includes', 0777, true);
file_put_contents($tmp.'/includes/Monitor.php', "<?php\n");
set_include_path($tmp.PATH_SEPARATOR.get_include_path());
// Undefined array keys surface as warnings/notices; turn them into errors so
// the unfixed file fails this test loudly.
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}, E_WARNING | E_NOTICE);
$_REQUEST = array();
$msg = null;
$error = null;
try {
include $root.'/web/ajax/alarm.php';
} catch (StopRequest $e) {
$msg = $e->getMessage();
} catch (ErrorException $e) {
$error = $e->getMessage();
}
check("no ErrorException about an undefined array key", is_null($error), true);
check("request without parameters reaches ajaxError", $msg, 'Insufficient permissions');
@unlink($tmp.'/includes/Monitor.php');
@rmdir($tmp.'/includes');
@rmdir($tmp);
echo "\n".($failures ? "FAILURES: $failures\n" : "All passed\n");
exit($failures ? 1 : 0);
}
+3 -3
View File
@@ -3,13 +3,13 @@ define('MSG_TIMEOUT', 2.0);
define('MSG_DATA_SIZE', 4+256);
require_once('includes/Monitor.php');
$monitor = new ZM\Monitor(validCardinal($_REQUEST['id']));
$monitor = new ZM\Monitor(validCardinal($_REQUEST['id'] ?? 0));
// Needs edit on this monitor, not only the global Monitors permission.
if ( $monitor->Id() and $monitor->canEdit() ) {
$zmuCommand = getZmuCommand(' -m '.$monitor->Id());
switch ( validJsStr($_REQUEST['command']) ) {
switch ( validJsStr($_REQUEST['command'] ?? '') ) {
case 'disableAlarms' :
$zmuCommand .= ' -n';
break;
@@ -23,7 +23,7 @@ if ( $monitor->Id() and $monitor->canEdit() ) {
$zmuCommand .= ' -c';
break;
default :
ajaxError("Unexpected command '".validJsStr($_REQUEST['command'])."'");
ajaxError("Unexpected command '".validJsStr($_REQUEST['command'] ?? '')."'");
}
$output = array();
$rc = 0;