Merge pull request #7457 from AJ0070/sec-27-monitor-edit-attr

fix: escape stored monitor fields printed in monitor edit page inputs
This commit is contained in:
Isaac Connor authored and GitHub committed 2026-11-22 21:05:27 -05:00
commit 2d4af3c2a9
2 files changed
+77 -3

No files matched your search

@@ -0,0 +1,74 @@
<?php
// The monitor edit page prints stored free-text monitor fields into the
// value="..." attribute of text inputs. If the printed value is not escaped, a
// stored value containing a double quote closes the attribute early and the
// rest of the value becomes markup.
//
// monitor.php cannot be rendered here: it needs the web bootstrap, a database
// and an authorised user. So this is a source scan. For each field it finds the
// line carrying name="newMonitor[FIELD]" and the value="<?php echo ... " part,
// part, and checks that the echoed expression is passed through validHtmlStr()
// or htmlspecialchars(). The "at least one line found" check makes a rename of
// the input visible instead of silently scanning nothing.
//
// Run as: php tests/php/test_monitor_edit_value_escape.php [repo-root]
$repoRoot = $argv[1] ?? realpath(__DIR__.'/../..');
$viewPath = $repoRoot.'/web/skins/classic/views/monitor.php';
$tests = 0;
$failures = 0;
function check($label, $cond) {
global $tests, $failures;
$tests++;
if ($cond) {
echo " ok - $label\n";
} else {
$failures++;
echo " NOT OK - $label\n";
}
}
$fields = array(
'LinkedMonitors',
'MQTT_Subscriptions',
'Janus_RTSP_Session_Timeout',
);
$source = is_readable($viewPath) ? file_get_contents($viewPath) : false;
foreach ($fields as $field) {
if ($source === false) {
check("$field: monitor.php is readable", false);
continue;
}
$name = 'name="newMonitor['.$field.']"';
$lines = preg_split('/\r\n|\n|\r/', $source);
$matches = array();
foreach ($lines as $offset => $line) {
if (strpos($line, $name) === false) {
continue;
}
if (!preg_match_all('/value="<\?php echo (.*?)\s*\?>/', $line, $values)) {
continue;
}
foreach ($values[1] as $expr) {
$matches[] = array('line' => $offset + 1, 'expr' => trim($expr));
}
}
check("$field: a value=\"...\" input is found to scan", count($matches) > 0);
foreach ($matches as $match) {
$expr = $match['expr'];
$escaped = strpos($expr, 'validHtmlStr(') === 0
|| strpos($expr, 'htmlspecialchars(') === 0;
check("$field: the value at monitor.php:{$match['line']} is escaped", $escaped);
}
}
echo "\n$tests tests, $failures failures\n";
exit($failures ? 1 : 0);
+3 -3
View File
@@ -1182,7 +1182,7 @@ echo htmlSelect('newMonitor[Decoder]', $decoders, $monitor->Decoder());
<li class="settingsGroup"><?php echo translate('OtherAnalysisSettings') ?></li>
<li class="LinkedMonitors">
<label><?php echo translate('LinkedMonitors'); echo makeHelpLink('OPTIONS_LINKED_MONITORS') ?></label>
<input type="text" name="newMonitor[LinkedMonitors]" value="<?php echo $monitor->LinkedMonitors() ?>" data-on-input="updateLinkedMonitorsUI"/><br/>
<input type="text" name="newMonitor[LinkedMonitors]" value="<?php echo validHtmlStr($monitor->LinkedMonitors()) ?>" data-on-input="updateLinkedMonitorsUI"/><br/>
<div id="LinkedMonitorsUI"></div>
</li>
<li id="function_use_Amcrest_API" class="use_Amcreat_API">
@@ -1446,7 +1446,7 @@ echo htmlSelect('newMonitor[OutputContainer]', $videowriter_containers, $monitor
</li>
<li id="FunctionJanusRTSPSessionTimeout">
<label><?php echo translate('Janus RTSP Session Timeout Override') ?></label>
<input type="text" name="newMonitor[Janus_RTSP_Session_Timeout]" value="<?php echo $monitor->Janus_RTSP_Session_Timeout()?>"/>
<input type="text" name="newMonitor[Janus_RTSP_Session_Timeout]" value="<?php echo validHtmlStr($monitor->Janus_RTSP_Session_Timeout()) ?>"/>
<?php
if ( isset($OLANG['FUNCTION_JANUS_RTSP_SESSION_TIMEOUT']) ) {
echo '<div class="form-text">'.$OLANG['FUNCTION_JANUS_RTSP_SESSION_TIMEOUT']['Help'].'</div>';
@@ -1949,7 +1949,7 @@ echo htmlSelect('newMonitor[ReturnLocation]', $return_options, $monitor->ReturnL
</li>
<li>
<label class="MQTT_Subscriptions"><?php echo translate('MQTT Subscriptions') ?></label>
<input type="text" name="newMonitor[MQTT_Subscriptions]" value="<?php echo $monitor->MQTT_Subscriptions() ?>" />
<input type="text" name="newMonitor[MQTT_Subscriptions]" value="<?php echo validHtmlStr($monitor->MQTT_Subscriptions()) ?>" />
</li>
<?php
break;