|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
|
|
|
|
|
|
set_error_handler(static function (int $severity, string $message,
|
|
|
|
|
string $file, int $line): bool {
|
|
|
|
|
if ((error_reporting() & $severity)
|
|
|
|
|
&& ($severity & (E_WARNING | E_USER_WARNING | E_DEPRECATED))) {
|
|
|
|
|
throw new ErrorException($message, 0, $severity, $file, $line);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$now = time();
|
|
|
|
|
$network = [
|
|
|
|
|
'id' => 31, 'vpc_key' => str_repeat('a', 32), 'name' => '生产私网',
|
|
|
|
|
'user_id' => 42, 'owner_name' => '演示客户',
|
|
|
|
|
'owner_email' => 'd***@example.test',
|
|
|
|
|
'owner_url' => '/admin/user/42/edit', 'region_id' => 7,
|
|
|
|
|
'region_name' => '香港', 'region_code' => 'hkg',
|
|
|
|
|
'cidr' => '10.64.0.0/24', 'gateway' => '10.64.0.1',
|
|
|
|
|
'desired_revision' => 8, 'state' => 'degraded',
|
|
|
|
|
'raw_state' => 'active',
|
|
|
|
|
'display' => ['label' => '需要处理', 'tone' => 'danger'],
|
|
|
|
|
'port_count' => 3, 'host_summary' => ['total' => 2, 'ready' => 1],
|
|
|
|
|
'active_batch' => ['job_id' => 91], 'updated_at' => $now - 30,
|
|
|
|
|
];
|
|
|
|
|
$page = [
|
|
|
|
|
'overview' => [
|
|
|
|
|
'feature' => ['enabled' => true, 'settings_url' => '/admin/config'],
|
|
|
|
|
'summary' => ['total' => 4, 'active' => 3, 'changing' => 0,
|
|
|
|
|
'degraded' => 1, 'ports' => 9, 'active_batches' => 1],
|
|
|
|
|
'regions' => [[
|
|
|
|
|
'id' => 7, 'code' => 'hkg', 'name' => '香港',
|
|
|
|
|
'country_code' => 'HK', 'status' => 'active',
|
|
|
|
|
'pools_total' => 1, 'pools_active' => 1, 'capacity' => 256,
|
|
|
|
|
'used' => 4, 'available' => 252, 'hosts_total' => 2,
|
|
|
|
|
'hosts_schedulable' => 2, 'hosts_ready' => 2,
|
|
|
|
|
'ready' => true, 'blockers' => [],
|
|
|
|
|
]],
|
|
|
|
|
'attention' => [$network], 'updated_at' => $now,
|
|
|
|
|
],
|
|
|
|
|
'networks' => [
|
|
|
|
|
'items' => [$network],
|
|
|
|
|
'filters' => ['q' => '', 'state' => '', 'region_id' => 0,
|
|
|
|
|
'page' => 1, 'per_page' => 20],
|
|
|
|
|
'pagination' => ['page' => 1, 'per_page' => 20, 'total' => 1,
|
|
|
|
|
'total_pages' => 1, 'from' => 1, 'to' => 1],
|
|
|
|
|
],
|
|
|
|
|
'pools' => [
|
|
|
|
|
'feature_enabled' => true,
|
|
|
|
|
'regions' => [['id' => 7, 'name' => '香港', 'code' => 'hkg']],
|
|
|
|
|
'items' => [[
|
|
|
|
|
'id' => 12, 'pool_key' => str_repeat('b', 32),
|
|
|
|
|
'region_id' => 7, 'region_name' => '香港',
|
|
|
|
|
'name' => '香港私网池', 'cidr' => '10.64.0.0/16',
|
|
|
|
|
'allocation_prefix' => 24, 'state' => 'active',
|
|
|
|
|
'revision' => 2, 'capacity' => 256, 'used' => 4,
|
|
|
|
|
'available' => 252, 'address_locked' => true,
|
|
|
|
|
]],
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$smarty = new Smarty();
|
|
|
|
|
$compileDir = sys_get_temp_dir() . '/admin-vpc-view-'
|
|
|
|
|
. bin2hex(random_bytes(6));
|
|
|
|
|
mkdir($compileDir);
|
|
|
|
|
$smarty->setCompileDir($compileDir);
|
|
|
|
|
$smarty->setTemplateDir(dirname(__DIR__) . '/resources/views/venix');
|
|
|
|
|
$smarty->assign('config', ['appName' => 'Venix Test']);
|
|
|
|
|
$smarty->assign('cdn', '');
|
|
|
|
|
$smarty->assign('user', (object) ['isLogin' => false]);
|
|
|
|
|
$smarty->assign('admin_csrf_token', 'fixture-not-a-real-token');
|
|
|
|
|
|
|
|
|
|
$render = static function (?array $payload, string $error = '',
|
|
|
|
|
bool $canManage = true) use ($smarty): string {
|
|
|
|
|
$smarty->assign('admin_can_manage', $canManage);
|
|
|
|
|
$smarty->assign('admin_vpc_page', $payload);
|
|
|
|
|
$smarty->assign('admin_vpc_error', $error);
|
|
|
|
|
$smarty->assign('admin_vpc_view', 'overview');
|
|
|
|
|
$smarty->assign('admin_vpc_bootstrap_json', json_encode($payload,
|
|
|
|
|
JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
|
|
|
|
|
| JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE));
|
|
|
|
|
return $smarty->fetch('admin/cloud/vpc.tpl');
|
|
|
|
|
};
|
|
|
|
|
$assert = static function (bool $condition, string $message): void {
|
|
|
|
|
if (!$condition) {
|
|
|
|
|
throw new RuntimeException($message);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$html = $render($page);
|
|
|
|
|
if (in_array('--fixture', $argv ?? [], true)) {
|
|
|
|
|
echo $html;
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$dom = new DOMDocument();
|
|
|
|
|
libxml_use_internal_errors(true);
|
|
|
|
|
$dom->loadHTML('<?xml encoding="UTF-8">' . $html);
|
|
|
|
|
$xpath = new DOMXPath($dom);
|
|
|
|
|
$assert($xpath->query('//*[@data-admin-vpc]')->length === 1
|
|
|
|
|
&& $xpath->query('//*[@role="tab"]')->length === 3
|
|
|
|
|
&& $xpath->query('//*[contains(@class,"admin-vpc-summary-card")]')
|
|
|
|
|
->length === 6,
|
|
|
|
|
'The administrator VPC workspace did not render its primary navigation.');
|
|
|
|
|
$assert($xpath->query('//*[@id="admin-vpc-regions"]//article')->length === 1
|
|
|
|
|
&& $xpath->query('//*[@id="admin-vpc-attention-body"]/tr')->length === 1
|
|
|
|
|
&& $xpath->query('//*[@id="admin-vpc-network-body"]/tr')->length === 1,
|
|
|
|
|
'VPC readiness and network data must be useful before JavaScript runs.');
|
|
|
|
|
$assert(str_contains($dom->textContent, '生产私网')
|
|
|
|
|
&& str_contains($dom->textContent, '10.64.0.0/24')
|
|
|
|
|
&& str_contains($dom->textContent, 'VPC 控制面已读取')
|
|
|
|
|
&& $xpath->query('//*[@data-vpc-detail="31"]')->length === 1,
|
|
|
|
|
'The first paint omitted the VPC identity, CIDR or detail entry point.');
|
|
|
|
|
|
|
|
|
|
$readOnly = $render($page, '', false);
|
|
|
|
|
$dom->loadHTML('<?xml encoding="UTF-8">' . $readOnly);
|
|
|
|
|
$xpath = new DOMXPath($dom);
|
|
|
|
|
$assert($xpath->query('//*[@id="admin-vpc-pool-new" and @disabled]')->length
|
|
|
|
|
=== 1
|
|
|
|
|
&& $xpath->query('//*[@id="admin-vpc-pool-form"]//*[@disabled]')->length
|
|
|
|
|
>= 6,
|
|
|
|
|
'Read-only administrators received an enabled address-pool mutation.');
|
|
|
|
|
|
|
|
|
|
$unsafe = $page;
|
|
|
|
|
$unsafe['networks']['items'][0]['name'] =
|
|
|
|
|
'</script><img src=x onerror=alert(1)>';
|
|
|
|
|
$unsafe['overview']['attention'][0]['owner_name'] =
|
|
|
|
|
'<script>window.bad=1</script>';
|
|
|
|
|
$unsafe['overview']['regions'][0]['name'] = '" onmouseover="alert(2)';
|
|
|
|
|
$unsafeHtml = $render($unsafe);
|
|
|
|
|
$dom->loadHTML('<?xml encoding="UTF-8">' . $unsafeHtml);
|
|
|
|
|
$xpath = new DOMXPath($dom);
|
|
|
|
|
$assert($xpath->query('//*[@onerror or @onmouseover]')->length === 0
|
|
|
|
|
&& !str_contains($unsafeHtml, '<script>window.bad=1</script>')
|
|
|
|
|
&& str_contains($dom->textContent,
|
|
|
|
|
'</script><img src=x onerror=alert(1)>'),
|
|
|
|
|
'Untrusted VPC names became executable markup.');
|
|
|
|
|
|
|
|
|
|
$failed = $render(null, 'VPC 控制面暂不可用,请重试');
|
|
|
|
|
$assert(str_contains($failed, 'VPC 控制面暂不可用,请重试')
|
|
|
|
|
&& !str_contains($failed, '生产私网'),
|
|
|
|
|
'An unavailable control plane was disguised as a successful response.');
|
|
|
|
|
|
|
|
|
|
$javascript = file_get_contents(dirname(__DIR__)
|
|
|
|
|
. '/public/cloud/admin-vpc.js');
|
|
|
|
|
$assert(!str_contains($javascript, 'setInterval(')
|
|
|
|
|
&& str_contains($javascript, 'renderDetail(payload)')
|
|
|
|
|
&& str_contains($javascript, 'pool.address_locked')
|
|
|
|
|
&& substr_count($javascript, 'drawerBody.scrollTop = 0') >= 2
|
|
|
|
|
&& str_contains($javascript, 'focusJob?.url'),
|
|
|
|
|
'The browser UI lost its bounded refresh, locked-pool or exact-task contract.');
|
|
|
|
|
|
|
|
|
|
echo "Administrator VPC first paint, permissions and escaping passed.\n";
|