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/AdminSystemLogsContractTest...

107 lines
5.6 KiB
PHTML

<?php
/**
* Contract checks for the read-only administrator system journal page.
*
* This intentionally avoids the application bootstrap: a checkout without
* Composer dependencies can still validate the route, command and escaping
* boundaries, while the parser fixture exercises the service directly.
*/
$root = dirname(__DIR__);
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
require_once $root . '/app/Services/Cloud/CloudSystemJournalService.php';
$fixture = implode("\n", [
json_encode([
'_SYSTEMD_UNIT' => 'virt-cloud-engine.service',
'PRIORITY' => '3',
'__REALTIME_TIMESTAMP' => '1791273697115460',
'MESSAGE' => "engine failed\x1b[31m\nwith control",
'_PID' => '12', '_COMM' => 'python', '_HOSTNAME' => 'host-a',
'__CURSOR' => 's=abc;i=1',
], JSON_UNESCAPED_UNICODE),
// Entries from other units must not cross the service allowlist.
json_encode([
'_SYSTEMD_UNIT' => 'ssh.service', 'PRIORITY' => 3,
'MESSAGE' => 'private host log',
], JSON_UNESCAPED_UNICODE),
'{invalid json',
json_encode([
'_SYSTEMD_UNIT' => 'virt-cloud-worker.service',
'PRIORITY' => 99, 'MESSAGE' => 'invalid priority is normalized',
], JSON_UNESCAPED_UNICODE),
]);
$rows = \App\Services\Cloud\CloudSystemJournalService::parseJournalJson($fixture);
$assert(count($rows) === 2, 'Journal parser must ignore malformed and disallowed units.');
$assert($rows[0]['unit'] === 'virt-cloud-engine.service'
&& $rows[0]['priority'] === 3
&& $rows[0]['timestamp'] === '2026-10-06T08:01:37.115460Z',
'Journal timestamp, unit and priority normalization changed.');
$assert(!str_contains($rows[0]['message'], "\x1b")
&& !str_contains($rows[0]['message'], "\x0a"),
'Journal message control characters must be removed before rendering.');
$assert($rows[1]['priority'] === 6 && $rows[1]['level'] === '信息',
'Out-of-range journal priorities must normalize to informational logs.');
$routes = file_get_contents($root . '/config/routes.php');
$apiRoutes = file_get_contents($root . '/config/routes_cloud.php');
$middleware = file_get_contents($root . '/app/Middleware/CloudApi.php');
$pageController = file_get_contents($root . '/app/Controllers/Admin/CloudPageController.php');
$apiController = file_get_contents($root . '/app/Controllers/CloudAdminSystemLogsController.php');
$service = file_get_contents($root . '/app/Services/Cloud/CloudSystemJournalService.php');
$template = file_get_contents($root . '/resources/views/venix/admin/cloud/system_logs.tpl');
$header = file_get_contents($root . '/resources/views/venix/admin/static_header.tpl');
$browser = file_get_contents($root . '/public/cloud/admin-system-logs.js');
$assert(substr_count($routes,
"get('/system-logs', 'App\\Controllers\\Admin\\CloudPageController:systemLogs')") === 1,
'Administrator system log page route is missing or duplicated.');
$assert(substr_count($apiRoutes,
"get('/admin/system-logs', 'App\\Controllers\\CloudAdminSystemLogsController:index')") === 1
&& str_contains($middleware, '/api/v1/admin/system-logs'),
'System journal API route or its API middleware boundary is missing.');
$assert(str_contains($pageController, 'public function systemLogs(')
&& str_contains($pageController, "CloudAccess::actor(false)")
&& str_contains($pageController, "system_logs_bootstrap_json")
&& str_contains($pageController, "admin/cloud/system_logs.tpl"),
'The page controller must authorize a local administrator and bootstrap safe data.');
$assert(str_contains($apiController, 'CloudSystemJournalService::page(')
&& str_contains($apiController, "getAttribute('cloud_actor')"),
'The API controller must use the journal service and authenticated cloud actor.');
$assert(str_contains($service, "'/usr/bin/journalctl'")
&& str_contains($service, "'--output=json'")
&& str_contains($service, "'bypass_shell' => true")
&& str_contains($service, 'private static function authorize('),
'Journal reads must use fixed journalctl JSON arguments without a shell.');
$assert(str_contains($service, 'private const UNITS = [')
&& str_contains($service, 'virt-cloud-engine.service')
&& str_contains($service, 'MAX_FETCH_LINES')
&& str_contains($service, 'MAX_MESSAGE_LENGTH')
&& str_contains($service, 'MAX_CURSOR_LENGTH'),
'Journal service must keep a fixed unit allowlist and bounded output.');
$assert(!str_contains($service, 'shell_exec(')
&& !str_contains($service, 'exec(')
&& !str_contains($service, 'system(')
&& !str_contains($service, 'popen('),
'Journal service must not execute user-controlled shell commands.');
$assert(str_contains($template, 'system_logs_bootstrap_json|default:\'null\' nofilter')
&& str_contains($pageController, 'JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT'),
'The template must only render the controller-produced, JSON-hex-escaped bootstrap.');
$assert(str_contains($header, 'href="/admin/system-logs"')
&& str_contains($header, "admin_active|default:'' == 'system-logs'"),
'Administrator navigation must expose the system log page with active state.');
$assert(!str_contains($browser, 'innerHTML')
&& str_contains($browser, "credentials: 'same-origin'")
&& str_contains($browser, 'AbortController')
&& str_contains($browser, '/api/v1/admin/system-logs'),
'Browser log rendering must remain text-only, same-origin and cancellable.');
echo "Administrator system journal route, parser, authorization and safety contracts passed.\n";