From 7a9a364a666bcd305bca76bb6a9450df0f1aab75 Mon Sep 17 00:00:00 2001 From: codex Date: Sun, 8 Nov 2026 23:21:28 +0800 Subject: [PATCH] feat(cloud): expose read-only abuse monitoring data --- .../CloudAdminAbuseMonitoringController.php | 34 + .../CloudAdminAbuseMonitoringService.php | 1222 +++++++++++++++++ .../CloudAdminAbuseMonitoringServiceTest.php | 518 +++++++ 3 files changed, 1774 insertions(+) create mode 100644 web/app/Controllers/CloudAdminAbuseMonitoringController.php create mode 100644 web/app/Services/Cloud/CloudAdminAbuseMonitoringService.php create mode 100644 web/tests/CloudAdminAbuseMonitoringServiceTest.php diff --git a/web/app/Controllers/CloudAdminAbuseMonitoringController.php b/web/app/Controllers/CloudAdminAbuseMonitoringController.php new file mode 100644 index 00000000..0c5a0896 --- /dev/null +++ b/web/app/Controllers/CloudAdminAbuseMonitoringController.php @@ -0,0 +1,34 @@ +getAttribute('cloud_actor') + ?? CloudAccess::actor(false); + $payload = (new CloudAdminAbuseMonitoringService())->page( + $actor, + $request->getQueryParams() + ); + return $response->withJson($payload) + ->withHeader('Cache-Control', 'private, no-store'); + } + + public function detail($request, $response, array $args) + { + $actor = $request->getAttribute('cloud_actor') + ?? CloudAccess::actor(false); + $payload = (new CloudAdminAbuseMonitoringService())->detail( + $actor, + $args['id'] ?? '' + ); + return $response->withJson($payload) + ->withHeader('Cache-Control', 'private, no-store'); + } +} diff --git a/web/app/Services/Cloud/CloudAdminAbuseMonitoringService.php b/web/app/Services/Cloud/CloudAdminAbuseMonitoringService.php new file mode 100644 index 00000000..0d4413f0 --- /dev/null +++ b/web/app/Services/Cloud/CloudAdminAbuseMonitoringService.php @@ -0,0 +1,1222 @@ + '对外 TCP SYN 扫描', + 'outbound_tcp_port_scan' => '对外 TCP 端口扫描', + 'outbound_udp_fanout' => '对外 UDP 扇出', + 'inbound_tcp_syn_attack' => '入站 TCP SYN 攻击', + 'inbound_tcp_port_scan' => '入站 TCP 端口扫描', + 'inbound_udp_flood' => '入站 UDP Flood', + ]; + private const EVIDENCE_LABELS = [ + 'tcp_active_flows' => 'TCP 活跃 Flow', + 'tcp_new_flows' => 'TCP 新建 Flow', + 'tcp_syn_flows' => 'TCP SYN Flow', + 'tcp_unreplied_flows' => 'TCP 未应答 Flow', + 'udp_active_flows' => 'UDP 活跃 Flow', + 'udp_new_flows' => 'UDP 新建 Flow', + 'unique_remote_ips' => '唯一远端 IP 数', + 'unique_remote_ports' => '唯一远端端口数', + 'unique_local_ports' => '唯一本地端口数', + 'max_remote_ports_per_remote_ip' => '单远端 IP 最大端口数', + 'max_remote_ips_per_remote_port' => '单远端端口最大 IP 数', + 'max_local_ports_per_remote_ip' => '单远端 IP 最大本地端口数', + 'max_remote_ips_per_local_port' => '单本地端口最大远端 IP 数', + ]; + + /** @var \Closure():int */ + private readonly \Closure $clock; + + /** @var \Closure():array> */ + private readonly \Closure $heartbeatProbe; + + public function __construct( + ?callable $clock = null, + ?callable $heartbeatProbe = null + ) { + $this->clock = \Closure::fromCallable( + $clock ?? static fn (): int => time() + ); + $this->heartbeatProbe = \Closure::fromCallable( + $heartbeatProbe + ?? [CloudRuntimeHeartbeat::class, 'snapshot'] + ); + } + + /** @param array $actor @param array $input */ + public function page(array $actor, array $input = []): array + { + self::authorize($actor); + $now = $this->now(); + $filters = self::filters($input); + $base = $this->baseQuery(); + $this->applySharedFilters($base, $filters); + $summary = $this->summary(clone $base, $now); + + $query = clone $base; + $this->applyRiskFilter($query, $filters['risk'], $now); + $total = max(0, (int) $query->count()); + $pages = max(1, (int) ceil($total / $filters['per_page'])); + $filters['page'] = min($filters['page'], $pages); + $driver = $query->getConnection()->getDriverName(); + $records = $query->select(self::columns()) + ->addSelect($query->getConnection()->raw( + self::jsonProjectionSql( + $driver, + 'state.summary_json', + 'summary_json', + self::validSummarySql($driver) + ) + )) + ->orderByDesc('state.updated_at') + ->orderBy('state.instance_id') + ->offset(($filters['page'] - 1) * $filters['per_page']) + ->limit($filters['per_page'])->get(); + $items = []; + foreach ($records as $record) { + $items[] = $this->row($record, $now); + } + $from = $total === 0 ? 0 + : (($filters['page'] - 1) * $filters['per_page']) + 1; + + return [ + 'summary' => $summary + [ + 'total' => $summary['normal'] + $summary['observing'] + + $summary['suspicious'] + $summary['high_risk'] + + $summary['collection_anomaly'], + ], + 'filters' => $filters, + 'options' => $this->options(), + 'health' => $this->health($now), + 'items' => $items, + 'pagination' => [ + 'page' => $filters['page'], + 'per_page' => $filters['per_page'], + 'total' => $total, + 'total_pages' => $pages, + 'from' => $from, + 'to' => $from === 0 ? 0 : $from + count($items) - 1, + 'previous_url' => self::pageUrl($filters, + max(1, $filters['page'] - 1)), + 'next_url' => self::pageUrl($filters, + min($pages, $filters['page'] + 1)), + ], + 'generated_at' => self::timestamp($now), + ]; + } + + /** @param array $actor */ + public function detail(array $actor, mixed $instanceId): array + { + self::authorize($actor); + $id = self::positiveInteger($instanceId, '实例编号'); + $now = $this->now(); + $query = $this->baseQuery()->where('state.instance_id', $id); + $driver = $query->getConnection()->getDriverName(); + $record = $query->select(self::columns()) + ->addSelect($query->getConnection()->raw( + self::jsonProjectionSql( + $driver, + 'state.summary_json', + 'summary_json', + self::validSummarySql($driver) + ) + ))->first(); + if ($record === null) { + throw new CloudError('监控记录不存在', 404); + } + + $main = User::getConnectionResolver()->connection('default'); + $findings = $main->table('cloud_abuse_findings') + ->where('instance_id', $id) + ->where('user_id', (int) $record->user_id) + ->orderByDesc('last_seen_at')->orderByDesc('id') + ->limit(self::DETAIL_FINDING_LIMIT) + ->select([ + 'id', 'rule_code', 'direction', 'ip_family', 'severity', + 'state', 'consecutive_windows', 'first_seen_at', + 'last_seen_at', 'resolved_at', + ])->addSelect($main->raw(self::jsonProjectionSql( + $main->getDriverName(), + 'evidence_json', + 'evidence_json' + )))->get(); + + return [ + 'item' => $this->row($record, $now), + 'series' => $this->series($id), + 'findings' => array_map( + [self::class, 'findingRow'], + $findings->all() + ), + ]; + } + + private function now(): int + { + $now = ($this->clock)(); + if (!is_int($now) || $now < 1) { + throw new \LogicException('Abuse monitoring clock is invalid'); + } + return $now; + } + + /** @param array $actor */ + private static function authorize(array $actor): void + { + if (!($actor['is_admin'] ?? false) + || (int) ($actor['user_id'] ?? 0) < 1) { + throw new CloudError('需要管理员权限', 403); + } + } + + /** @param array $input @return array */ + public static function filters(array $input): array + { + $allowed = [ + 'risk', 'direction', 'host_id', 'line_id', 'user_id', + 'query', 'ip_version', 'page', 'per_page', + ]; + foreach ($input as $key => $value) { + if (!in_array($key, $allowed, true) + || (!is_string($value) && !is_int($value))) { + throw new CloudError('筛选参数无效', 400); + } + } + $filters = []; + foreach ([ + 'risk' => self::RISKS, + 'direction' => self::DIRECTIONS, + 'ip_version' => self::IP_VERSIONS, + ] as $key => $values) { + $value = trim((string) ($input[$key] ?? '')); + if ($value !== '' && !in_array($value, $values, true)) { + throw new CloudError('筛选参数无效', 400); + } + $filters[$key] = $value; + } + foreach (['host_id', 'line_id', 'user_id'] as $key) { + $value = trim((string) ($input[$key] ?? '')); + $filters[$key] = $value === '' + ? null : self::positiveInteger($value, $key); + } + $query = trim((string) ($input['query'] ?? '')); + if (mb_strlen($query, 'UTF-8') > 120 + || preg_match('/[\x00-\x1f\x7f]/u', $query) !== 0) { + throw new CloudError('搜索关键词无效', 400); + } + $filters['query'] = $query; + $filters['page'] = self::boundedInteger( + $input['page'] ?? 1, + 1, + 100000, + '页码' + ); + $filters['per_page'] = self::boundedInteger( + $input['per_page'] ?? 20, + 1, + 100, + '每页数量' + ); + return $filters; + } + + private function baseQuery() + { + return User::getConnectionResolver()->connection('default') + ->table('cloud_abuse_instance_states as state') + ->join('cloud_instances as instance', + 'instance.id', '=', 'state.instance_id') + ->leftJoin('user as owner', 'owner.id', '=', 'state.user_id') + ->leftJoin('cloud_hosts as host', 'host.id', '=', 'state.host_id') + ->leftJoin('cloud_resource_pools as pool', + 'pool.id', '=', 'instance.resource_pool_id') + ->leftJoin('cloud_network_lines as line', + 'line.id', '=', 'pool.line_id') + ->where('instance.status', '<>', 'deleted'); + } + + /** @return list */ + private static function columns(): array + { + return [ + 'state.instance_id', 'state.user_id', 'state.host_id', + 'state.risk_state', 'state.collection_state', + 'state.latest_direction', 'state.latest_rule_code', + 'state.latest_severity', + 'state.last_bucket_start', 'state.last_sample_at', + 'state.last_evaluated_at', 'state.updated_at', + 'instance.name as instance_name', + 'instance.uuid as instance_uuid', + 'instance.resource_pool_id', + 'owner.user_name as user_name', + 'host.name as host_name', + 'host.region as host_region', + 'line.id as line_id', 'line.name as line_name', + ]; + } + + /** @param array $filters */ + private function applySharedFilters(object $query, array $filters): void + { + foreach ([ + 'host_id' => 'state.host_id', + 'user_id' => 'state.user_id', + 'line_id' => 'line.id', + ] as $filter => $column) { + if ($filters[$filter] !== null) { + $query->where($column, $filters[$filter]); + } + } + if ($filters['direction'] !== '') { + $query->where('state.latest_direction', $filters['direction']); + } + if ($filters['ip_version'] !== '') { + $family = $filters['ip_version']; + $query->whereExists(static function ($finding) use ($family): void { + $finding->selectRaw('1')->from( + 'cloud_abuse_findings as family_finding' + )->whereColumn( + 'family_finding.instance_id', + 'state.instance_id' + )->whereColumn( + 'family_finding.user_id', + 'state.user_id' + )->where('family_finding.state', 'open') + ->whereIn('family_finding.ip_family', [$family, 'mixed']); + }); + } + if ($filters['query'] === '') { + return; + } + $escaped = str_replace( + ['!', '%', '_'], + ['!!', '!%', '!_'], + $filters['query'] + ); + $like = '%' . $escaped . '%'; + $numeric = ctype_digit($filters['query']) + ? (int) $filters['query'] : null; + $query->where(static function ($search) use ($like, $numeric): void { + $search->whereRaw("instance.name LIKE ? ESCAPE '!'", [$like]) + ->orWhereRaw("instance.uuid LIKE ? ESCAPE '!'", [$like]) + ->orWhereRaw("owner.user_name LIKE ? ESCAPE '!'", [$like]); + if ($numeric !== null && $numeric > 0) { + $search->orWhere('state.instance_id', $numeric) + ->orWhere('state.user_id', $numeric); + } + }); + } + + private function applyRiskFilter(object $query, string $risk, int $now): void + { + if ($risk === '') { + return; + } + $cutoff = $now - self::STALE_SECONDS; + $validSummary = self::validSummarySql( + $query->getConnection()->getDriverName() + ); + if ($risk === 'collection_anomaly') { + $query->where(static function ($anomaly) use ( + $cutoff, + $validSummary + ): void { + $anomaly->where('state.collection_state', '<>', 'healthy') + ->orWhereNull('state.last_sample_at') + ->orWhere('state.last_sample_at', '<', $cutoff) + ->orWhereRaw('NOT (' . $validSummary . ')'); + }); + return; + } + $query->where('state.collection_state', 'healthy') + ->whereNotNull('state.last_sample_at') + ->where('state.last_sample_at', '>=', $cutoff) + ->whereRaw($validSummary) + ->where('state.risk_state', $risk); + } + + /** @return array */ + private function summary(object $query, int $now): array + { + $cutoff = $now - self::STALE_SECONDS; + $valid = self::validSummarySql( + $query->getConnection()->getDriverName() + ); + $effective = "CASE WHEN state.collection_state = 'healthy' " + . 'AND state.last_sample_at >= ? AND ' . $valid . ' ' + . "THEN state.risk_state ELSE 'collection_anomaly' END"; + $rows = $query->selectRaw( + $effective . ' AS effective_risk, COUNT(*) AS aggregate_count, ' + . 'MAX(state.last_sample_at) AS last_sample_at', + [$cutoff] + )->groupBy('effective_risk')->get(); + $result = array_fill_keys(self::RISKS, 0); + $lastSample = null; + foreach ($rows as $row) { + $risk = (string) ($row->effective_risk ?? ''); + if (array_key_exists($risk, $result)) { + $result[$risk] = max(0, (int) $row->aggregate_count); + } + if ($row->last_sample_at !== null) { + $lastSample = $lastSample === null + ? (int) $row->last_sample_at + : max($lastSample, (int) $row->last_sample_at); + } + } + return $result + [ + 'last_sample_at' => self::timestampOrNull($lastSample) ?? '', + ]; + } + + /** @return array */ + private function row(object $record, int $now): array + { + $summary = self::normalizeSummary( + self::decodeObject((string) $record->summary_json) + ); + $valid = $summary !== null; + $rates = $valid ? $summary['rates'] : self::nullRates(); + $metrics = $valid ? $summary['metrics'] : null; + $flow = $metrics === null + ? self::nullFlowSummary() : self::flowSummary($metrics); + $stale = $record->last_sample_at === null + || (int) $record->last_sample_at < $now - self::STALE_SECONDS; + $healthy = $valid && !$stale + && (string) $record->collection_state === 'healthy'; + $qualityFlags = $valid ? $summary['quality_flags'] : null; + $sampleQuality = $healthy && $qualityFlags !== null + && !in_array('incomplete', $qualityFlags, true) + ? 'complete' : ($stale ? 'stale' : 'incomplete'); + $effectiveRisk = $healthy + ? (string) $record->risk_state : 'collection_anomaly'; + + return [ + 'instance_id' => (string) $record->instance_id, + 'instance_name' => (string) ($record->instance_name ?? ''), + 'instance_uuid' => (string) ($record->instance_uuid ?? ''), + 'user_id' => (string) $record->user_id, + 'user_name' => (string) ($record->user_name ?? ''), + 'host_id' => (string) $record->host_id, + 'host_name' => (string) ($record->host_name ?? ''), + 'host_region' => (string) ($record->host_region ?? ''), + 'line_id' => $record->line_id === null + ? '' : (string) $record->line_id, + 'line_name' => (string) ($record->line_name ?? ''), + 'risk_state' => $effectiveRisk, + 'collection_state' => $valid + ? ($stale ? 'stale' : (string) $record->collection_state) + : 'degraded', + 'direction' => in_array( + (string) $record->latest_direction, + self::DIRECTIONS, + true + ) ? (string) $record->latest_direction : 'none', + 'latest_rule_code' => $healthy + ? (string) $record->latest_rule_code : '', + 'latest_severity' => $healthy + ? (string) $record->latest_severity : 'none', + 'metrics' => $rates + $flow + [ + 'rx_bps_display' => self::rateDisplay($rates['rx_bps'], false), + 'tx_bps_display' => self::rateDisplay($rates['tx_bps'], false), + 'rx_pps_display' => self::rateDisplay($rates['rx_pps'], true), + 'tx_pps_display' => self::rateDisplay($rates['tx_pps'], true), + ], + 'sample' => [ + 'bucket_start' => self::timestampOrNull( + $record->last_bucket_start + ), + 'sampled_at' => self::timestampOrNull( + $record->last_sample_at + ), + 'quality' => $sampleQuality, + 'quality_label' => $sampleQuality === 'complete' + ? '样本完整' : ($sampleQuality === 'stale' + ? '采集已过期' : '样本不完整'), + ], + 'sample_count' => $valid ? $summary['sample_count'] : null, + 'warmup_remaining' => $valid + ? $summary['warmup_remaining'] : null, + 'last_evaluated_at' => self::timestampOrNull( + $record->last_evaluated_at + ), + ]; + } + + /** @return list> */ + private function series(int $instanceId): array + { + $log = User::getConnectionResolver()->connection('log'); + $driver = $log->getDriverName(); + $records = $log->table('cloud_vm_network_observation_samples') + ->where('instance_id', $instanceId) + ->orderByDesc('bucket_start')->orderByDesc('sample_key') + ->limit(self::DETAIL_SAMPLE_LIMIT + 1) + ->select(['sample_key', 'bucket_start', 'complete']) + ->addSelect($log->raw(self::jsonProjectionSql( + $driver, + 'metrics_json', + 'metrics_json' + )))->addSelect($log->raw(self::jsonProjectionSql( + $driver, + 'quality_flags_json', + 'quality_flags_json' + )))->get()->all(); + $records = array_reverse($records); + $series = []; + $previous = null; + foreach ($records as $record) { + $metrics = self::normalizeMetrics( + self::decodeObject((string) $record->metrics_json) + ); + $flags = self::qualityFlags( + self::decodeList((string) $record->quality_flags_json) + ); + $complete = (int) $record->complete === 1 + && $metrics !== null && $flags !== null + && !in_array('incomplete', $flags, true); + $interface = $metrics === null + ? null : self::interfaceCounters($metrics['interface'] ?? null); + $rates = self::nullRates(); + if ($complete && $interface !== null && $previous !== null + && $previous['complete'] + && $previous['interface'] !== null + && (int) $record->bucket_start + - $previous['bucket_start'] === 60) { + $rates = self::interfaceRates( + $previous['interface'], + $interface + ); + } + $flow = $metrics === null + ? self::nullFlowSummary() : self::flowSummary($metrics); + $series[] = [ + 'bucket_start' => self::timestamp((int) $record->bucket_start), + 'quality' => $complete ? 'complete' : 'incomplete', + ] + $rates + $flow; + $previous = [ + 'bucket_start' => (int) $record->bucket_start, + 'complete' => $complete, + 'interface' => $interface, + ]; + } + if (count($series) > self::DETAIL_SAMPLE_LIMIT) { + array_shift($series); + } + return $series; + } + + /** @return array */ + private static function findingRow(object $finding): array + { + $evidence = self::decodeObject((string) $finding->evidence_json); + $parts = []; + if ($evidence !== null) { + foreach (self::EVIDENCE_LABELS as $key => $label) { + $value = self::nonNegativeInteger($evidence[$key] ?? null); + if ($value !== null) { + $parts[] = $label . ' ' . number_format($value, 0, '.', ','); + } + if (count($parts) >= 4) { + break; + } + } + } + return [ + 'finding_id' => (string) $finding->id, + 'rule_code' => (string) $finding->rule_code, + 'label' => self::RULE_LABELS[(string) $finding->rule_code] + ?? '网络行为规则', + 'direction' => (string) $finding->direction, + 'ip_family' => (string) $finding->ip_family, + 'severity' => (string) $finding->severity, + 'state' => (string) $finding->state, + 'consecutive_windows' => max(1, + (int) $finding->consecutive_windows), + 'first_seen_at' => self::timestamp((int) $finding->first_seen_at), + 'last_seen_at' => self::timestamp((int) $finding->last_seen_at), + 'resolved_at' => self::timestampOrNull($finding->resolved_at), + 'evidence_summary' => $parts === [] + ? '聚合证据不可用;未保存远端地址或端口明细。' + : implode(' · ', $parts), + ]; + } + + /** @return array{hosts:list>,lines:list>} */ + private function options(): array + { + $main = User::getConnectionResolver()->connection('default'); + $hosts = []; + foreach ($main->table('cloud_hosts')->orderBy('name')->orderBy('id') + ->get(['id', 'name']) as $host) { + $hosts[] = ['id' => (string) $host->id, + 'name' => (string) $host->name]; + } + $lines = []; + foreach ($main->table('cloud_network_lines') + ->orderBy('name')->orderBy('id')->get(['id', 'name']) as $line) { + $lines[] = ['id' => (string) $line->id, + 'name' => (string) $line->name]; + } + return ['hosts' => $hosts, 'lines' => $lines]; + } + + /** @return array */ + private function health(int $now): array + { + $worker = null; + try { + foreach (($this->heartbeatProbe)() as $row) { + if (is_array($row) && ($row['key'] ?? null) === 'abuse_monitor') { + $worker = $row; + break; + } + } + } catch (\Throwable) { + $worker = null; + } + try { + $main = User::getConnectionResolver()->connection('default'); + $hostIds = array_map('intval', $main->table('cloud_hosts') + ->where('enabled', 1)->orderBy('id')->pluck('id')->all()); + $log = User::getConnectionResolver()->connection('log'); + $cursors = $hostIds === [] ? [] : $log->table( + 'cloud_vm_network_monitor_cursors' + )->whereIn('host_id', $hostIds)->get()->keyBy('host_id'); + } catch (\Throwable) { + return self::healthRow( + 'unavailable', null, 'monitor_database_unavailable', + '监控账本暂不可读取;不能确认实例网络状态。' + ); + } + if ($hostIds === []) { + return self::healthRow( + 'unavailable', null, 'no_enabled_hosts', + '没有已启用计算节点,当前无可验证的监控覆盖。' + ); + } + $healthy = 0; + $stale = 0; + $unavailable = 0; + $degraded = 0; + $lastSuccess = null; + foreach ($hostIds as $hostId) { + $cursor = $cursors->get($hostId); + if ($cursor === null) { + ++$unavailable; + continue; + } + $success = $cursor->last_success_at === null + ? null : (int) $cursor->last_success_at; + if ($success !== null) { + $lastSuccess = $lastSuccess === null + ? $success : max($lastSuccess, $success); + } + if ((string) $cursor->collection_state === 'unavailable' + || $success === null) { + ++$unavailable; + } elseif ($success < $now - self::STALE_SECONDS) { + ++$stale; + } elseif ((string) $cursor->collection_state !== 'healthy') { + ++$degraded; + } else { + ++$healthy; + } + } + $workerStatus = is_array($worker) + ? (string) ($worker['status'] ?? 'unknown') : 'unknown'; + $workerReady = in_array($workerStatus, ['ok', 'busy'], true); + if (!$workerReady) { + return self::healthRow( + $workerStatus === 'warning' ? 'degraded' : 'unavailable', + $lastSuccess, + 'monitor_worker_unavailable', + '监控 Worker 没有可验证的常驻心跳;历史数据不会被视为当前健康。' + ); + } + if ($healthy === count($hostIds)) { + return self::healthRow( + 'healthy', $lastSuccess, '', '所有已启用节点最近采集正常。' + ); + } + if ($unavailable === count($hostIds)) { + return self::healthRow( + 'unavailable', $lastSuccess, 'all_hosts_unavailable', + '所有已启用节点均缺少可验证的最新采集。' + ); + } + if ($stale > 0 && $healthy === 0 && $degraded === 0 + && $unavailable === 0) { + return self::healthRow( + 'stale', $lastSuccess, 'host_collection_stale', + '节点采集记录均已过期;不会据此判断实例正常。' + ); + } + return self::healthRow( + 'degraded', $lastSuccess, 'partial_collection_failure', + '部分节点采集缺失、过期或异常,请核查节点状态。' + ); + } + + private static function healthRow( + string $state, + ?int $lastSuccess, + string $error, + string $message + ): array { + return [ + 'worker_state' => $state, + 'last_success_at' => self::timestampOrNull($lastSuccess) ?? '', + 'error_code' => $error, + 'message' => $message, + ]; + } + + /** @return null|array */ + private static function rates(mixed $value): ?array + { + if (!is_array($value) || array_is_list($value)) { + return null; + } + $result = []; + foreach (self::RATE_FIELDS as $field) { + if (!array_key_exists($field, $value)) { + return null; + } + $parsed = $value[$field] === null + ? null : self::nonNegativeInteger($value[$field]); + if ($value[$field] !== null && $parsed === null) { + return null; + } + $result[$field] = $parsed; + } + return $result; + } + + /** @return array */ + private static function nullRates(): array + { + return array_fill_keys(self::RATE_FIELDS, null); + } + + /** @return array */ + private static function flowSummary(array $metrics): array + { + $result = []; + foreach (self::FLOW_FIELDS as $field) { + $sum = 0; + $valid = true; + $families = 0; + foreach (['ipv4', 'ipv6'] as $family) { + if (!is_array($metrics[$family] ?? null)) { + continue; + } + ++$families; + foreach (['inbound', 'outbound'] as $direction) { + $value = self::nonNegativeInteger( + $metrics[$family][$direction][$field] ?? null + ); + if ($value === null || $sum > PHP_INT_MAX - $value) { + $valid = false; + break 2; + } + $sum += $value; + } + } + $result[$field] = $valid && $families > 0 ? $sum : null; + } + return $result; + } + + /** @return array */ + private static function nullFlowSummary(): array + { + return array_fill_keys(self::FLOW_FIELDS, null); + } + + /** @return null|array */ + private static function interfaceCounters(mixed $value): ?array + { + if (!is_array($value) || array_is_list($value)) { + return null; + } + $result = []; + foreach (self::INTERFACE_FIELDS as $field) { + $parsed = self::nonNegativeInteger($value[$field] ?? null); + if ($parsed === null) { + return null; + } + $result[$field] = $parsed; + } + return $result; + } + + /** @param array $previous @param array $current */ + private static function interfaceRates(array $previous, array $current): array + { + $rates = self::nullRates(); + foreach ([ + 'rx_bytes' => 'rx_bps', 'tx_bytes' => 'tx_bps', + 'rx_packets' => 'rx_pps', 'tx_packets' => 'tx_pps', + ] as $counter => $rate) { + if ($current[$counter] < $previous[$counter]) { + continue; + } + $delta = $current[$counter] - $previous[$counter]; + if (str_ends_with($counter, '_bytes')) { + if ($delta > intdiv(PHP_INT_MAX, 8)) { + continue; + } + $rates[$rate] = intdiv($delta * 8, 60); + } else { + $rates[$rate] = intdiv($delta, 60); + } + } + return $rates; + } + + /** @return null|list */ + private static function qualityFlags(mixed $value): ?array + { + if (!is_array($value) || !array_is_list($value) + || count($value) > 16) { + return null; + } + $result = []; + foreach ($value as $flag) { + if (!is_string($flag) + || preg_match('/^[a-z0-9_]{3,64}$/D', $flag) !== 1) { + return null; + } + $result[] = $flag; + } + return $result; + } + + private static function decodeObject(string $json): ?array + { + if ($json === '' || strlen($json) > self::JSON_LIMIT) { + return null; + } + try { + // MySQL rejects JSON documents deeper than 100 levels. Keep the + // reader above that storage limit so harmless unknown fields do + // not make PHP disagree with the SQL validity projection. + $value = json_decode($json, true, 128, JSON_THROW_ON_ERROR); + } catch (\JsonException) { + return null; + } + return is_array($value) && !array_is_list($value) ? $value : null; + } + + private static function normalizeSummary(?array $value): ?array + { + if ($value === null) { + return null; + } + $rates = self::rates($value['rates'] ?? null); + $metrics = self::normalizeMetrics($value['metrics'] ?? null); + $flags = self::summaryQualityFlags( + $value['quality_flags'] ?? null + ); + $sampleCount = self::nonNegativeInteger( + $value['sample_count'] ?? null + ); + $warmup = self::nonNegativeInteger( + $value['warmup_remaining'] ?? null + ); + if ($rates === null || $metrics === null || $flags === null + || $sampleCount === null || $warmup === null) { + return null; + } + return [ + 'rates' => $rates, + 'sample_count' => $sampleCount, + 'warmup_remaining' => $warmup, + 'quality_flags' => $flags, + 'metrics' => $metrics, + ]; + } + + private static function validSummarySql(string $driver): string + { + $column = 'state.summary_json'; + $rootType = $driver === 'sqlite' + ? "json_type({$column})" : "JSON_TYPE({$column})"; + $objectType = $driver === 'sqlite' ? 'object' : 'OBJECT'; + $arrayType = $driver === 'sqlite' ? 'array' : 'ARRAY'; + $stringType = $driver === 'sqlite' ? 'text' : 'STRING'; + $checks = [ + $rootType . " = '{$objectType}'", + self::jsonTypeSql($driver, $column, '$.rates') + . " = '{$objectType}'", + self::jsonTypeSql($driver, $column, '$.metrics') + . " = '{$objectType}'", + self::jsonTypeSql($driver, $column, '$.metrics.interface') + . " = '{$objectType}'", + self::jsonTypeSql($driver, $column, '$.quality_flags') + . " = '{$arrayType}'", + self::jsonArrayLengthSql( + $driver, + $column, + '$.quality_flags' + ) . ' <= 16', + self::jsonIntegerSql( + $driver, + $column, + '$.sample_count' + ), + self::jsonIntegerSql( + $driver, + $column, + '$.warmup_remaining' + ), + ]; + foreach (self::RATE_FIELDS as $field) { + $checks[] = self::jsonIntegerSql( + $driver, + $column, + '$.rates.' . $field, + true + ); + } + foreach (self::INTERFACE_FIELDS as $field) { + $checks[] = self::jsonIntegerSql( + $driver, + $column, + '$.metrics.interface.' . $field + ); + } + for ($index = 0; $index < 16; ++$index) { + $type = self::jsonTypeSql( + $driver, + $column, + '$.quality_flags[' . $index . ']' + ); + $checks[] = '(' . $type . ' IS NULL OR ' + . $type . " = '{$stringType}')"; + } + $familyValid = []; + $familyAbsent = []; + $jsonNull = $driver === 'sqlite' ? 'null' : 'NULL'; + foreach (['ipv4', 'ipv6'] as $family) { + $familyValid[$family] = self::validFamilySql( + $driver, + $column, + $family + ); + $familyType = self::jsonTypeSql( + $driver, + $column, + '$.metrics.' . $family + ); + $familyAbsent[$family] = '(' . $familyType . ' IS NULL OR ' + . $familyType . " = '{$jsonNull}')"; + } + $checks[] = '((' . $familyValid['ipv4'] . ') OR (' + . $familyValid['ipv6'] . '))'; + foreach (['ipv4', 'ipv6'] as $family) { + $checks[] = '((' . $familyAbsent[$family] . ') OR (' + . $familyValid[$family] . '))'; + } + $jsonValid = $driver === 'sqlite' + ? "json_valid({$column}) = 1" + : "JSON_VALID({$column}) = 1"; + $withinLimit = self::jsonByteLengthSql($driver, $column) + . ' <= ' . self::JSON_LIMIT; + return '(CASE WHEN ' . $jsonValid . ' AND ' . $withinLimit + . ' THEN CASE WHEN ' . implode(' AND ', $checks) + . ' THEN 1 ELSE 0 END ELSE 0 END) = 1'; + } + + /** @return null|array */ + private static function normalizeMetrics(mixed $value): ?array + { + if (!is_array($value) || array_is_list($value)) { + return null; + } + $interface = self::interfaceCounters($value['interface'] ?? null); + if ($interface === null) { + return null; + } + $result = ['interface' => $interface]; + $families = 0; + foreach (['ipv4', 'ipv6'] as $family) { + if (!array_key_exists($family, $value) + || $value[$family] === null) { + continue; + } + if (!is_array($value[$family]) + || array_is_list($value[$family])) { + return null; + } + $normalized = []; + foreach (['inbound', 'outbound'] as $direction) { + $flows = $value[$family][$direction] ?? null; + if (!is_array($flows) || array_is_list($flows)) { + return null; + } + $directionValues = []; + foreach (self::FLOW_FIELDS as $field) { + $parsed = self::nonNegativeInteger( + $flows[$field] ?? null + ); + if ($parsed === null) { + return null; + } + $directionValues[$field] = $parsed; + } + $normalized[$direction] = $directionValues; + } + $result[$family] = $normalized; + ++$families; + } + return $families > 0 ? $result : null; + } + + /** @return null|list */ + private static function summaryQualityFlags(mixed $value): ?array + { + if (!is_array($value) || !array_is_list($value) + || count($value) > 16) { + return null; + } + foreach ($value as $flag) { + if (!is_string($flag)) { + return null; + } + } + return $value; + } + + private static function validFamilySql( + string $driver, + string $column, + string $family + ): string { + $objectType = $driver === 'sqlite' ? 'object' : 'OBJECT'; + $checks = [self::jsonTypeSql( + $driver, + $column, + '$.metrics.' . $family + ) . " = '{$objectType}'"]; + foreach (['inbound', 'outbound'] as $direction) { + $base = '$.metrics.' . $family . '.' . $direction; + $checks[] = self::jsonTypeSql($driver, $column, $base) + . " = '{$objectType}'"; + foreach (self::FLOW_FIELDS as $field) { + $checks[] = self::jsonIntegerSql( + $driver, + $column, + $base . '.' . $field + ); + } + } + return '(' . implode(' AND ', $checks) . ')'; + } + + private static function jsonIntegerSql( + string $driver, + string $column, + string $path, + bool $nullable = false + ): string { + $type = self::jsonTypeSql($driver, $column, $path); + $value = self::jsonValueSql($driver, $column, $path); + $integerType = $driver === 'sqlite' ? 'integer' : 'INTEGER'; + $jsonNull = $driver === 'sqlite' ? 'null' : 'NULL'; + $integer = '(' . $type . " = '{$integerType}' AND " + . $value . ' >= 0 AND ' . $value . ' <= ' . PHP_INT_MAX . ')'; + return $nullable + ? '(' . $type . " = '{$jsonNull}' OR " . $integer . ')' + : $integer; + } + + private static function jsonTypeSql( + string $driver, + string $column, + string $path + ): string { + return $driver === 'sqlite' + ? "json_type({$column}, '" . $path . "')" + : "JSON_TYPE(JSON_EXTRACT({$column}, '" . $path . "'))"; + } + + private static function jsonValueSql( + string $driver, + string $column, + string $path + ): string { + return $driver === 'sqlite' + ? "json_extract({$column}, '" . $path . "')" + : "JSON_EXTRACT({$column}, '" . $path . "')"; + } + + private static function jsonArrayLengthSql( + string $driver, + string $column, + string $path + ): string { + return $driver === 'sqlite' + ? "json_array_length({$column}, '" . $path . "')" + : "JSON_LENGTH(JSON_EXTRACT({$column}, '" . $path . "'))"; + } + + private static function jsonByteLengthSql( + string $driver, + string $column + ): string { + return $driver === 'sqlite' + ? 'length(CAST(' . $column . ' AS BLOB))' + : 'OCTET_LENGTH(' . $column . ')'; + } + + private static function jsonProjectionSql( + string $driver, + string $column, + string $alias, + ?string $predicate = null + ): string { + $predicate ??= self::jsonByteLengthSql($driver, $column) + . ' <= ' . self::JSON_LIMIT; + return 'CASE WHEN COALESCE((' . $predicate . '), 0) = 1 THEN ' + . $column . ' ELSE NULL END AS ' . $alias; + } + + private static function decodeList(string $json): ?array + { + if ($json === '' || strlen($json) > self::JSON_LIMIT) { + return null; + } + try { + $value = json_decode($json, true, 12, JSON_THROW_ON_ERROR); + } catch (\JsonException) { + return null; + } + return is_array($value) && array_is_list($value) ? $value : null; + } + + private static function nonNegativeInteger(mixed $value): ?int + { + return is_int($value) && $value >= 0 ? $value : null; + } + + private static function rateDisplay(?int $value, bool $packets): string + { + if ($value === null) { + return '不可用'; + } + if ($packets) { + return number_format($value, 0, '.', ',') . ' pps'; + } + $units = ['bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps']; + $scaled = (float) $value; + $unit = 0; + while ($scaled >= 1000 && $unit < count($units) - 1) { + $scaled /= 1000; + ++$unit; + } + $precision = $scaled >= 100 || floor($scaled) === $scaled ? 0 : 1; + return number_format($scaled, $precision, '.', '') . ' ' + . $units[$unit]; + } + + private static function timestamp(int $value): string + { + return gmdate('Y-m-d\TH:i:s\Z', max(1, $value)); + } + + private static function timestampOrNull(mixed $value): ?string + { + if ((!is_int($value) && !ctype_digit((string) $value)) + || (int) $value < 1) { + return null; + } + return self::timestamp((int) $value); + } + + /** @param array $filters */ + private static function pageUrl(array $filters, int $page): string + { + $query = []; + foreach ([ + 'risk', 'direction', 'host_id', 'line_id', 'user_id', + 'query', 'ip_version', 'per_page', + ] as $key) { + $value = $filters[$key]; + if ($value !== '' && $value !== null + && !($key === 'per_page' && $value === 20)) { + $query[$key] = (string) $value; + } + } + if ($page > 1) { + $query['page'] = (string) $page; + } + return '/admin/abuse-monitoring' + . ($query === [] ? '' : '?' . http_build_query( + $query, '', '&', PHP_QUERY_RFC3986 + )); + } + + private static function positiveInteger(mixed $value, string $name): int + { + return self::boundedInteger($value, 1, PHP_INT_MAX, $name); + } + + private static function boundedInteger( + mixed $value, + int $minimum, + int $maximum, + string $name + ): int { + if ((!is_int($value) && !is_string($value)) + || !ctype_digit((string) $value)) { + throw new CloudError($name . '无效', 400); + } + $parsed = filter_var((string) $value, FILTER_VALIDATE_INT, [ + 'options' => ['min_range' => $minimum, 'max_range' => $maximum], + ]); + if ($parsed === false) { + throw new CloudError($name . '无效', 400); + } + return (int) $parsed; + } +} diff --git a/web/tests/CloudAdminAbuseMonitoringServiceTest.php b/web/tests/CloudAdminAbuseMonitoringServiceTest.php new file mode 100644 index 00000000..a7c2d157 --- /dev/null +++ b/web/tests/CloudAdminAbuseMonitoringServiceTest.php @@ -0,0 +1,518 @@ +addConnection([ + 'driver' => 'sqlite', 'database' => ':memory:', +], 'default'); +$capsule->addConnection([ + 'driver' => 'sqlite', 'database' => ':memory:', +], 'log'); +$capsule->setAsGlobal(); +$capsule->bootEloquent(); + +$mainSchema = $capsule->schema('default'); +$mainSchema->create('user', static function (Blueprint $table): void { + $table->increments('id'); + $table->string('user_name'); +}); +$mainSchema->create('cloud_hosts', static function (Blueprint $table): void { + $table->increments('id'); + $table->string('name'); + $table->string('region'); + $table->boolean('enabled'); +}); +$mainSchema->create('cloud_network_lines', static function (Blueprint $table): void { + $table->increments('id'); + $table->string('name'); +}); +$mainSchema->create('cloud_resource_pools', static function (Blueprint $table): void { + $table->increments('id'); + $table->integer('line_id'); +}); +$mainSchema->create('cloud_instances', static function (Blueprint $table): void { + $table->increments('id'); + $table->string('uuid'); + $table->integer('host_id'); + $table->integer('user_id'); + $table->integer('resource_pool_id')->nullable(); + $table->string('name'); + $table->string('status'); +}); +$mainSchema->create('cloud_abuse_instance_states', + static function (Blueprint $table): void { + $table->integer('instance_id')->primary(); + $table->integer('user_id'); + $table->integer('host_id'); + $table->string('risk_state'); + $table->string('collection_state'); + $table->string('latest_direction'); + $table->string('latest_rule_code'); + $table->string('latest_severity'); + $table->text('summary_json'); + $table->integer('last_bucket_start')->nullable(); + $table->integer('last_sample_at')->nullable(); + $table->integer('last_evaluated_at')->nullable(); + $table->integer('updated_at'); + }); +$mainSchema->create('cloud_abuse_findings', + static function (Blueprint $table): void { + $table->increments('id'); + $table->string('finding_key'); + $table->integer('instance_id'); + $table->integer('user_id'); + $table->integer('host_id'); + $table->string('rule_code'); + $table->integer('rule_version'); + $table->string('direction'); + $table->string('ip_family'); + $table->string('severity'); + $table->string('state'); + $table->integer('first_seen_at'); + $table->integer('last_seen_at'); + $table->integer('consecutive_windows'); + $table->text('evidence_json'); + $table->integer('resolved_at')->nullable(); + $table->integer('created_at'); + $table->integer('updated_at'); + }); + +$logSchema = $capsule->schema('log'); +$logSchema->create('cloud_vm_network_observation_samples', + static function (Blueprint $table): void { + $table->string('sample_key')->primary(); + $table->integer('instance_id'); + $table->integer('bucket_start'); + $table->boolean('complete'); + $table->text('metrics_json'); + $table->text('quality_flags_json'); + }); +$logSchema->create('cloud_vm_network_monitor_cursors', + static function (Blueprint $table): void { + $table->integer('host_id')->primary(); + $table->string('collection_state'); + $table->integer('last_success_at')->nullable(); + }); + +$main = $capsule->connection('default'); +$log = $capsule->connection('log'); +$now = 2_000_000; +$actor = ['user_id' => 9, 'is_admin' => true]; +$main->table('user')->insert([ + ['id' => 11, 'user_name' => 'alpha'], + ['id' => 12, 'user_name' => 'beta'], +]); +$main->table('cloud_hosts')->insert([ + ['id' => 1, 'name' => 'HKG-A', 'region' => 'HongKong', 'enabled' => 1], + ['id' => 2, 'name' => 'HKG-B', 'region' => 'HongKong', 'enabled' => 1], +]); +$main->table('cloud_network_lines')->insert([ + ['id' => 3, 'name' => 'International'], +]); +$main->table('cloud_resource_pools')->insert([ + ['id' => 4, 'line_id' => 3], +]); +$uuid = static fn (int $id): string => sprintf( + '123e4567-e89b-42d3-a456-%012d', + $id +); +foreach ([ + [1, 11, 1, 'normal-vm'], + [2, 12, 1, 'high-vm'], + [3, 11, 2, 'stale-vm'], + [4, 11, 1, 'broken-vm'], +] as [$id, $user, $host, $name]) { + $main->table('cloud_instances')->insert([ + 'id' => $id, 'uuid' => $uuid($id), 'host_id' => $host, + 'user_id' => $user, 'resource_pool_id' => 4, + 'name' => $name, 'status' => 'active', + ]); +} +$flow = array_fill_keys([ + 'tcp_active_flows', 'tcp_new_flows', 'tcp_syn_flows', + 'tcp_unreplied_flows', 'udp_active_flows', 'udp_new_flows', + 'other_active_flows', 'unique_remote_ips', + 'unique_remote_ports', 'unique_local_ports', + 'max_remote_ports_per_remote_ip', + 'max_remote_ips_per_remote_port', + 'max_local_ports_per_remote_ip', + 'max_remote_ips_per_local_port', +], 0); +$metrics = static function (int $counter, int $flows = 0) use ($flow): array { + $outbound = $flow; + $outbound['tcp_active_flows'] = $flows; + $outbound['tcp_new_flows'] = $flows; + return [ + 'interface' => [ + 'rx_bytes' => $counter, 'tx_bytes' => $counter * 2, + 'rx_packets' => intdiv($counter, 10), + 'tx_packets' => intdiv($counter, 5), + 'rx_errors' => 0, 'tx_errors' => 0, + 'rx_drops' => 0, 'tx_drops' => 0, + ], + 'ipv4' => ['inbound' => $flow, 'outbound' => $outbound], + 'ipv6' => ['inbound' => $flow, 'outbound' => $flow], + ]; +}; +$summary = static function (array $metrics, array $rates): string { + return json_encode([ + 'rates' => $rates, + 'sample_count' => 80, + 'warmup_remaining' => 0, + 'quality_flags' => [], + 'metrics' => $metrics, + ], JSON_THROW_ON_ERROR); +}; +$rate = ['rx_bps' => 800, 'tx_bps' => 1600, + 'rx_pps' => 10, 'tx_pps' => 20]; +$main->table('cloud_abuse_instance_states')->insert([ + ['instance_id' => 1, 'user_id' => 11, 'host_id' => 1, + 'risk_state' => 'normal', 'collection_state' => 'healthy', + 'latest_direction' => 'none', 'latest_rule_code' => '', + 'latest_severity' => 'none', + 'summary_json' => $summary($metrics(1000), $rate), + 'last_bucket_start' => $now - 60, + 'last_sample_at' => $now - 1, 'last_evaluated_at' => $now - 1, + 'updated_at' => $now - 1], + ['instance_id' => 2, 'user_id' => 12, 'host_id' => 1, + 'risk_state' => 'high_risk', 'collection_state' => 'healthy', + 'latest_direction' => 'outbound', + 'latest_rule_code' => 'outbound_tcp_syn_sweep', + 'latest_severity' => 'high', + 'summary_json' => $summary($metrics(2000, 6000), $rate), + 'last_bucket_start' => $now - 60, + 'last_sample_at' => $now - 2, 'last_evaluated_at' => $now - 2, + 'updated_at' => $now - 2], + ['instance_id' => 3, 'user_id' => 11, 'host_id' => 2, + 'risk_state' => 'high_risk', 'collection_state' => 'healthy', + 'latest_direction' => 'inbound', + 'latest_rule_code' => 'inbound_tcp_syn_attack', + 'latest_severity' => 'high', + 'summary_json' => $summary($metrics(3000), $rate), + 'last_bucket_start' => $now - 600, + 'last_sample_at' => $now - 540, + 'last_evaluated_at' => $now - 540, + 'updated_at' => $now - 540], + ['instance_id' => 4, 'user_id' => 11, 'host_id' => 1, + 'risk_state' => 'normal', 'collection_state' => 'healthy', + 'latest_direction' => 'none', 'latest_rule_code' => '', + 'latest_severity' => 'none', 'summary_json' => '{broken', + 'last_bucket_start' => $now - 60, + 'last_sample_at' => $now - 1, 'last_evaluated_at' => $now - 1, + 'updated_at' => $now - 1], +]); +$main->table('cloud_abuse_findings')->insert([ + 'finding_key' => hash('sha256', 'finding'), + 'instance_id' => 2, 'user_id' => 12, 'host_id' => 1, + 'rule_code' => 'outbound_tcp_syn_sweep', 'rule_version' => 1, + 'direction' => 'outbound', 'ip_family' => 'ipv6', + 'severity' => 'high', 'state' => 'open', + 'first_seen_at' => $now - 120, 'last_seen_at' => $now - 60, + 'consecutive_windows' => 2, + 'evidence_json' => json_encode([ + 'tcp_syn_flows' => 6000, 'unique_remote_ips' => 700, + ], JSON_THROW_ON_ERROR), + 'resolved_at' => null, 'created_at' => $now - 120, + 'updated_at' => $now - 60, +]); +$log->table('cloud_vm_network_monitor_cursors')->insert([ + 'host_id' => 1, 'collection_state' => 'healthy', + 'last_success_at' => $now - 1, +]); +for ($index = 0; $index < 61; ++$index) { + $bucket = $now - (60 * (60 - $index)); + $log->table('cloud_vm_network_observation_samples')->insert([ + 'sample_key' => hash('sha256', 'sample-' . $index), + 'instance_id' => 2, 'bucket_start' => $bucket, + 'complete' => 1, + 'metrics_json' => json_encode( + $metrics(1000 + $index * 600, 100 + $index), + JSON_THROW_ON_ERROR + ), + 'quality_flags_json' => '[]', + ]); +} + +$heartbeat = static fn (): array => [[ + 'key' => 'abuse_monitor', 'status' => 'ok', + 'last_success_at' => $now - 1, +]]; +$service = new CloudAdminAbuseMonitoringService( + static fn (): int => $now, + $heartbeat +); +$page = $service->page($actor); +$assert($page['summary']['normal'] === 1 + && $page['summary']['high_risk'] === 1 + && $page['summary']['collection_anomaly'] === 2 + && $page['summary']['total'] === 4, + 'Risk summary did not use the same stale/corrupt boundaries as the list.'); +$assert($page['health']['worker_state'] === 'degraded' + && $page['health']['error_code'] === 'partial_collection_failure', + 'Missing enabled-host coverage was incorrectly projected healthy.'); +$broken = array_values(array_filter( + $page['items'], + static fn (array $row): bool => $row['instance_id'] === '4' +))[0] ?? null; +$assert($broken !== null + && $broken['risk_state'] === 'collection_anomaly' + && $broken['metrics']['rx_bps'] === null + && $broken['metrics']['tcp_active_flows'] === null, + 'Corrupt summary JSON became healthy zero data or caused a page failure.'); + +$high = $service->page($actor, ['risk' => 'high_risk']); +$assert($high['pagination']['total'] === 1 + && $high['items'][0]['instance_id'] === '2', + 'Stale risk state leaked into the verified high-risk filter.'); +$ipv6 = $service->page($actor, ['ip_version' => 'ipv6']); +$assert($ipv6['pagination']['total'] === 1 + && $ipv6['items'][0]['instance_id'] === '2' + && $ipv6['summary']['high_risk'] === 1, + 'IP-family filter and summary scope diverged.'); +$search = $service->page($actor, ['query' => 'alpha']); +$assert($search['pagination']['total'] === 3, + 'Owner/name query did not retain its bounded exact scope.'); + +$insertState = static function ( + int $id, + string $name, + string $risk, + string $summaryJson +) use ($main, $uuid, $now): void { + $main->table('cloud_instances')->insert([ + 'id' => $id, 'uuid' => $uuid($id), 'host_id' => 1, + 'user_id' => 11, 'resource_pool_id' => 4, + 'name' => $name, 'status' => 'active', + ]); + $main->table('cloud_abuse_instance_states')->insert([ + 'instance_id' => $id, 'user_id' => 11, 'host_id' => 1, + 'risk_state' => $risk, 'collection_state' => 'healthy', + 'latest_direction' => 'none', 'latest_rule_code' => '', + 'latest_severity' => 'none', 'summary_json' => $summaryJson, + 'last_bucket_start' => $now - 60, + 'last_sample_at' => $now - 1, + 'last_evaluated_at' => $now - 1, + 'updated_at' => $now - 1, + ]); +}; +$insertState(5, 'missing-summary-shape', 'normal', '{}'); +$insertState(6, 'oversized-summary', 'normal', json_encode([ + 'rates' => $rate, + 'sample_count' => 80, + 'warmup_remaining' => 0, + 'quality_flags' => [], + 'metrics' => $metrics(6000), + 'padding' => str_repeat('x', 140000), +], JSON_THROW_ON_ERROR)); +$insertState(7, 'junk-metrics', 'normal', json_encode([ + 'rates' => $rate, + 'sample_count' => 80, + 'warmup_remaining' => 0, + 'quality_flags' => [], + 'metrics' => ['junk' => 1], +], JSON_THROW_ON_ERROR)); +$insertState(8, 'null-rate-warmup', 'observing', json_encode([ + 'rates' => [ + 'rx_bps' => null, 'tx_bps' => null, + 'rx_pps' => null, 'tx_pps' => null, + ], + 'sample_count' => 1, + 'warmup_remaining' => 79, + 'quality_flags' => [], + 'metrics' => $metrics(8000), +], JSON_THROW_ON_ERROR)); + +$adversarialPage = $service->page($actor, ['per_page' => 100]); +$adversarialAnomalies = $service->page($actor, [ + 'risk' => 'collection_anomaly', 'per_page' => 100, +]); +$adversarialNormal = $service->page($actor, [ + 'risk' => 'normal', 'per_page' => 100, +]); +$adversarialObserving = $service->page($actor, [ + 'risk' => 'observing', 'per_page' => 100, +]); +$anomalyIds = array_column($adversarialAnomalies['items'], 'instance_id'); +$normalIds = array_column($adversarialNormal['items'], 'instance_id'); +$assert($adversarialPage['pagination']['total'] === 8 + && $adversarialPage['summary']['total'] === 8 + && $adversarialPage['summary']['collection_anomaly'] === 5 + && $adversarialAnomalies['pagination']['total'] === 5 + && array_diff(['5', '6', '7'], $anomalyIds) === [] + && array_intersect(['5', '6', '7'], $normalIds) === [] + && $adversarialObserving['pagination']['total'] === 1 + && $adversarialObserving['items'][0]['instance_id'] === '8' + && $adversarialObserving['items'][0]['sample']['quality'] === 'complete', + 'Strict summary classification diverged for missing, oversized, junk or nullable-rate data.'); + +$detail = $service->detail($actor, 2); +$encoded = json_encode($detail, JSON_THROW_ON_ERROR); +$assert(count($detail['series']) === 60 + && $detail['series'][0]['tx_bps'] === 160 + && count($detail['findings']) === 1 + && str_contains($detail['findings'][0]['evidence_summary'], + 'TCP SYN Flow 6,000') + && !str_contains($encoded, 'evidence_json') + && !str_contains($encoded, 'metrics_json') + && !str_contains($encoded, 'quality_flags_json') + && !str_contains($encoded, 'remote_addresses') + && !str_contains($encoded, '"remote_ports"'), + 'Detail response leaked raw evidence or failed its bounded trend contract.'); + +$main->table('cloud_abuse_findings')->insert([ + 'finding_key' => hash('sha256', 'mixed-family-finding'), + 'instance_id' => 1, 'user_id' => 11, 'host_id' => 1, + 'rule_code' => 'outbound_udp_fanout', 'rule_version' => 1, + 'direction' => 'outbound', 'ip_family' => 'mixed', + 'severity' => 'suspicious', 'state' => 'open', + 'first_seen_at' => $now - 120, 'last_seen_at' => $now - 60, + 'consecutive_windows' => 2, 'evidence_json' => '{}', + 'resolved_at' => null, 'created_at' => $now - 120, + 'updated_at' => $now - 60, +]); +$mixedIpv4 = $service->page($actor, [ + 'ip_version' => 'ipv4', 'per_page' => 100, +]); +$mixedIpv6 = $service->page($actor, [ + 'ip_version' => 'ipv6', 'per_page' => 100, +]); +$assert(array_column($mixedIpv4['items'], 'instance_id') === ['1'] + && array_diff( + ['1', '2'], + array_column($mixedIpv6['items'], 'instance_id') + ) === [], + 'Mixed-family open findings were not visible from both IP-family filters.'); + +$main->table('cloud_abuse_findings')->insert([ + [ + 'finding_key' => hash('sha256', 'old-owner-finding'), + 'instance_id' => 2, 'user_id' => 11, 'host_id' => 1, + 'rule_code' => 'outbound_udp_fanout', 'rule_version' => 1, + 'direction' => 'outbound', 'ip_family' => 'ipv4', + 'severity' => 'suspicious', 'state' => 'resolved', + 'first_seen_at' => $now - 180, + 'last_seen_at' => $now - 30, + 'consecutive_windows' => 2, + 'evidence_json' => '{"udp_new_flows":200}', + 'resolved_at' => $now - 20, 'created_at' => $now - 180, + 'updated_at' => $now - 20, + ], + [ + 'finding_key' => hash('sha256', 'oversized-evidence'), + 'instance_id' => 2, 'user_id' => 12, 'host_id' => 1, + 'rule_code' => 'inbound_tcp_port_scan', 'rule_version' => 1, + 'direction' => 'inbound', 'ip_family' => 'ipv4', + 'severity' => 'suspicious', 'state' => 'open', + 'first_seen_at' => $now - 50, + 'last_seen_at' => $now - 10, + 'consecutive_windows' => 2, + 'evidence_json' => json_encode([ + 'tcp_new_flows' => 20, + 'padding' => str_repeat('y', 140000), + ], JSON_THROW_ON_ERROR), + 'resolved_at' => null, 'created_at' => $now - 50, + 'updated_at' => $now - 10, + ], +]); +$log->table('cloud_vm_network_observation_samples')->insert([ + [ + 'sample_key' => hash('sha256', 'junk-series'), + 'instance_id' => 2, 'bucket_start' => $now + 60, + 'complete' => 1, 'metrics_json' => '{"junk":1}', + 'quality_flags_json' => '[]', + ], + [ + 'sample_key' => hash('sha256', 'oversized-series-metrics'), + 'instance_id' => 2, 'bucket_start' => $now + 120, + 'complete' => 1, 'metrics_json' => json_encode([ + 'interface' => $metrics(9000)['interface'], + 'ipv4' => $metrics(9000)['ipv4'], + 'ipv6' => $metrics(9000)['ipv6'], + 'padding' => str_repeat('z', 140000), + ], JSON_THROW_ON_ERROR), + 'quality_flags_json' => '[]', + ], + [ + 'sample_key' => hash('sha256', 'oversized-series-flags'), + 'instance_id' => 2, 'bucket_start' => $now + 180, + 'complete' => 1, + 'metrics_json' => json_encode($metrics(9600), JSON_THROW_ON_ERROR), + 'quality_flags_json' => json_encode([ + str_repeat('q', 140000), + ], JSON_THROW_ON_ERROR), + ], +]); +$adversarialDetail = $service->detail($actor, 2); +$adversarialEncoded = json_encode( + $adversarialDetail, + JSON_THROW_ON_ERROR +); +$recentSeries = array_slice($adversarialDetail['series'], -3); +$assert(count($adversarialDetail['series']) === 60 + && array_column($recentSeries, 'quality') === [ + 'incomplete', 'incomplete', 'incomplete', + ] + && count($adversarialDetail['findings']) === 2 + && !in_array( + 'outbound_udp_fanout', + array_column($adversarialDetail['findings'], 'rule_code'), + true + ) + && str_contains( + $adversarialDetail['findings'][0]['evidence_summary'], + '聚合证据不可用' + ) + && !str_contains($adversarialEncoded, str_repeat('y', 1000)) + && !str_contains($adversarialEncoded, str_repeat('z', 1000)), + 'Detail admitted malformed samples, oversized JSON or a previous owner finding.'); + +$noHeartbeat = new CloudAdminAbuseMonitoringService( + static fn (): int => $now, + static fn (): array => [] +); +$unavailable = $noHeartbeat->page($actor); +$assert($unavailable['health']['worker_state'] === 'unavailable' + && $unavailable['health']['error_code'] === 'monitor_worker_unavailable', + 'Historical cursors were used to invent a healthy worker.'); + +foreach ([ + ['actor' => ['user_id' => 1, 'is_admin' => false], 'input' => []], + ['actor' => $actor, 'input' => ['risk' => 'invented']], + ['actor' => $actor, 'input' => ['unknown' => '1']], + ['actor' => $actor, 'input' => ['per_page' => '101']], +] as $case) { + try { + $service->page($case['actor'], $case['input']); + throw new RuntimeException('Invalid admin request was accepted.'); + } catch (CloudError $error) { + $assert(in_array($error->httpStatus, [400, 403], true), + 'Invalid request returned an unexpected error class.'); + } +} + +$source = file_get_contents(dirname(__DIR__) + . '/app/Services/Cloud/CloudAdminAbuseMonitoringService.php'); +$controller = file_get_contents(dirname(__DIR__) + . '/app/Controllers/CloudAdminAbuseMonitoringController.php'); +$assert(is_string($source) && is_string($controller) + && !str_contains($source, 'cloud_jobs') + && !str_contains($source, 'service_status') + && !str_contains($source, 'network_status') + && !str_contains($source, 'remote_addresses') + && str_contains($controller, "'Cache-Control', 'private, no-store'") + && !preg_match('/->(?:insert|update|delete|lockForUpdate)\s*\(/', $source), + 'Administrator monitor gained a mutation, enforcement or raw-flow path.'); + +echo "Administrator abuse-monitoring service tests passed.\n";