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.
virt/web/tests/CloudAdminAbuseAlertHistory...

292 lines
9.7 KiB
PHP

<?php
declare(strict_types=1);
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudAdminAbuseAlertHistoryService;
use App\Services\Cloud\CloudError;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$capsule = new Capsule();
$capsule->addConnection([
'driver' => 'sqlite', 'database' => ':memory:',
], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$schema = $capsule->schema('default');
$schema->create('user', static function (Blueprint $table): void {
$table->increments('id');
$table->string('user_name');
});
$schema->create('cloud_hosts', static function (Blueprint $table): void {
$table->increments('id');
$table->string('name');
});
$schema->create('cloud_instances', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('user_id');
$table->integer('host_id');
$table->string('name');
});
$schema->create('cloud_network_findings',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('instance_id');
$table->integer('policy_revision');
$table->string('rule_code');
$table->string('direction');
$table->string('ip_family');
$table->string('severity');
$table->string('state');
});
$schema->create('cloud_network_alert_events',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('finding_id');
$table->integer('transition_revision');
$table->string('transition_type');
$table->string('from_severity');
$table->string('to_severity');
$table->text('evidence_json');
$table->string('resolution_reason');
$table->integer('occurred_at');
$table->integer('created_at');
});
$schema->create('cloud_network_alert_deliveries',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('event_id')->unique();
$table->string('binding_generation')->nullable();
$table->integer('bound_admin_id')->nullable();
$table->string('chat_id')->nullable();
$table->integer('telegram_message_id')->nullable();
$table->string('state');
$table->text('message_text');
$table->string('content_hash');
$table->string('error_code')->default('');
$table->string('skip_reason')->default('');
$table->integer('dispatch_started_at')->nullable();
$table->integer('sent_at')->nullable();
$table->integer('terminal_at')->nullable();
});
$db = $capsule->connection('default');
$db->table('user')->insert([
['id' => 10, 'user_name' => 'owner-a'],
['id' => 11, 'user_name' => 'owner-b'],
]);
$db->table('cloud_hosts')->insert([
['id' => 3, 'name' => 'host-a'],
['id' => 4, 'name' => 'host-b'],
]);
$db->table('cloud_instances')->insert([
['id' => 41, 'user_id' => 10, 'host_id' => 3, 'name' => 'vm-a'],
['id' => 42, 'user_id' => 11, 'host_id' => 4, 'name' => 'vm-b'],
]);
$db->table('cloud_network_findings')->insert([
[
'id' => 1,
'instance_id' => 41,
'policy_revision' => 1,
'rule_code' => 'outbound_tcp_syn_sweep',
'direction' => 'outbound',
'ip_family' => 'ipv4',
'severity' => 'high',
'state' => 'open',
],
[
'id' => 2,
'instance_id' => 42,
'policy_revision' => 1,
'rule_code' => 'outbound_udp_fanout',
'direction' => 'outbound',
'ip_family' => 'ipv4',
'severity' => 'suspicious',
'state' => 'open',
],
[
'id' => 3,
'instance_id' => 41,
'policy_revision' => 1,
'rule_code' => 'inbound_tcp_syn_attack',
'direction' => 'inbound',
'ip_family' => 'ipv6',
'severity' => 'high',
'state' => 'open',
],
]);
$db->table('cloud_network_alert_events')->insert([
[
'id' => 1,
'finding_id' => 1,
'transition_revision' => 1,
'transition_type' => 'opened',
'from_severity' => 'none',
'to_severity' => 'high',
'evidence_json' => json_encode([
'tcp_syn_flows' => 900,
'unique_remote_ips' => 140,
'remote_ip' => '203.0.113.5',
'nested' => ['token' => 'must-not-leak'],
], JSON_THROW_ON_ERROR),
'resolution_reason' => '',
'occurred_at' => 120,
'created_at' => 130,
],
[
'id' => 2,
'finding_id' => 2,
'transition_revision' => 1,
'transition_type' => 'opened',
'from_severity' => 'none',
'to_severity' => 'suspicious',
'evidence_json' => '{"udp_new_flows":45}',
'resolution_reason' => '',
'occurred_at' => 180,
'created_at' => 190,
],
[
'id' => 3,
'finding_id' => 3,
'transition_revision' => 2,
'transition_type' => 'escalated',
'from_severity' => 'suspicious',
'to_severity' => 'high',
'evidence_json' => '{"tcp_unreplied_flows":77}',
'resolution_reason' => '',
'occurred_at' => 240,
'created_at' => 250,
],
]);
$secretMessage = 'secret telegram message body';
$db->table('cloud_network_alert_deliveries')->insert([
[
'id' => 1,
'event_id' => 2,
'binding_generation' => str_repeat('b', 32),
'bound_admin_id' => 77,
'chat_id' => '9988776655',
'telegram_message_id' => 123,
'state' => 'sent',
'message_text' => $secretMessage,
'content_hash' => str_repeat('c', 64),
'error_code' => '',
'skip_reason' => '',
'dispatch_started_at' => 191,
'sent_at' => 192,
'terminal_at' => 192,
],
[
'id' => 2,
'event_id' => 3,
'binding_generation' => str_repeat('d', 32),
'bound_admin_id' => 77,
'chat_id' => '1122334455',
'telegram_message_id' => null,
'state' => 'unknown',
'message_text' => 'another private message',
'content_hash' => str_repeat('e', 64),
'error_code' => 'remote_effect_unknown',
'skip_reason' => '',
'dispatch_started_at' => 251,
'sent_at' => null,
'terminal_at' => 252,
],
]);
$service = new CloudAdminAbuseAlertHistoryService();
$admin = ['user_id' => 8, 'is_admin' => true, 'can_manage' => false];
$page = $service->page($admin, ['per_page' => '2']);
$encoded = json_encode($page, JSON_THROW_ON_ERROR);
$assert($page['summary']['queued'] === 1
&& $page['summary']['sent'] === 1
&& $page['summary']['unknown'] === 1
&& $page['summary']['total'] === 3
&& $page['pagination']['total'] === 3
&& count($page['items']) === 2,
'Alert history summary or pagination lost delivery state.');
$assert(!str_contains($encoded, '9988776655')
&& !str_contains($encoded, $secretMessage)
&& !str_contains($encoded, str_repeat('c', 64))
&& !str_contains($encoded, '203.0.113.5')
&& !str_contains($encoded, 'must-not-leak')
&& !str_contains($encoded, 'binding_generation')
&& !str_contains($encoded, 'content_hash')
&& !str_contains($encoded, 'evidence_json'),
'History DTO leaked destination, frozen content or raw evidence.');
$assert(($page['items'][0]['delivery']['status'] ?? '') === 'unknown'
&& ($page['items'][0]['delivery']['automatic_replay_allowed'] ?? true)
=== false
&& str_contains(
(string) $page['items'][0]['delivery']['detail'],
'禁止自动重放'
), 'Unknown delivery lost its no-replay operator boundary.');
$queued = $service->page($admin, [
'status' => 'queued',
'instance_id' => '41',
'user_id' => '10',
'host_id' => '3',
'event_id' => '1',
]);
$wrongOwner = $service->page($admin, [
'instance_id' => '41', 'user_id' => '11',
]);
$noCrossMatch = $service->page($admin, [
'transition' => 'opened',
'direction' => 'inbound',
'ip_family' => 'ipv4',
]);
$matched = $service->page($admin, [
'transition' => 'opened',
'direction' => 'outbound',
'ip_family' => 'ipv4',
]);
$assert($queued['pagination']['total'] === 1
&& ($queued['items'][0]['event_id'] ?? '') === '1'
&& $wrongOwner['pagination']['total'] === 0
&& $noCrossMatch['pagination']['total'] === 0
&& $matched['pagination']['total'] === 2,
'Identity or direct transition scope filters drifted.');
foreach ([
['status' => 'future'],
['instance_id' => 'no'],
['query' => '99999999999999999999'],
['extra' => '1'],
] as $invalid) {
try {
$service->page($admin, $invalid);
throw new RuntimeException('Invalid alert history filter succeeded.');
} catch (CloudError $error) {
$assert($error->httpStatus === 400,
'Invalid alert history filter returned wrong status.');
}
}
try {
$service->page(['user_id' => 9, 'is_admin' => false]);
throw new RuntimeException('Non-admin alert history access succeeded.');
} catch (CloudError $error) {
$assert($error->httpStatus === 403,
'Alert history authorization returned wrong status.');
}
$source = file_get_contents(dirname(__DIR__)
. '/app/Services/Cloud/CloudAdminAbuseAlertHistoryService.php');
$assert(is_string($source)
&& !str_contains($source, 'cloud_abuse_alert_')
&& !str_contains($source, 'alert_event_items')
&& !str_contains($source, 'event.instance_id')
&& !str_contains($source, 'event.user_id')
&& !str_contains($source, 'event.host_id'),
'Alert history retained the legacy grouped-event projection.');
echo "Administrator compact abuse-alert history tests passed.\n";