From 34af78c9bdfd882697496e85f16d7cfc90d0e73c Mon Sep 17 00:00:00 2001 From: Thomas Vincent Date: Sat, 7 Nov 2026 20:46:19 -0800 Subject: [PATCH 1/2] fix: include hosts from all selected trees Signed-off-by: Thomas Vincent --- CHANGELOG.md | 2 + includes/functions.php | 21 ++++- tests/Unit/GexportTreeHostSelectionTest.php | 90 +++++++++++++++++++++ tests/bootstrap-unit.php | 38 +++++++++ 4 files changed, 147 insertions(+), 4 deletions(-) create mode 100644 tests/Unit/GexportTreeHostSelectionTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index af3f7a4..f1c363a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ - dev: Run the CI poller with the shared runner group so RRD files and structured directories pass permission verification - dev: Pin the CI database image and shared test dependencies, disable persisted checkout credentials, and limit workflow permissions and runtime +- issue#96: Include hosts from every selected tree in tree exports + --- 2.1 --- - security: Replace the confirmation pages' inline `onClick='cactiReturnTo()'` Cancel buttons with the CSP-safe `cactiReturnTo` class so the pages no longer trip Cacti's Content-Security-Policy script-src-attr directive diff --git a/includes/functions.php b/includes/functions.php index 87a6fc1..79640f3 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -895,6 +895,22 @@ function check_system_paths(&$export, $export_path) { } } +/** + * Gets the host ids associated with the selected trees. + * + * @param array $tree_ids The selected graph_tree_items.graph_tree_id values. + * + * @return string|false The matching host ids, or false when none exist. + */ +function gexport_get_tree_hosts($tree_ids) { + $placeholders = implode(', ', array_fill(0, cacti_sizeof($tree_ids), '?')); + + return db_fetch_cell_prepared('SELECT GROUP_CONCAT(DISTINCT host_id) + FROM graph_tree_items + WHERE graph_tree_id IN(' . $placeholders . ')', + array_values($tree_ids)); +} + /** * This function exports all the graphs and some html for mgtg view data. * These are all the graphs that are in scope for the export be it a tree @@ -1027,10 +1043,7 @@ function export_graphs(&$export, $export_path) { export_debug('There are ' . cacti_sizeof($graphs) . ' graphs not in hosts to export for all trees'); - $hosts = db_fetch_cell_prepared('SELECT GROUP_CONCAT(DISTINCT host_id) - FROM graph_tree_items - WHERE graph_tree_id IN(?)', - [implode(', ', $ntree)]); + $hosts = gexport_get_tree_hosts($ntree); export_debug('There are ' . cacti_sizeof(explode(',',$hosts)) . ' hosts to export for all trees'); diff --git a/tests/Unit/GexportTreeHostSelectionTest.php b/tests/Unit/GexportTreeHostSelectionTest.php new file mode 100644 index 0000000..6038df5 --- /dev/null +++ b/tests/Unit/GexportTreeHostSelectionTest.php @@ -0,0 +1,90 @@ +toBe('10,20') + ->and(preg_replace('/\s+/', ' ', trim($query))) + ->toBe( + 'SELECT GROUP_CONCAT(DISTINCT host_id) ' . + 'FROM graph_tree_items WHERE graph_tree_id IN(?, ?)' + ) + ->and($params)->toBe([3, 8]); +}); + +it('binds one tree id with a single placeholder', function () { + $query = null; + $params = null; + + gexport_test_mock_db('db_fetch_cell_prepared', function ($sql, $bound) use (&$query, &$params) { + $query = $sql; + $params = $bound; + + return '10'; + }, '10'); + + expect(gexport_get_tree_hosts([3]))->toBe('10') + ->and(preg_replace('/\s+/', ' ', trim($query))) + ->toBe( + 'SELECT GROUP_CONCAT(DISTINCT host_id) ' . + 'FROM graph_tree_items WHERE graph_tree_id IN(?)' + ) + ->and($params)->toBe([3]); +}); + +it('uses hosts from all selected trees when finding exportable graphs', function () { + $GLOBALS['__test_allowed_trees'] = [ + ['id' => 3, 'name' => 'Tree 3'], + ['id' => 8, 'name' => 'Tree 8'], + ]; + + gexport_test_mock_db('db_fetch_cell_prepared', function ($sql, $bound) { + return count($bound) === 2 && $bound === [3, 8] ? '10,20' : false; + }, '10,20'); + + $export = [ + 'export_clear' => 'off', + 'export_effective_user' => 5, + 'graph_tree' => '3,8', + 'graph_site' => '0', + 'id' => 1, + 'export_presentation' => 'tree', + 'export_threads' => 0, + 'graph_max' => 0, + ]; + $export_path = sys_get_temp_dir() . '/gexport-tree-hosts-' . uniqid(); + + try { + expect(export_graphs($export, $export_path))->toBe(0) + ->and($GLOBALS['__test_allowed_graph_calls'][0]['sql_where']) + ->toBe('gl.host_id IN(10,20)'); + } finally { + delTree($export_path); + } +}); diff --git a/tests/bootstrap-unit.php b/tests/bootstrap-unit.php index 9214edc..a49d28d 100644 --- a/tests/bootstrap-unit.php +++ b/tests/bootstrap-unit.php @@ -81,6 +81,9 @@ $GLOBALS['__test_db_calls'] = array(); $GLOBALS['__test_db_fixtures'] = array(); +$GLOBALS['__test_allowed_trees'] = []; +$GLOBALS['__test_allowed_graphs'] = []; +$GLOBALS['__test_allowed_graph_calls'] = []; if (!function_exists('gexport_test_mock_db')) { function gexport_test_mock_db($fn, $match, $result) { @@ -328,6 +331,37 @@ function cacti_log($message, $also_print = false, $log_type = '', $level = 0) { } } +if (!function_exists('get_allowed_trees')) { + function get_allowed_trees($edit = false, $return_sql = false, $sql_where = '', $sql_order = 'name', $sql_limit = '', &$total_rows = 0, $user_id = 0) { + return $GLOBALS['__test_allowed_trees']; + } +} + +if (!function_exists('get_allowed_graphs')) { + function get_allowed_graphs($sql_where = '', $sql_order = 'gtg.title_cache', $sql_limit = '', &$total_rows = 0, $user_id = 0, $graph_id = 0) { + $GLOBALS['__test_allowed_graph_calls'][] = [ + 'sql_where' => $sql_where, + 'user_id' => $user_id, + ]; + + return $GLOBALS['__test_allowed_graphs']; + } +} + +if (!function_exists('array_rekey')) { + function array_rekey($rows, $key, $value) { + $rekeyed = []; + + if (is_array($rows)) { + foreach ($rows as $row) { + $rekeyed[$row[$key]] = $row[$value]; + } + } + + return $rekeyed; + } +} + if (!function_exists('cacti_sizeof')) { function cacti_sizeof($array) { return is_array($array) ? count($array) : 0; @@ -389,6 +423,10 @@ function sql_save($array, $table, $key = 'id') { define('POLLER_VERBOSITY_LOW', 2); } +if (!defined('POLLER_VERBOSITY_HIGH')) { + define('POLLER_VERBOSITY_HIGH', 4); +} + if (!defined('POLLER_VERBOSITY_MEDIUM')) { define('POLLER_VERBOSITY_MEDIUM', 3); } From f4ba1e16b07f1dbf3aeb32263611eafa00ea8f52 Mon Sep 17 00:00:00 2001 From: Thomas Vincent Date: Sun, 8 Nov 2026 01:46:52 -0800 Subject: [PATCH 2/2] fix: skip non-device tree items and avoid GROUP_CONCAT truncation Graph and header tree items have host_id 0, so the tree host list produced gl.host_id IN(0, ...) and exported every allowed non-device graph, including graphs on no selected tree. Restrict the lookup to host_id > 0. GROUP_CONCAT() truncates at group_concat_max_len (1024 bytes by default), which a multi-tree selection reaches sooner and which can leave a partial id that matches another host. Fetch the ids as rows and join them in PHP instead. Signed-off-by: Thomas Vincent --- CHANGELOG.md | 2 +- includes/functions.php | 19 ++++++-- tests/Unit/GexportTreeHostSelectionTest.php | 48 +++++++++++++++------ 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1c363a..5f78af9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ - dev: Run the CI poller with the shared runner group so RRD files and structured directories pass permission verification - dev: Pin the CI database image and shared test dependencies, disable persisted checkout credentials, and limit workflow permissions and runtime -- issue#96: Include hosts from every selected tree in tree exports +- issue#96: Include hosts from every selected tree in tree exports, without pulling in non-device graphs or truncating long host lists --- 2.1 --- diff --git a/includes/functions.php b/includes/functions.php index 79640f3..4f28b2a 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -898,17 +898,30 @@ function check_system_paths(&$export, $export_path) { /** * Gets the host ids associated with the selected trees. * + * Graph and header tree items carry host_id 0, which would pull every + * non-device graph into the export, so only device items are used. The ids + * are fetched as rows rather than with GROUP_CONCAT(), which truncates at + * group_concat_max_len and can leave a partial id at the end of the list. + * * @param array $tree_ids The selected graph_tree_items.graph_tree_id values. * - * @return string|false The matching host ids, or false when none exist. + * @return string The matching host ids as a comma-separated list, or '' + * when none exist. */ function gexport_get_tree_hosts($tree_ids) { $placeholders = implode(', ', array_fill(0, cacti_sizeof($tree_ids), '?')); - return db_fetch_cell_prepared('SELECT GROUP_CONCAT(DISTINCT host_id) + $rows = db_fetch_assoc_prepared('SELECT DISTINCT host_id FROM graph_tree_items - WHERE graph_tree_id IN(' . $placeholders . ')', + WHERE graph_tree_id IN(' . $placeholders . ') + AND host_id > 0', array_values($tree_ids)); + + if (!cacti_sizeof($rows)) { + return ''; + } + + return implode(',', array_map('intval', array_column($rows, 'host_id'))); } /** diff --git a/tests/Unit/GexportTreeHostSelectionTest.php b/tests/Unit/GexportTreeHostSelectionTest.php index 6038df5..ec99974 100644 --- a/tests/Unit/GexportTreeHostSelectionTest.php +++ b/tests/Unit/GexportTreeHostSelectionTest.php @@ -22,18 +22,18 @@ $query = null; $params = null; - gexport_test_mock_db('db_fetch_cell_prepared', function ($sql, $bound) use (&$query, &$params) { + gexport_test_mock_db('db_fetch_assoc_prepared', function ($sql, $bound) use (&$query, &$params) { $query = $sql; $params = $bound; - return '10,20'; - }, '10,20'); + return true; + }, [['host_id' => '10'], ['host_id' => '20']]); expect(gexport_get_tree_hosts([3, 8]))->toBe('10,20') ->and(preg_replace('/\s+/', ' ', trim($query))) ->toBe( - 'SELECT GROUP_CONCAT(DISTINCT host_id) ' . - 'FROM graph_tree_items WHERE graph_tree_id IN(?, ?)' + 'SELECT DISTINCT host_id ' . + 'FROM graph_tree_items WHERE graph_tree_id IN(?, ?) AND host_id > 0' ) ->and($params)->toBe([3, 8]); }); @@ -42,31 +42,53 @@ $query = null; $params = null; - gexport_test_mock_db('db_fetch_cell_prepared', function ($sql, $bound) use (&$query, &$params) { + gexport_test_mock_db('db_fetch_assoc_prepared', function ($sql, $bound) use (&$query, &$params) { $query = $sql; $params = $bound; - return '10'; - }, '10'); + return true; + }, [['host_id' => '10']]); expect(gexport_get_tree_hosts([3]))->toBe('10') ->and(preg_replace('/\s+/', ' ', trim($query))) ->toBe( - 'SELECT GROUP_CONCAT(DISTINCT host_id) ' . - 'FROM graph_tree_items WHERE graph_tree_id IN(?)' + 'SELECT DISTINCT host_id ' . + 'FROM graph_tree_items WHERE graph_tree_id IN(?) AND host_id > 0' ) ->and($params)->toBe([3]); }); +it('returns an empty host list when the selected trees hold no devices', function () { + gexport_test_mock_db('db_fetch_assoc_prepared', 'FROM graph_tree_items', []); + + expect(gexport_get_tree_hosts([3]))->toBe(''); +}); + +it('keeps every host id intact when the list is long', function () { + // GROUP_CONCAT() stops at group_concat_max_len (1024 bytes by default). + $rows = []; + + for ($id = 100000; $id < 100400; $id++) { + $rows[] = ['host_id' => (string) $id]; + } + + gexport_test_mock_db('db_fetch_assoc_prepared', 'FROM graph_tree_items', $rows); + + $hosts = explode(',', gexport_get_tree_hosts([3])); + + expect($hosts)->toHaveCount(400) + ->and(end($hosts))->toBe('100399'); +}); + it('uses hosts from all selected trees when finding exportable graphs', function () { $GLOBALS['__test_allowed_trees'] = [ ['id' => 3, 'name' => 'Tree 3'], ['id' => 8, 'name' => 'Tree 8'], ]; - gexport_test_mock_db('db_fetch_cell_prepared', function ($sql, $bound) { - return count($bound) === 2 && $bound === [3, 8] ? '10,20' : false; - }, '10,20'); + gexport_test_mock_db('db_fetch_assoc_prepared', function ($sql, $bound) { + return strpos($sql, 'FROM graph_tree_items') !== false && $bound === [3, 8]; + }, [['host_id' => '10'], ['host_id' => '20']]); $export = [ 'export_clear' => 'off',