Files
cacti/tests/Unit/Security/ImportPackageLimitsTest.php
T
Thomas Vincent 92354267c4 fix(import): cap Package size and reject manifest names outside the repository (#8711)
* fix(import): cap Package size and reject manifest names outside the repository

The import decompressed Packages whole, and get_repo_file() appended the
unsigned manifest's file names to the repository location unchecked.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test(import): load the manifest file name check into the manifest fixture

get_repo_file() now calls package_repo_filename_valid(), which lives in
lib/import.php. The manifest transport fixture only sliced get_repo_file()
out of package_import.php, so the call was undefined and the fixture exited
with a fatal error. Slice the unchanged helper into the fixture namespace as
well.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test(package): cover the Package size limit and repository file name checks in-process

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test(package): run the manifest fixture when lib/import.php changes

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test(package): expect the manifest fixture's 47 cases

The fixture gained the repository file name cases, so it now passes 47
checks, not 32.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

---------

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
2026-11-17 13:55:25 -05:00

107 lines
3.9 KiB
PHP

<?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 |
+-------------------------------------------------------------------------+
*/
/*
* Package files arrive from repositories whose manifest is not signed, and
* the import decompresses them whole. A manifest file name must name a file
* directly inside the repository, and a Package that decompresses past
* import_package_max_bytes() is refused before any reader loads it.
*/
require_once dirname(__DIR__, 2) . '/Helpers/CactiStubs.php';
require_once dirname(__DIR__, 2) . '/Helpers/FakeMySQLPDO.php';
require_once dirname(__DIR__, 3) . '/include/global.php';
require_once dirname(__DIR__, 3) . '/lib/import.php';
require_once dirname(__DIR__, 3) . '/lib/xml.php';
/*
* Write $bytes of zeros as a gzip file in 1 MiB chunks, so the test never
* holds the decompressed Package in memory.
*/
function oversizedPackage(int $bytes): string {
$file = tempnam(sys_get_temp_dir(), 'cacti-pkg-limit-');
$gz = gzopen($file, 'wb9');
$left = $bytes;
while ($left > 0) {
$size = min($left, 1048576);
gzwrite($gz, str_repeat("\0", $size));
$left -= $size;
}
gzclose($gz);
return $file;
}
describe('package_repo_filename_valid()', function () {
it('accepts a file directly inside the repository', function () {
expect(package_repo_filename_valid('package.manifest'))->toBeTrue()
->and(package_repo_filename_valid('Cisco_Router.xml.gz'))->toBeTrue();
});
it('rejects a name that leaves the repository or is hidden', function (string $name) {
expect(package_repo_filename_valid($name))->toBeFalse();
})->with([
'empty' => [''],
'parent' => ['../package.xml.gz'],
'subdirectory' => ['dir/package.xml.gz'],
'absolute' => ['/etc/passwd'],
'backslash' => ['..\\package.xml.gz'],
'hidden' => ['.htaccess'],
'null byte' => ["package.xml.gz\0.php"],
]);
});
describe('import_package_within_size_limit()', function () {
it('allows Packages far larger than any template set by default', function () {
expect(import_package_max_bytes())->toBe(64 * 1024 * 1024);
});
it('accepts a Package within the limit and rejects one past it', function () {
$file = tempnam(sys_get_temp_dir(), 'cacti-pkg-limit-');
file_put_contents($file, gzencode(str_repeat('a', 4096)));
expect(import_package_within_size_limit($file, 4096))->toBeTrue()
->and(import_package_within_size_limit($file, 4095))->toBeFalse();
unlink($file);
});
it('rejects a damaged or missing Package', function () {
$file = tempnam(sys_get_temp_dir(), 'cacti-pkg-limit-');
$good = gzencode(str_repeat('abc', 300));
file_put_contents($file, substr($good, 0, 10) . str_repeat("\xff", 40) . substr($good, 50));
expect(import_package_within_size_limit($file))->toBeFalse()
->and(import_package_within_size_limit($file . '-missing'))->toBeFalse();
unlink($file);
});
});
describe('Package readers', function () {
it('refuse a Package that decompresses past the limit', function () {
$file = oversizedPackage(import_package_max_bytes() + 1);
$public_key = '';
expect(import_read_package_data($file, $public_key, true))->toBeFalse()
->and(import_get_package_info($file))->toBeFalse()
->and(import_package_get_details($file))->toBe(['public_key' => ''])
->and(import_package_get_public_key($file))->toBe('');
unlink($file);
});
});