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.
101 lines
3.4 KiB
PHP
101 lines
3.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Services\Cloud\CloudAbuseAlertMessageFormatter;
|
|
use App\Services\Config;
|
|
|
|
$assert = static function (bool $condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
$throws = static function (callable $callback, string $message) use ($assert): void {
|
|
try {
|
|
$callback();
|
|
} catch (InvalidArgumentException) {
|
|
return;
|
|
}
|
|
$assert(false, $message);
|
|
};
|
|
$event = static function (array $overrides = []): object {
|
|
return (object) array_replace([
|
|
'id' => 41,
|
|
'instance_id' => 91,
|
|
'user_id' => 17,
|
|
'host_id' => 3,
|
|
'policy_revision' => 4,
|
|
'rule_code' => 'outbound_tcp_syn_sweep',
|
|
'direction' => 'outbound',
|
|
'ip_family' => 'ipv4',
|
|
'transition_type' => 'opened',
|
|
'from_severity' => 'none',
|
|
'to_severity' => 'high',
|
|
'evidence_json' => json_encode([
|
|
'tcp_syn_flows' => 4321,
|
|
'unique_remote_ips' => 600,
|
|
], JSON_THROW_ON_ERROR),
|
|
'resolution_reason' => '',
|
|
'occurred_at' => 1794182400,
|
|
], $overrides);
|
|
};
|
|
|
|
$formatter = new CloudAbuseAlertMessageFormatter();
|
|
Config::set('baseUrl', 'https://panel.example.test');
|
|
$message = $formatter->format($event());
|
|
$assert(str_contains($message, '网络滥用高风险告警')
|
|
&& str_contains($message, '告警:#41')
|
|
&& str_contains($message, '实例:#91')
|
|
&& str_contains($message, '用户:#17')
|
|
&& str_contains($message, '宿主机:#3')
|
|
&& str_contains($message, '出站可疑行为')
|
|
&& str_contains($message, 'TCP SYN Flow 4,321')
|
|
&& str_contains($message,
|
|
'https://panel.example.test/admin/abuse-monitoring?query=91'),
|
|
'Formatter omitted transition identity, direction or aggregate evidence.');
|
|
$assert(!str_contains($message, '203.0.113.9')
|
|
&& !str_contains($message, '通信正文:')
|
|
&& mb_strlen($message, 'UTF-8') <= 4096,
|
|
'Formatter exposed sensitive data or exceeded Telegram limits.');
|
|
|
|
$resolved = $formatter->format($event([
|
|
'rule_code' => 'inbound_udp_flood',
|
|
'direction' => 'inbound',
|
|
'ip_family' => 'ipv6',
|
|
'transition_type' => 'resolved',
|
|
'from_severity' => 'suspicious',
|
|
'to_severity' => 'none',
|
|
'evidence_json' => '{"udp_active_flows":0,"udp_new_flows":0}',
|
|
'resolution_reason' => 'policy_replaced',
|
|
]));
|
|
$assert(str_contains($resolved, '网络风险已恢复')
|
|
&& str_contains($resolved, '策略更新后关闭')
|
|
&& str_contains($resolved, '入站受攻击'),
|
|
'Resolved policy transition was not rendered correctly.');
|
|
|
|
$throws(
|
|
static fn () => $formatter->format($event([
|
|
'evidence_json' => '{"remote_ip":"203.0.113.9","tcp_syn_flows":5}',
|
|
])),
|
|
'Address-bearing evidence was accepted.'
|
|
);
|
|
$throws(
|
|
static fn () => $formatter->format($event([
|
|
'transition_type' => 'resolved',
|
|
'from_severity' => 'high',
|
|
'to_severity' => 'none',
|
|
'resolution_reason' => 'unknown_reason',
|
|
])),
|
|
'Unknown resolution reason was accepted.'
|
|
);
|
|
$throws(
|
|
static function () use ($formatter, $event): void {
|
|
Config::set('baseUrl', "https://panel.example.test\n.invalid");
|
|
$formatter->format($event());
|
|
},
|
|
'Unsafe administrator base URL was accepted.'
|
|
);
|
|
|
|
echo "Cloud abuse alert message formatter tests passed.\n";
|