From 5ceb737be9ddb9906f3a49a7b057d08c5b7fe9cd Mon Sep 17 00:00:00 2001 From: Harsh Singh <143034341+singhharsh1708@users.noreply.github.com> Date: Fri, 27 Nov 2026 05:09:42 +0530 Subject: [PATCH] fix: API monitor add and edit refuse a ManufacturerId or ModelId that matches no row (#8401) --- tests/php/test_api_monitor_reference_ids.php | 54 ++++++++++++++++++++ web/api/app/Model/Monitor.php | 25 +++++++++ 2 files changed, 79 insertions(+) create mode 100644 tests/php/test_api_monitor_reference_ids.php diff --git a/tests/php/test_api_monitor_reference_ids.php b/tests/php/test_api_monitor_reference_ids.php new file mode 100644 index 0000000000..ba9ab3bb5d --- /dev/null +++ b/tests/php/test_api_monitor_reference_ids.php @@ -0,0 +1,54 @@ +alias = $alias; $this->ids = $ids; } + public function find($type, $options) { + $id = $options['conditions'][$this->alias.'.Id']; + return in_array((string)$id, $this->ids, true) ? array($this->alias => array('Id' => $id)) : array(); + } +} +class ClassRegistry { + public static function init($name) { + return $name == 'Manufacturer' ? new FakeTable('Manufacturer', array('1', '4')) : new FakeTable('CameraModel', array('2')); + } +} + +$repoRoot = $argv[1] ?? realpath(__DIR__.'/../..'); +require_once "$repoRoot/web/api/app/Model/Monitor.php"; + +$passes = 0; +$failures = 0; +function check($ok, $what) { + global $passes, $failures; + if ($ok) { $passes++; echo "ok - $what\n"; } else { $failures++; echo "not ok - $what\n"; } +} + +$m = new Monitor(); +foreach (array('ManufacturerId' => array('1', '4', '77'), 'ModelId' => array('2', '9')) as $field => $values) { + $rule = isset($m->validate[$field]['rule']) ? $m->validate[$field]['rule'] : null; + check($rule !== null, "Monitor validates $field"); + if ($rule === null) continue; + $ok = function($value) use ($m, $rule, $field) { return $m->{$rule[0]}(array($field => $value), $rule[1]); }; + check($ok(null) === true, "$field null is accepted"); + check($ok('') === true, "$field empty is accepted"); + check($ok(array('a' => array('b' => 1))) === true, "$field nested array is left to AppModel and not looked up"); + check($ok($values[0]) === true, "$field $values[0] that exists is accepted"); + check($ok(end($values)) === false, "$field ".end($values)." that does not exist is refused"); + check($ok('0') === false, "$field 0 is refused, no row has that Id"); +} + +echo "\n$passes passed, $failures failed\n"; +exit($failures ? 1 : 0); diff --git a/web/api/app/Model/Monitor.php b/web/api/app/Model/Monitor.php index 420a9cc6f6..82fe15f733 100644 --- a/web/api/app/Model/Monitor.php +++ b/web/api/app/Model/Monitor.php @@ -79,6 +79,14 @@ class Monitor extends AppModel { 'rule' => array('rowExists', 'Storage'), 'message' => 'StorageId does not match a storage area', ), + 'ManufacturerId' => array( + 'rule' => array('refExists', 'Manufacturer'), + 'message' => 'ManufacturerId does not match a manufacturer', + ), + 'ModelId' => array( + 'rule' => array('refExists', 'CameraModel'), + 'message' => 'ModelId does not match a camera model', + ), // These codec values mimic acceptable ones for FFMPEG // Edit this if the list in ZM changes 'OutputCodec' => array ( @@ -113,6 +121,23 @@ class Monitor extends AppModel { )); } + // Foreign keys: MySQL refuses an id that matches no row, so refuse it here instead of as a 500. + // Empty and null mean none. + public function refExists($check, $model) { + $value = reset($check); + if ($value === null or $value === '') { + return true; + } + // AppModel::beforeValidate() already refuses an array. + if (!is_scalar($value)) { + return true; + } + return (bool)ClassRegistry::init($model)->find('first', array( + 'conditions' => array($model.'.Id' => $value), + 'recursive' => -1, + )); + } + public function validDevicePath($check) { $value = reset($check); if ($value === null || $value === '') {