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
8 changes: 8 additions & 0 deletions lib/poller.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ function exec_poll_php($command, $using_proc_function, $pipes, $proc_fd) {
function exec_background($filename, $args = '', $redirect_args = '') {
global $config, $debug;

if (is_array($args)) {
$args = implode(' ', array_map('cacti_escapeshellarg', $args));
}

if (is_array($redirect_args)) {
$redirect_args = '';
}

cacti_log("DEBUG: About to Spawn a Remote Process [CMD: $filename, ARGS: $args]", true, 'POLLER', ($debug ? POLLER_VERBOSITY_NONE:POLLER_VERBOSITY_DEBUG));

if (file_exists($filename)) {
Expand Down
5 changes: 5 additions & 0 deletions lib/rrd.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ function rrdtool_execute() {
function __rrd_execute($command_line, $log_to_stdout, $output_flag, $rrdtool_pipe = false, $logopt = 'WEBLOG') {
global $config;

if (is_array($command_line)) {
$cmd = array_shift($command_line);
$command_line = $cmd . ' ' . implode(' ', array_map('cacti_escapeshellarg', $command_line));
Comment on lines +259 to +260
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When $command_line is an array, array_shift() can return null (e.g., empty array), which would produce an invalid $command_line and later calls like substr()/logging/proc I/O will behave unpredictably. Consider validating that the array is non-empty and that the first element is a non-empty command token, and fail fast (return false and/or log an error) if not.

Suggested change
$cmd = array_shift($command_line);
$command_line = $cmd . ' ' . implode(' ', array_map('cacti_escapeshellarg', $command_line));
if (empty($command_line)) {
cacti_log('RRDTool command execution failed: empty command array provided to __rrd_execute()', false, 'SYSTEM', POLLER_VERBOSITY_MEDIUM);
return false;
}
$cmd = array_shift($command_line);
if (!is_string($cmd) || $cmd === '') {
cacti_log('RRDTool command execution failed: invalid command token provided to __rrd_execute()', false, 'SYSTEM', POLLER_VERBOSITY_MEDIUM);
return false;
}
$escaped_args = array_map('cacti_escapeshellarg', $command_line);
if (!empty($escaped_args)) {
$command_line = $cmd . ' ' . implode(' ', $escaped_args);
} else {
$command_line = $cmd;
}

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the array form, only the arguments after the first element are escaped. If the first element ($cmd) can contain whitespace/newlines (even accidentally), it defeats the goal of treating each array entry as an atomic token. Consider either escaping/quoting the first element too (in a way RRDtool accepts) or validating it against an allowed token pattern (no spaces/control chars).

Suggested change
$command_line = $cmd . ' ' . implode(' ', array_map('cacti_escapeshellarg', $command_line));
$command_line = cacti_escapeshellarg($cmd) . ' ' . implode(' ', array_map('cacti_escapeshellarg', $command_line));

Copilot uses AI. Check for mistakes.
}

static $last_command;

if (!is_numeric($output_flag)) {
Expand Down
Loading