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.
148 lines
8.6 KiB
PHP
148 lines
8.6 KiB
PHP
<?php
|
|
|
|
// Static contract test: exercising the proxy itself would require a live,
|
|
// HMAC-authenticated local update helper. These assertions keep the browser/PHP
|
|
// security boundary reviewable without application boot or network access.
|
|
$root = dirname(__DIR__);
|
|
$read = static function (string $relative) use ($root): string {
|
|
$contents = file_get_contents($root . '/' . $relative);
|
|
if ($contents === false) {
|
|
throw new RuntimeException('Cannot read ' . $relative);
|
|
}
|
|
return $contents;
|
|
};
|
|
$assert = static function ($condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
|
|
$routes = $read('config/routes.php');
|
|
$apiRoutes = $read('config/routes_cloud.php');
|
|
$controller = $read('app/Controllers/AdminUpdateController.php');
|
|
$helperClient = $read('app/Services/Cloud/CloudUpdateHelperClient.php');
|
|
$header = $read('resources/views/venix/admin/static_header.tpl');
|
|
$template = $read('resources/views/venix/admin/update.tpl');
|
|
$script = $read('public/cloud/update.js');
|
|
|
|
$assert(str_contains(
|
|
$routes,
|
|
"\$this->get('/update', 'App\\Controllers\\Admin\\CloudPageController:update')->add(new SuperAdmin());"
|
|
), 'The update page is not protected by the SuperAdmin middleware.');
|
|
$assert(str_contains($header, "{if \$admin_can_manage|default:false}<li><a")
|
|
&& str_contains($header, 'href="/admin/update"'),
|
|
'The Venix update navigation is not limited by admin_can_manage.');
|
|
|
|
foreach ([
|
|
"['GET', 'PUT'], '/admin/update/settings'",
|
|
"post('/admin/update/check'",
|
|
"post('/admin/update/apply'",
|
|
"get('/admin/update/jobs/{id:[A-Za-z0-9_-]{16,64}}'",
|
|
] as $routeContract) {
|
|
$assert(str_contains($apiRoutes, $routeContract), 'Missing update API route: ' . $routeContract);
|
|
}
|
|
$assert(substr_count($controller, '$actor = $this->actor($request);') === 4,
|
|
'Every update controller action must resolve the managed actor.');
|
|
$assert(str_contains($controller, 'CloudAccess::manage($actor);'),
|
|
'Update API actions do not force CloudAccess::manage.');
|
|
$assert(str_contains($controller, "'GET', '/v1/settings'")
|
|
&& str_contains($controller, "'POST', '/v1/check'")
|
|
&& str_contains($controller, "'POST', '/v1/apply'")
|
|
&& str_contains($controller, "'GET', '/v1/jobs/' . rawurlencode(\$jobId)"),
|
|
'The PHP proxy does not use fixed local-helper update paths.');
|
|
$assert(str_contains($controller, "CloudInput::only(\$body, ['access_token', 'clear_token']);")
|
|
&& str_contains($controller, "['expected_current_commit', 'expected_remote_commit']"),
|
|
'The update proxy request allowlists changed.');
|
|
$assert(str_contains($controller, 'new CloudUpdateHelperClient()')
|
|
&& str_contains($controller, '), 202);'),
|
|
'Update requests must use the local helper and apply must return HTTP 202.');
|
|
$assert(str_contains($controller, "['access_token', 'gitea_access_token']")
|
|
&& str_contains($controller, "withHeader('Cache-Control', 'no-store')"),
|
|
'Update responses are not defensively stripped/no-store.');
|
|
$assert(str_contains($controller, "getUri()->getScheme()) === 'https'")
|
|
&& str_contains($controller, "getServerParam('REMOTE_ADDR', '')")
|
|
&& str_contains($controller, "getUri()->getHost()")
|
|
&& str_contains($controller, "['localhost', '127.0.0.1', '::1']")
|
|
&& str_contains($controller, "['127.0.0.1', '::1']")
|
|
&& !str_contains($controller, 'X-Forwarded-Proto'),
|
|
'The PHP proxy may accept a repository token over an untrusted transport.');
|
|
|
|
$assert(str_contains($helperClient, "private const SOCKET_PATH = '/run/virt-cloud/update.sock'")
|
|
&& str_contains($helperClient, 'CURLOPT_UNIX_SOCKET_PATH')
|
|
&& str_contains($helperClient, 'CloudAgentClient::headers(')
|
|
&& str_contains($helperClient, "'allow_redirects' => false")
|
|
&& !str_contains($helperClient, 'CloudEngineClient'),
|
|
'The updater must use only the authenticated fixed Unix socket.');
|
|
|
|
$assert(str_contains($template, 'id="update-repository"')
|
|
&& str_contains($template, 'id="update-branch"')
|
|
&& !str_contains($template, 'https://g.luoli.live/root/virt.git'),
|
|
'The fixed repository and branch must be rendered from the API response.');
|
|
$assert(preg_match('/<input id="update-access-token"[^>]*type="password"[^>]*>/s', $template, $tokenInput) === 1
|
|
&& !str_contains($tokenInput[0], ' value='),
|
|
'The access token field must be an empty password input.');
|
|
$assert(str_contains($template, 'id="update-clear-token"')
|
|
&& str_contains($template, '/cloud/update.js?v={$venix_asset_version}')
|
|
&& !str_contains($template, '/cloud/admin.js'),
|
|
'The page needs explicit token clearing and its independent update script.');
|
|
$assert(str_contains($template, 'web/composer.json')
|
|
&& str_contains($template, 'Go module')
|
|
&& str_contains($template, 'deploy/**')
|
|
&& str_contains($template, '不会安装 Composer 或 Go 依赖')
|
|
&& str_contains($template, '不会改写 systemd unit')
|
|
&& !str_contains($template, 'Python'),
|
|
'The page must describe the code-only update boundary and all rejected paths.');
|
|
$assert(str_contains($template, '<code>blocked</code>')
|
|
&& str_contains($template, '维护锁会保持启用')
|
|
&& str_contains($template, '仅回退源码不会解除维护锁')
|
|
&& str_contains($template, '必须通过 SSH 完成核对并验证服务后再协调状态'),
|
|
'Blocked updates must direct the operator to SSH recovery without unlocking mutations.');
|
|
|
|
foreach (['innerHTML', 'insertAdjacentHTML', 'localStorage', 'sessionStorage', 'document.cookie'] as $unsafeBrowserApi) {
|
|
$assert(!str_contains($script, $unsafeBrowserApi),
|
|
'Update UI must not use unsafe rendering or durable browser storage: ' . $unsafeBrowserApi);
|
|
}
|
|
$assert(str_contains($script, '.textContent =')
|
|
&& str_contains($script, "headers['X-Admin-CSRF-Token'] = csrfToken")
|
|
&& str_contains($script, "credentials: 'same-origin'"),
|
|
'Safe rendering, CSRF, or same-origin credentials are missing.');
|
|
$assert(str_contains($script, "const body = clearToken ? { clear_token: true } : { access_token: token };")
|
|
&& str_contains($script, "tokenInput.value = ''"),
|
|
'Blank-token preservation, explicit clearing, or credential cleanup changed.');
|
|
$assert(str_contains($script, "window.location.protocol === 'https:'")
|
|
&& str_contains($script, "['localhost', '127.0.0.1', '::1', '[::1]']"),
|
|
'The browser may send a repository token over an insecure transport.');
|
|
$assert(str_contains($script, 'state.checkedCommits = Object.freeze({')
|
|
&& str_contains($script, 'expected_current_commit: result.current_commit')
|
|
&& str_contains($script, 'expected_remote_commit: result.remote_commit')
|
|
&& str_contains($script, "state.updateAvailable = result.status === 'update_available'")
|
|
&& str_contains($script, 'state.checkedCommits === null || !state.updateAvailable')
|
|
&& str_contains($script, 'state.maintenanceBlocked || !state.updateAvailable'),
|
|
'Check results do not pin both commits for apply.');
|
|
$assert(str_contains($script, 'settings.token_configured === true && settings.checkout_ready === true')
|
|
&& str_contains($script, 'window.confirm(')
|
|
&& str_contains($script, '仅快进应用代码')
|
|
&& str_contains($script, '不会安装 Composer 或 Go 依赖,也不会修改 systemd 配置')
|
|
&& str_contains($script, '依赖或部署文件变更必须通过 SSH 升级')
|
|
&& str_contains($script, '执行全新数据库结构校验'),
|
|
'Update readiness or the destructive-impact confirmation changed.');
|
|
$assert(substr_count($script, "api('/apply', { method: 'POST', body: pinned })") === 1
|
|
&& str_contains($script, "api(`/jobs/\${encodeURIComponent(jobId)}`, { method: 'GET' })")
|
|
&& str_contains($script, 'transientFailures > 30')
|
|
&& str_contains($script, 'recoverJobAfterApply')
|
|
&& str_contains($script, '正在只读恢复任务')
|
|
&& str_contains($script, 'job.rollback_ref')
|
|
&& str_contains($script, '人工恢复回滚引用')
|
|
&& str_contains($script, '请勿重复提交'),
|
|
'Apply must be single-shot while job polling survives service restarts.');
|
|
$assert(str_contains($script, "new Set(['succeeded', 'failed', 'blocked'])")
|
|
&& str_contains($script, 'state.maintenanceBlocked = true')
|
|
&& str_contains($script, "job.status === 'blocked'")
|
|
&& str_contains($script, '代码或数据库状态尚未确认')
|
|
&& str_contains($script, '若任务在全新数据库结构校验期间中断')
|
|
&& str_contains($script, '仅回退源码不会解除维护锁')
|
|
&& str_contains($script, 'busy || state.maintenanceBlocked'),
|
|
'Blocked is a polling terminal state but must keep every mutation control locked.');
|
|
|
|
echo "Admin online-update page, proxy, and browser contracts passed.\n";
|