Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ installation guidance and source-checked behavior.

--- develop ---
* dev: Run the CI poller with the shared runner group so RRD files and structured directories pass permission verification
* security: Escape the data template name in the Associated Thresholds Template filter, and skip data sources whose RRD file name contains whitespace or quote characters instead of passing the path to rrdtool
* issue: Shorten the VRULE graph drilldown button title so it reads cleanly as a label in Cacti's graph action menu, using two complete 'Toggle Threshold VRULES On'/'Off' strings (keeping the Threshold reference) so the label stays grammatical across translations
* issue: Log each template import error using its own message (#757)
* bug: Element-qualify the `.thold-pill` CSS so it outranks the single-class theme rules (e.g. `.deviceUpFull` `width:14%`) that were clipping the pill to a fixed width; status pills now size to their label on the Device Status page and everywhere else
Expand Down
27 changes: 25 additions & 2 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6445,6 +6445,22 @@ function thold_api_thold_template_remove($id) {
[$id]);
}

/**
* thold_rrd_path_is_safe - checks that a data source RRD path can be passed to rrdtool as a single argument.
*
* The RRD file name may contain whitespace or quote characters, which split or terminate the argument
* when the path is placed in an rrdtool command line, so such files are skipped rather than passed on.
*
* @param string $path The RRD file path, as stored for the data source.
*
* @return bool True when the file part of the path has no whitespace or quote characters.
*/
function thold_rrd_path_is_safe($path) {
$file = basename(str_replace('\\', '/', (string) $path));

return $file !== '' && preg_match('/[\s\'"`]/', $file) !== 1;
}

/**
* thold_rrd_last - fetches the last-updated timestamp recorded in a data source's RRD file.
*
Expand All @@ -6453,7 +6469,14 @@ function thold_api_thold_template_remove($id) {
* @return string The RRD's last-update Unix timestamp, as returned by rrdtool.
*/
function thold_rrd_last($local_data_id) {
$last_time_entry = @rrdtool_execute('last ' . trim(get_data_source_path($local_data_id, true)), false, RRDTOOL_OUTPUT_STDOUT);
$rrdfile = trim(get_data_source_path($local_data_id, true));

// An unusable path yields no timestamp, which callers already treat as 'not polled yet'.
if ($rrdfile !== '' && !thold_rrd_path_is_safe($rrdfile)) {
return '';
}

$last_time_entry = @rrdtool_execute('last ' . $rrdfile, false, RRDTOOL_OUTPUT_STDOUT);

return trim($last_time_entry);
}
Expand Down Expand Up @@ -11350,7 +11373,7 @@ function thold_get_rrd_statistics($local_data_id, $current_time, $prev_time, $av
WHERE local_data_id = ?',
[$local_data_id]);

if ($rrdfile == '') {
if ($rrdfile == '' || !thold_rrd_path_is_safe($rrdfile)) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion notify_lists.php
Original file line number Diff line number Diff line change
Expand Up @@ -1672,7 +1672,7 @@ function tholds($header_label) {
<option value='-1'><?php print __('Any', 'thold'); ?></option>
<?php
foreach ($data_templates as $row) {
print "<option value='" . $row['id'] . "'" . (isset_request_var('template') && $row['id'] == get_request_var('template') ? ' selected' : '') . '>' . $row['name'] . '</option>';
print "<option value='" . $row['id'] . "'" . (isset_request_var('template') && $row['id'] == get_request_var('template') ? ' selected' : '') . '>' . html_escape($row['name']) . '</option>';
}
?>
</select>
Expand Down
89 changes: 89 additions & 0 deletions tests/Unit/OutputEscapingAndRrdPathTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/

/**
* A data source RRD path whose file name holds whitespace or quote
* characters must not be placed in an rrdtool command line, where it would
* split into several arguments.
*/
final class OutputEscapingAndRrdPathTest extends TestCase {
/**
* @return void
*/
public static function setUpBeforeClass(): void {
self::loadPluginSource('includes/functions.php');
}

/**
* @return array<string, array{0: string, 1: bool}>
*/
public static function pathProvider(): array {
return [
'plain' => ['/var/lib/cacti/rra/host_1.rrd', true],
'space in directory only' => ['/var/lib/cacti/my rra/host_1.rrd', true],
'space in file' => ['/var/lib/cacti/rra/host 1.rrd', false],
'tab in file' => ["/var/lib/cacti/rra/host\t1.rrd", false],
'newline in file' => ["/var/lib/cacti/rra/host\n1.rrd", false],
'apostrophe in file' => ["/var/lib/cacti/rra/a'b.rrd", false],
'double quote in file' => ['/var/lib/cacti/rra/a"b.rrd', false],
'backtick in file' => ['/var/lib/cacti/rra/a`b.rrd', false],
'windows path with space' => ['C:\\rra\\host 1.rrd', false],
'empty' => ['', false],
];
}

/**
* @dataProvider pathProvider
*
* @param string $path RRD path.
* @param bool $expected Whether the path may be passed to rrdtool.
*
* @return void
*/
public function testPathSafety(string $path, bool $expected): void {
$this->assertSame($expected, thold_rrd_path_is_safe($path));
}

/**
* @return void
*/
public function testStatisticsSkipsAnUnsafePathWithoutCallingRrdtool(): void {
CactiStubs::willReturn('db_fetch_cell_prepared', "/var/lib/cacti/rra/a 'b.rrd");

$this->assertFalse(thold_get_rrd_statistics(4, 1700000300, 1700000000, 'AVERAGE'));
$this->assertSame([], CactiStubs::callsTo('rrdtool_execute'));
}

public function testLastUpdateSkipsAFileNameWithSpaces(): void {
CactiStubs::willReturn('get_data_source_path', '/var/lib/cacti/rra/router 4.rrd');

$this->assertSame('', thold_rrd_last(4));
$this->assertSame([], CactiStubs::callsTo('rrdtool_execute'));
}

/**
* The data template name is user-editable, so the Template filter must
* print it escaped as option text.
*
* @return void
*/
public function testNotifyListTemplateOptionEscapesTheName(): void {
$source = file_get_contents(__DIR__ . '/../../notify_lists.php');

$this->assertStringContainsString("'>' . html_escape(\$row['name']) . '</option>'", $source);
$this->assertStringNotContainsString("'>' . \$row['name'] . '</option>'", $source);
}
}
2 changes: 1 addition & 1 deletion tests/bootstrap-unit.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ function rrdtool_function_fetch($local_data_id, $start_time, $end_time, $resolut

if (!function_exists('get_data_source_path')) {
function get_data_source_path($local_data_id, $expand_paths = true) {
return '/var/lib/cacti/rra/test_' . (int) $local_data_id . '.rrd';
return CactiStubs::nextReturn('get_data_source_path', '/var/lib/cacti/rra/test_' . (int) $local_data_id . '.rrd');
}
}

Expand Down
Loading