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.
205 lines
9.8 KiB
PHP
205 lines
9.8 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Services\Cloud\CloudPlatformDefaultNetworkPolicy as Policy;
|
|
|
|
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;
|
|
});
|
|
$assert = static function (bool $condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
$reject = static function (array $manifest, string $message) use ($assert): void {
|
|
try {
|
|
Policy::validate($manifest);
|
|
} catch (RuntimeException $exception) {
|
|
return;
|
|
}
|
|
$assert(false, $message);
|
|
};
|
|
|
|
$manifest = Policy::manifest();
|
|
$expectedTopKeys = ['schema_version', 'policy_version', 'scope', 'applies_to',
|
|
'migrate_existing', 'rules', 'explicitly_unrestricted'];
|
|
$actualTopKeys = array_keys($manifest);
|
|
sort($actualTopKeys, SORT_STRING);
|
|
sort($expectedTopKeys, SORT_STRING);
|
|
$assert($actualTopKeys === $expectedTopKeys,
|
|
'Manifest top-level exact schema changed.');
|
|
$assert($manifest['schema_version'] === 1 && $manifest['policy_version'] === 1
|
|
&& $manifest['scope'] === 'public_ipv4_interface'
|
|
&& $manifest['applies_to'] === 'new_public_cloud_image_instances_only'
|
|
&& $manifest['migrate_existing'] === false,
|
|
'Manifest version or new-public-interface scope changed.');
|
|
|
|
$matrix = [];
|
|
foreach ($manifest['rules'] as $rule) {
|
|
$matrix[$rule['id']] = [
|
|
$rule['category'], $rule['protocols'], $rule['port_from'],
|
|
$rule['port_to'], $rule['directions'],
|
|
];
|
|
}
|
|
$assert($matrix === [
|
|
'qotd' => ['permanent', ['tcp', 'udp'], 17, 17, ['ingress', 'egress']],
|
|
'chargen' => ['permanent', ['tcp', 'udp'], 19, 19, ['ingress', 'egress']],
|
|
'ssdp' => ['permanent', ['tcp', 'udp'], 1900, 1900, ['ingress', 'egress']],
|
|
'memcached' => ['permanent', ['udp'], 11211, 11211, ['ingress', 'egress']],
|
|
'brand-rex-53413' => ['permanent', ['udp'], 53413, 53413, ['ingress', 'egress']],
|
|
'smtp-25' => ['default', ['tcp'], 25, 25, ['egress']],
|
|
'netbios' => ['default', ['tcp', 'udp'], 137, 139, ['ingress', 'egress']],
|
|
'smb' => ['default', ['tcp', 'udp'], 445, 445, ['ingress', 'egress']],
|
|
'kms-1688' => ['default', ['tcp'], 1688, 1688, ['ingress']],
|
|
], 'The authoritative restricted-port matrix changed.');
|
|
$assert($manifest['explicitly_unrestricted'] === [[
|
|
'protocol' => 'tcp', 'port' => 587,
|
|
'reason' => 'SMTP 提交端口当前不受平台默认策略限制。',
|
|
]], 'TCP 587 must remain explicit unrestricted metadata.');
|
|
|
|
$malformed = $manifest;
|
|
$malformed['unexpected'] = true;
|
|
$reject($malformed, 'Unknown manifest fields were accepted.');
|
|
$malformed = $manifest;
|
|
$malformed['rules'][0]['protocols'][] = 'icmp';
|
|
$reject($malformed, 'Unknown protocols were accepted.');
|
|
$malformed = $manifest;
|
|
$malformed['rules'][0]['directions'] = ['ingress', 'ingress'];
|
|
$reject($malformed, 'Duplicate directions were accepted.');
|
|
$malformed = $manifest;
|
|
$malformed['rules'][] = array_replace($manifest['rules'][0], [
|
|
'id' => 'duplicate-qotd',
|
|
]);
|
|
$reject($malformed, 'Duplicate protocol/port/direction rules were accepted.');
|
|
$malformed = $manifest;
|
|
$malformed['rules'] = [];
|
|
for ($index = 0; $index < 18; $index++) {
|
|
$malformed['rules'][] = [
|
|
'id' => 'expanded-' . $index,
|
|
'label' => 'Expanded ' . $index,
|
|
'category' => $index === 0 ? 'permanent' : 'default',
|
|
'protocols' => ['tcp', 'udp'],
|
|
'port_from' => 1000 + $index,
|
|
'port_to' => 1000 + $index,
|
|
'directions' => ['ingress', 'egress'],
|
|
'reason' => 'Expanded rule limit fixture.',
|
|
];
|
|
}
|
|
$reject($malformed, 'More than 70 expanded platform rules were accepted.');
|
|
$malformed = $manifest;
|
|
$malformed['explicitly_unrestricted'][0]['port'] = 25;
|
|
$reject($malformed, 'Unrestricted metadata was allowed to conflict with a rule.');
|
|
$malformed = $manifest;
|
|
$malformed['migrate_existing'] = true;
|
|
$reject($malformed, 'Existing-instance migration was accepted.');
|
|
|
|
$page = Policy::page();
|
|
$assert(count($page['permanent_rules']) === 5
|
|
&& count($page['default_rules']) === 4
|
|
&& count($page['explicitly_unrestricted']) === 1,
|
|
'Administrator policy projection lost a category.');
|
|
$smtp = array_values(array_filter($page['default_rules'],
|
|
static fn (array $rule): bool => $rule['id'] === 'smtp-25'))[0] ?? null;
|
|
$kms = array_values(array_filter($page['default_rules'],
|
|
static fn (array $rule): bool => $rule['id'] === 'kms-1688'))[0] ?? null;
|
|
$assert(($smtp['direction_label'] ?? null) === '仅公网出站'
|
|
&& ($kms['direction_label'] ?? null) === '仅公网入站',
|
|
'One-way restrictions were mislabeled.');
|
|
|
|
$root = dirname(__DIR__);
|
|
$routes = file_get_contents($root . '/config/routes.php');
|
|
$apiRoutes = file_get_contents($root . '/config/routes_cloud.php');
|
|
$controller = file_get_contents(
|
|
$root . '/app/Controllers/Admin/CloudPageController.php');
|
|
$header = file_get_contents(
|
|
$root . '/resources/views/venix/admin/static_header.tpl');
|
|
$userSidebar = file_get_contents(
|
|
$root . '/resources/views/venix/user/panel_sidebar.tpl');
|
|
$assert(substr_count($routes,
|
|
"get('/platform-default-policy', 'App\\Controllers\\Admin\\CloudPageController:platformDefaultPolicy')") === 1
|
|
&& str_contains($routes, '})->add(new Admin());'),
|
|
'Administrator GET page route or Admin middleware is missing.');
|
|
$assert(!str_contains($routes, "post('/platform-default-policy'")
|
|
&& !str_contains($routes, "put('/platform-default-policy'")
|
|
&& !str_contains($routes, "delete('/platform-default-policy'")
|
|
&& !str_contains($apiRoutes, 'platform-default-policy'),
|
|
'The read-only page gained a write or API route.');
|
|
$assert(str_contains($controller,
|
|
'public function platformDefaultPolicy(')
|
|
&& str_contains($controller, 'CloudPlatformDefaultNetworkPolicy::page()')
|
|
&& str_contains($controller, 'CloudAccess::actor(false)')
|
|
&& str_contains($controller, "Cache-Control', 'private, no-store"),
|
|
'The page controller lost strict local policy loading or private admin authorization.');
|
|
$assert(str_contains($header, 'href="/admin/platform-default-policy"')
|
|
&& str_contains($header,
|
|
"\$admin_active|default:'' == 'platform-default-policy'"),
|
|
'The audit sidebar link or active state is missing.');
|
|
$assert(!str_contains($userSidebar, '/admin/platform-default-policy')
|
|
&& !str_contains($userSidebar, '平台默认策略'),
|
|
'The administrator-only policy leaked into customer navigation.');
|
|
|
|
$smarty = new Smarty();
|
|
$compileDir = sys_get_temp_dir() . '/admin-platform-policy-view-'
|
|
. bin2hex(random_bytes(6));
|
|
mkdir($compileDir);
|
|
$smarty->setCompileDir($compileDir);
|
|
$smarty->setTemplateDir($root . '/resources/views/venix');
|
|
$smarty->assign('config', ['appName' => 'Venix Test']);
|
|
$smarty->assign('cdn', '');
|
|
$smarty->assign('user', (object) ['isLogin' => false]);
|
|
$smarty->assign('admin_can_manage', false);
|
|
$smarty->assign('admin_csrf_token', 'fixture-not-a-real-token');
|
|
$render = static function (?array $value, string $error = '') use ($smarty): string {
|
|
$smarty->assign('platform_policy', $value);
|
|
$smarty->assign('platform_policy_error', $error);
|
|
return $smarty->fetch('admin/cloud/platform_default_policy.tpl');
|
|
};
|
|
$html = $render($page);
|
|
$dom = new DOMDocument();
|
|
libxml_use_internal_errors(true);
|
|
$dom->loadHTML('<?xml encoding="UTF-8">' . $html);
|
|
$xpath = new DOMXPath($dom);
|
|
$assert($xpath->query('//tbody[@id="platform-policy-permanent-body"]/tr')->length === 5
|
|
&& $xpath->query('//tbody[@id="platform-policy-default-body"]/tr')->length === 4
|
|
&& $xpath->query('//tbody[@id="platform-policy-unrestricted-body"]/tr')->length === 1,
|
|
'SSR did not render all policy categories.');
|
|
$restrictedText = '';
|
|
foreach ($xpath->query('//tbody[@id="platform-policy-permanent-body" or @id="platform-policy-default-body"]') as $body) {
|
|
$restrictedText .= ' ' . $body->textContent;
|
|
}
|
|
$assert(!str_contains($restrictedText, '587')
|
|
&& str_contains($xpath->query('//tbody[@id="platform-policy-unrestricted-body"]')->item(0)->textContent, '587'),
|
|
'TCP 587 appeared in the restricted tables or disappeared from metadata.');
|
|
$assert($xpath->query('//main[@data-admin-platform-default-policy]//form
|
|
| //main[@data-admin-platform-default-policy]//button
|
|
| //main[@data-admin-platform-default-policy]//script')->length === 0,
|
|
'The administrator policy page must remain pure read-only SSR.');
|
|
$assert($xpath->query('//a[@href="/admin/platform-default-policy" and contains(concat(" ", normalize-space(@class), " "), " active ")]')->length === 1,
|
|
'The audit navigation does not identify the active policy page.');
|
|
$assert(str_contains($dom->textContent, '不迁移')
|
|
&& str_contains($dom->textContent, '拒绝 IPv6 以防绕过 IPv4 端口基线')
|
|
&& str_contains($dom->textContent, '例外申请、审核和解除逻辑尚未实现')
|
|
&& str_contains($dom->textContent, '不统计宿主机或实例的实时执行状态'),
|
|
'The page overclaims migration, exception handling, or observed enforcement.');
|
|
|
|
$unsafe = $page;
|
|
$unsafe['permanent_rules'][0]['label'] = '</script><img src=x onerror=alert(1)>';
|
|
$unsafeHtml = $render($unsafe);
|
|
$dom->loadHTML('<?xml encoding="UTF-8">' . $unsafeHtml);
|
|
$xpath = new DOMXPath($dom);
|
|
$assert($xpath->query('//*[@onerror]')->length === 0
|
|
&& str_contains($dom->textContent, '</script><img src=x onerror=alert(1)>'),
|
|
'Manifest text was rendered as executable markup.');
|
|
$failed = $render(null, '策略清单暂时无法读取,请检查部署文件。');
|
|
$assert(str_contains($failed, '策略清单暂时无法读取,请检查部署文件。')
|
|
&& !str_contains($failed, '平台永久限制规则'),
|
|
'An unavailable manifest was disguised as an empty successful policy.');
|
|
|
|
echo "Administrator platform default network policy manifest, validation, route and SSR contracts passed.\n";
|