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->string('name'); }); $schema->create('cloud_abuse_alert_events', static function (Blueprint $table): void { $table->increments('id'); $table->integer('instance_id'); $table->integer('user_id'); $table->integer('host_id'); $table->integer('bucket_start'); $table->integer('transition_count'); $table->string('highest_severity'); $table->integer('created_at'); }); $schema->create('cloud_abuse_alert_event_items', static function (Blueprint $table): void { $table->increments('id'); $table->integer('event_id'); $table->string('rule_code'); $table->string('direction'); $table->string('ip_family'); $table->string('transition_type'); $table->string('from_severity'); $table->string('to_severity'); $table->text('evidence_json'); $table->integer('occurred_at'); }); $schema->create('cloud_abuse_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, 'name' => 'vm-a'], ['id' => 42, 'name' => 'vm-b'], ]); $db->table('cloud_abuse_alert_events')->insert([ [ 'id' => 1, 'instance_id' => 41, 'user_id' => 10, 'host_id' => 3, 'bucket_start' => 120, 'transition_count' => 2, 'highest_severity' => 'high', 'created_at' => 130, ], [ 'id' => 2, 'instance_id' => 42, 'user_id' => 11, 'host_id' => 4, 'bucket_start' => 180, 'transition_count' => 1, 'highest_severity' => 'suspicious', 'created_at' => 190, ], [ 'id' => 3, 'instance_id' => 99, 'user_id' => 10, 'host_id' => 3, 'bucket_start' => 240, 'transition_count' => 1, 'highest_severity' => 'high', 'created_at' => 250, ], ]); $db->table('cloud_abuse_alert_event_items')->insert([ [ 'event_id' => 1, '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' => 900, 'unique_remote_ips' => 140, 'remote_ip' => '203.0.113.5', 'nested' => ['token' => 'must-not-leak'], ], JSON_THROW_ON_ERROR), 'occurred_at' => 120, ], [ 'event_id' => 1, 'rule_code' => 'inbound_udp_flood', 'direction' => 'inbound', 'ip_family' => 'ipv6', 'transition_type' => 'resolved', 'from_severity' => 'high', 'to_severity' => 'none', 'evidence_json' => '{"udp_active_flows":0}', 'occurred_at' => 120, ], [ 'event_id' => 2, 'rule_code' => 'outbound_udp_fanout', 'direction' => 'outbound', 'ip_family' => 'ipv4', 'transition_type' => 'opened', 'from_severity' => 'none', 'to_severity' => 'suspicious', 'evidence_json' => '{"udp_new_flows":45}', 'occurred_at' => 180, ], [ 'event_id' => 3, 'rule_code' => 'inbound_tcp_syn_attack', 'direction' => 'inbound', 'ip_family' => 'mixed', 'transition_type' => 'escalated', 'from_severity' => 'suspicious', 'to_severity' => 'high', 'evidence_json' => '{"tcp_unreplied_flows":77}', 'occurred_at' => 240, ], ]); $secretMessage = 'secret telegram message body'; $db->table('cloud_abuse_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 an effective 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 delivery identity, frozen content, hash, or raw evidence.'); $assert(($page['items'][0]['instance_deleted'] ?? false) === true && ($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 did not retain its no-replay operator boundary.'); $queued = $service->page($admin, [ 'status' => 'queued', 'instance_id' => '41', 'user_id' => '10', 'host_id' => '3', 'event_id' => '1', ]); $assert($queued['pagination']['total'] === 1 && ($queued['items'][0]['event_id'] ?? '') === '1', 'Independent event, instance, user, host, or queued filters drifted.'); $wrongOwner = $service->page($admin, [ 'instance_id' => '41', 'user_id' => '11', ]); $assert($wrongOwner['pagination']['total'] === 0, 'Independent identity filters were collapsed into an OR search.'); $sameItem = $service->page($admin, [ 'transition' => 'opened', 'direction' => 'inbound', 'ip_family' => 'ipv4', ]); $assert($sameItem['pagination']['total'] === 0, 'Combined item filters were incorrectly satisfied by different items.'); $matchedItem = $service->page($admin, [ 'transition' => 'opened', 'direction' => 'outbound', 'ip_family' => 'ipv4', ]); $assert($matchedItem['pagination']['total'] === 2, 'A valid same-item transition/direction/family filter did not match.'); 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 the wrong status.'); } } try { $service->page(['user_id' => 9, 'is_admin' => false]); throw new RuntimeException('Non-administrator alert history access succeeded.'); } catch (CloudError $error) { $assert($error->httpStatus === 403, 'Alert history authorization returned the wrong status.'); } echo "Administrator abuse-alert history service tests passed.\n";