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.
96 lines
5.2 KiB
PHTML
96 lines
5.2 KiB
PHTML
|
2 months ago
|
<?php
|
||
|
|
|
||
|
|
// Static performance contract for the SSH-key page. The authenticated HTML
|
||
|
|
// response owns the initial key list and CSRF bootstrap; JavaScript only
|
||
|
|
// hydrates interactions and performs explicit create/delete mutations.
|
||
|
|
$root = dirname(__DIR__);
|
||
|
|
$assert = static function ($condition, string $message): void {
|
||
|
|
if (!$condition) {
|
||
|
|
throw new RuntimeException($message);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
$section = static function (string $source, string $start, string $end): string {
|
||
|
|
$offset = strpos($source, $start);
|
||
|
|
if ($offset === false) {
|
||
|
|
return '';
|
||
|
|
}
|
||
|
|
$limit = strpos($source, $end, $offset + strlen($start));
|
||
|
|
return $limit === false ? substr($source, $offset)
|
||
|
|
: substr($source, $offset, $limit - $offset);
|
||
|
|
};
|
||
|
|
|
||
|
|
$controller = file_get_contents($root . '/app/Controllers/CloudPageController.php');
|
||
|
|
$service = file_get_contents($root . '/app/Services/Cloud/CloudDeploymentPageService.php');
|
||
|
|
$apiController = file_get_contents($root . '/app/Controllers/CloudApiController.php');
|
||
|
|
$template = file_get_contents($root . '/resources/views/venix/cloud/pages/keys.tpl');
|
||
|
|
$module = file_get_contents($root . '/public/cloud/ssh-keys.js');
|
||
|
|
$middleware = file_get_contents($root . '/app/Middleware/CloudApi.php');
|
||
|
|
|
||
|
|
$pageMethod = $section($controller, 'public function sshKeys(',
|
||
|
|
'public function invoices(');
|
||
|
|
$keyOptions = $section($controller, 'private function sshKeyOptions(',
|
||
|
|
'private function reinstallVm(');
|
||
|
|
$keyService = $section($service, 'public static function sshKeys(',
|
||
|
|
'public static function templateCatalogForPage(');
|
||
|
|
$keyApi = $section($apiController, 'public function sshKeys(',
|
||
|
|
'public function deleteKey(');
|
||
|
|
$initialize = $section($module, 'export function initialize(', 'export ');
|
||
|
|
if ($initialize === '') {
|
||
|
|
$initialize = $section($module, 'export async function initialize(', 'export ');
|
||
|
|
}
|
||
|
|
|
||
|
|
$assert(str_contains($pageMethod, 'CloudAccess::actor(false)')
|
||
|
|
&& str_contains($pageMethod, "getAttribute('auth_user')")
|
||
|
|
&& (str_contains($pageMethod, 'CloudDeploymentPageService::sshKeys(')
|
||
|
|
|| str_contains($pageMethod, '$this->sshKeyOptions('))
|
||
|
|
&& str_contains($pageMethod, "'user' => \$this->userBootstrap(\$user, \$actor)")
|
||
|
|
&& str_contains($pageMethod, "'ssh_keys' => \$keys")
|
||
|
|
&& str_contains($pageMethod, "'cloud_ssh_keys' => \$keys")
|
||
|
|
&& str_contains($pageMethod, "'cloud_ssh_keys_bootstrap_json' => \$this->safeJson(\$payload)"),
|
||
|
|
'SSH-key controller must embed an owner-scoped first-paint list and session bootstrap.');
|
||
|
|
$assert(str_contains($controller, 'JSON_HEX_TAG')
|
||
|
|
&& str_contains($controller, "'csrf_token' => AdminCsrf::token()")
|
||
|
|
&& str_contains($controller, "withHeader('Cache-Control', 'private, no-store')"),
|
||
|
|
'SSH-key HTML bootstrap must be script-safe, authenticated and non-cacheable.');
|
||
|
|
$assert(str_contains($keyService, "CloudSSHKey::where('user_id', \$userId)")
|
||
|
|
&& str_contains($keyService, "'id', 'name', 'fingerprint', 'created_at'")
|
||
|
|
&& !str_contains($keyService, "'public_key'")
|
||
|
|
&& !str_contains($keyOptions, "'public_key'"),
|
||
|
|
'The server-rendered key list must be owner scoped and omit key material.');
|
||
|
|
|
||
|
|
$assert(str_contains($template, 'id="ssh-keys-bootstrap" type="application/json"')
|
||
|
|
&& str_contains($template, '{$cloud_ssh_keys_bootstrap_json nofilter}')
|
||
|
|
&& str_contains($template, '{foreach $cloud_ssh_keys as $key}')
|
||
|
|
&& str_contains($template, '{$key.name|escape:"html"}')
|
||
|
|
&& str_contains($template, '{$key.fingerprint|escape:"html"}')
|
||
|
|
&& str_contains($template, '还没有公钥。')
|
||
|
|
&& !str_contains($template, '正在加载')
|
||
|
|
&& !str_contains($template, '$key.public_key'),
|
||
|
|
'SSH-key HTML must render the initial owner-scoped rows instead of a loading shell.');
|
||
|
|
|
||
|
|
$assert(!str_contains($module, '/cloud/runtime.js')
|
||
|
|
&& !str_contains($module, '/api/v1/session')
|
||
|
|
&& !str_contains($module, 'initializeSession')
|
||
|
|
&& str_contains($module, 'ssh-keys-bootstrap')
|
||
|
|
&& (str_contains($module, 'function hydrate(')
|
||
|
|
|| str_contains($module, 'function readBootstrap(')),
|
||
|
|
'SSH-key interactions must hydrate the embedded payload without the all-feature runtime or session API.');
|
||
|
|
$assert($initialize !== ''
|
||
|
|
&& !preg_match("/api\\(\\s*['\"]\\/api\\/v1\\/ssh-keys['\"]\\s*\\)/", $module)
|
||
|
|
&& !preg_match("/fetch\\(\\s*['\"]\\/api\\/v1\\/ssh-keys['\"]\\s*\\)/", $module),
|
||
|
|
'SSH-key first paint must not issue an unqualified collection GET after hydration.');
|
||
|
|
$assert(str_contains($module, '/api/v1/ssh-keys')
|
||
|
|
&& str_contains($module, "method: 'POST'")
|
||
|
|
&& str_contains($module, "method: 'DELETE'"),
|
||
|
|
'SSH-key JavaScript must retain explicit create and delete mutations.');
|
||
|
|
$assert(str_contains($keyApi,
|
||
|
|
"['id' => (int) \$key->id, 'name' => \$key->name, 'fingerprint' => \$key->fingerprint]")
|
||
|
|
&& !str_contains($keyApi, "'public_key' => \$key->public_key"),
|
||
|
|
'SSH-key create API must return the safe fields required for local list hydration.');
|
||
|
|
|
||
|
|
$assert(str_contains($middleware, 'CloudAccess::actor(!$databaseOnly)')
|
||
|
|
&& str_contains($middleware, '#^/api/v1/ssh-keys(?:/|$)#D'),
|
||
|
|
'SSH-key API calls are database-only and must not create or contact a cloud engine session.');
|
||
|
|
|
||
|
|
echo "SSH-key server-rendered first-page performance contract passed.\n";
|