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.
149 lines
8.7 KiB
PHTML
149 lines
8.7 KiB
PHTML
|
2 months ago
|
<?php
|
||
|
|
|
||
|
|
// Static contract test: exercising the proxy itself would require a live,
|
||
|
|
// HMAC-authenticated private engine. 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');
|
||
|
|
$engineClient = $read('app/Services/Cloud/CloudEngineClient.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, "private const UPDATE_BASE = '/internal/v1/update';")
|
||
|
|
&& str_contains($controller, "self::UPDATE_BASE . '/settings'")
|
||
|
|
&& str_contains($controller, "self::UPDATE_BASE . '/check'")
|
||
|
|
&& str_contains($controller, "self::UPDATE_BASE . '/apply'")
|
||
|
|
&& str_contains($controller, "self::UPDATE_BASE . '/jobs/' . rawurlencode(\$jobId)"),
|
||
|
|
'The PHP proxy does not use fixed private engine 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 CloudEngineClient(null, 90.0)')
|
||
|
|
&& str_contains($controller, '), 202);'),
|
||
|
|
'Update requests need a Git-aware timeout 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($engineClient, '/internal/v1/update/(?:settings|check|apply|jobs/')
|
||
|
|
&& str_contains($engineClient, "\$result['error'] ?? (\$result['detail'] ?? null)")
|
||
|
|
&& str_contains($engineClient, 'strlen($detail) > 500'),
|
||
|
|
'Only bounded safe update errors should pass through the engine client.');
|
||
|
|
|
||
|
|
$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, 'engine/requirements.txt')
|
||
|
|
&& str_contains($template, 'web/composer.json')
|
||
|
|
&& str_contains($template, 'web/composer.lock')
|
||
|
|
&& str_contains($template, 'deploy/**')
|
||
|
|
&& str_contains($template, '不安装 Composer/Python 依赖')
|
||
|
|
&& str_contains($template, '不准备依赖暂存区')
|
||
|
|
&& str_contains($template, '不改写 systemd unit')
|
||
|
|
&& !str_contains($template, 'PHP 锁定依赖会先隔离准备'),
|
||
|
|
'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, '必须人工完成并验证迁移与服务兼容性'),
|
||
|
|
'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/Python 依赖或修改 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";
|