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
12 changes: 7 additions & 5 deletions mactrack_devices.php
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,11 @@ function mactrack_device_export(): void {
$devices = mactrack_get_devices($sql_where, 0, false);

$xport_array = [];
// the community string, auth password and privacy passphrase are not
// exported; the import matches columns by name, so it keeps the stored ones
array_push($xport_array, 'site_id, site_name, device_id, device_name, notes, ' .
'hostname, snmp_options, snmp_readstring, snmp_version, ' .
'snmp_username, snmp_password, snmp_auth_protocol, snmp_priv_passphrase, ' .
'hostname, snmp_options, snmp_version, ' .
'snmp_username, snmp_auth_protocol, ' .
'snmp_priv_protocol, snmp_context, snmp_engine_id, ' .
'snmp_port, snmp_timeout, snmp_retries, max_oids, snmp_sysName, snmp_sysLocation, ' .
'snmp_sysContact, snmp_sysObjectID, snmp_sysDescr, snmp_sysUptime, ' .
Expand All @@ -532,10 +534,10 @@ function mactrack_device_export(): void {
$device['site_id'] . '","' . $device['site_name'] . '","' .
$device['device_id'] . '","' . $device['device_name'] . '","' .
$device['notes'] . '","' . $device['hostname'] . '","' .
$device['snmp_options'] . '","' . $device['snmp_readstring'] . '","' .
$device['snmp_options'] . '","' .
$device['snmp_version'] . '","' . $device['snmp_username'] . '","' .
$device['snmp_password'] . '","' . $device['snmp_auth_protocol'] . '","' .
$device['snmp_priv_passphrase'] . '","' . $device['snmp_priv_protocol'] . '","' .
$device['snmp_auth_protocol'] . '","' .
$device['snmp_priv_protocol'] . '","' .
$device['snmp_context'] . '","' . $device['snmp_engine_id'] . '","' .
$device['snmp_port'] . '","' . $device['snmp_timeout'] . '","' .
$device['snmp_retries'] . '","' . $device['max_oids'] . '","' .
Expand Down
51 changes: 51 additions & 0 deletions tests/Support/load_function.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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/ |
+-------------------------------------------------------------------------+
*/

/*
* Some plugin scripts handle the request or run the poller as soon as they
* are included, so a test cannot require them. This copies one function out
* of such a script into a temporary file and loads that instead.
*/

/**
* Load a single top-level function from a plugin script.
*
* @param string $file Path relative to the plugin root.
* @param string $function Function name.
*
* @return void
*/
function mactrack_test_load_function(string $file, string $function): void {
if (function_exists($function)) {
return;
}

$source = (string) file_get_contents(dirname(__DIR__, 2) . '/' . $file);
$start = strpos($source, "\nfunction $function(");

if ($start === false) {
throw new RuntimeException("$function() not found in $file");
}

$end = strpos($source, "\n}\n", $start);
$path = tempnam(sys_get_temp_dir(), 'mactrack_fn_');

file_put_contents($path, "<?php\n" . substr($source, $start, $end - $start + 3));

require $path;

unlink($path);
}
74 changes: 74 additions & 0 deletions tests/Unit/DeviceExportCredentialsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?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/ |
+-------------------------------------------------------------------------+
*/

/*
* The device CSV export leaves out the SNMP community string, auth password
* and privacy passphrase (#317). The import matches columns by header name,
* so re-importing an export keeps the stored values.
*/

/*
* mactrack_devices.php handles the request when it is included, so load only
* the export function from its source, with its two helpers stubbed.
*/
require_once dirname(__DIR__) . '/Support/load_function.php';

mactrack_test_load_function('mactrack_devices.php', 'mactrack_device_export');

if (!function_exists('mactrack_device_request_validation')) {
function mactrack_device_request_validation(): void {
}
}

if (!function_exists('mactrack_get_devices')) {
function mactrack_get_devices(&$sql_where, $rows, $apply_limits = true): array {
return $GLOBALS['__test_export_devices'];
}
}

test('the device export leaves out the SNMP secrets', function () {
$device = array_fill_keys([
'site_id', 'site_name', 'device_id', 'device_name', 'notes', 'hostname',
'snmp_options', 'snmp_version', 'snmp_username', 'snmp_auth_protocol',
'snmp_priv_protocol', 'snmp_context', 'snmp_engine_id', 'snmp_port',
'snmp_timeout', 'snmp_retries', 'max_oids', 'snmp_sysName',
'snmp_sysLocation', 'snmp_sysContact', 'snmp_sysObjectID', 'snmp_sysDescr',
'snmp_sysUptime', 'ignorePorts', 'scan_type', 'disabled', 'ports_total',
'ports_active', 'ports_trunk', 'macs_active', 'last_rundate', 'last_runduration',
], 'x');

$device['hostname'] = 'sw1.example.net';
$device['snmp_readstring'] = 'community-secret';
$device['snmp_password'] = 'auth-secret';
$device['snmp_priv_passphrase'] = 'priv-secret';

$GLOBALS['__test_export_devices'] = [$device];

ob_start();
mactrack_device_export();
$lines = explode("\n", trim((string) ob_get_clean()));

$header = array_map('trim', explode(',', $lines[0]));
$row = str_getcsv($lines[1], ',', '"', '');

expect($header)->not->toContain('snmp_readstring')
->and($header)->not->toContain('snmp_password')
->and($header)->not->toContain('snmp_priv_passphrase')
->and($header)->toContain('snmp_username')
->and($row)->toHaveCount(count($header))
->and($row[array_search('hostname', $header, true)])->toBe('sw1.example.net')
->and($lines[1])->not->toContain('secret');
});
Loading