From 6a006d26ce95400b338591cf6320f99a1cd8bb1d Mon Sep 17 00:00:00 2001 From: Thomas Vincent Date: Wed, 18 Nov 2026 15:47:14 -0800 Subject: [PATCH 1/2] fix(security): escape template filter names and skip unsafe RRD paths Escape the data template name in the Associated Thresholds filter, and skip data sources whose RRD file name contains whitespace or quote characters instead of passing the path to rrdtool. Signed-off-by: Thomas Vincent --- CHANGELOG.md | 1 + includes/functions.php | 27 ++++++- notify_lists.php | 2 +- tests/Unit/OutputEscapingAndRrdPathTest.php | 82 +++++++++++++++++++++ 4 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 tests/Unit/OutputEscapingAndRrdPathTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index b23c0de0..9831acd3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/includes/functions.php b/includes/functions.php index c78c0cdc..1ab1548d 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -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. * @@ -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); } @@ -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; } diff --git a/notify_lists.php b/notify_lists.php index b5e5998f..b59692b6 100644 --- a/notify_lists.php +++ b/notify_lists.php @@ -1672,7 +1672,7 @@ function tholds($header_label) { ' . $row['name'] . ''; + print "'; } ?> diff --git a/tests/Unit/OutputEscapingAndRrdPathTest.php b/tests/Unit/OutputEscapingAndRrdPathTest.php new file mode 100644 index 00000000..4d2fff6f --- /dev/null +++ b/tests/Unit/OutputEscapingAndRrdPathTest.php @@ -0,0 +1,82 @@ + + */ + 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')); + } + + /** + * 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']) . ''", $source); + $this->assertStringNotContainsString("'>' . \$row['name'] . ''", $source); + } +} From f290186afbca67d408d7fee39a742ec292672411 Mon Sep 17 00:00:00 2001 From: Thomas Vincent Date: Thu, 19 Nov 2026 01:04:01 -0800 Subject: [PATCH 2/2] test(thold): cover skipped last-update reads for spaced filenames Signed-off-by: Thomas Vincent --- tests/Unit/OutputEscapingAndRrdPathTest.php | 7 +++++++ tests/bootstrap-unit.php | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/Unit/OutputEscapingAndRrdPathTest.php b/tests/Unit/OutputEscapingAndRrdPathTest.php index 4d2fff6f..8afa6f8f 100644 --- a/tests/Unit/OutputEscapingAndRrdPathTest.php +++ b/tests/Unit/OutputEscapingAndRrdPathTest.php @@ -67,6 +67,13 @@ public function testStatisticsSkipsAnUnsafePathWithoutCallingRrdtool(): void { $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. diff --git a/tests/bootstrap-unit.php b/tests/bootstrap-unit.php index bfffd08b..b224f24e 100644 --- a/tests/bootstrap-unit.php +++ b/tests/bootstrap-unit.php @@ -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'); } }