feat(abuse): deliver durable Telegram alerts

go
codex 1 week ago
parent fa66714db9
commit b54bde8bce

@ -160,9 +160,9 @@ libvirt 的持久 pool 定义就是 Agent 后续使用的映射,不另写存
公网网络接入在宿主机的“网络接入”中按资源池线路独立配置,可使用 `bridge:br0`、其他 Linux 网桥或 `network:default` 这样的 libvirt network。名称以字母或数字开头,后续只能包含字母、数字、点、下划线和短横线,bridge 名称为 1~15 字符,libvirt network 名称为 1~64 字符。宿主机上的公网网桥或 libvirt network 必须由运维预先准备。Agent 只发现、验证和使用现有公网网络,不创建公网网桥、不启动 libvirt network,也不修改公网地址、上游网关或挂载。`cloud_hosts.default_network` 仅保留为兼容展示值,不参与 Agent 就绪、调度、恢复、迁移或换 IP 决策;真正的运行时权威是资源池线路、当前 Agent 代次已验证的网络接入,以及绑定该接入的公网地址池。VPC 使用下述独立的 Agent 受管 bridge 和 L3 路由,不改变这一公网边界。
当前完整 schema 版本为 `go-agent-wss-v45-abuse-alert-events`。从 v44 升级时,必须先停止滥用观测与投影 worker,为既有 finding 回填 `occurrence_generation = 1` 与 `transition_revision = 1`,再创建 `cloud_abuse_alert_events` 和 `cloud_abuse_alert_event_items`。历史 finding 不反向合成事件;事件只从升级后主库事务内真实发生的 `opened`、`escalated` 和 `evidence_clear` 解决转移产生。完成列、约束、索引和外键核对后才能恢复 worker。
当前完整 schema 版本为 `go-agent-wss-v46-abuse-alert-delivery`。从 v44 升级时,必须先停止滥用观测与投影 worker,为既有 finding 回填 `occurrence_generation = 1` 与 `transition_revision = 1`,再创建 `cloud_abuse_alert_events` 和 `cloud_abuse_alert_event_items`;从 v45 升级还必须停止通知 worker,创建 `cloud_abuse_alert_deliveries`、`administrator_telegram_delivery_gate` 并写入唯一 gate 行。历史 finding 不反向合成事件;事件只从升级后主库事务内真实发生的 `opened`、`escalated` 和 `evidence_clear` 解决转移产生。完成列、约束、索引、外键和 gate 核对后,先恢复滥用观测,再恢复通知 worker。
事件 header 与 projection receipt 在同一个主库事务中写入,且事件先于 receipt;同一实例同一采样桶的多规则、双协议族转移合并为一个 header 和多个 item。`continued` 不产生事件,`policy_replaced` 与 `owner_changed` 只关闭 finding,不能伪造“恢复”事件。item 已冻结当时的聚合 evidence、规则身份和 finding 代次,后续投递不得回读会变化的 finding。v45 尚未加入 Telegram 投递状态,因此这些事件暂不进入 180 天清理;待投递账本具有明确终态后再启用保留策略。删除用户时按事件冻结的 `user_id` 显式删除 header,item 由外键级联,避免实例转交后遗留原租户数据。
事件 header 与 projection receipt 在同一个主库事务中写入,且事件先于 receipt;同一实例同一采样桶的多规则、双协议族转移合并为一个 header 和多个 item。`continued` 不产生事件,`policy_replaced` 与 `owner_changed` 只关闭 finding,不能伪造“恢复”事件。item 已冻结当时的聚合 evidence、规则身份和 finding 代次,后续投递不得回读会变化的 finding。v46 为每个事件建立至多一条 Telegram 投递决策;远端调用开始后的中断或回执不确定统一进入 `unknown`,禁止自动重放。删除用户时按事件冻结的 `user_id` 显式删除 header,item 与投递账本按当前外键和账户清理规则收敛,避免实例转交后遗留原租户数据。
`occurrence_generation` 与 `transition_revision` 只描述当前仍保留的 finding 生命周期,不是跨 retention 的全局幂等键;旧 finding 被清理后,同一规则以后允许从 `1/1` 重新建立。未来投递只能以不可变的 `event_id` / `item_key` 建立幂等边界,不能使用 generation/revision 猜测是否已经外发。账户删除与投影都必须遵循“先锁 user、再锁 instance”的顺序;正式部署还应在真实 MySQL 双连接下验收删除和投影并发,SQLite 行为测试不等价于 InnoDB 行锁门禁。
@ -266,7 +266,7 @@ systemctl enable --now virt-cloud-worker.service \
`virt-cloud-storage-health-worker.service` 与副本复制 worker 彻底隔离:它每次只领取一个到期后端,在数据库事务外执行最长 15 秒的 `HeadBucket`,并且只能按领取时冻结的配置版本结算。成功只恢复健康证据,不会把人工设置的 `read_only`、`draining` 或 `disabled` 改回 `active`;失败使正在 `active` 的后端转为 `read_only`、按 60 秒到 1 小时退避,新写入随即停止选择它。这个 worker 不执行 PUT、COPY、DELETE 或 Abort,也不把凭据写入心跳、日志或命令行。
`virt-cloud-abuse-monitor.service` 是独立、只读宿主机的网络滥用观测进程。Agent 仅发布每分钟公网接口与 conntrack 的有界聚合计数,不采集报文正文、远端 IP 列表或远端端口列表;PHP 将样本、投影意图与宿主机游标原子写入日志库,再用幂等 receipt 投影管理员只读状态和不可变转移事件。该服务不进入 `cloud_jobs`,不会自动断网、限速、修改防火墙、实例生命周期或账单。当前事件账本也不发送 Telegram;外发决策和投递效果将在独立阶段加入,不能从事件存在本身推断已经通知。采集缺口、Agent generation 变化及不完整样本均按异常证据显示,不会当作正常零值参与判定。正式启用前需确认宿主机 conntrack 可读、Agent 网络观测能力可用,并同时检查 `abuse_monitor` 心跳与后台采集健康状态。
`virt-cloud-abuse-monitor.service` 是独立、只读宿主机的网络滥用观测进程。Agent 仅发布每分钟公网接口与 conntrack 的有界聚合计数,不采集报文正文、远端 IP 列表或远端端口列表;PHP 将样本、投影意图与宿主机游标原子写入日志库,再用幂等 receipt 投影管理员只读状态和不可变转移事件。该服务不进入 `cloud_jobs`,不会自动断网、限速、修改防火墙、实例生命周期或账单。Telegram 外发由通知 worker 根据不可变事件和独立投递账本处理;不能从事件存在本身推断已经通知,也不能把 `dispatching` 或 `unknown` 当作可安全重发。采集缺口、Agent generation 变化及不完整样本均按异常证据显示,不会当作正常零值参与判定。正式启用前需确认宿主机 conntrack 可读、Agent 网络观测能力可用,并同时检查 `abuse_monitor` 心跳、通知 worker 与后台采集健康状态。
实例补偿的批次、明细和收件人账本已包含在当前完整 schema 中。`php xcat cloudInit` 只能为全新空数据库创建和验证当前全部结构,不能冒充既有数据库的增量升级。既有安装发布前必须备份数据库,使用经过审核的一次性迁移落地当前完整结构并核对 schema 指纹;升级成功后删除迁移脚本。然后按本节重启普通、通知及对象存储 workers。补偿本身不调用 Agent/Gateway,因此仅补偿功能不要求更新或重新编译宿主机 Agent 和面板 Gateway。完整语义见 [`SERVICE_COMPENSATION.md`](SERVICE_COMPENSATION.md)。

@ -47,7 +47,7 @@
## 部署
全新数据库必须由当前 `CloudSetup` 创建并验证 `go-agent-wss-v45-abuse-alert-events` 完整 schema;其中补偿模块包括:
全新数据库必须由当前 `CloudSetup` 创建并验证 `go-agent-wss-v46-abuse-alert-delivery` 完整 schema;其中补偿模块包括:
- `cloud_service_compensations`;
- `cloud_service_compensation_items`;

@ -8,7 +8,7 @@ use App\Services\Config;
/** Initializes only the clean-break PHP business and Agent log schemas. */
final class CloudSetup
{
private const SCHEMA_VERSION = 'go-agent-wss-v45-abuse-alert-events';
private const SCHEMA_VERSION = 'go-agent-wss-v46-abuse-alert-delivery';
/** Tables installed by ss.sql before cloudInit and owned by this schema. */
private const PREINSTALLED_MAIN_TABLES = [
'ui_copy_locales', 'ui_copy_entries', 'ui_copy_drafts',
@ -68,6 +68,8 @@ final class CloudSetup
'cloud_abuse_policy_revisions', 'cloud_abuse_policy_rules',
'cloud_abuse_instance_states', 'cloud_abuse_findings',
'cloud_abuse_alert_events', 'cloud_abuse_alert_event_items',
'cloud_abuse_alert_deliveries',
'administrator_telegram_delivery_gate',
'cloud_abuse_projection_receipts',
'cloud_jobs', 'cloud_agent_completion_events',
'cloud_ssh_keys',
@ -630,6 +632,16 @@ final class CloudSetup
'to_severity', 'evidence_json', 'resolution_reason',
'occurred_at', 'created_at',
],
'cloud_abuse_alert_deliveries' => [
'id', 'event_id', 'binding_generation', 'bound_admin_id',
'chat_id', 'telegram_message_id', 'state', 'message_text',
'content_hash', 'error_code', 'skip_reason',
'dispatch_started_at', 'sent_at', 'terminal_at',
'destination_redacted_at', 'created_at', 'updated_at',
],
'administrator_telegram_delivery_gate' => [
'id', 'next_send_at', 'updated_at',
],
'cloud_abuse_projection_receipts' => [
'sample_key', 'instance_id', 'rule_version', 'policy_revision',
'policy_hash', 'bucket_start', 'projected_at',
@ -834,6 +846,15 @@ final class CloudSetup
'chk_cloud_abuse_alert_item_scope',
'chk_cloud_abuse_alert_item_transition',
],
'cloud_abuse_alert_deliveries' => [
'chk_cloud_abuse_alert_delivery_state',
'chk_cloud_abuse_alert_delivery_content',
'chk_cloud_abuse_alert_delivery_destination',
'chk_cloud_abuse_alert_delivery_shape',
],
'administrator_telegram_delivery_gate' => [
'chk_administrator_telegram_delivery_gate',
],
'cloud_abuse_projection_receipts' => [
'chk_cloud_abuse_projection_key',
'chk_cloud_abuse_projection_policy',
@ -1160,6 +1181,14 @@ final class CloudSetup
'cloud_abuse_alert_item_event',
'cloud_abuse_alert_item_finding',
],
'cloud_abuse_alert_deliveries' => [
'PRIMARY', 'cloud_abuse_alert_delivery_event',
'cloud_abuse_alert_delivery_message',
'cloud_abuse_alert_delivery_due',
'cloud_abuse_alert_delivery_admin',
'cloud_abuse_alert_delivery_terminal',
],
'administrator_telegram_delivery_gate' => ['PRIMARY'],
'cloud_abuse_projection_receipts' => [
'PRIMARY', 'cloud_abuse_projection_instance',
'cloud_abuse_projection_retention',
@ -1802,6 +1831,27 @@ final class CloudSetup
'columns' => ['event_id', 'id'],
],
],
'cloud_abuse_alert_deliveries' => [
'cloud_abuse_alert_delivery_event' => [
'non_unique' => 0,
'columns' => ['event_id'],
],
'cloud_abuse_alert_delivery_message' => [
'non_unique' => 0,
'columns' => [
'binding_generation', 'chat_id',
'telegram_message_id',
],
],
'cloud_abuse_alert_delivery_due' => [
'non_unique' => 1,
'columns' => ['state', 'updated_at', 'id'],
],
'cloud_abuse_alert_delivery_terminal' => [
'non_unique' => 1,
'columns' => ['terminal_at', 'state', 'id'],
],
],
'cloud_abuse_projection_receipts' => [
'PRIMARY' => [
'non_unique' => 0,
@ -1928,6 +1978,9 @@ final class CloudSetup
'cloud_abuse_alert_event_items' => [
'fk_cloud_abuse_alert_item_event',
],
'cloud_abuse_alert_deliveries' => [
'fk_cloud_abuse_alert_delivery_event',
],
'cloud_abuse_projection_receipts' => [
'fk_cloud_abuse_projection_instance',
],

@ -0,0 +1,493 @@
<?php
namespace App\Services\Cloud;
use App\Models\User;
use App\Services\Telegram\AdministratorTelegramChannel;
use App\Services\Telegram\AdministratorTelegramDeliveryChannel;
use App\Services\Telegram\AdministratorTelegramDestination;
use App\Services\Telegram\AdministratorTelegramLocalUnavailable;
use App\Services\Telegram\AdministratorTelegramPreflightException;
use App\Services\Telegram\AdministratorTelegramRateBacklog;
use App\Services\Telegram\AdministratorTelegramRemoteEffectUnknown;
/** Durable, at-most-once Telegram delivery for immutable abuse alerts. */
final class CloudAbuseAlertDeliveryService
{
public const DISPATCH_STALE_SECONDS = 30;
private readonly \Closure $clock;
private readonly \Closure $afterRemoteSuccess;
public function __construct(
private readonly ?AdministratorTelegramDeliveryChannel $channel = null,
private readonly ?CloudAbuseAlertMessageFormatter $formatter = null,
?callable $clock = null,
?callable $afterRemoteSuccess = null
) {
$this->clock = \Closure::fromCallable(
$clock ?? static fn (): int => time()
);
$this->afterRemoteSuccess = \Closure::fromCallable(
$afterRemoteSuccess ?? static function (int $messageId): void {}
);
}
/** Process at most one stale fence and one fresh event/delivery. */
public function tick(): bool
{
$now = $this->now();
if ($this->settleStaleDispatch($now)) {
return true;
}
$reserved = $this->reserveNextEvent($now);
if ($reserved === 'local_unavailable') {
throw new \RuntimeException(
'Abuse alert Telegram channel is temporarily unavailable'
);
}
if ($reserved === 'terminal') {
return true;
}
return $this->dispatchNextPending($now)
|| $reserved === 'pending';
}
/** @return 'none'|'pending'|'terminal'|'local_unavailable' */
private function reserveNextEvent(int $now): string
{
$main = self::connection();
$event = $main->table('cloud_abuse_alert_events as event')
->whereNotExists(static function ($query): void {
$query->selectRaw('1')
->from('cloud_abuse_alert_deliveries as delivery')
->whereColumn('delivery.event_id', 'event.id');
})->orderBy('event.id')->first(['event.*']);
if ($event === null) {
return 'none';
}
$items = $main->table('cloud_abuse_alert_event_items')
->where('event_id', (int) $event->id)
->orderBy('id')->get()->all();
$formatFailed = false;
try {
$message = $this->messageFormatter()->format($event, $items);
} catch (\Throwable $error) {
$formatFailed = true;
$message = '网络滥用告警事件格式无效,请在管理后台核查事件 #'
. (int) $event->id . '。';
}
self::assertFrozenMessage($message);
$destination = null;
if (!$formatFailed) {
try {
$destination = $this->telegramChannel()
->currentDestination();
} catch (AdministratorTelegramLocalUnavailable $error) {
return 'local_unavailable';
}
}
return $main->transaction(function () use (
$main, $event, $message, $destination, $formatFailed, $now
): string {
$effectiveDestination = $destination;
if ($effectiveDestination !== null) {
// Lock recipient before event, matching account deletion's
// user -> delivery/event order. This fences a late pending
// row from appearing after that user's deletion scan.
$administrator = $main->table('user')
->where('id', $effectiveDestination->administratorId)
->lockForUpdate()->first(['id', 'is_admin', 'enable']);
if ($administrator === null
|| (int) $administrator->is_admin !== 1
|| (int) $administrator->enable !== 1) {
$effectiveDestination = null;
}
}
$locked = $main->table('cloud_abuse_alert_events')
->where('id', (int) $event->id)
->where('event_key', (string) $event->event_key)
->lockForUpdate()->first();
if ($locked === null || $main->table(
'cloud_abuse_alert_deliveries'
)->where('event_id', (int) $event->id)->exists()) {
return 'none';
}
$state = 'pending';
$errorCode = '';
$skipReason = '';
$terminalAt = null;
if ($formatFailed) {
$state = 'failed';
$errorCode = 'message_format_invalid';
$terminalAt = $now;
} elseif ($effectiveDestination === null) {
$state = 'skipped';
$skipReason = $destination === null
? 'channel_unavailable' : 'destination_ineligible';
$terminalAt = $now;
}
$main->table('cloud_abuse_alert_deliveries')->insert([
'event_id' => (int) $locked->id,
'binding_generation' =>
$effectiveDestination?->bindingGeneration,
'bound_admin_id' =>
$effectiveDestination?->administratorId,
'chat_id' => $effectiveDestination?->chatId,
'telegram_message_id' => null,
'state' => $state,
'message_text' => $message,
'content_hash' => hash('sha256', $message),
'error_code' => $errorCode,
'skip_reason' => $skipReason,
'dispatch_started_at' => null,
'sent_at' => null,
'terminal_at' => $terminalAt,
'destination_redacted_at' => null,
'created_at' => $now,
'updated_at' => $now,
]);
return $state === 'pending' ? 'pending' : 'terminal';
}, 3);
}
private function dispatchNextPending(int $now): bool
{
$main = self::connection();
$claim = $main->transaction(function () use ($main, $now): ?array {
$candidate = $main->table('cloud_abuse_alert_deliveries')
->where('state', 'pending')->orderBy('created_at')
->orderBy('id')->first([
'id', 'event_id', 'bound_admin_id',
]);
if ($candidate === null) {
return null;
}
$administrator = $main->table('user')
->where('id', (int) $candidate->bound_admin_id)
->lockForUpdate()->first(['id', 'is_admin', 'enable']);
$event = $main->table('cloud_abuse_alert_events')
->where('id', (int) $candidate->event_id)
->lockForUpdate()->first(['id']);
if ($event === null) {
return null;
}
$delivery = $main->table('cloud_abuse_alert_deliveries')
->where('id', (int) $candidate->id)
->where('event_id', (int) $event->id)
->where('state', 'pending')
->lockForUpdate()->first();
if ($delivery === null) {
return null;
}
if ($administrator === null
|| (int) $administrator->is_admin !== 1
|| (int) $administrator->enable !== 1
|| !$this->telegramChannel()->eligibleAdministrator(
(int) $administrator->id
)) {
$updated = $main->table('cloud_abuse_alert_deliveries')
->where('id', (int) $delivery->id)
->where('state', 'pending')->update([
'state' => 'skipped',
'skip_reason' => 'destination_ineligible',
'terminal_at' => $now,
'updated_at' => $now,
]);
if ($updated !== 1) {
throw new \RuntimeException(
'Abuse alert recipient settlement failed'
);
}
return ['terminal' => true];
}
$message = (string) $delivery->message_text;
if (!self::validDestination($delivery)
|| !self::validFrozenMessage(
$message,
(string) $delivery->content_hash
)) {
$updated = $main->table('cloud_abuse_alert_deliveries')
->where('id', (int) $delivery->id)
->where('state', 'pending')->update([
'state' => 'failed',
'error_code' => 'delivery_identity_invalid',
'terminal_at' => $now,
'updated_at' => $now,
]);
if ($updated !== 1) {
throw new \RuntimeException(
'Abuse alert invalid delivery finalization failed'
);
}
return ['terminal' => true];
}
$updated = $main->table('cloud_abuse_alert_deliveries')
->where('id', (int) $delivery->id)
->where('state', 'pending')->update([
'state' => 'dispatching',
'dispatch_started_at' => $now,
'updated_at' => $now,
]);
if ($updated !== 1) {
throw new \RuntimeException('Abuse alert delivery claim failed');
}
return [
'id' => (int) $delivery->id,
'message' => $message,
'destination' => new AdministratorTelegramDestination(
(int) $delivery->bound_admin_id,
(string) $delivery->chat_id,
(string) $delivery->binding_generation
),
];
}, 3);
if ($claim === null) {
return false;
}
if (($claim['terminal'] ?? false) === true) {
return true;
}
return $this->sendClaim($claim, $now);
}
/** @param array{id:int,message:string,destination:AdministratorTelegramDestination} $claim */
private function sendClaim(array $claim, int $now): bool
{
$id = $claim['id'];
try {
$messageId = $this->telegramChannel()->send(
$claim['destination'],
$claim['message'],
['disable_web_page_preview' => 'true']
);
} catch (AdministratorTelegramRateBacklog $error) {
$this->returnToPending($id, $now);
throw new \RuntimeException(
'Abuse alert Telegram delivery backlog is full'
);
} catch (AdministratorTelegramLocalUnavailable $error) {
$this->returnToPending($id, $now);
throw new \RuntimeException(
'Abuse alert Telegram channel is temporarily unavailable'
);
} catch (AdministratorTelegramPreflightException $error) {
if ($error->reason
=== AdministratorTelegramPreflightException::PAYLOAD_INVALID) {
$this->failInvalidPayload($id, $now);
} else {
$this->skipPreflight(
$id,
$now,
$error->reason
=== AdministratorTelegramPreflightException::DESTINATION_CHANGED
? 'destination_changed'
: 'channel_disabled_or_unavailable'
);
}
return true;
} catch (AdministratorTelegramRemoteEffectUnknown $error) {
$this->markUnknown($id, $now, null);
throw new \RuntimeException(
'Abuse alert Telegram remote effect is unknown'
);
} catch (\Throwable $error) {
$this->markUnknown($id, $now, null);
throw new \RuntimeException(
'Abuse alert Telegram remote effect is unknown'
);
}
try {
($this->afterRemoteSuccess)($messageId);
$updated = self::connection()->table(
'cloud_abuse_alert_deliveries'
)->where('id', $id)->where('state', 'dispatching')->update([
'telegram_message_id' => $messageId,
'state' => 'sent',
'sent_at' => $now,
'terminal_at' => $now,
'updated_at' => $now,
]);
if ($updated !== 1) {
throw new \RuntimeException(
'Abuse alert Telegram delivery finalization failed'
);
}
} catch (\Throwable $error) {
$this->markUnknown($id, $now, $messageId);
throw new \RuntimeException(
'Abuse alert Telegram local finalization is unknown'
);
}
return true;
}
private function settleStaleDispatch(int $now): bool
{
$main = self::connection();
return $main->transaction(function () use ($main, $now): bool {
$row = $main->table('cloud_abuse_alert_deliveries')
->where('state', 'dispatching')
->where('updated_at', '<=',
$now - self::DISPATCH_STALE_SECONDS)
->orderBy('updated_at')->orderBy('id')
->lockForUpdate()->first();
if ($row === null) {
return false;
}
$updated = $main->table('cloud_abuse_alert_deliveries')
->where('id', (int) $row->id)
->where('state', 'dispatching')->update([
'state' => 'unknown',
'error_code' => 'worker_interrupted',
'terminal_at' => $now,
'updated_at' => $now,
]);
if ($updated !== 1) {
throw new \RuntimeException(
'Stale abuse alert delivery settlement failed'
);
}
return true;
}, 3);
}
private function returnToPending(int $id, int $now): void
{
$updated = self::connection()->table(
'cloud_abuse_alert_deliveries'
)->where('id', $id)->where('state', 'dispatching')->update([
'state' => 'pending',
'dispatch_started_at' => null,
'updated_at' => $now,
]);
if ($updated !== 1) {
throw new \RuntimeException(
'Abuse alert local retry release failed'
);
}
}
private function skipPreflight(
int $id,
int $now,
string $reason
): void
{
$updated = self::connection()->table(
'cloud_abuse_alert_deliveries'
)->where('id', $id)->where('state', 'dispatching')->update([
'state' => 'skipped',
'skip_reason' => $reason,
'dispatch_started_at' => null,
'terminal_at' => $now,
'updated_at' => $now,
]);
if ($updated !== 1) {
throw new \RuntimeException(
'Abuse alert destination-change settlement failed'
);
}
}
private function failInvalidPayload(int $id, int $now): void
{
$updated = self::connection()->table(
'cloud_abuse_alert_deliveries'
)->where('id', $id)->where('state', 'dispatching')->update([
'state' => 'failed',
'error_code' => 'payload_invalid',
'dispatch_started_at' => null,
'terminal_at' => $now,
'updated_at' => $now,
]);
if ($updated !== 1) {
throw new \RuntimeException(
'Abuse alert payload failure settlement failed'
);
}
}
private function markUnknown(
int $id,
int $now,
?int $messageId
): void {
try {
$updated = self::connection()->table(
'cloud_abuse_alert_deliveries'
)->where('id', $id)->where('state', 'dispatching')->update([
'telegram_message_id' => $messageId,
'state' => 'unknown',
'error_code' => 'remote_effect_unknown',
'terminal_at' => $now,
'updated_at' => $now,
]);
if ($updated !== 1) {
throw new \RuntimeException(
'Abuse alert unknown-effect settlement failed'
);
}
} catch (\Throwable $error) {
// A retained dispatching row remains fenced and will be converted
// by the stale reconciler. It must never be replayed.
error_log('Abuse alert Telegram reconciliation failed');
}
}
private static function validDestination(object $delivery): bool
{
return (int) $delivery->bound_admin_id > 0
&& preg_match('/^[a-f0-9]{32}$/D',
(string) $delivery->binding_generation) === 1
&& preg_match('/^[1-9][0-9]{0,18}$/D',
(string) $delivery->chat_id) === 1;
}
private static function assertFrozenMessage(string $message): void
{
if (!self::validFrozenMessage($message, hash('sha256', $message))) {
throw new \LogicException('Abuse alert message is invalid');
}
}
private static function validFrozenMessage(
string $message,
string $hash
): bool {
$length = mb_strlen($message, 'UTF-8');
return $length >= 1 && $length <= 4096
&& preg_match('/^[a-f0-9]{64}$/D', $hash) === 1
&& hash_equals($hash, hash('sha256', $message));
}
private function telegramChannel(): AdministratorTelegramDeliveryChannel
{
return $this->channel ?? new AdministratorTelegramChannel();
}
private function messageFormatter(): CloudAbuseAlertMessageFormatter
{
return $this->formatter ?? new CloudAbuseAlertMessageFormatter();
}
private function now(): int
{
$now = ($this->clock)();
if (!is_int($now) || $now < 1) {
throw new \LogicException('Abuse alert delivery clock is invalid');
}
return $now;
}
private static function connection(): object
{
return User::getConnectionResolver()->connection('default');
}
}

@ -0,0 +1,317 @@
<?php
namespace App\Services\Cloud;
use App\Services\Config;
/**
* Renders one immutable abuse-alert event into a bounded Telegram message.
*
* The formatter accepts only the event/item ledger snapshot. It never reads
* live instances or findings, so a pending delivery cannot drift after an
* instance is renamed, transferred or deleted.
*/
final class CloudAbuseAlertMessageFormatter
{
private const RULE_LABELS = [
'outbound_tcp_syn_sweep' => '对外 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 数',
];
private const MAX_LENGTH = 4096;
private const CONTENT_BUDGET = 3700;
/**
* @param list<object> $items
*/
public function format(object $event, array $items): string
{
$eventId = self::positiveInteger(self::value($event, 'id'), 'event id');
$eventKey = self::hexIdentity(self::value($event, 'event_key'), 'event key');
$instanceId = self::positiveInteger(
self::value($event, 'instance_id'),
'instance id'
);
$userId = self::positiveInteger(self::value($event, 'user_id'), 'user id');
$hostId = self::positiveInteger(self::value($event, 'host_id'), 'host id');
$bucketStart = self::positiveInteger(
self::value($event, 'bucket_start'),
'bucket start'
);
$ruleVersion = self::positiveInteger(
self::value($event, 'rule_version'),
'rule version'
);
$policyRevision = self::positiveInteger(
self::value($event, 'policy_revision'),
'policy revision'
);
$transitionCount = self::positiveInteger(
self::value($event, 'transition_count'),
'transition count'
);
$highest = (string) self::value($event, 'highest_severity');
if ($bucketStart % 60 !== 0
|| !in_array($highest, ['suspicious', 'high'], true)
|| $transitionCount !== count($items)) {
throw new \InvalidArgumentException('Invalid abuse alert event snapshot');
}
$url = self::adminUrl((string) Config::get('baseUrl', ''), $instanceId);
$normalized = [];
$seen = [];
foreach ($items as $item) {
$row = self::normalizeItem($item, $eventId, $bucketStart);
if (isset($seen[$row['item_key']])) {
throw new \InvalidArgumentException(
'Duplicate abuse alert event item'
);
}
$seen[$row['item_key']] = true;
$normalized[] = $row;
}
usort($normalized, static fn (array $left, array $right): int => [
$left['transition_type'],
$left['direction'],
$left['rule_code'],
$left['ip_family'],
$left['item_key'],
] <=> [
$right['transition_type'],
$right['direction'],
$right['rule_code'],
$right['ip_family'],
$right['item_key'],
]);
$onlyResolved = array_reduce(
$normalized,
static fn (bool $carry, array $item): bool => $carry
&& $item['transition_type'] === 'resolved',
true
);
$title = $onlyResolved
? '✅ 网络风险已恢复'
: ($highest === 'high'
? '🔴 网络滥用高风险告警'
: '🟠 网络异常告警');
$lines = [
$title,
'',
'实例:#' . $instanceId,
'用户:#' . $userId,
'宿主机:#' . $hostId,
'发生时间:' . gmdate('Y-m-d H:i:s', $bucketStart) . ' UTC',
'告警:#' . $eventId,
'策略:r' . $policyRevision . ' / 引擎 v' . $ruleVersion,
];
$included = 0;
foreach ($normalized as $item) {
$block = self::itemLines($item);
$candidate = implode("\n", array_merge($lines, [''], $block));
if (mb_strlen($candidate, 'UTF-8') > self::CONTENT_BUDGET) {
break;
}
$lines[] = '';
array_push($lines, ...$block);
$included++;
}
if ($included < count($normalized)) {
$lines[] = '';
$lines[] = '其他规则:' . (count($normalized) - $included)
. ' 条,请在后台查看完整证据。';
}
$lines[] = '';
$lines[] = '后台:' . $url;
$lines[] = '说明:仅包含聚合元数据,不包含通信正文或远端地址明细。';
$message = implode("\n", $lines);
if (mb_strlen($message, 'UTF-8') > self::MAX_LENGTH) {
throw new \LogicException('Abuse alert message exceeded Telegram limit');
}
return $message;
}
/** @return array<string,mixed> */
private static function normalizeItem(
object $item,
int $eventId,
int $bucketStart
): array {
$itemKey = self::hexIdentity(self::value($item, 'item_key'), 'item key');
$findingKey = self::hexIdentity(
self::value($item, 'finding_key'),
'finding key'
);
$itemEventId = self::positiveInteger(
self::value($item, 'event_id'),
'item event id'
);
$rule = (string) self::value($item, 'rule_code');
$direction = (string) self::value($item, 'direction');
$family = (string) self::value($item, 'ip_family');
$type = (string) self::value($item, 'transition_type');
$from = (string) self::value($item, 'from_severity');
$to = (string) self::value($item, 'to_severity');
$reason = (string) self::value($item, 'resolution_reason');
$occurredAt = self::positiveInteger(
self::value($item, 'occurred_at'),
'item occurred at'
);
if ($itemEventId !== $eventId || $occurredAt !== $bucketStart
|| !isset(self::RULE_LABELS[$rule])
|| !in_array($direction, ['inbound', 'outbound'], true)
|| !in_array($family, ['ipv4', 'ipv6', 'mixed'], true)
|| !self::validTransition($type, $from, $to, $reason)) {
throw new \InvalidArgumentException('Invalid abuse alert event item');
}
$evidence = self::decodeEvidence(self::value($item, 'evidence_json'));
return [
'item_key' => $itemKey,
'finding_key' => $findingKey,
'rule_code' => $rule,
'direction' => $direction,
'ip_family' => $family,
'transition_type' => $type,
'from_severity' => $from,
'to_severity' => $to,
'evidence' => $evidence,
];
}
private static function validTransition(
string $type,
string $from,
string $to,
string $reason
): bool {
return ($type === 'opened' && $from === 'none'
&& in_array($to, ['suspicious', 'high'], true)
&& $reason === '')
|| ($type === 'escalated' && $from === 'suspicious'
&& $to === 'high' && $reason === '')
|| ($type === 'resolved'
&& in_array($from, ['suspicious', 'high'], true)
&& $to === 'none' && $reason === 'evidence_clear');
}
/** @return array<string,int> */
private static function decodeEvidence(mixed $json): array
{
if (!is_string($json) || $json === '' || strlen($json) > 16384) {
throw new \InvalidArgumentException('Invalid abuse alert evidence');
}
try {
$value = json_decode($json, true, 8, JSON_THROW_ON_ERROR);
} catch (\JsonException $error) {
throw new \InvalidArgumentException(
'Invalid abuse alert evidence',
0,
$error
);
}
if (!is_array($value) || array_is_list($value)) {
throw new \InvalidArgumentException('Invalid abuse alert evidence');
}
$normalized = [];
foreach ($value as $key => $count) {
if (!is_string($key) || !isset(self::EVIDENCE_LABELS[$key])
|| !is_int($count) || $count < 0) {
throw new \InvalidArgumentException('Invalid abuse alert evidence');
}
$normalized[$key] = $count;
}
ksort($normalized, SORT_STRING);
return $normalized;
}
/** @param array<string,mixed> $item @return list<string> */
private static function itemLines(array $item): array
{
$transition = [
'opened' => '新触发',
'escalated' => '风险升级',
'resolved' => '已恢复',
][$item['transition_type']];
$icon = $item['transition_type'] === 'resolved' ? '✅'
: (($item['to_severity'] === 'high') ? '🔴' : '🟠');
$direction = $item['direction'] === 'inbound'
? '入站受攻击'
: '出站可疑行为';
$severity = $item['transition_type'] === 'resolved'
? (($item['from_severity'] === 'high') ? '高风险已解除' : '可疑状态已解除')
: (($item['to_severity'] === 'high') ? '高风险' : '可疑');
$evidence = [];
foreach ($item['evidence'] as $key => $count) {
$evidence[] = self::EVIDENCE_LABELS[$key] . ' '
. number_format($count, 0, '.', ',');
}
return [
$icon . ' ' . $transition . ' · ' . self::RULE_LABELS[$item['rule_code']],
'方向:' . $direction . ' · ' . strtoupper($item['ip_family'])
. ' · ' . $severity,
'证据:' . ($evidence === [] ? '未记录聚合计数' : implode(';', $evidence)),
];
}
private static function adminUrl(string $baseUrl, int $instanceId): string
{
$baseUrl = rtrim(trim($baseUrl), '/');
$parts = parse_url($baseUrl);
if ($baseUrl === '' || !is_array($parts)
|| !in_array($parts['scheme'] ?? '', ['http', 'https'], true)
|| empty($parts['host']) || isset($parts['user'])
|| isset($parts['pass']) || isset($parts['query'])
|| isset($parts['fragment'])
|| preg_match('/[\x00-\x20\x7f]/', $baseUrl) === 1) {
throw new \InvalidArgumentException('Invalid administrator base URL');
}
return $baseUrl . '/admin/abuse-monitoring?query=' . $instanceId;
}
private static function positiveInteger(mixed $value, string $label): int
{
if ((!is_int($value) && !is_string($value))
|| preg_match('/^[1-9][0-9]*$/D', (string) $value) !== 1
|| strlen((string) $value) > strlen((string) PHP_INT_MAX)
|| (strlen((string) $value) === strlen((string) PHP_INT_MAX)
&& strcmp((string) $value, (string) PHP_INT_MAX) > 0)) {
throw new \InvalidArgumentException('Invalid abuse alert ' . $label);
}
return (int) $value;
}
private static function hexIdentity(mixed $value, string $label): string
{
$value = is_string($value) ? $value : '';
if (preg_match('/^[a-f0-9]{64}$/D', $value) !== 1) {
throw new \InvalidArgumentException('Invalid abuse alert ' . $label);
}
return $value;
}
private static function value(object $row, string $key): mixed
{
return $row->{$key} ?? null;
}
}

@ -41,7 +41,8 @@ final class CloudAbuseRetentionService
* samples:int,
* baselines:int,
* receipts:int,
* findings:int
* findings:int,
* alert_events:int
* }
*/
public function prune(int $limit = self::DEFAULT_BATCH_SIZE): array
@ -75,6 +76,10 @@ final class CloudAbuseRetentionService
$now
),
'findings' => $this->pruneFindings($readModelCutoff, $limit),
'alert_events' => $this->pruneAlertEvents(
$readModelCutoff,
$limit
),
];
}
@ -402,4 +407,68 @@ final class CloudAbuseRetentionService
->delete();
});
}
/**
* Unknown, pending, dispatching and not-yet-decided events are durable
* safety fences. Only old, terminal, known-effect delivery decisions may
* release their immutable event and item snapshots.
*/
private function pruneAlertEvents(int $cutoff, int $limit): int
{
$main = User::getConnectionResolver()->connection();
$candidateIds = $main->table('cloud_abuse_alert_events')
->where('created_at', '<=', $cutoff)
->whereExists(static function ($query) use ($cutoff): void {
$query->selectRaw('1')
->from('cloud_abuse_alert_deliveries as delivery')
->whereColumn(
'delivery.event_id',
'cloud_abuse_alert_events.id'
)->whereIn('delivery.state', [
'sent', 'failed', 'skipped',
])->whereNotNull('delivery.terminal_at')
->where('delivery.terminal_at', '<=', $cutoff);
})->orderBy('created_at')->orderBy('id')->limit($limit)
->pluck('id')->map(static fn ($id): int => (int) $id)->all();
sort($candidateIds, SORT_NUMERIC);
if ($candidateIds === []) {
return 0;
}
return $main->transaction(static function () use (
$main,
$cutoff,
$candidateIds
): int {
$eventIds = $main->table('cloud_abuse_alert_events')
->whereIn('id', $candidateIds)
->where('created_at', '<=', $cutoff)
->orderBy('id')->lockForUpdate()->pluck('id')
->map(static fn ($id): int => (int) $id)->all();
if ($eventIds === []) {
return 0;
}
$deliveries = $main->table('cloud_abuse_alert_deliveries')
->whereIn('event_id', $eventIds)->orderBy('id')->lockForUpdate()
->get(['event_id', 'state', 'terminal_at']);
$eligible = [];
foreach ($deliveries as $delivery) {
if (in_array((string) $delivery->state, [
'sent', 'failed', 'skipped',
], true) && $delivery->terminal_at !== null
&& (int) $delivery->terminal_at <= $cutoff) {
$eligible[] = (int) $delivery->event_id;
}
}
$eventIds = array_values(array_unique($eligible));
sort($eventIds, SORT_NUMERIC);
if ($eventIds === []) {
return 0;
}
return $main->table('cloud_abuse_alert_events')
->whereIn('id', $eventIds)
->where('created_at', '<=', $cutoff)->delete();
});
}
}

@ -7,12 +7,14 @@ final class CloudNotificationDeliveryService
{
private readonly \Closure $maintenanceTick;
private readonly \Closure $compensationTick;
private bool $maintenanceFirst = true;
private readonly \Closure $abuseAlertTick;
private int $firstChannel = 0;
private array $errors = [];
public function __construct(
?callable $maintenanceTick = null,
?callable $compensationTick = null
?callable $compensationTick = null,
?callable $abuseAlertTick = null
) {
if ($maintenanceTick === null) {
$maintenance = new CloudMaintenanceNoticeDeliveryService();
@ -22,23 +24,30 @@ final class CloudNotificationDeliveryService
$compensation = new CloudServiceCompensationNotificationService();
$compensationTick = [$compensation, 'tick'];
}
if ($abuseAlertTick === null) {
$abuseAlerts = new CloudAbuseAlertDeliveryService();
$abuseAlertTick = [$abuseAlerts, 'tick'];
}
$this->maintenanceTick = \Closure::fromCallable($maintenanceTick);
$this->compensationTick = \Closure::fromCallable($compensationTick);
$this->abuseAlertTick = \Closure::fromCallable($abuseAlertTick);
}
public function tick(): bool
{
$this->errors = [];
$channels = $this->maintenanceFirst
? [
['maintenance_notification_failed', $this->maintenanceTick],
['notification_failed', $this->compensationTick],
]
: [
['notification_failed', $this->compensationTick],
['maintenance_notification_failed', $this->maintenanceTick],
$catalog = [
['maintenance_notification_failed', $this->maintenanceTick],
['notification_failed', $this->compensationTick],
['abuse_alert_notification_failed', $this->abuseAlertTick],
];
$channels = [];
for ($offset = 0; $offset < count($catalog); $offset++) {
$channels[] = $catalog[
($this->firstChannel + $offset) % count($catalog)
];
$this->maintenanceFirst = !$this->maintenanceFirst;
}
$this->firstChannel = ($this->firstChannel + 1) % count($catalog);
$worked = false;
foreach ($channels as [$errorCode, $tick]) {

@ -39,6 +39,7 @@ final class CloudRuntimeHeartbeat
'idle' => '等待下一轮', 'stopped' => '已退出'];
private const ERRORS = ['worker_tick_failed', 'recovery_tick_failed',
'notification_failed', 'maintenance_notification_failed',
'abuse_alert_notification_failed',
'lifecycle_tick_failed', 'provision_expiry_failed', 'invoice_generation_failed',
'automatic_backup_schedule_failed',
'automatic_backup_retention_failed',

@ -14,6 +14,7 @@ use App\Services\TicketTelegramConfig;
* Ticket and abuse-alert ledgers, formatting and reply semantics stay outside.
*/
final class AdministratorTelegramChannel
implements AdministratorTelegramDeliveryChannel
{
private const ALLOWED_OPTIONS = [
'reply_to_message_id',
@ -22,7 +23,8 @@ final class AdministratorTelegramChannel
];
public function __construct(
private readonly AdministratorTelegramTransport $transport = new TelegramBotApiTransport()
private readonly AdministratorTelegramTransport $transport = new TelegramBotApiTransport(),
private readonly AdministratorTelegramRateLimiter $rateLimiter = new DatabaseAdministratorTelegramRateGate()
) {}
public function eligibleAdministrator(int $administratorId): bool
@ -103,12 +105,14 @@ final class AdministratorTelegramChannel
$textLength = mb_strlen($text, 'UTF-8');
if ($textLength < 1 || $textLength > 4096) {
throw new AdministratorTelegramPreflightException(
AdministratorTelegramPreflightException::PAYLOAD_INVALID,
'Telegram message length is invalid'
);
}
foreach (array_keys($options) as $key) {
if (!is_string($key) || !in_array($key, self::ALLOWED_OPTIONS, true)) {
throw new AdministratorTelegramPreflightException(
AdministratorTelegramPreflightException::PAYLOAD_INVALID,
'Telegram send option is not allowed'
);
}
@ -117,6 +121,7 @@ final class AdministratorTelegramChannel
&& (!is_int($options['reply_to_message_id'])
|| $options['reply_to_message_id'] <= 0)) {
throw new AdministratorTelegramPreflightException(
AdministratorTelegramPreflightException::PAYLOAD_INVALID,
'Telegram reply message id is invalid'
);
}
@ -132,6 +137,7 @@ final class AdministratorTelegramChannel
}
if ($config === null) {
throw new AdministratorTelegramPreflightException(
AdministratorTelegramPreflightException::CHANNEL_UNAVAILABLE,
'Administrator Telegram configuration is unavailable'
);
}
@ -144,6 +150,41 @@ final class AdministratorTelegramChannel
}
if ($current === null || !$current->equals($expected)) {
throw new AdministratorTelegramPreflightException(
AdministratorTelegramPreflightException::DESTINATION_CHANGED,
'Administrator Telegram destination changed'
);
}
// This local transaction finishes before the remote boundary. A gate
// failure is therefore known not to have contacted Telegram.
$this->rateLimiter->acquire($current);
// A queued permit may wait for the preceding sender. Re-read both the
// credential and recipient after that wait so an unbind, rebind or
// disable can never send with the stale pre-gate snapshot.
try {
$config = TicketTelegramConfig::runtimeForDelivery();
} catch (\Throwable $exception) {
throw new AdministratorTelegramLocalUnavailable(
'Administrator Telegram configuration is temporarily unavailable'
);
}
if ($config === null) {
throw new AdministratorTelegramPreflightException(
AdministratorTelegramPreflightException::CHANNEL_UNAVAILABLE,
'Administrator Telegram configuration is unavailable'
);
}
try {
$current = $this->boundDestination();
} catch (\Throwable $exception) {
throw new AdministratorTelegramLocalUnavailable(
'Administrator Telegram binding is temporarily unavailable'
);
}
if ($current === null || !$current->equals($expected)) {
throw new AdministratorTelegramPreflightException(
AdministratorTelegramPreflightException::DESTINATION_CHANGED,
'Administrator Telegram destination changed'
);
}

@ -0,0 +1,18 @@
<?php
namespace App\Services\Telegram;
/** Narrow one-shot channel contract for durable business delivery ledgers. */
interface AdministratorTelegramDeliveryChannel
{
public function eligibleAdministrator(int $administratorId): bool;
public function currentDestination(): ?AdministratorTelegramDestination;
/** @param array<string,int|string|bool> $options */
public function send(
AdministratorTelegramDestination $expected,
string $text,
array $options = []
): int;
}

@ -3,4 +3,25 @@
namespace App\Services\Telegram;
/** The Telegram network request is known not to have started. */
final class AdministratorTelegramPreflightException extends \RuntimeException {}
final class AdministratorTelegramPreflightException extends \RuntimeException
{
public const CHANNEL_UNAVAILABLE = 'channel_disabled_or_unavailable';
public const DESTINATION_CHANGED = 'destination_changed';
public const PAYLOAD_INVALID = 'payload_invalid';
public function __construct(
public readonly string $reason,
string $message
) {
if (!in_array($reason, [
self::CHANNEL_UNAVAILABLE,
self::DESTINATION_CHANGED,
self::PAYLOAD_INVALID,
], true)) {
throw new \InvalidArgumentException(
'Administrator Telegram preflight reason is invalid'
);
}
parent::__construct($message);
}
}

@ -0,0 +1,6 @@
<?php
namespace App\Services\Telegram;
/** No remote call started, but the shared queue exceeded its safe wait bound. */
final class AdministratorTelegramRateBacklog extends \RuntimeException {}

@ -0,0 +1,11 @@
<?php
namespace App\Services\Telegram;
/** Acquire one local permit before an administrator Telegram remote call. */
interface AdministratorTelegramRateLimiter
{
public function acquire(
AdministratorTelegramDestination $destination
): void;
}

@ -0,0 +1,120 @@
<?php
namespace App\Services\Telegram;
use App\Models\Configurations;
/** Cross-process singleton rate gate for every administrator Telegram send. */
final class DatabaseAdministratorTelegramRateGate
implements AdministratorTelegramRateLimiter
{
public const RATE_LIMIT_SECONDS = 2;
public const MAX_BACKLOG_SECONDS = 2;
private readonly \Closure $clock;
private readonly \Closure $sleeper;
public function __construct(
?callable $clock = null,
?callable $sleeper = null
)
{
$this->clock = \Closure::fromCallable(
$clock ?? static fn (): int => time()
);
$this->sleeper = \Closure::fromCallable(
$sleeper ?? static function (int $microseconds): void {
usleep($microseconds);
}
);
}
public function acquire(
AdministratorTelegramDestination $destination
): void {
$now = ($this->clock)();
if (!is_int($now) || $now < 1) {
throw new AdministratorTelegramLocalUnavailable(
'Administrator Telegram rate clock is unavailable'
);
}
try {
$connection = (new Configurations())->getConnection();
$reservation = $connection->transaction(function () use (
$connection,
$now
): array {
$gate = $connection->table(
'administrator_telegram_delivery_gate'
)->where('id', 1)->lockForUpdate()->first();
if ($gate === null) {
throw new \RuntimeException(
'Administrator Telegram rate gate is missing'
);
}
$oldNext = (int) $gate->next_send_at;
$oldUpdated = (int) $gate->updated_at;
if ($oldUpdated > $now + self::MAX_BACKLOG_SECONDS
|| $oldNext > $now + self::MAX_BACKLOG_SECONDS
+ self::RATE_LIMIT_SECONDS) {
$updated = $connection->table(
'administrator_telegram_delivery_gate'
)->where('id', 1)->where('next_send_at', $oldNext)
->update([
// Quarantine one full normal collision window.
// The current call does not receive a permit.
'next_send_at' => $now
+ self::MAX_BACKLOG_SECONDS
+ self::RATE_LIMIT_SECONDS,
'updated_at' => $now,
]);
if ((int) $updated !== 1) {
throw new \RuntimeException(
'Administrator Telegram rate gate recovery failed'
);
}
return ['slot' => null, 'recovered' => true];
}
$slot = max($now, $oldNext);
if ($slot - $now > self::MAX_BACKLOG_SECONDS) {
throw new AdministratorTelegramRateBacklog(
'Administrator Telegram delivery backlog is full'
);
}
$updated = $connection->table(
'administrator_telegram_delivery_gate'
)->where('id', 1)
->where('next_send_at', $oldNext)
->update([
'next_send_at' => $slot + self::RATE_LIMIT_SECONDS,
'updated_at' => $now,
]);
if ((int) $updated !== 1) {
throw new \RuntimeException(
'Administrator Telegram rate gate update failed'
);
}
return ['slot' => $slot, 'recovered' => false];
}, 3);
if ($reservation['recovered']) {
throw new AdministratorTelegramLocalUnavailable(
'Administrator Telegram rate clock recovered'
);
}
$slot = (int) $reservation['slot'];
$wait = $slot - $now;
if ($wait > 0) {
($this->sleeper)($wait * 1000000);
}
} catch (AdministratorTelegramRateBacklog $exception) {
throw $exception;
} catch (AdministratorTelegramLocalUnavailable $exception) {
throw $exception;
} catch (\Throwable $exception) {
throw new AdministratorTelegramLocalUnavailable(
'Administrator Telegram rate gate is temporarily unavailable'
);
}
}
}

@ -17,6 +17,7 @@ use App\Services\Telegram\AdministratorTelegramChannel;
use App\Services\Telegram\AdministratorTelegramDestination;
use App\Services\Telegram\AdministratorTelegramLocalUnavailable;
use App\Services\Telegram\AdministratorTelegramPreflightException;
use App\Services\Telegram\AdministratorTelegramRateBacklog;
use TelegramBot\Api\BotApi;
/** Administrator-only ticket alerts and replies to mapped notification messages. */
@ -67,12 +68,25 @@ final class TicketTelegramNotifier
public static function createBinding(int $adminId): array
{
TicketTelegramConfig::runtime(true);
if (!self::channel()->eligibleAdministrator($adminId)) {
throw new \RuntimeException('只有超级管理员可以绑定工单通知');
}
$code = bin2hex(random_bytes(24));
self::ensureBindingRow();
(new Configurations())->getConnection()->transaction(function () use ($adminId, $code) {
$connection = (new Configurations())->getConnection();
$connection->transaction(function () use (
$connection,
$adminId,
$code
): void {
$administrator = $connection->table('user')
->where('id', $adminId)->lockForUpdate()
->first(['id', 'is_admin', 'enable']);
if ($administrator === null
|| (int) $administrator->is_admin !== 1
|| (int) $administrator->enable !== 1
|| !self::channel()->eligibleAdministrator($adminId)) {
throw new \RuntimeException(
'只有启用的超级管理员可以绑定工单通知'
);
}
self::ensureBindingRow();
$row = Configurations::where('name', self::KEY)->lockForUpdate()->firstOrFail();
$state = self::readState($row);
$state['pending_hash'] = hash('sha256', $code);
@ -82,7 +96,7 @@ final class TicketTelegramNotifier
if (!$row->save()) {
throw new \RuntimeException('Administrator Telegram binding storage failed');
}
});
}, 3);
return ['code' => $code, 'expires_in' => self::BINDING_TTL];
}
@ -133,12 +147,31 @@ final class TicketTelegramNotifier
if ($command === null) {
return self::handleTicketReply($update);
}
$bound = (new Configurations())->getConnection()->transaction(function () use ($command) {
$snapshot = self::readState(
Configurations::where('name', self::KEY)->first()
);
$expectedAdminId = (int) ($snapshot['pending_admin_id'] ?? 0);
if ($expectedAdminId <= 0) {
return false;
}
$connection = (new Configurations())->getConnection();
$bound = $connection->transaction(function () use (
$connection,
$command,
$expectedAdminId
): bool {
$administrator = $connection->table('user')
->where('id', $expectedAdminId)->lockForUpdate()
->first(['id', 'is_admin', 'enable']);
$row = Configurations::where('name', self::KEY)->lockForUpdate()->first();
$state = self::readState($row);
$adminId = (int) ($state['pending_admin_id'] ?? 0);
if ($row === null || (int) ($state['pending_expires_at'] ?? 0) <= time()
|| $adminId !== $expectedAdminId
|| !hash_equals((string) ($state['pending_hash'] ?? ''), hash('sha256', $command['code']))
|| $administrator === null
|| (int) $administrator->is_admin !== 1
|| (int) $administrator->enable !== 1
|| !self::channel()->eligibleAdministrator($adminId)) {
return false;
}
@ -152,7 +185,7 @@ final class TicketTelegramNotifier
throw new \RuntimeException('Administrator Telegram binding storage failed');
}
return true;
});
}, 3);
if (!$bound) {
return false;
}
@ -181,6 +214,37 @@ final class TicketTelegramNotifier
)->where('chat_id', $command['chat_id'])
->where('telegram_message_id', $command['reply_to_message_id'])
->where('state', 'sent')->first();
try {
$abuseDelivery = self::abuseReplyDelivery(
$destination,
$command['reply_to_message_id']
);
} catch (\Throwable $exception) {
error_log('Administrator Telegram abuse reply mapping unavailable');
self::sendReplyAcknowledgement(
$command['chat_id'],
$command['message_id'],
'暂时无法核对这条通知,请勿通过 Telegram 回复,请在管理后台处理。'
);
return true;
}
if ($delivery !== null && $abuseDelivery !== null) {
error_log('Administrator Telegram reply target mapping conflict');
self::sendReplyAcknowledgement(
$command['chat_id'],
$command['message_id'],
'这条通知的回复映射存在冲突,请在管理后台处理。'
);
return true;
}
if ($abuseDelivery !== null) {
self::sendReplyAcknowledgement(
$command['chat_id'],
$command['message_id'],
'网络滥用告警为只读通知,不能通过 Telegram 回复,请在管理后台处理。'
);
return true;
}
if ($delivery === null || (int) $delivery->bound_admin_id !== $adminId) {
self::sendReplyAcknowledgement(
$command['chat_id'],
@ -244,6 +308,25 @@ final class TicketTelegramNotifier
return true;
}
private static function abuseReplyDelivery(
AdministratorTelegramDestination $destination,
int $replyToMessageId
): ?object {
$connection = (new Configurations())->getConnection();
if (!$connection->getSchemaBuilder()->hasTable(
'cloud_abuse_alert_deliveries'
)) {
return null;
}
return $connection->table('cloud_abuse_alert_deliveries')
->where('binding_generation', $destination->bindingGeneration)
->where('bound_admin_id', $destination->administratorId)
->where('chat_id', $destination->chatId)
->where('telegram_message_id', $replyToMessageId)
->whereIn('state', ['sent', 'unknown'])
->whereNull('destination_redacted_at')->first();
}
private static function ticketReplyCommand(array $update): ?array
{
$message = $update['message'] ?? null;
@ -370,6 +453,9 @@ final class TicketTelegramNotifier
throw new \RuntimeException('Telegram delivery finalization failed');
}
return TicketTelegramNotificationResult::Sent;
} catch (AdministratorTelegramRateBacklog $exception) {
self::deferBackloggedDelivery((int) $delivery->id);
return TicketTelegramNotificationResult::Failed;
} catch (AdministratorTelegramPreflightException|AdministratorTelegramLocalUnavailable $exception) {
self::releasePreflightDelivery((int) $delivery->id);
return TicketTelegramNotificationResult::Failed;
@ -396,30 +482,80 @@ final class TicketTelegramNotifier
$generation = $destination->bindingGeneration;
$messageId = (int) $event->result->message->id;
$kind = (string) $event->kind;
$existing = self::deliveryForEvent($generation, $messageId, $kind);
if ($existing !== null) {
self::settleStaleDispatching($existing);
return [$existing, false];
}
$delivery = new TicketTelegramDelivery();
$delivery->binding_generation = $generation;
$delivery->bound_admin_id = $destination->administratorId;
$delivery->chat_id = $destination->chatId;
$delivery->ticket_id = (int) $event->result->root->id;
$delivery->ticket_message_id = $messageId;
$delivery->event_kind = $kind;
$delivery->telegram_message_id = null;
$delivery->state = 'dispatching';
$delivery->content_hash = hash('sha256', $text);
$delivery->created_at = time();
$delivery->sent_at = null;
$delivery->updated_at = time();
try {
if (!$delivery->save()) {
throw new \RuntimeException('Telegram delivery reservation failed');
}
return [$delivery, true];
$connection = (new Configurations())->getConnection();
return $connection->transaction(function () use (
$connection,
$destination,
$generation,
$messageId,
$kind,
$event,
$text
): array {
$ownerId = (int) $event->result->root->userid;
$userIds = array_values(array_unique([
$ownerId, $destination->administratorId,
]));
sort($userIds, SORT_NUMERIC);
$lockedUsers = [];
foreach ($userIds as $userId) {
$locked = $connection->table('user')
->where('id', $userId)->lockForUpdate()
->first(['id', 'is_admin', 'enable']);
if ($locked !== null) {
$lockedUsers[(int) $locked->id] = $locked;
}
}
$administrator = $lockedUsers[
$destination->administratorId
] ?? null;
if (!isset($lockedUsers[$ownerId])) {
throw new \RuntimeException(
'Telegram ticket owner no longer exists'
);
}
if ($administrator === null
|| (int) $administrator->is_admin !== 1
|| (int) $administrator->enable !== 1
|| !self::channel()->eligibleAdministrator(
$destination->administratorId
)) {
throw new AdministratorTelegramPreflightException(
AdministratorTelegramPreflightException::DESTINATION_CHANGED,
'Administrator Telegram destination is ineligible'
);
}
$existing = self::deliveryForEvent(
$generation,
$messageId,
$kind
);
if ($existing !== null) {
self::settleStaleDispatching($existing);
return [$existing, false];
}
$delivery = new TicketTelegramDelivery();
$delivery->binding_generation = $generation;
$delivery->bound_admin_id = $destination->administratorId;
$delivery->chat_id = $destination->chatId;
$delivery->ticket_id = (int) $event->result->root->id;
$delivery->ticket_message_id = $messageId;
$delivery->event_kind = $kind;
$delivery->telegram_message_id = null;
$delivery->state = 'dispatching';
$delivery->content_hash = hash('sha256', $text);
$delivery->created_at = time();
$delivery->sent_at = null;
$delivery->updated_at = time();
if (!$delivery->save()) {
throw new \RuntimeException(
'Telegram delivery reservation failed'
);
}
return [$delivery, true];
}, 3);
} catch (\Throwable $exception) {
// The unique event key elects one sender when dispatchers race.
$existing = self::deliveryForEvent($generation, $messageId, $kind);
@ -485,6 +621,26 @@ final class TicketTelegramNotifier
}
}
private static function deferBackloggedDelivery(int $deliveryId): void
{
try {
$updated = TicketTelegramDelivery::where('id', $deliveryId)
->where('state', 'dispatching')->update([
'state' => 'failed',
'updated_at' => time(),
]);
if ((int) $updated !== 1) {
throw new \RuntimeException(
'Telegram delivery backlog settlement failed'
);
}
} catch (\Throwable $exception) {
// The remote call is known not to have started. Retain the
// dispatch fence if that known-no-send settlement cannot persist.
error_log('Administrator ticket Telegram backlog settlement failed');
}
}
private static function notificationText(TicketEvent $event): string
{
$root = $event->result->root;

@ -71,7 +71,8 @@ class UserDataDeletionService
// the attempt that ultimately commits.
$ticketAttachmentStorageKeys = [];
// Row locks and a second non-admin condition protect concurrent promotion.
$users = User::whereIn('id', $requested)->where('is_admin', 0)->lockForUpdate()->get();
$users = User::whereIn('id', $requested)->where('is_admin', 0)
->orderBy('id')->lockForUpdate()->get();
if ($users->isEmpty()) {
return $this->emptyResult($requested);
}
@ -175,6 +176,10 @@ class UserDataDeletionService
$maintenanceNoticeIds[(int) $recipient->notice_id] = true;
}
}
// This must precede every destructive log-database operation:
// that separate transaction cannot roll back with this one.
$this->fenceAdministratorTelegramDeliveries($connection, $ids);
$this->clearFormerAdministratorTelegramBinding($connection, $ids);
foreach ($users as $user) {
LoginSessionRevocationService::revokeUser($user);
}
@ -199,6 +204,8 @@ class UserDataDeletionService
// not survive as orphaned administrator audit records.
$resourceJobRows = $this->deleteCloudResourceJobs($connection, $ids);
$this->anonymizeCompensationScopes($connection, $ids);
$abuseAlertDestinationRows =
$this->anonymizeAbuseAlertDestinations($connection, $ids);
$trafficInstanceIds = $this->pluckByUsers(
$connection,
'cloud_instances',
@ -230,7 +237,8 @@ class UserDataDeletionService
$trafficInstanceIds,
$trafficPeriodIds
);
$rows = $deletedMailRows + $resourceJobRows + $trafficScanRows
$rows = $deletedMailRows + $resourceJobRows
+ $abuseAlertDestinationRows + $trafficScanRows
+ $relatedRows;
$deleted = (int) User::whereIn('id', $ids)->where('is_admin', 0)->delete();
if ($deleted !== count($ids)) {
@ -419,6 +427,252 @@ class UserDataDeletionService
})->delete();
}
/**
* Hold every relevant administrator-Telegram ledger through commit. Abuse
* rows use parent event -> child delivery order. Ticket delivery rows are
* fenced by already locked owner/former-administrator user rows.
*/
private function fenceAdministratorTelegramDeliveries(
$connection,
array $userIds
): void {
if ($this->hasColumns($connection, 'ticket_telegram_deliveries', [
'id', 'bound_admin_id', 'ticket_id', 'state', 'updated_at',
])) {
$ticketDeliveryIds = $connection->table(
'ticket_telegram_deliveries'
)
->whereIn('bound_admin_id', $userIds)
->pluck('id')->all();
if ($this->hasColumns($connection, 'ticket', [
'id', 'userid', 'rootid',
])) {
$rootIds = $connection->table('ticket')
->whereIn('userid', $userIds)->where('rootid', 0)
->pluck('id')->all();
$rootIds = array_values(array_unique(array_map(
'intval', $rootIds
)));
sort($rootIds, SORT_NUMERIC);
if ($rootIds !== []) {
$ticketDeliveryIds = array_merge(
$ticketDeliveryIds,
$connection->table('ticket_telegram_deliveries')
->whereIn('ticket_id', $rootIds)
->pluck('id')->all()
);
}
}
$ticketDeliveryIds = array_values(array_unique(array_map(
'intval', $ticketDeliveryIds
)));
sort($ticketDeliveryIds, SORT_NUMERIC);
$ticketRows = $ticketDeliveryIds === []
? collect()
: $connection->table('ticket_telegram_deliveries')
->whereIn('id', $ticketDeliveryIds)
->orderBy('id')->lockForUpdate()
->get(['id', 'state', 'updated_at']);
$now = time();
foreach ($ticketRows as $row) {
if ((string) $row->state === 'dispatching') {
if ((int) $row->updated_at <= $now - 30) {
$settled = $connection->table(
'ticket_telegram_deliveries'
)->where('id', (int) $row->id)
->where('state', 'dispatching')->update([
'state' => 'unknown',
'updated_at' => $now,
]);
if ((int) $settled !== 1) {
throw new \RuntimeException(
'陈旧工单通知投递核对失败,账户删除已取消'
);
}
continue;
}
throw new \RuntimeException(
'用户仍有投递中的工单通知,请等待投递结果核对后再删除账户'
);
}
}
}
if (!$this->hasColumns($connection, 'cloud_abuse_alert_events', [
'id', 'user_id',
]) || !$this->hasColumns(
$connection,
'cloud_abuse_alert_deliveries',
[
'id', 'event_id', 'bound_admin_id', 'state',
'error_code', 'terminal_at', 'updated_at',
]
)) {
return;
}
$owned = $connection->table('cloud_abuse_alert_events')
->whereIn('user_id', $userIds)->pluck('id')->all();
$bound = $connection->table('cloud_abuse_alert_deliveries')
->whereIn('bound_admin_id', $userIds)->pluck('event_id')->all();
$eventIds = array_values(array_unique(array_map(
'intval',
array_merge($owned, $bound)
)));
sort($eventIds, SORT_NUMERIC);
if ($eventIds === []) {
return;
}
$lockedEventIds = $connection->table('cloud_abuse_alert_events')
->whereIn('id', $eventIds)->orderBy('id')->lockForUpdate()
->pluck('id')->map(static fn ($id): int => (int) $id)->all();
if ($lockedEventIds === []) {
return;
}
$deliveryRows = $connection->table('cloud_abuse_alert_deliveries')
->whereIn('event_id', $lockedEventIds)->orderBy('id')
->lockForUpdate()->get(['id', 'state', 'updated_at']);
$now = time();
foreach ($deliveryRows as $row) {
if ((string) $row->state === 'dispatching') {
if ((int) $row->updated_at <= $now - 30) {
$settled = $connection->table(
'cloud_abuse_alert_deliveries'
)->where('id', (int) $row->id)
->where('state', 'dispatching')->update([
'state' => 'unknown',
'error_code' => 'worker_interrupted',
'terminal_at' => $now,
'updated_at' => $now,
]);
if ((int) $settled !== 1) {
throw new \RuntimeException(
'陈旧网络滥用告警投递核对失败,账户删除已取消'
);
}
continue;
}
throw new \RuntimeException(
'用户仍有投递中的网络滥用告警,请等待投递结果核对后再删除账户'
);
}
}
}
/** A recycled user ID must never inherit a former administrator chat. */
private function clearFormerAdministratorTelegramBinding(
$connection,
array $userIds
): void {
if (!$this->hasColumns($connection, 'configurations', [
'id', 'name', 'value',
])) {
return;
}
$binding = $connection->table('configurations')
->where('name', TicketTelegramConfig::BINDING)
->lockForUpdate()->first(['id', 'value']);
if ($binding === null) {
return;
}
$state = json_decode((string) $binding->value, true);
if (!is_array($state)) {
return;
}
$boundMatches = in_array(
(int) ($state['bound_admin_id'] ?? 0), $userIds, true
);
$pendingMatches = in_array(
(int) ($state['pending_admin_id'] ?? 0), $userIds, true
);
if (!$boundMatches && !$pendingMatches) {
return;
}
$query = $connection->table('configurations')
->where('id', (int) $binding->id)
->where('name', TicketTelegramConfig::BINDING);
if ($boundMatches) {
$changed = $query->delete();
} else {
unset(
$state['pending_admin_id'],
$state['pending_hash'],
$state['pending_expires_at']
);
$changed = $query->update([
'value' => json_encode($state, JSON_THROW_ON_ERROR),
]);
}
if ((int) $changed !== 1) {
throw new \RuntimeException(
'管理员 Telegram 绑定清理失败,账户删除已取消'
);
}
}
/**
* Preserve the unique event disposition while removing a former bound
* administrator's Telegram destination. Deleting the delivery row would
* make the immutable event eligible for a second send.
*/
private function anonymizeAbuseAlertDestinations(
$connection,
array $userIds
): int {
if (!$this->hasColumns($connection,
'cloud_abuse_alert_deliveries', [
'id', 'bound_admin_id', 'state', 'binding_generation',
'chat_id', 'telegram_message_id', 'dispatch_started_at',
'terminal_at', 'destination_redacted_at', 'updated_at',
])) {
return 0;
}
$rows = $connection->table('cloud_abuse_alert_deliveries')
->whereIn('bound_admin_id', $userIds)
->whereNotExists(static function ($query) use ($userIds): void {
$query->selectRaw('1')
->from('cloud_abuse_alert_events as alert_event')
->whereColumn(
'alert_event.id',
'cloud_abuse_alert_deliveries.event_id'
)->whereIn('alert_event.user_id', $userIds);
})->orderBy('id')->get(['id', 'state']);
if ($rows->isEmpty()) {
return 0;
}
$now = time();
$updated = 0;
foreach ($rows as $row) {
$values = [
'binding_generation' => null,
'bound_admin_id' => null,
'chat_id' => null,
'telegram_message_id' => null,
'destination_redacted_at' => $now,
'updated_at' => $now,
];
if ((string) $row->state === 'pending') {
$values += [
'state' => 'skipped',
'skip_reason' => 'destination_owner_deleted',
'terminal_at' => $now,
];
}
$count = $connection->table('cloud_abuse_alert_deliveries')
->where('id', (int) $row->id)
->where('state', (string) $row->state)
->whereIn('bound_admin_id', $userIds)
->update($values);
if ($count !== 1) {
throw new \RuntimeException(
'网络滥用告警接收方匿名化失败,账户删除已取消'
);
}
$updated += $count;
}
return $updated;
}
/**
* Batch audit survives account deletion, but a historical single-instance
* scope may have frozen the tenant's chosen instance name and user ID.

@ -3182,6 +3182,111 @@ CREATE TABLE cloud_abuse_alert_event_items (
ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- One immutable Telegram delivery decision per alert event. The message body
-- is frozen before the row becomes pending, so a restarted worker never
-- reformats an event with changed application code. Once dispatching begins,
-- every uncertain outcome is terminal and must never be replayed.
CREATE TABLE cloud_abuse_alert_deliveries (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
event_id BIGINT UNSIGNED NOT NULL,
binding_generation CHAR(32) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL,
bound_admin_id BIGINT UNSIGNED DEFAULT NULL,
chat_id VARCHAR(20) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL,
telegram_message_id BIGINT UNSIGNED DEFAULT NULL,
state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
message_text TEXT NOT NULL,
content_hash CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
error_code VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '',
skip_reason VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '',
dispatch_started_at BIGINT UNSIGNED DEFAULT NULL,
sent_at BIGINT UNSIGNED DEFAULT NULL,
terminal_at BIGINT UNSIGNED DEFAULT NULL,
destination_redacted_at BIGINT UNSIGNED DEFAULT NULL,
created_at BIGINT UNSIGNED NOT NULL,
updated_at BIGINT UNSIGNED NOT NULL,
UNIQUE KEY cloud_abuse_alert_delivery_event (event_id),
UNIQUE KEY cloud_abuse_alert_delivery_message (
binding_generation, chat_id, telegram_message_id
),
KEY cloud_abuse_alert_delivery_due (state, updated_at, id),
KEY cloud_abuse_alert_delivery_admin (bound_admin_id, created_at, id),
KEY cloud_abuse_alert_delivery_terminal (terminal_at, state, id),
CONSTRAINT chk_cloud_abuse_alert_delivery_state CHECK (
state IN ('pending','dispatching','sent','failed','unknown','skipped')
),
CONSTRAINT chk_cloud_abuse_alert_delivery_content CHECK (
CHAR_LENGTH(message_text) BETWEEN 1 AND 4096
AND content_hash REGEXP '^[a-f0-9]{64}$'
),
CONSTRAINT chk_cloud_abuse_alert_delivery_destination CHECK (
(
destination_redacted_at IS NULL
AND (
(binding_generation IS NULL AND bound_admin_id IS NULL
AND chat_id IS NULL AND telegram_message_id IS NULL)
OR (binding_generation IS NOT NULL
AND binding_generation REGEXP '^[a-f0-9]{32}$'
AND bound_admin_id IS NOT NULL AND bound_admin_id > 0
AND chat_id IS NOT NULL
AND chat_id REGEXP '^[1-9][0-9]{0,18}$'
AND (telegram_message_id IS NULL
OR telegram_message_id > 0))
)
)
OR (
destination_redacted_at IS NOT NULL
AND state IN ('sent','failed','unknown','skipped')
AND binding_generation IS NULL AND bound_admin_id IS NULL
AND chat_id IS NULL AND telegram_message_id IS NULL
)
),
CONSTRAINT chk_cloud_abuse_alert_delivery_shape CHECK (
(state = 'pending'
AND binding_generation IS NOT NULL AND telegram_message_id IS NULL
AND dispatch_started_at IS NULL AND sent_at IS NULL
AND terminal_at IS NULL AND error_code = '' AND skip_reason = '')
OR (state = 'dispatching'
AND binding_generation IS NOT NULL AND telegram_message_id IS NULL
AND dispatch_started_at IS NOT NULL AND sent_at IS NULL
AND terminal_at IS NULL AND error_code = '' AND skip_reason = '')
OR (state = 'sent'
AND dispatch_started_at IS NOT NULL AND sent_at IS NOT NULL
AND terminal_at IS NOT NULL AND error_code = '' AND skip_reason = ''
AND (destination_redacted_at IS NOT NULL
OR telegram_message_id IS NOT NULL))
OR (state = 'failed'
AND telegram_message_id IS NULL
AND dispatch_started_at IS NULL AND sent_at IS NULL
AND terminal_at IS NOT NULL
AND error_code <> '' AND skip_reason = '')
OR (state = 'unknown'
AND dispatch_started_at IS NOT NULL AND sent_at IS NULL
AND terminal_at IS NOT NULL
AND error_code <> '' AND skip_reason = '')
OR (state = 'skipped'
AND dispatch_started_at IS NULL AND sent_at IS NULL
AND terminal_at IS NOT NULL AND error_code = '' AND skip_reason <> '')
),
CONSTRAINT fk_cloud_abuse_alert_delivery_event FOREIGN KEY (event_id)
REFERENCES cloud_abuse_alert_events (id)
ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Cross-process per-chat rate gate. The singleton is advanced before a remote
-- request so concurrent notification workers cannot exceed the provider rate.
CREATE TABLE administrator_telegram_delivery_gate (
id TINYINT UNSIGNED NOT NULL PRIMARY KEY,
next_send_at BIGINT UNSIGNED NOT NULL DEFAULT 0,
updated_at BIGINT UNSIGNED NOT NULL DEFAULT 0,
CONSTRAINT chk_administrator_telegram_delivery_gate CHECK (
id = 1 AND next_send_at >= updated_at
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO administrator_telegram_delivery_gate (
id, next_send_at, updated_at
) VALUES (1, 0, 0);
-- Main-database idempotency receipt for one log-database outbox projection.
-- The state row's last_projection_key/last_bucket_start pair is the ordering
-- fence; a replay may observe this receipt and safely mark the outbox done.

@ -88,14 +88,35 @@ namespace {
use App\Services\Telegram\AdministratorTelegramChannel;
use App\Services\Telegram\AdministratorTelegramLocalUnavailable;
use App\Services\Telegram\AdministratorTelegramPreflightException;
use App\Services\Telegram\AdministratorTelegramRateLimiter;
use App\Services\Telegram\AdministratorTelegramRemoteEffectUnknown;
use App\Services\Telegram\AdministratorTelegramTransport;
require dirname(__DIR__) . '/app/Services/Telegram/AdministratorTelegramDestination.php';
require dirname(__DIR__) . '/app/Services/Telegram/AdministratorTelegramDeliveryChannel.php';
require dirname(__DIR__) . '/app/Services/Telegram/AdministratorTelegramTransport.php';
require dirname(__DIR__) . '/app/Services/Telegram/AdministratorTelegramPreflightException.php';
require dirname(__DIR__) . '/app/Services/Telegram/AdministratorTelegramLocalUnavailable.php';
require dirname(__DIR__) . '/app/Services/Telegram/AdministratorTelegramRemoteEffectUnknown.php';
require dirname(__DIR__) . '/app/Services/Telegram/AdministratorTelegramRateBacklog.php';
require dirname(__DIR__) . '/app/Services/Telegram/AdministratorTelegramRateLimiter.php';
require dirname(__DIR__) . '/app/Services/Telegram/DatabaseAdministratorTelegramRateGate.php';
final class AdministratorTelegramChannelFakeRateLimiter
implements AdministratorTelegramRateLimiter
{
public int $calls = 0;
public mixed $hook = null;
public function acquire(
\App\Services\Telegram\AdministratorTelegramDestination $destination
): void {
$this->calls++;
if (is_callable($this->hook)) {
($this->hook)();
}
}
}
final class AdministratorTelegramChannelConnection
{
@ -181,7 +202,8 @@ namespace {
User::$rows[1] = new User(1);
$transport = new AdministratorTelegramChannelFakeTransport();
$channel = new AdministratorTelegramChannel($transport);
$rateLimiter = new AdministratorTelegramChannelFakeRateLimiter();
$channel = new AdministratorTelegramChannel($transport, $rateLimiter);
$setBinding(['bound_admin_id' => 1, 'chat_id' => '123456']);
$destination = $channel->currentDestination();
@ -197,10 +219,53 @@ namespace {
'disable_web_page_preview' => 'true',
]);
$assert($messageId === 9001 && count($transport->calls) === 1
&& $rateLimiter->calls === 1
&& $transport->calls[0]['chatId'] === '123456'
&& $transport->calls[0]['botToken'] === TicketTelegramConfig::$token,
'Authoritative destination was not sent exactly once.');
$rateLimiter->hook = static function () use ($setBinding): void {
$setBinding([
'bound_admin_id' => 1,
'chat_id' => '654321',
'binding_generation' => bin2hex(random_bytes(16)),
]);
};
$postWaitFailure = null;
try {
$channel->send($destination, 'Rebound while waiting');
} catch (Throwable $exception) {
$postWaitFailure = $exception;
}
$assert($postWaitFailure instanceof AdministratorTelegramPreflightException
&& $postWaitFailure->reason
=== AdministratorTelegramPreflightException::DESTINATION_CHANGED
&& count($transport->calls) === 1,
'Post-gate rebind reached the stale Telegram destination.');
$rateLimiter->hook = null;
$setBinding([
'bound_admin_id' => 1,
'chat_id' => '123456',
'binding_generation' => $destination->bindingGeneration,
]);
$rateLimiter->hook = static function (): void {
TicketTelegramConfig::$enabled = false;
};
$postWaitFailure = null;
try {
$channel->send($destination, 'Disabled while waiting');
} catch (Throwable $exception) {
$postWaitFailure = $exception;
}
$assert($postWaitFailure instanceof AdministratorTelegramPreflightException
&& $postWaitFailure->reason
=== AdministratorTelegramPreflightException::CHANNEL_UNAVAILABLE
&& count($transport->calls) === 1,
'Post-gate disable reached Telegram.');
TicketTelegramConfig::$enabled = true;
$rateLimiter->hook = null;
$expectFailure(
fn () => $channel->send($destination, 'Unsafe', ['parse_mode' => 'HTML']),
'Unknown Telegram option was accepted.'

@ -0,0 +1,108 @@
<?php
declare(strict_types=1);
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\Configurations;
use App\Services\Telegram\AdministratorTelegramDestination;
use App\Services\Telegram\AdministratorTelegramLocalUnavailable;
use App\Services\Telegram\AdministratorTelegramRateBacklog;
use App\Services\Telegram\DatabaseAdministratorTelegramRateGate;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
$capsule = new Capsule();
$capsule->addConnection([
'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '',
], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
Configurations::setConnectionResolver($capsule->getDatabaseManager());
$db = $capsule->getConnection('default');
$db->getSchemaBuilder()->create(
'administrator_telegram_delivery_gate',
static function (Blueprint $table): void {
$table->integer('id')->primary();
$table->integer('next_send_at');
$table->integer('updated_at');
}
);
$db->table('administrator_telegram_delivery_gate')->insert([
'id' => 1, 'next_send_at' => 0, 'updated_at' => 0,
]);
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$destination = new AdministratorTelegramDestination(
1, '123456', str_repeat('a', 32)
);
$now = 2000;
$sleeps = [];
$gate = new DatabaseAdministratorTelegramRateGate(
static function () use (&$now): int { return $now; },
static function (int $microseconds) use (&$sleeps, $db): void {
if ($db->transactionLevel() !== 0) {
throw new RuntimeException('Rate wait occurred inside transaction');
}
$sleeps[] = $microseconds;
}
);
$gate->acquire($destination);
$assert($sleeps === []
&& (int) $db->table('administrator_telegram_delivery_gate')
->value('next_send_at') === 2002,
'First Telegram permit was not reserved immediately.');
$gate->acquire($destination);
$assert($sleeps === [2000000]
&& (int) $db->table('administrator_telegram_delivery_gate')
->value('next_send_at') === 2004,
'Second Telegram permit did not reserve and wait for the adjacent slot.');
$backlog = null;
try {
$gate->acquire($destination);
} catch (Throwable $error) {
$backlog = $error;
}
$assert($backlog instanceof AdministratorTelegramRateBacklog
&& count($sleeps) === 1,
'Third concurrent permit was not classified as known-no-remote backlog.');
// A stale future wall-clock value is quarantined and rewritten. The current
// request receives no permit, while later callers can recover automatically.
$now = 3000;
$db->table('administrator_telegram_delivery_gate')->where('id', 1)->update([
'next_send_at' => 9002, 'updated_at' => 9000,
]);
$clockFailure = null;
try {
$gate->acquire($destination);
} catch (Throwable $error) {
$clockFailure = $error;
}
$recovered = $db->table('administrator_telegram_delivery_gate')
->where('id', 1)->first();
$assert($clockFailure instanceof AdministratorTelegramLocalUnavailable
&& (int) $recovered->updated_at === 3000
&& (int) $recovered->next_send_at === 3004,
'Future Telegram gate timestamp was not safely quarantined.');
$now = 3002;
$gate->acquire($destination);
$assert(end($sleeps) === 2000000,
'Recovered Telegram gate did not resume through a bounded slot.');
$db->getSchemaBuilder()->drop('administrator_telegram_delivery_gate');
$missing = null;
try {
$gate->acquire($destination);
} catch (Throwable $error) {
$missing = $error;
}
$assert($missing instanceof AdministratorTelegramLocalUnavailable,
'Missing shared Telegram gate was not a known local failure.');
echo "Administrator Telegram rate gate tests passed.\n";

@ -0,0 +1,238 @@
<?php
declare(strict_types=1);
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\User;
use App\Services\Config;
use App\Services\Cloud\CloudAbuseAlertDeliveryService;
use App\Services\Cloud\CloudAbuseAlertMessageFormatter;
use App\Services\Telegram\AdministratorTelegramDeliveryChannel;
use App\Services\Telegram\AdministratorTelegramDestination;
use App\Services\Telegram\AdministratorTelegramLocalUnavailable;
use App\Services\Telegram\AdministratorTelegramPreflightException;
use App\Services\Telegram\AdministratorTelegramRateBacklog;
use App\Services\Telegram\AdministratorTelegramRemoteEffectUnknown;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
$capsule = new Capsule();
$capsule->addConnection([
'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '',
], 'default');
$capsule->setAsGlobal(); $capsule->bootEloquent();
User::setConnectionResolver($capsule->getDatabaseManager());
$db = $capsule->getConnection('default'); $schema = $db->getSchemaBuilder();
$schema->create('user', static function (Blueprint $t): void {
$t->increments('id'); $t->integer('is_admin'); $t->integer('enable');
});
$schema->create('cloud_abuse_alert_events', static function (Blueprint $t): void {
$t->increments('id'); $t->string('event_key'); $t->string('sample_key');
$t->integer('instance_id'); $t->integer('user_id'); $t->integer('host_id');
$t->integer('bucket_start'); $t->integer('rule_version');
$t->integer('policy_revision'); $t->string('policy_hash');
$t->string('transition_digest'); $t->integer('transition_count');
$t->string('highest_severity'); $t->integer('created_at');
});
$schema->create('cloud_abuse_alert_event_items', static function (Blueprint $t): void {
$t->increments('id'); $t->integer('event_id'); $t->string('item_key');
$t->string('finding_key'); $t->integer('occurrence_generation');
$t->integer('transition_revision'); $t->string('rule_code');
$t->string('direction'); $t->string('ip_family');
$t->string('transition_type'); $t->string('from_severity');
$t->string('to_severity'); $t->text('evidence_json');
$t->string('resolution_reason'); $t->integer('occurred_at');
$t->integer('created_at');
});
$schema->create('cloud_abuse_alert_deliveries', static function (Blueprint $t): void {
$t->increments('id'); $t->integer('event_id')->unique();
$t->string('binding_generation')->nullable();
$t->integer('bound_admin_id')->nullable(); $t->string('chat_id')->nullable();
$t->integer('telegram_message_id')->nullable(); $t->string('state');
$t->text('message_text'); $t->string('content_hash');
$t->string('error_code')->default(''); $t->string('skip_reason')->default('');
$t->integer('dispatch_started_at')->nullable(); $t->integer('sent_at')->nullable();
$t->integer('terminal_at')->nullable(); $t->integer('destination_redacted_at')->nullable();
$t->integer('created_at'); $t->integer('updated_at');
});
$db->table('user')->insert(['id' => 1, 'is_admin' => 1, 'enable' => 1]);
Config::set('baseUrl', 'https://panel.example.test');
$assert = static function (bool $condition, string $message): void {
if (!$condition) { throw new RuntimeException($message); }
};
final class AbuseDeliveryFakeChannel implements AdministratorTelegramDeliveryChannel
{
public ?AdministratorTelegramDestination $destination;
public string $destinationMode = 'ready'; public string $sendMode = 'success';
public int $calls = 0; public array $messages = [];
public function __construct() { $this->destination = new AdministratorTelegramDestination(
1, '123456', str_repeat('a', 32)
); }
public function eligibleAdministrator(int $administratorId): bool
{
return $administratorId === 1;
}
public function currentDestination(): ?AdministratorTelegramDestination
{
if ($this->destinationMode === 'local') {
throw new AdministratorTelegramLocalUnavailable('synthetic');
}
return $this->destination;
}
public function send(AdministratorTelegramDestination $expected,
string $text, array $options = []): int
{
if ($this->sendMode === 'local') {
throw new AdministratorTelegramLocalUnavailable('synthetic');
}
if ($this->sendMode === 'preflight') {
throw new AdministratorTelegramPreflightException(
AdministratorTelegramPreflightException::DESTINATION_CHANGED,
'synthetic'
);
}
if ($this->sendMode === 'payload') {
throw new AdministratorTelegramPreflightException(
AdministratorTelegramPreflightException::PAYLOAD_INVALID,
'synthetic'
);
}
if ($this->sendMode === 'channel') {
throw new AdministratorTelegramPreflightException(
AdministratorTelegramPreflightException::CHANNEL_UNAVAILABLE,
'synthetic'
);
}
if ($this->sendMode === 'backlog') {
throw new AdministratorTelegramRateBacklog('synthetic');
}
$this->calls++; $this->messages[] = $text;
if ($this->sendMode === 'remote') {
throw new AdministratorTelegramRemoteEffectUnknown('synthetic');
}
return 7000 + $this->calls;
}
}
$identity = 1;
$addEvent = static function () use ($db, &$identity): int {
$number = $identity++; $bucket = 1800000000 + ($number * 60);
$id = (int) $db->table('cloud_abuse_alert_events')->insertGetId([
'event_key' => hash('sha256', 'event-' . $number),
'sample_key' => hash('sha256', 'sample-' . $number),
'instance_id' => 100 + $number, 'user_id' => 10, 'host_id' => 20,
'bucket_start' => $bucket, 'rule_version' => 1,
'policy_revision' => 1, 'policy_hash' => str_repeat('b', 64),
'transition_digest' => str_repeat('c', 64), 'transition_count' => 1,
'highest_severity' => 'high', 'created_at' => $bucket,
]);
$db->table('cloud_abuse_alert_event_items')->insert([
'event_id' => $id, 'item_key' => hash('sha256', 'item-' . $number),
'finding_key' => hash('sha256', 'finding-' . $number),
'occurrence_generation' => 1, 'transition_revision' => 1,
'rule_code' => 'outbound_tcp_syn_sweep', 'direction' => 'outbound',
'ip_family' => 'ipv4', 'transition_type' => 'opened',
'from_severity' => 'none', 'to_severity' => 'high',
'evidence_json' => '{"tcp_syn_flows":5000,"unique_remote_ips":600}',
'resolution_reason' => '', 'occurred_at' => $bucket,
'created_at' => $bucket,
]);
return $id;
};
$now = 1900000000; $channel = new AbuseDeliveryFakeChannel();
$service = new CloudAbuseAlertDeliveryService($channel,
new CloudAbuseAlertMessageFormatter(),
static function () use (&$now): int { return $now; });
$first = $addEvent();
$assert($service->tick() && $channel->calls === 1,
'Fresh alert was not sent exactly once.');
$sent = $db->table('cloud_abuse_alert_deliveries')->where('event_id', $first)->first();
$assert($sent?->state === 'sent' && (int) $sent->telegram_message_id === 7001
&& hash_equals((string) $sent->content_hash,
hash('sha256', (string) $sent->message_text)),
'Sent delivery did not retain its frozen message identity.');
$assert(!$service->tick() && $channel->calls === 1, 'Sent event was replayed.');
$second = $addEvent();
$assert($service->tick() && $channel->calls === 2,
'A second alert was not delegated to the shared channel.');
$channel->destination = null; $skipped = $addEvent();
$assert($service->tick() && $db->table('cloud_abuse_alert_deliveries')
->where('event_id', $skipped)->value('state') === 'skipped',
'Unbound channel did not terminally skip the event.');
$channel->destination = new AdministratorTelegramDestination(
1, '123456', str_repeat('a', 32)
);
$local = $addEvent(); $channel->destinationMode = 'local'; $threw = false;
try { $service->tick(); } catch (RuntimeException) { $threw = true; }
$assert($threw && !$db->table('cloud_abuse_alert_deliveries')
->where('event_id', $local)->exists(),
'Local destination failure was hidden or consumed the event.');
$channel->destinationMode = 'ready'; $channel->sendMode = 'preflight'; $now += 2;
$assert($service->tick() && $db->table('cloud_abuse_alert_deliveries')
->where('event_id', $local)->value('state') === 'skipped',
'Destination change was not safely skipped before network I/O.');
$payload = $addEvent(); $channel->sendMode = 'payload'; $now += 2;
$assert($service->tick() && $db->table('cloud_abuse_alert_deliveries')
->where('event_id', $payload)->value('state') === 'failed'
&& $db->table('cloud_abuse_alert_deliveries')
->where('event_id', $payload)->value('dispatch_started_at') === null,
'Invalid payload was not a known pre-network failure.');
$disabled = $addEvent(); $channel->sendMode = 'channel'; $now += 2;
$assert($service->tick() && $db->table('cloud_abuse_alert_deliveries')
->where('event_id', $disabled)->value('skip_reason')
=== 'channel_disabled_or_unavailable',
'Disabled channel was mislabeled as a destination change.');
$backlogged = $addEvent(); $channel->sendMode = 'backlog'; $now += 2;
$threw = false;
try { $service->tick(); } catch (RuntimeException) { $threw = true; }
$assert($threw && $db->table('cloud_abuse_alert_deliveries')
->where('event_id', $backlogged)->value('state') === 'pending',
'Known-no-remote Telegram backlog was not retained as pending.');
$channel->sendMode = 'success'; $now += 2;
$assert($service->tick(), 'Backlogged alert did not resume safely.');
$retryable = $addEvent(); $channel->sendMode = 'local'; $now += 2; $threw = false;
try { $service->tick(); } catch (RuntimeException) { $threw = true; }
$assert($threw && $db->table('cloud_abuse_alert_deliveries')
->where('event_id', $retryable)->value('state') === 'pending',
'Local send failure did not return frozen work to pending.');
$channel->sendMode = 'remote'; $now += 2; $threw = false;
try { $service->tick(); } catch (RuntimeException) { $threw = true; }
$assert($threw && $db->table('cloud_abuse_alert_deliveries')
->where('event_id', $retryable)->value('state') === 'unknown',
'Remote-effect failure was not fenced as unknown.');
$afterRemote = $addEvent(); $channel->sendMode = 'success'; $now += 2;
$failing = new CloudAbuseAlertDeliveryService($channel,
new CloudAbuseAlertMessageFormatter(),
static function () use (&$now): int { return $now; },
static function (int $messageId): void { throw new RuntimeException('fail'); });
$threw = false; try { $failing->tick(); } catch (RuntimeException) { $threw = true; }
$row = $db->table('cloud_abuse_alert_deliveries')->where('event_id', $afterRemote)->first();
$assert($threw && $row?->state === 'unknown'
&& (int) $row->telegram_message_id > 0,
'Post-send finalization failure was not retained as unknown.');
$stale = $addEvent();
$db->table('cloud_abuse_alert_deliveries')->insert([
'event_id' => $stale, 'binding_generation' => str_repeat('a', 32),
'bound_admin_id' => 1, 'chat_id' => '123456', 'telegram_message_id' => null,
'state' => 'dispatching', 'message_text' => 'frozen',
'content_hash' => hash('sha256', 'frozen'), 'error_code' => '',
'skip_reason' => '', 'dispatch_started_at' => $now - 40,
'sent_at' => null, 'terminal_at' => null,
'destination_redacted_at' => null, 'created_at' => $now - 40,
'updated_at' => $now - 40,
]);
$assert($service->tick() && $db->table('cloud_abuse_alert_deliveries')
->where('event_id', $stale)->value('state') === 'unknown',
'Stale dispatch was not fenced as unknown.');
echo "Cloud abuse alert delivery service tests passed.\n";

@ -0,0 +1,141 @@
<?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 = (object) [
'id' => 41,
'event_key' => str_repeat('a', 64),
'instance_id' => 91,
'user_id' => 17,
'host_id' => 3,
'bucket_start' => 1794182400,
'rule_version' => 1,
'policy_revision' => 4,
'transition_count' => 2,
'highest_severity' => 'high',
];
$item = static function (array $overrides = []): object {
return (object) array_replace([
'event_id' => 41,
'item_key' => str_repeat('b', 64),
'finding_key' => str_repeat('c', 64),
'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);
};
$items = [
$item(),
$item([
'item_key' => str_repeat('d', 64),
'finding_key' => str_repeat('e', 64),
'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' => 'evidence_clear',
]),
];
$formatter = new CloudAbuseAlertMessageFormatter();
Config::set('baseUrl', 'https://panel.example.test');
$message = $formatter->format($event, $items);
$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, '入站受攻击')
&& str_contains($message, 'TCP SYN Flow 4,321')
&& str_contains($message,
'https://panel.example.test/admin/abuse-monitoring?query=91'),
'Formatter omitted immutable identity, direction or aggregate evidence.');
$assert(!str_contains($message, '203.0.113.9')
&& !str_contains($message, '通信正文:'),
'Formatter exposed address or packet-content fields.');
$assert(mb_strlen($message, 'UTF-8') <= 4096,
'Formatter exceeded Telegram message length.');
$resolvedEvent = clone $event;
$resolvedEvent->transition_count = 1;
$resolvedEvent->highest_severity = 'suspicious';
Config::set('baseUrl', 'http://127.0.0.1:8765');
$resolved = $formatter->format($resolvedEvent, [$items[1]]);
$assert(str_contains($resolved, '网络风险已恢复')
&& str_contains($resolved, '可疑状态已解除'),
'Resolved alert was not rendered as a recovery.');
$manyItems = [];
for ($index = 0; $index < 80; $index++) {
$manyItems[] = $item([
'item_key' => hash('sha256', 'item-' . $index),
'finding_key' => hash('sha256', 'finding-' . $index),
]);
}
$manyEvent = clone $event;
$manyEvent->transition_count = count($manyItems);
Config::set('baseUrl', 'https://panel.example.test/base');
$bounded = $formatter->format($manyEvent, $manyItems);
$assert(mb_strlen($bounded, 'UTF-8') <= 4096
&& str_contains($bounded, '其他规则:'),
'Large immutable event was not summarized within Telegram limits.');
$invalidEvidence = array_map(static fn (object $row): object => clone $row, $items);
$invalidEvidence[0]->evidence_json =
'{"remote_ip":"203.0.113.9","tcp_syn_flows":5}';
$throws(
static fn () => $formatter->format($event, $invalidEvidence),
'Address-bearing evidence was accepted.'
);
$wrongEvent = clone $event;
$wrongEvent->transition_count = 1;
$throws(
static fn () => $formatter->format($wrongEvent, $items),
'Event/item count mismatch was accepted.'
);
$wrongBucket = array_map(static fn (object $row): object => clone $row, $items);
$wrongBucket[0]->occurred_at++;
$throws(
static fn () => $formatter->format($event, $wrongBucket),
'Cross-bucket alert item was accepted.'
);
$throws(
static function () use ($formatter, $event, $items): void {
Config::set('baseUrl', "https://panel.example.test\n.invalid");
$formatter->format($event, $items);
},
'Unsafe administrator base URL was accepted.'
);
echo "Cloud abuse alert message formatter tests passed.\n";

@ -63,6 +63,16 @@ $mainTables = [
'to_severity', 'evidence_json', 'resolution_reason',
'occurred_at', 'created_at',
],
'cloud_abuse_alert_deliveries' => [
'id', 'event_id', 'binding_generation', 'bound_admin_id',
'chat_id', 'telegram_message_id', 'state', 'message_text',
'content_hash', 'error_code', 'skip_reason',
'dispatch_started_at', 'sent_at', 'terminal_at',
'destination_redacted_at', 'created_at', 'updated_at',
],
'administrator_telegram_delivery_gate' => [
'id', 'next_send_at', 'updated_at',
],
'cloud_abuse_projection_receipts' => [
'sample_key', 'instance_id', 'rule_version', 'policy_revision',
'policy_hash', 'bucket_start', 'projected_at',
@ -153,6 +163,18 @@ foreach ([
) && str_contains($setup, "'" . $index . "'"),
'Missing abuse alert item index: ' . $index);
}
foreach ([
'cloud_abuse_alert_delivery_event',
'cloud_abuse_alert_delivery_message',
'cloud_abuse_alert_delivery_due',
'cloud_abuse_alert_delivery_admin',
'cloud_abuse_alert_delivery_terminal',
] as $index) {
$assert(str_contains(
$definitions['cloud_abuse_alert_deliveries'], $index
) && str_contains($setup, "'" . $index . "'"),
'Missing abuse alert delivery index: ' . $index);
}
foreach ([
'cloud_abuse_finding_key', 'cloud_abuse_finding_instance',
'cloud_abuse_finding_owner', 'cloud_abuse_finding_host',
@ -258,6 +280,23 @@ $assert(str_contains($definitions['cloud_abuse_alert_events'],
&& str_contains($definitions['cloud_abuse_alert_event_items'],
"transition_type = 'resolved'"),
'Abuse alert event transition constraints are incomplete.');
$delivery = $definitions['cloud_abuse_alert_deliveries'];
$assert(str_contains($delivery,
'chk_cloud_abuse_alert_delivery_destination CHECK')
&& str_contains($delivery, 'binding_generation IS NOT NULL')
&& str_contains($delivery, 'chat_id IS NOT NULL')
&& str_contains($delivery, 'OR telegram_message_id > 0')
&& str_contains($delivery,
"state = 'failed'\n AND telegram_message_id IS NULL\n AND dispatch_started_at IS NULL")
&& str_contains($delivery,
"state = 'unknown'\n AND dispatch_started_at IS NOT NULL")
&& str_contains($definitions['administrator_telegram_delivery_gate'],
'chk_administrator_telegram_delivery_gate CHECK')
&& str_contains($setup,
"'chk_cloud_abuse_alert_delivery_destination'")
&& str_contains($setup,
"'chk_administrator_telegram_delivery_gate'"),
'Abuse delivery or shared Telegram gate constraints are incomplete.');
$assert(str_contains($definitions['cloud_abuse_instance_states'],
'fk_cloud_abuse_state_instance FOREIGN KEY (instance_id)')
@ -282,6 +321,13 @@ $assert(!str_contains($definitions['cloud_abuse_alert_events'],
&& str_contains($definitions['cloud_abuse_alert_event_items'],
'ON DELETE CASCADE ON UPDATE RESTRICT'),
'Alert events are not independent or item cleanup is not bounded.');
$assert(str_contains($delivery,
'fk_cloud_abuse_alert_delivery_event FOREIGN KEY (event_id)')
&& str_contains($delivery,
'ON DELETE CASCADE ON UPDATE RESTRICT')
&& str_contains($setup,
"'fk_cloud_abuse_alert_delivery_event'"),
'Abuse delivery does not cascade only from its immutable event.');
$outboxDelete = strpos(
$deletion, "'cloud_vm_network_projection_outbox'"
@ -332,7 +378,7 @@ $assert(str_contains(
$assert(str_contains(
$setup,
"SCHEMA_VERSION = 'go-agent-wss-v45-abuse-alert-events'"
"SCHEMA_VERSION = 'go-agent-wss-v46-abuse-alert-delivery'"
), 'CloudSetup schema version was not advanced for abuse observation.');
foreach (array_keys($mainTables + $logTables) as $table) {

@ -96,7 +96,7 @@ foreach ($defaults->rules() as $code => $rule) {
'The fresh schema rule seed drifted from PHP: ' . $code);
}
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v45-abuse-alert-events'")
"SCHEMA_VERSION = 'go-agent-wss-v46-abuse-alert-delivery'")
&& str_contains($service, "->where('active_slot', 1)->lockForUpdate()")
&& str_contains($service, "->where('active_slot', 1)->update")
&& str_contains($service, 'insertGetId'),

@ -258,7 +258,7 @@ $assert(str_contains($projection, '->snapshotByIdentity($revision, $hash)')
&& !str_contains($projection, '->activeSnapshot()'),
'Projection can drift from the outbox identity to the current policy.');
$assert(str_contains($deployment,
'`go-agent-wss-v45-abuse-alert-events`')
'`go-agent-wss-v46-abuse-alert-delivery`')
&& str_contains($deployment,
'`occurrence_generation = 1` 与 `transition_revision = 1`')
&& str_contains($deployment,

@ -81,6 +81,17 @@ $mainSchema->create('cloud_abuse_alert_event_items',
$table->increments('id');
$table->integer('event_id');
$table->string('finding_key');
$table->foreign('event_id')->references('id')
->on('cloud_abuse_alert_events')->cascadeOnDelete();
});
$mainSchema->create('cloud_abuse_alert_deliveries',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('event_id')->unique();
$table->string('state');
$table->integer('terminal_at')->nullable();
$table->foreign('event_id')->references('id')
->on('cloud_abuse_alert_events')->cascadeOnDelete();
});
$logSchema = $capsule->schema('log');
@ -122,6 +133,7 @@ $logSchema->create(
$main = $capsule->connection('default');
$log = $capsule->connection('log');
$main->statement('PRAGMA foreign_keys = ON');
$now = 20_000_000;
$telemetryCutoff = $now
- CloudAbuseRetentionService::TELEMETRY_RETENTION_SECONDS;
@ -246,6 +258,27 @@ $main->table('cloud_abuse_alert_event_items')->insert([
'event_id' => 1,
'finding_key' => $oldEventFindingKey,
]);
foreach ([2, 3, 4] as $eventId) {
$main->table('cloud_abuse_alert_events')->insert([
'id' => $eventId,
'sample_key' => $key('delivery-alert-' . $eventId),
'instance_id' => 1,
'user_id' => 1,
'created_at' => $readCutoff - 1,
]);
$main->table('cloud_abuse_alert_event_items')->insert([
'id' => $eventId,
'event_id' => $eventId,
'finding_key' => $key('delivery-finding-' . $eventId),
]);
}
$main->table('cloud_abuse_alert_deliveries')->insert([
['event_id' => 2, 'state' => 'sent',
'terminal_at' => $readCutoff - 1],
['event_id' => 3, 'state' => 'unknown',
'terminal_at' => $readCutoff - 1],
['event_id' => 4, 'state' => 'pending', 'terminal_at' => null],
]);
$service = new CloudAbuseRetentionService(static fn (): int => $now);
$result = $service->prune();
@ -256,6 +289,7 @@ $assert($result === [
'baselines' => 1,
'receipts' => 2,
'findings' => 1,
'alert_events' => 1,
], 'Retention pass returned unexpected bounded deletion counts.');
$assert(!$log->table('cloud_vm_network_projection_outbox')
->where('sample_key', $doneOld)->exists()
@ -296,6 +330,12 @@ $assert($main->table('cloud_abuse_alert_events')->where('id', 1)->exists()
&& $main->table('cloud_abuse_alert_event_items')
->where('event_id', 1)->exists(),
'Phase-6 retention removed an alert before delivery finality exists.');
$assert(!$main->table('cloud_abuse_alert_events')->where('id', 2)->exists()
&& !$main->table('cloud_abuse_alert_event_items')
->where('event_id', 2)->exists()
&& $main->table('cloud_abuse_alert_events')
->whereIn('id', [3, 4])->count() === 2,
'Alert retention removed pending/unknown work or retained known terminal work.');
// A storage failure after terminal outbox deletion must roll the log
// transaction back, retaining both records for a later retry.
@ -345,6 +385,7 @@ $assert($service->prune() === [
'baselines' => 0,
'receipts' => 0,
'findings' => 1,
'alert_events' => 0,
], 'A restart-safe retention pass was not idempotent.');
// Protected rows at the front of a bounded scan must not permanently starve

@ -64,7 +64,7 @@ foreach ([
'CloudSetup omits backup preparation contract: ' . $required);
}
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v45-abuse-alert-events'")
"SCHEMA_VERSION = 'go-agent-wss-v46-abuse-alert-delivery'")
&& str_contains($setup,
"'columns' => [\n 'create_request_actor_id',\n 'create_request_idempotency_key',")
&& str_contains($setup, "'non_unique' => 0"),

@ -27,10 +27,10 @@ $assert(stripos($schema, 'compatibility import') === false
$assert(!preg_match('/\bengine_[A-Za-z0-9_]*\b/', $schema),
'cloud.sql retained an engine-prefixed identifier.');
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v45-abuse-alert-events'")
"SCHEMA_VERSION = 'go-agent-wss-v46-abuse-alert-delivery'")
&& !str_contains($setup, "SCHEMA_VERSION = 'go-v1'")
&& str_contains($deployment, '`go-agent-wss-v45-abuse-alert-events`')
&& str_contains($compensation, '`go-agent-wss-v45-abuse-alert-events`'),
&& str_contains($deployment, '`go-agent-wss-v46-abuse-alert-delivery`')
&& str_contains($compensation, '`go-agent-wss-v46-abuse-alert-delivery`'),
'Outbound Agent schema must reject the incompatible earlier Go database.');
$assert(str_contains($schema,
"timezone VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'UTC'")

@ -56,7 +56,7 @@ foreach ([
}
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v45-abuse-alert-events'"),
"SCHEMA_VERSION = 'go-agent-wss-v46-abuse-alert-delivery'"),
'CloudSetup schema version was not advanced for maintenance notices.');
echo "Maintenance notice schema contracts passed.\n";

@ -14,6 +14,7 @@ $assert = static function (bool $condition, string $message): void {
$order = [];
$maintenanceCalls = 0;
$compensationCalls = 0;
$abuseCalls = 0;
$service = new CloudNotificationDeliveryService(
static function () use (&$order, &$maintenanceCalls): bool {
$order[] = 'maintenance';
@ -24,18 +25,31 @@ $service = new CloudNotificationDeliveryService(
$order[] = 'compensation';
$compensationCalls++;
return true;
},
static function () use (&$order, &$abuseCalls): bool {
$order[] = 'abuse';
$abuseCalls++;
return false;
}
);
$assert($service->tick() === true
&& $order === ['maintenance', 'compensation']
&& $order === ['maintenance', 'compensation', 'abuse']
&& $maintenanceCalls === 1 && $compensationCalls === 1
&& $abuseCalls === 1
&& $service->observationErrors() === [],
'First notification pass was not bounded or did not report work.');
$order = [];
$assert($service->tick() === true
&& $order === ['compensation', 'maintenance']
&& $maintenanceCalls === 2 && $compensationCalls === 2,
&& $order === ['compensation', 'abuse', 'maintenance']
&& $maintenanceCalls === 2 && $compensationCalls === 2
&& $abuseCalls === 2,
'Notification channel priority did not alternate fairly.');
$order = [];
$assert($service->tick() === true
&& $order === ['abuse', 'maintenance', 'compensation']
&& $maintenanceCalls === 3 && $compensationCalls === 3
&& $abuseCalls === 3,
'Third notification channel did not receive fair first priority.');
$order = [];
$isolated = new CloudNotificationDeliveryService(
@ -46,26 +60,46 @@ $isolated = new CloudNotificationDeliveryService(
static function () use (&$order): bool {
$order[] = 'compensation';
return true;
},
static function () use (&$order): bool {
$order[] = 'abuse';
return true;
}
);
$assert($isolated->tick() === true
&& $order === ['maintenance', 'compensation']
&& $order === ['maintenance', 'compensation', 'abuse']
&& $isolated->observationErrors() === [
'maintenance_notification_failed',
], 'A maintenance failure blocked compensation delivery or was hidden.');
$abuseUnavailable = new CloudNotificationDeliveryService(
static fn (): bool => false,
static fn (): bool => false,
static function (): bool {
throw new RuntimeException('expected local Telegram failure');
}
);
$assert($abuseUnavailable->tick() === false
&& $abuseUnavailable->observationErrors() === [
'abuse_alert_notification_failed',
], 'A local Telegram failure was reported as a healthy idle pass.');
$bothFailed = new CloudNotificationDeliveryService(
static function (): bool {
throw new RuntimeException('expected maintenance failure');
},
static function (): bool {
throw new RuntimeException('expected compensation failure');
},
static function (): bool {
throw new RuntimeException('expected abuse alert failure');
}
);
$assert($bothFailed->tick() === false
&& $bothFailed->observationErrors() === [
'maintenance_notification_failed',
'notification_failed',
'abuse_alert_notification_failed',
], 'Multiple notification channel failures were not retained.');
echo "Cloud notification delivery service test passed.\n";

@ -53,7 +53,7 @@ $assert(is_string($schema)
'Public-pool replacement lacks durable batch, child, or claim schema.');
$assert(is_string($setup)
&& str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v45-abuse-alert-events'")
"SCHEMA_VERSION = 'go-agent-wss-v46-abuse-alert-delivery'")
&& str_contains($setup,
"'cloud_public_ip_pool_replacement_batches'")
&& str_contains($setup,

@ -170,7 +170,7 @@ $assert(str_contains($setup,
'CloudSetup does not verify index uniqueness and ordered columns.');
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v45-abuse-alert-events'")
"SCHEMA_VERSION = 'go-agent-wss-v46-abuse-alert-delivery'")
&& str_contains($setup, "'cloud_public_network_transitions'")
&& str_contains($setup, "'cloud_public_network_transition_ips'")
&& str_contains($setup, "'reservation_ip_claim_id'")

@ -57,7 +57,7 @@ $assert($replica !== ''
&& str_contains($replica, 'cleanup_resolved_at BIGINT UNSIGNED'),
'Rescue replica cannot prove exact identity or retain cleanup debt.');
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v45-abuse-alert-events'")
"SCHEMA_VERSION = 'go-agent-wss-v46-abuse-alert-delivery'")
&& str_contains($setup, "'cloud_rescue_artifact_delete_due'")
&& str_contains($setup, "'cloud_rescue_replica_cleanup'"),
'CloudSetup version or rescue catalog indexes are incomplete.');

@ -218,7 +218,7 @@ foreach ([
'CloudSetup omits restore/migration ledger contract: ' . $required);
}
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v45-abuse-alert-events'")
"SCHEMA_VERSION = 'go-agent-wss-v46-abuse-alert-delivery'")
&& str_contains($attemptModel,
"protected \$table = 'cloud_backup_restore_attempts';")
&& str_contains($attemptModel, "'active_attempt' => 'int'")

@ -105,7 +105,7 @@ $assert(str_contains($setup,
'CloudSetup lost the open-compensation fence index contract.');
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v45-abuse-alert-events'"),
"SCHEMA_VERSION = 'go-agent-wss-v46-abuse-alert-delivery'"),
'CloudSetup schema version was not advanced for service compensation.');
$appliedShape = preg_match(

@ -138,7 +138,7 @@ $assert(is_string($publicationModel)
&& str_contains($artifactModel, "'maximum_size_bytes' => 'int'"),
'Storage publication/artifact model contracts are incomplete.');
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v45-abuse-alert-events'"),
"SCHEMA_VERSION = 'go-agent-wss-v46-abuse-alert-delivery'"),
'CloudSetup schema version was not advanced for durable publications.');
echo "Storage publication schema contracts passed.\n";

@ -63,7 +63,7 @@ $assert(str_contains($schema,
&& str_contains($schema,
'CONSTRAINT chk_cloud_template_import_completion CHECK (')
&& str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v45-abuse-alert-events'"),
"SCHEMA_VERSION = 'go-agent-wss-v46-abuse-alert-delivery'"),
'Template imports are missing durable identity or terminal-state constraints.');
$assert(str_contains($routes, "'/admin/cloud-template-imports'")
&& str_contains($routes, "'/admin/cloud-template-imports/{id}'")

@ -71,7 +71,7 @@ $assert(str_contains($schema,
'Service-period traffic-pool identity is not frozen or indexed.');
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v45-abuse-alert-events'")
"SCHEMA_VERSION = 'go-agent-wss-v46-abuse-alert-delivery'")
&& str_contains($setup, "'chk_cloud_traffic_entitlement_balance'")
&& str_contains($setup, "'cloud_traffic_ledger_sequence'"),
'CloudSetup schema version or named-object validation is incomplete.');

@ -101,7 +101,7 @@ $assert(str_contains($jobs, 'vpc_id BIGINT UNSIGNED DEFAULT NULL')
'Cloud jobs cannot be correlated to VPC or VPC-port work.');
$assert(str_contains($setup,
"private const SCHEMA_VERSION = 'go-agent-wss-v45-abuse-alert-events'")
"private const SCHEMA_VERSION = 'go-agent-wss-v46-abuse-alert-delivery'")
&& str_contains($setup, "'name' => 'cloud_vpc_enabled'")
&& str_contains($setup, "'value' => 'false'")
&& str_contains($setup,

@ -94,6 +94,8 @@ $schema->create('ticket_telegram_deliveries', static function (Blueprint $table)
$table->increments('id');
$table->integer('ticket_id');
$table->integer('bound_admin_id');
$table->string('state')->default('sent');
$table->integer('updated_at')->default(0);
});
$schema->create('yipay_orders', static function (Blueprint $table) {
$table->increments('id');
@ -133,6 +135,24 @@ $schema->create('cloud_abuse_alert_event_items',
$table->foreign('event_id')->references('id')
->on('cloud_abuse_alert_events')->cascadeOnDelete();
});
$schema->create('cloud_abuse_alert_deliveries',
static function (Blueprint $table) {
$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->string('error_code')->default('');
$table->string('skip_reason')->default('');
$table->integer('dispatch_started_at')->nullable();
$table->integer('terminal_at')->nullable();
$table->integer('destination_redacted_at')->nullable();
$table->integer('updated_at')->default(0);
$table->foreign('event_id')->references('id')
->on('cloud_abuse_alert_events')->cascadeOnDelete();
});
$schema->create('cloud_orders', static function (Blueprint $table) {
$table->increments('id'); $table->integer('user_id'); $table->string('status');
});
@ -281,6 +301,22 @@ Capsule::table('cloud_abuse_alert_event_items')->insert([
['id' => 1, 'event_id' => 1],
['id' => 2, 'event_id' => 2],
]);
Capsule::table('cloud_abuse_alert_deliveries')->insert([
[
'id' => 1, 'event_id' => 1,
'binding_generation' => str_repeat('a', 32),
'bound_admin_id' => 1, 'chat_id' => '111111',
'telegram_message_id' => 101, 'state' => 'sent',
'terminal_at' => time() - 10, 'updated_at' => time() - 10,
],
[
'id' => 2, 'event_id' => 2,
'binding_generation' => str_repeat('b', 32),
'bound_admin_id' => 1, 'chat_id' => '111111',
'telegram_message_id' => 102, 'state' => 'sent',
'terminal_at' => time() - 10, 'updated_at' => time() - 10,
],
]);
Capsule::table('cloud_orders')->insert([
['id' => 102, 'user_id' => 1, 'status' => 'completed'],
['id' => 302, 'user_id' => 3, 'status' => 'completed'],
@ -391,6 +427,16 @@ $assert(Capsule::table('cloud_instances')->where('id', 301)->exists()
&& Capsule::table('cloud_abuse_alert_event_items')
->orderBy('id')->pluck('event_id')->all() === [2],
'Transferred instance retained old-owner abuse data or was deleted.');
$retainedAbuseDelivery = Capsule::table('cloud_abuse_alert_deliveries')
->where('event_id', 2)->first();
$assert(Capsule::table('cloud_abuse_alert_deliveries')->count() === 1
&& $retainedAbuseDelivery !== null
&& $retainedAbuseDelivery->state === 'sent'
&& $retainedAbuseDelivery->bound_admin_id === null
&& $retainedAbuseDelivery->chat_id === null
&& $retainedAbuseDelivery->telegram_message_id === null
&& $retainedAbuseDelivery->destination_redacted_at !== null,
'Account deletion removed an event disposition or retained its former administrator destination.');
$assert((int) User::find(3)->ref_by === 0, 'Deleted referrer was not cleared.');
$assert(Capsule::table('ticket')->pluck('id')->all() === [20], 'Ticket replies from the deleted thread were retained.');
$assert(Capsule::table('ticket_attachments')->pluck('id')->all() === [3],
@ -614,6 +660,227 @@ $assert($pendingMailDeletion['deleted_user_ids'] === [11]
&& Capsule::table('cloud_service_compensations')->where('id', 5)->exists(),
'Locked pending compensation mail was not safely removed with the account.');
Capsule::table('user')->insert([
'id' => 12, 'email' => 'former-alert-admin@example.com',
'is_admin' => 0, 'ref_by' => 0,
]);
Capsule::table('cloud_abuse_alert_events')->insert([
'id' => 12, 'instance_id' => 301, 'user_id' => 3,
]);
Capsule::table('cloud_abuse_alert_deliveries')->insert([
'id' => 12, 'event_id' => 12,
'binding_generation' => str_repeat('c', 32),
'bound_admin_id' => 12, 'chat_id' => '121212',
'telegram_message_id' => null, 'state' => 'dispatching',
'dispatch_started_at' => time(), 'terminal_at' => null,
'updated_at' => time(),
]);
$logConnection->table('site_visit_events')->insert([
'id' => 12, 'user_id' => 12,
]);
Capsule::table('configurations')->insert([
'id' => 12,
'name' => \App\Services\TicketTelegramConfig::BINDING,
'value' => json_encode([
'bound_admin_id' => 12,
'chat_id' => '121212',
'binding_generation' => str_repeat('c', 32),
], JSON_THROW_ON_ERROR),
]);
try {
(new UserDataDeletionService())->deleteUsers([12], false);
throw new RuntimeException('Dispatching abuse alert recipient was deleted.');
} catch (RuntimeException $error) {
$assert(str_contains($error->getMessage(), '投递中的网络滥用告警'),
'Dispatching abuse alert deletion returned the wrong error.');
}
$assert(User::find(12) !== null
&& Capsule::table('cloud_abuse_alert_deliveries')
->where('id', 12)->value('state') === 'dispatching',
'Dispatching abuse alert deletion fence did not roll back.');
$assert($logConnection->table('site_visit_events')
->where('user_id', 12)->exists()
&& Capsule::table('configurations')->where('id', 12)->exists(),
'Dispatching abuse guard ran after irreversible log or binding cleanup.');
Capsule::table('cloud_abuse_alert_deliveries')->where('id', 12)->update([
'state' => 'unknown', 'terminal_at' => time(),
]);
$formerAdminDeletion = (new UserDataDeletionService())
->deleteUsers([12], false);
$formerAdminDelivery = Capsule::table('cloud_abuse_alert_deliveries')
->where('id', 12)->first();
$assert($formerAdminDeletion['deleted_user_ids'] === [12]
&& $formerAdminDelivery?->state === 'unknown'
&& $formerAdminDelivery->bound_admin_id === null
&& $formerAdminDelivery->chat_id === null
&& $formerAdminDelivery->destination_redacted_at !== null,
'Former alert administrator was not anonymized without losing event disposition.');
$assert(!Capsule::table('configurations')->where('id', 12)->exists(),
'Deleted former administrator retained an active Telegram binding.');
Capsule::table('user')->insert([
'id' => 13, 'email' => 'pending-binding-admin@example.com',
'is_admin' => 0, 'ref_by' => 0,
]);
Capsule::table('configurations')->insert([
'id' => 13,
'name' => \App\Services\TicketTelegramConfig::BINDING,
'value' => json_encode([
'bound_admin_id' => 2,
'chat_id' => '222222',
'binding_generation' => str_repeat('d', 32),
'pending_admin_id' => 13,
'pending_hash' => str_repeat('e', 64),
'pending_expires_at' => time() + 300,
], JSON_THROW_ON_ERROR),
]);
(new UserDataDeletionService())->deleteUsers([13], false);
$retainedBinding = json_decode((string) Capsule::table('configurations')
->where('id', 13)->value('value'), true);
$assert(($retainedBinding['bound_admin_id'] ?? 0) === 2
&& !array_key_exists('pending_admin_id', $retainedBinding)
&& !array_key_exists('pending_hash', $retainedBinding)
&& !array_key_exists('pending_expires_at', $retainedBinding),
'Deleted pending administrator was left consumable after user-id reuse.');
Capsule::table('user')->insert([
'id' => 14, 'email' => 'ticket-dispatch-admin@example.com',
'is_admin' => 0, 'ref_by' => 0,
]);
Capsule::table('ticket_telegram_deliveries')->insert([
'id' => 14, 'ticket_id' => 0, 'bound_admin_id' => 14,
'state' => 'dispatching', 'updated_at' => time(),
]);
$logConnection->table('site_visit_events')->insert([
'id' => 14, 'user_id' => 14,
]);
try {
(new UserDataDeletionService())->deleteUsers([14], false);
throw new RuntimeException('Dispatching ticket recipient was deleted.');
} catch (RuntimeException $error) {
$assert(str_contains($error->getMessage(), '投递中的工单通知'),
'Dispatching ticket deletion returned the wrong error.');
}
$assert(User::find(14) !== null
&& $logConnection->table('site_visit_events')
->where('user_id', 14)->exists(),
'Ticket dispatch guard ran after irreversible log deletion.');
Capsule::table('ticket_telegram_deliveries')->where('id', 14)->update([
'state' => 'failed',
]);
(new UserDataDeletionService())->deleteUsers([14], false);
$assert(User::find(14) === null
&& !$logConnection->table('site_visit_events')
->where('user_id', 14)->exists(),
'Known terminal ticket delivery prevented safe account deletion.');
Capsule::table('user')->insert([
'id' => 16, 'email' => 'stale-ticket-admin@example.com',
'is_admin' => 0, 'ref_by' => 0,
]);
Capsule::table('ticket_telegram_deliveries')->insert([
'id' => 16, 'ticket_id' => 0, 'bound_admin_id' => 16,
'state' => 'dispatching', 'updated_at' => time() - 40,
]);
(new UserDataDeletionService())->deleteUsers([16], false);
$assert(User::find(16) === null
&& !Capsule::table('ticket_telegram_deliveries')->where('id', 16)->exists(),
'Stale ticket dispatch permanently blocked account deletion.');
Capsule::table('user')->insert([
'id' => 18, 'email' => 'ticket-owner-dispatch@example.com',
'is_admin' => 0, 'ref_by' => 0,
]);
Capsule::table('ticket')->insert([
'id' => 180, 'userid' => 18, 'rootid' => 0,
]);
Capsule::table('ticket_telegram_deliveries')->insert([
'id' => 18, 'ticket_id' => 180, 'bound_admin_id' => 2,
'state' => 'dispatching', 'updated_at' => time(),
]);
$logConnection->table('site_visit_events')->insert([
'id' => 18, 'user_id' => 18,
]);
try {
(new UserDataDeletionService())->deleteUsers([18], false);
throw new RuntimeException('Ticket owner was deleted during dispatch.');
} catch (RuntimeException $error) {
$assert(str_contains($error->getMessage(), '投递中的工单通知'),
'Owner-ticket dispatch deletion returned the wrong error.');
}
$assert(User::find(18) !== null
&& Capsule::table('ticket_telegram_deliveries')->where('id', 18)->exists()
&& $logConnection->table('site_visit_events')
->where('user_id', 18)->exists(),
'Owner-ticket dispatch fence allowed partial deletion.');
Capsule::table('ticket_telegram_deliveries')->where('id', 18)->update([
'state' => 'unknown',
]);
(new UserDataDeletionService())->deleteUsers([18], false);
$assert(User::find(18) === null
&& !Capsule::table('ticket_telegram_deliveries')->where('id', 18)->exists(),
'Terminal owner-ticket delivery prevented safe account deletion.');
Capsule::table('user')->insert([
'id' => 15, 'email' => 'alert-owner@example.com',
'is_admin' => 0, 'ref_by' => 0,
]);
Capsule::table('cloud_abuse_alert_events')->insert([
'id' => 15, 'instance_id' => 301, 'user_id' => 15,
]);
Capsule::table('cloud_abuse_alert_deliveries')->insert([
'id' => 15, 'event_id' => 15,
'binding_generation' => str_repeat('f', 32),
'bound_admin_id' => 2, 'chat_id' => '222222',
'telegram_message_id' => null, 'state' => 'dispatching',
'dispatch_started_at' => time(), 'terminal_at' => null,
'updated_at' => time(),
]);
$logConnection->table('site_visit_events')->insert([
'id' => 15, 'user_id' => 15,
]);
try {
(new UserDataDeletionService())->deleteUsers([15], false);
throw new RuntimeException('Owner event was cascaded during dispatch.');
} catch (RuntimeException $error) {
$assert(str_contains($error->getMessage(), '投递中的网络滥用告警'),
'Owner-event dispatch deletion returned the wrong error.');
}
$assert(User::find(15) !== null
&& Capsule::table('cloud_abuse_alert_events')->where('id', 15)->exists()
&& $logConnection->table('site_visit_events')
->where('user_id', 15)->exists(),
'Owner-event dispatch fence allowed partial account deletion.');
Capsule::table('cloud_abuse_alert_deliveries')->where('id', 15)->update([
'state' => 'unknown', 'terminal_at' => time(),
]);
(new UserDataDeletionService())->deleteUsers([15], false);
$assert(User::find(15) === null
&& !Capsule::table('cloud_abuse_alert_events')->where('id', 15)->exists()
&& !Capsule::table('cloud_abuse_alert_deliveries')->where('id', 15)->exists(),
'Terminal owner event did not cascade with tenant deletion.');
Capsule::table('user')->insert([
'id' => 17, 'email' => 'stale-alert-owner@example.com',
'is_admin' => 0, 'ref_by' => 0,
]);
Capsule::table('cloud_abuse_alert_events')->insert([
'id' => 17, 'instance_id' => 301, 'user_id' => 17,
]);
Capsule::table('cloud_abuse_alert_deliveries')->insert([
'id' => 17, 'event_id' => 17,
'binding_generation' => str_repeat('a', 32),
'bound_admin_id' => 2, 'chat_id' => '222222',
'telegram_message_id' => null, 'state' => 'dispatching',
'dispatch_started_at' => time() - 40, 'terminal_at' => null,
'updated_at' => time() - 40,
]);
(new UserDataDeletionService())->deleteUsers([17], false);
$assert(User::find(17) === null
&& !Capsule::table('cloud_abuse_alert_events')->where('id', 17)->exists()
&& !Capsule::table('cloud_abuse_alert_deliveries')->where('id', 17)->exists(),
'Stale abuse dispatch permanently blocked owner deletion.');
Capsule::table('user')->insert([
'id' => 7, 'email' => 'rollback-attachment@example.com',
'is_admin' => 0, 'ref_by' => 0,

@ -39,6 +39,10 @@ namespace App\Services {
$connection->table('user')->whereIn('id', [$user->id])->update(['login_session_version' => 1]);
}
}
class TicketTelegramConfig
{
public const BINDING = 'ticket_telegram_binding';
}
require dirname(__DIR__) . '/app/Services/UserDataDeletionService.php';
}
@ -79,6 +83,7 @@ namespace {
public function __construct($connection, $table) { $this->connection = $connection; $this->table = $table; }
public function whereIn($column, $values) { $this->conditions[] = static fn ($row) => in_array($row[$column], $values, true); return $this; }
public function where($column, $value) { $this->conditions[] = static fn ($row) => $row[$column] === $value; return $this; }
public function orderBy($column) { return $this; }
public function lockForUpdate() { return $this; }
private function matches($row)
{
@ -93,6 +98,13 @@ namespace {
}
return new DeleteTestCollection($rows);
}
public function first($columns = ['*'])
{
foreach ($this->connection->rows[$this->table] as $row) {
if ($this->matches($row)) { return (object) $row; }
}
return null;
}
public function pluck($column)
{
$values = [];
@ -116,6 +128,10 @@ namespace {
return count($old) - count($this->connection->rows[$this->table]);
}
}
function collect(array $rows = []): DeleteTestCollection
{
return new DeleteTestCollection($rows);
}
$assert = static function ($condition, $message) { if (!$condition) { throw new \RuntimeException($message); } };
$rows = [
'user' => [

@ -221,6 +221,30 @@ namespace App\Services\Cloud {
}
}
namespace App\Services\Telegram {
require_once dirname(__DIR__) . '/app/Services/Telegram/AdministratorTelegramDestination.php';
require_once dirname(__DIR__) . '/app/Services/Telegram/AdministratorTelegramDeliveryChannel.php';
require_once dirname(__DIR__) . '/app/Services/Telegram/AdministratorTelegramRateLimiter.php';
final class DatabaseAdministratorTelegramRateGate
implements AdministratorTelegramRateLimiter
{
public static int $permits = 0;
public static string $mode = 'ready';
public function acquire(
AdministratorTelegramDestination $destination
): void {
self::$permits++;
if (self::$mode === 'backlog') {
throw new AdministratorTelegramRateBacklog(
'synthetic shared backlog'
);
}
}
}
}
namespace App\Services\Ticket {
final readonly class ReplyTicketCommand
{
@ -346,6 +370,12 @@ namespace {
use App\Controllers\TelegramWebhookController;
use TelegramBot\Api\BotApi;
final class TelegramTestAbuseStore
{
public static array $rows = [];
public static bool $queryFails = false;
}
final class TelegramTestConnection
{
public function transaction($callback)
@ -354,6 +384,118 @@ namespace {
try { return $callback(); }
finally { Configurations::$transactions--; }
}
public function table(string $table): object
{
if ($table === 'user') {
return new TelegramTestUserQuery();
}
if ($table === 'ticket') {
return new TelegramTestTicketRootQuery();
}
if ($table === 'cloud_abuse_alert_deliveries') {
if (TelegramTestAbuseStore::$queryFails) {
throw new RuntimeException('synthetic abuse ledger failure');
}
return new TelegramTestAbuseQuery();
}
throw new RuntimeException('Unexpected table: ' . $table);
}
public function getSchemaBuilder(): object
{
return new class {
public function hasTable(string $table): bool
{
return $table === 'cloud_abuse_alert_deliveries';
}
};
}
}
final class TelegramTestUserQuery
{
private ?int $id = null;
public function where(string $column, mixed $value): self
{
if ($column === 'id') { $this->id = (int) $value; }
return $this;
}
public function lockForUpdate(): self
{
if (Configurations::$transactions === 0) {
throw new RuntimeException('User lock outside transaction');
}
Configurations::$locks++;
return $this;
}
public function first(array $columns = ['*']): ?User
{
return $this->id === null ? null : (User::$users[$this->id] ?? null);
}
}
final class TelegramTestTicketRootQuery
{
private array $filters = [];
public function where(string $column, mixed $value): self
{
$this->filters[$column] = $value;
return $this;
}
public function lockForUpdate(): self
{
if (Configurations::$transactions === 0) {
throw new RuntimeException('Ticket lock outside transaction');
}
Configurations::$locks++;
return $this;
}
public function first(array $columns = ['*']): ?object
{
$row = (object) ['id' => 42, 'rootid' => 0];
foreach ($this->filters as $column => $value) {
if (($row->{$column} ?? null) != $value) { return null; }
}
return $row;
}
}
final class TelegramTestAbuseQuery
{
private array $filters = [];
private array $inFilters = [];
private array $nullFilters = [];
public function where(string $column, mixed $value): self
{
$this->filters[$column] = $value;
return $this;
}
public function whereIn(string $column, array $values): self
{
$this->inFilters[$column] = $values;
return $this;
}
public function whereNull(string $column): self
{
$this->nullFilters[] = $column;
return $this;
}
public function first(): ?object
{
foreach (TelegramTestAbuseStore::$rows as $row) {
foreach ($this->filters as $column => $value) {
if (($row->{$column} ?? null) != $value) { continue 2; }
}
foreach ($this->inFilters as $column => $values) {
if (!in_array($row->{$column} ?? null, $values, true)) {
continue 2;
}
}
foreach ($this->nullFilters as $column) {
if (($row->{$column} ?? null) !== null) { continue 2; }
}
return $row;
}
return null;
}
}
final class TelegramTestRedisBackend
{
@ -446,11 +588,13 @@ namespace {
require dirname(__DIR__) . '/app/Services/AdminPermission.php';
require dirname(__DIR__) . '/app/Services/TicketTelegramCredentialCipher.php';
require dirname(__DIR__) . '/app/Services/TicketTelegramConfig.php';
require dirname(__DIR__) . '/app/Services/Telegram/AdministratorTelegramDestination.php';
require_once dirname(__DIR__) . '/app/Services/Telegram/AdministratorTelegramDestination.php';
require_once dirname(__DIR__) . '/app/Services/Telegram/AdministratorTelegramRateLimiter.php';
require dirname(__DIR__) . '/app/Services/Telegram/AdministratorTelegramTransport.php';
require dirname(__DIR__) . '/app/Services/Telegram/AdministratorTelegramPreflightException.php';
require dirname(__DIR__) . '/app/Services/Telegram/AdministratorTelegramLocalUnavailable.php';
require dirname(__DIR__) . '/app/Services/Telegram/AdministratorTelegramRemoteEffectUnknown.php';
require dirname(__DIR__) . '/app/Services/Telegram/AdministratorTelegramRateBacklog.php';
require dirname(__DIR__) . '/app/Services/Telegram/TelegramBotApiTransport.php';
require dirname(__DIR__) . '/app/Services/Telegram/AdministratorTelegramChannel.php';
require dirname(__DIR__) . '/app/Services/Ticket/TicketWriteResult.php';
@ -679,6 +823,28 @@ namespace {
&& count(BotApi::$calls) === $callsAfterUnknown,
'An uncertain Telegram delivery was not fenced against replay.');
\App\Services\Telegram\DatabaseAdministratorTelegramRateGate::$mode
= 'backlog';
$callsBeforeBacklog = count(BotApi::$calls);
$backlogEvent = $ticketEvent('user_reply', 48);
$assert(TicketTelegramNotifier::notify($backlogEvent)
=== TicketTelegramNotificationResult::Failed,
'Shared Telegram backlog was reported as a successful ticket alert.');
$backlogRows = array_values(array_filter(
TicketTelegramDelivery::$rows,
static fn ($row): bool => $row->ticket_message_id === 48
));
$assert(count($backlogRows) === 1
&& $backlogRows[0]->state === 'failed'
&& $backlogRows[0]->telegram_message_id === null
&& count(BotApi::$calls) === $callsBeforeBacklog
&& TicketTelegramNotifier::notify($backlogEvent)
=== TicketTelegramNotificationResult::Failed
&& count(BotApi::$calls) === $callsBeforeBacklog,
'Known-no-remote Telegram backlog lost its terminal ticket ledger.');
\App\Services\Telegram\DatabaseAdministratorTelegramRateGate::$mode
= 'ready';
// A concurrent rebind after reservation but before the network request is
// a known preflight failure: no message was sent and no unknown row remains.
$bindingBeforePreflight = $state();
@ -739,6 +905,76 @@ namespace {
&& count(BotApi::$calls) === $receiptCount,
'A replayed Telegram update duplicated the reply notification or receipt.');
$abuseRow = static function (
?int $messageId,
string $state = 'sent',
?int $redactedAt = null
) use ($delivery): object {
return (object) [
'binding_generation' => $delivery->binding_generation,
'bound_admin_id' => 1,
'chat_id' => '123456',
'telegram_message_id' => $messageId,
'state' => $state,
'destination_redacted_at' => $redactedAt,
];
};
$commandsBeforeAbuse = count(TicketThreadService::$commands);
TelegramTestAbuseStore::$rows = [$abuseRow(3001)];
$assert(TicketTelegramNotifier::handleUpdate(
$ticketReply(601, 'must not become ticket reply', '123456', 3001)
) && count(TicketThreadService::$commands) === $commandsBeforeAbuse
&& str_contains(
BotApi::$calls[count(BotApi::$calls) - 1][1]['text'],
'网络滥用告警为只读通知'
), 'Sent abuse alert reply reached the ticket workflow.');
TelegramTestAbuseStore::$rows = [$abuseRow(3002, 'unknown')];
$assert(TicketTelegramNotifier::handleUpdate(
$ticketReply(602, 'unknown alert reply', '123456', 3002)
) && count(TicketThreadService::$commands) === $commandsBeforeAbuse,
'Unknown abuse alert with a remote message id reached ticket reply.');
TelegramTestAbuseStore::$rows = [$abuseRow(1000)];
$assert(TicketTelegramNotifier::handleUpdate(
$ticketReply(603, 'conflicting reply', '123456', 1000)
) && count(TicketThreadService::$commands) === $commandsBeforeAbuse
&& str_contains(
BotApi::$calls[count(BotApi::$calls) - 1][1]['text'],
'回复映射存在冲突'
), 'Ticket/abuse reply mapping conflict was not refused.');
TelegramTestAbuseStore::$rows = [
$abuseRow(3003, 'sent', time()),
];
$assert(TicketTelegramNotifier::handleUpdate(
$ticketReply(604, 'redacted alert reply', '123456', 3003)
) && count(TicketThreadService::$commands) === $commandsBeforeAbuse
&& str_contains(
BotApi::$calls[count(BotApi::$calls) - 1][1]['text'],
'无法识别这条工单通知'
), 'Redacted abuse destination was still treated as a live mapping.');
TelegramTestAbuseStore::$rows = [$abuseRow(null, 'unknown')];
$assert(TicketTelegramNotifier::handleUpdate(
$ticketReply(606, 'no-message alert reply', '123456', 3004)
) && count(TicketThreadService::$commands) === $commandsBeforeAbuse
&& str_contains(
BotApi::$calls[count(BotApi::$calls) - 1][1]['text'],
'无法识别这条工单通知'
), 'Abuse delivery without a remote message id became replyable.');
TelegramTestAbuseStore::$queryFails = true;
$assert(TicketTelegramNotifier::handleUpdate(
$ticketReply(605, 'mapping unavailable', '123456', 1000)
) && count(TicketThreadService::$commands) === $commandsBeforeAbuse
&& str_contains(
BotApi::$calls[count(BotApi::$calls) - 1][1]['text'],
'暂时无法核对这条通知'
), 'Abuse mapping failure created a ticket reply.');
TelegramTestAbuseStore::$queryFails = false;
TelegramTestAbuseStore::$rows = [];
$groupReply = $ticketReply(502);
$groupReply['message']['chat']['type'] = 'group';
$forwardedReply = $ticketReply(503);
@ -837,11 +1073,17 @@ namespace {
$fallbackEvent->result->root->userid = 999;
$fallbackEvent->result->root->instance_id = 999;
$fallbackEvent->result->message->content = str_repeat('测', 5000);
$callsBeforeMissingOwner = count(BotApi::$calls);
$assert(TicketTelegramNotifier::notify($fallbackEvent)
=== TicketTelegramNotificationResult::Failed
&& count(BotApi::$calls) === $callsBeforeMissingOwner,
'A deleted ticket owner received a late Telegram delivery ledger.');
$fallbackEvent->result->root->userid = 7;
$assert(TicketTelegramNotifier::notify($fallbackEvent)
=== TicketTelegramNotificationResult::Sent,
'A missing user/instance projection suppressed the Telegram notification.');
'A missing instance projection suppressed a valid owner notification.');
$fallbackText = BotApi::$calls[count(BotApi::$calls) - 1][1]['text'] ?? '';
$assert(str_contains($fallbackText, '用户:UID 999')
$assert(str_contains($fallbackText, 'UID 7')
&& !str_contains($fallbackText, '关联实例:')
&& mb_strlen($fallbackText, 'UTF-8') <= 4096,
'Telegram notification fallback or length limit is invalid.');

@ -104,7 +104,7 @@ foreach ($models as $class => $modelTable) {
}
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v45-abuse-alert-events'")
"SCHEMA_VERSION = 'go-agent-wss-v46-abuse-alert-delivery'")
&& str_contains($setup,
"self::schema('ss.sql') . \"\\0\" . self::schema('cloud.sql')")
&& str_contains($setup,

Loading…
Cancel
Save