mirror of
https://github.com/Cacti/cacti.git
synced 2026-10-23 03:27:02 +09:00
* feat(validator): add settings constraint validation pilot Routes saved settings through Symfony Validator so include/global_settings.php can declare per-setting constraints inline. The save handler runs one validator pass and surfaces violations via the existing raise_message() pattern, replacing scattered ad-hoc checks. Pilot covers 12 high-value settings; the rest stay on the existing implicit form-render checks. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * ci(infection): allow infection/extension-installer composer plugin Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * docs(validator): document must-be-posted-to-validate contract Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(bootstrap): guard Symfony autoload in global_settings before use declarations Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(test): remove redundant autoload require in CactiSettingsTest Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(test): restore require_once for CactiSettings (not in Composer autoload) Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * feat(validator): fold helper into settings pilot * fix(validator): translate default Symfony constraint messages Default Symfony NotBlank/Length/Range/Choice/Positive messages render in English regardless of Cacti locale because the validator runs without a Symfony Translator. Pass explicit __()-wrapped message, minMessage, maxMessage, and notInRangeMessage options on each bare constraint in include/global_settings.php so violations participate in Cacti's gettext pipeline. Documents the no-Translator decision in CactiValidator so future contributors know to translate at the call site. Addresses Copilot PR #7077 review feedback. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(graph): improve remote agent parsing and RRD path validation * feat(validator): migrate automation_graph_rules.php to symfony validator * refactor(validator): move automation validator helper to shared library and enhance tests * feat(validator): migrate automation_networks.php to symfony validator * feat(validator): migrate automation_snmp.php to symfony validator * feat(validator): migrate automation_templates.php to symfony validator * feat(validator): migrate automation_tree_rules.php to symfony validator * refactor(automation): move api_networks_save to shared library for testability * feat(validator): add validateInput helper and migrate color_templates.php * feat(validator): migrate host_templates.php to symfony validator * feat(validator): migrate graph_templates.php to symfony validator * feat(validator): migrate data_templates.php to symfony validator * feat(validator): migrate data_input.php to symfony validator * feat(validator): migrate data_queries.php to symfony validator * feat(validator): migrate graphs_new.php to symfony validator * feat(validator): migrate graphs.php to symfony validator * feat(validator): migrate data_sources.php to symfony validator * feat(validator): migrate user_group_admin.php to symfony validator * feat(validator): migrate managers.php to symfony validator * feat(validator): migrate package_repos.php to symfony validator * Refactor pollers.php to use CactiValidator * fix(security): close validator migration regressions * test(snmp): add spine docker harness * fix(review): address validator migration feedback * fix(deps): include validator runtime packages * chore(deps): normalize composer.json require-block indentation The validator migration introduced tab-indented entries in an otherwise space-indented require block. Re-indent the stevenmaguire/* and symfony/* lines to 8 spaces so the block is consistent. Co-Authored-By: Oz <oz-agent@warp.dev> * fix(graphs): repair graph json response branch * fix(validation): apply php cs fixer * ci(codeql): skip autobuild for interpreted scans * fix(link): satisfy phpstan path guard * chore(pr): remove branch pollution * ci(codeql): align autobuild pin with init and analyze Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * refactor(automation): use CactiValidator::validateInput, drop wrapper automation_validate_input() in lib/api_automation.php was a byte-for-byte duplicate of CactiValidator::validateInput() (identical session handling and raise_message() semantics, and it already delegated to CactiValidator::validate internally). Per review feedback, use the shared class directly everywhere instead of a parallel automation-only wrapper. - Remove automation_validate_input() from lib/api_automation.php - Replace all call sites with CactiValidator::validateInput() (automation_templates/graph_rules/tree_rules/snmp and api_networks_save) - Update the automation validator unit tests to match - Add @param/@return PHPDoc to CactiValidator specific-validator methods Co-Authored-By: Oz <oz-agent@warp.dev> --------- Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> Co-authored-by: Thomas Vincent <thomas@vincent.dev> Co-authored-by: Oz <oz-agent@warp.dev> Co-authored-by: TheWitness <thewitness@cacti.net>
146 lines
5.9 KiB
PHP
146 lines
5.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 |
|
|
+-------------------------------------------------------------------------+
|
|
*/
|
|
|
|
require_once dirname(__DIR__, 2) . '/include/vendor/autoload.php';
|
|
require_once dirname(__DIR__, 2) . '/lib/CactiValidator.php';
|
|
|
|
use Symfony\Component\Validator\Constraints as Assert;
|
|
|
|
beforeEach(function () {
|
|
CactiValidator::reset();
|
|
});
|
|
|
|
it('validates a numeric host id', function () {
|
|
expect(CactiValidator::isValidHostId(123))->toBeTrue();
|
|
expect(CactiValidator::isValidHostId('456'))->toBeTrue();
|
|
expect(CactiValidator::isValidHostId(0))->toBeTrue();
|
|
});
|
|
|
|
it('rejects a negative host id', function () {
|
|
expect(CactiValidator::isValidHostId(-1))->toBeFalse();
|
|
});
|
|
|
|
it('rejects a non-numeric host id', function () {
|
|
expect(CactiValidator::isValidHostId('abc'))->toBeFalse();
|
|
expect(CactiValidator::isValidHostId(''))->toBeFalse();
|
|
expect(CactiValidator::isValidHostId(null))->toBeFalse();
|
|
});
|
|
|
|
it('validates a safe rrd path', function () {
|
|
expect(CactiValidator::isValidRrdPath('host_1/traffic_in.rrd'))->toBeTrue();
|
|
expect(CactiValidator::isValidRrdPath('archive/2023-01-01.rrd'))->toBeTrue();
|
|
expect(CactiValidator::isValidRrdPath('local.rrd'))->toBeTrue();
|
|
});
|
|
|
|
it('rejects rrd path with traversal', function () {
|
|
expect(CactiValidator::isValidRrdPath('../../etc/passwd'))->toBeFalse();
|
|
expect(CactiValidator::isValidRrdPath('..'))->toBeFalse();
|
|
expect(CactiValidator::isValidRrdPath('/var/www/html/cacti/rra/../config.php'))->toBeFalse();
|
|
});
|
|
|
|
it('rejects rrd path with invalid characters', function () {
|
|
expect(CactiValidator::isValidRrdPath('my graph; rm -rf /'))->toBeFalse();
|
|
expect(CactiValidator::isValidRrdPath('host_1/<script>alert(1)</script>.rrd'))->toBeFalse();
|
|
});
|
|
|
|
it('validates ipv4 and ipv6 addresses', function () {
|
|
expect(CactiValidator::isValidIpAddress('127.0.0.1'))->toBeTrue();
|
|
expect(CactiValidator::isValidIpAddress('192.168.1.1'))->toBeTrue();
|
|
expect(CactiValidator::isValidIpAddress('::1'))->toBeTrue();
|
|
expect(CactiValidator::isValidIpAddress('2001:0db8:85a3:0000:0000:8a2e:0370:7334'))->toBeTrue();
|
|
|
|
expect(CactiValidator::isValidIpAddress('not-an-ip'))->toBeFalse();
|
|
expect(CactiValidator::isValidIpAddress('256.256.256.256'))->toBeFalse();
|
|
});
|
|
|
|
it('validates email addresses', function () {
|
|
expect(CactiValidator::isValidEmail('test@example.com'))->toBeTrue();
|
|
expect(CactiValidator::isValidEmail('user.name+tag@domain.co.uk'))->toBeTrue();
|
|
|
|
expect(CactiValidator::isValidEmail('invalid-email'))->toBeFalse();
|
|
expect(CactiValidator::isValidEmail('test@example'))->toBeFalse();
|
|
});
|
|
|
|
it('validates snmp community strings', function () {
|
|
expect(CactiValidator::isValidSnmpCommunity('public'))->toBeTrue();
|
|
expect(CactiValidator::isValidSnmpCommunity('my-community_123'))->toBeTrue();
|
|
expect(CactiValidator::isValidSnmpCommunity('public@site'))->toBeTrue();
|
|
expect(CactiValidator::isValidSnmpCommunity('read only/community:1'))->toBeTrue();
|
|
|
|
expect(CactiValidator::isValidSnmpCommunity(''))->toBeFalse();
|
|
expect(CactiValidator::isValidSnmpCommunity("comm\nunity"))->toBeFalse();
|
|
expect(CactiValidator::isValidSnmpCommunity("comm\x7funity"))->toBeFalse();
|
|
});
|
|
|
|
it('can validate against custom constraints', function () {
|
|
$constraints = [
|
|
new Assert\Email(mode: 'html5'),
|
|
new Assert\NotBlank(),
|
|
];
|
|
|
|
expect(CactiValidator::isValid('test@example.com', $constraints))->toBeTrue();
|
|
expect(CactiValidator::isValid('invalid-email', $constraints))->toBeFalse();
|
|
});
|
|
|
|
it('rejects rrd path with absolute root', function () {
|
|
expect(CactiValidator::isValidRrdPath('/etc/passwd_no_dots'))->toBeFalse();
|
|
expect(CactiValidator::isValidRrdPath('/var/www/html/cacti/rra/host_1.rrd'))->toBeFalse();
|
|
});
|
|
|
|
it('rejects rrd path with NUL byte', function () {
|
|
expect(CactiValidator::isValidRrdPath("foo\0..rrd"))->toBeFalse();
|
|
});
|
|
|
|
it('rejects empty rrd path', function () {
|
|
expect(CactiValidator::isValidRrdPath(''))->toBeFalse();
|
|
});
|
|
|
|
it('enforces rraRoot containment when supplied', function () {
|
|
$root = sys_get_temp_dir() . '/cacti_rra_' . uniqid();
|
|
mkdir($root, 0700, true);
|
|
file_put_contents($root . '/host_1.rrd', '');
|
|
try {
|
|
expect(CactiValidator::isValidRrdPath('host_1.rrd', $root))->toBeTrue();
|
|
// Improved: allow missing files if containment can be verified
|
|
expect(CactiValidator::isValidRrdPath('missing.rrd', $root))->toBeTrue();
|
|
} finally {
|
|
// nosemgrep: php.lang.security.unlink-use.unlink-use - test cleanup of file we just created in temp dir
|
|
@unlink($root . '/host_1.rrd');
|
|
@rmdir($root);
|
|
}
|
|
});
|
|
|
|
it('validates ipv4-mapped ipv6 and reserved ranges as syntactically valid', function () {
|
|
expect(CactiValidator::isValidIpAddress('::ffff:127.0.0.1'))->toBeTrue();
|
|
});
|
|
|
|
it('rejects malformed ipv4 with leading zeros that some parsers accept', function () {
|
|
// Symfony Ip validator accepts "192.168.001.001" - assert current behavior so regressions are visible.
|
|
$value = CactiValidator::isValidIpAddress('192.168.001.001');
|
|
expect($value)->toBeBool();
|
|
});
|
|
|
|
it('rejects email with header injection attempt', function () {
|
|
expect(CactiValidator::isValidEmail("a@b.com\r\nBcc: c@d.com"))->toBeFalse();
|
|
});
|
|
|
|
it('rejects oversize email', function () {
|
|
$local = str_repeat('a', 320);
|
|
expect(CactiValidator::isValidEmail($local . '@example.com'))->toBeFalse();
|
|
});
|
|
|
|
it('rejects snmp community with non-ascii', function () {
|
|
expect(CactiValidator::isValidSnmpCommunity("p\xc3\xa9blic"))->toBeFalse();
|
|
});
|