You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/CliTaskSafetyTest.php

88 lines
4.2 KiB
PHTML

<?php
// Deliberately avoid bootstrap and databases, even in a deployment checkout.
require dirname(__DIR__) . '/app/Services/Config.php';
require dirname(__DIR__) . '/app/Command/Job.php';
require dirname(__DIR__) . '/app/Command/XCat.php';
require dirname(__DIR__) . '/app/Utils/Hash.php';
use App\Command\Job;
use App\Command\XCat;
use App\Utils\Hash;
$assert = function ($condition, $message) {
if (!$condition) {
fwrite(STDERR, $message . PHP_EOL);
exit(1);
}
};
$method = function ($name) {
return new ReflectionMethod(Job::class, $name);
};
$normalized = $method('connectionOptions')->invoke(null, [
'driver' => 'mysql', 'host' => '[2001:db8::1]:3307', 'database' => '--example-db',
'username' => 'test-admin', 'password' => "synthetic\"\\\n[evil]",
]);
$assert($normalized['host'] === '2001:db8::1' && $normalized['port'] === 3307,
'Bracketed IPv6 database host/port was not parsed.');
$options = $method('clientOptions')->invoke(null, $normalized);
$assert(substr_count($options, "\n") === 6 && !str_contains($options, "\n[evil]"),
'Database credentials can inject option-file lines.');
$args = $method('dumpArguments')->invoke(null, '/usr/bin/mariadb-dump', '/private/test.cnf', $normalized);
$assert(is_array($args) && $args[1] === '--defaults-extra-file=/private/test.cnf'
&& array_slice($args, -3) === ['--databases', '--', '--example-db'],
'Database dump does not use safe argument boundaries and first credential-file option.');
$assert(!in_array($normalized['password'], $args, true) && !in_array($normalized['username'], $args, true),
'Database credentials appear in process arguments.');
$assert(!str_contains(implode(' ', $args), '--all-databases'),
'Backup is not restricted to the configured application databases.');
$System_Config = [
'db_driver' => 'mysql', 'db_host' => 'db.example.invalid:3307', 'db_database' => 'application',
'db_username' => 'primary-user', 'db_password' => 'synthetic-primary',
'enable_log' => 'false', 'log_db_host' => '', 'log_db_database' => 'events',
'log_db_user' => 'log-user', 'log_db_password' => 'synthetic-logs',
];
$connections = $method('backupConnections')->invoke(null);
$assert(array_keys($connections) === ['primary'], 'Disabled log database is unexpectedly backed up.');
$System_Config['enable_log'] = 'true';
$connections = $method('backupConnections')->invoke(null);
$assert(array_keys($connections) === ['primary', 'logs'] && $connections['logs']['host'] === 'localhost',
'Enabled log database or its local default host was omitted.');
$System_Config['log_db_database'] = '';
$rejected = false;
try {
$method('backupConnections')->invoke(null);
} catch (RuntimeException $exception) {
$rejected = true;
}
$assert($rejected, 'Incomplete enabled log database must fail rather than produce a partial backup.');
$generator = new ReflectionMethod(XCat::class, 'generatePassword');
for ($i = 0; $i < 10; $i++) {
$password = $generator->invoke(null);
$assert(strlen($password) === 28 && Hash::newPasswordError($password) === null,
'CLI administrator password does not satisfy the strong-password policy.');
}
ob_start();
$helpStatus = (new XCat(['xcat', 'help']))->boot();
$help = ob_get_clean();
$assert($helpStatus === 0 && str_contains($help, 'backupJob') && str_contains($help, 'setTelegram'),
'Retained CLI commands are missing.');
foreach (['dailyjob', 'checkjob', 'resetTraffic', 'resetBought', 'cleanuser', 'createUser'] as $retired) {
$assert(!str_contains($help, $retired), 'Retired CLI command is still advertised: ' . $retired);
$assert(!method_exists(Job::class, $retired) && !method_exists(XCat::class, $retired),
'Retired CLI command still has an implementation: ' . $retired);
}
$jobSource = file_get_contents(dirname(__DIR__) . '/app/Command/Job.php');
$assert(str_contains($jobSource, 'ZipArchive::EM_AES_256')
&& str_contains($jobSource, "Config::get('auto_backup_passwd', '')")
&& !str_contains($jobSource, 'auto_backup_webroot')
&& !str_contains($jobSource, 'cleanupRawEvents'),
'Backup encryption, SQL-only archive, or no-auto-deletion boundary regressed.');
echo 'CLI safe database arguments, SQL-only AES backups and strong administrator passwords passed.' . PHP_EOL;