From a0b70fc30d881411d7af0bec35447140ba2baa91 Mon Sep 17 00:00:00 2001 From: codex Date: Fri, 6 Nov 2026 03:41:13 +0800 Subject: [PATCH] fix(cloud): anonymize retained compensation scopes --- .../Cloud/CloudServiceCompensationPlanner.php | 6 ++- web/app/Services/UserDataDeletionService.php | 44 +++++++++++++++++++ ...ompensationAccountDeletionContractTest.php | 5 +++ .../CloudServiceCompensationPlannerTest.php | 8 +++- web/tests/ManualAccountDeletionSqliteTest.php | 17 ++++++- 5 files changed, 76 insertions(+), 4 deletions(-) diff --git a/web/app/Services/Cloud/CloudServiceCompensationPlanner.php b/web/app/Services/Cloud/CloudServiceCompensationPlanner.php index 408ec0b3..26c94eb5 100644 --- a/web/app/Services/Cloud/CloudServiceCompensationPlanner.php +++ b/web/app/Services/Cloud/CloudServiceCompensationPlanner.php @@ -151,9 +151,11 @@ final class CloudServiceCompensationPlanner return [ 'type' => $scopeType, 'id' => $scopeId, - 'label' => (string) $instance->name, + // The per-instance rows already freeze the tenant-owned name + // and owner. Keep the long-lived batch scope free of tenant + // PII so account deletion can retain aggregate audit safely. + 'label' => '单实例 #' . $scopeId, 'instance_id' => $scopeId, - 'user_id' => (int) $instance->user_id, 'host_id' => (int) $instance->host_id, ]; } diff --git a/web/app/Services/UserDataDeletionService.php b/web/app/Services/UserDataDeletionService.php index 788d770a..45d20ce2 100644 --- a/web/app/Services/UserDataDeletionService.php +++ b/web/app/Services/UserDataDeletionService.php @@ -192,6 +192,7 @@ class UserDataDeletionService // jobs before their parent rows so encrypted owner/VM payloads do // not survive as orphaned administrator audit records. $resourceJobRows = $this->deleteCloudResourceJobs($connection, $ids); + $this->anonymizeCompensationScopes($connection, $ids); $trafficInstanceIds = $this->pluckByUsers( $connection, 'cloud_instances', @@ -412,6 +413,49 @@ class UserDataDeletionService })->delete(); } + /** + * Batch audit survives account deletion, but a historical single-instance + * scope may have frozen the tenant's chosen instance name and user ID. + * Replace that snapshot while its item rows still identify the affected + * batches. Host and public-pool scopes contain provider metadata only. + */ + private function anonymizeCompensationScopes( + $connection, + array $userIds + ): void { + if (!$this->hasColumns($connection, + 'cloud_service_compensation_items', ['batch_id', 'user_id']) + || !$this->hasColumns($connection, + 'cloud_service_compensations', [ + 'id', 'scope_type', 'scope_id', 'scope_snapshot_json', + ])) { + return; + } + $batchIds = $connection->table('cloud_service_compensation_items') + ->whereIn('user_id', $userIds)->orderBy('batch_id') + ->pluck('batch_id')->map(static fn ($id): int => (int) $id) + ->unique()->values()->all(); + if ($batchIds === []) { + return; + } + $batches = $connection->table('cloud_service_compensations') + ->whereIn('id', $batchIds)->where('scope_type', 'instance') + ->orderBy('id')->lockForUpdate()->get(['id', 'scope_id']); + foreach ($batches as $batch) { + $scopeId = (int) $batch->scope_id; + $snapshot = json_encode([ + 'type' => 'instance', + 'id' => $scopeId, + 'label' => '单实例 #' . $scopeId, + 'instance_id' => $scopeId, + ], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES + | JSON_UNESCAPED_UNICODE); + $connection->table('cloud_service_compensations') + ->where('id', (int) $batch->id) + ->update(['scope_snapshot_json' => $snapshot]); + } + } + private function deleteCloudTrafficScanFailures( $connection, array $instanceIds, diff --git a/web/tests/CloudServiceCompensationAccountDeletionContractTest.php b/web/tests/CloudServiceCompensationAccountDeletionContractTest.php index b6d3ac0b..de89e2c0 100644 --- a/web/tests/CloudServiceCompensationAccountDeletionContractTest.php +++ b/web/tests/CloudServiceCompensationAccountDeletionContractTest.php @@ -18,6 +18,11 @@ $assert(str_contains($deletion, $assert(!str_contains($deletion, "['cloud_service_compensations', 'requested_by_user_id']"), 'Account deletion must retain compensation batch audit.'); +$assert(str_contains($deletion, 'anonymizeCompensationScopes(') + && str_contains($deletion, "->where('scope_type', 'instance')") + && str_contains($deletion, "'label' => '单实例 #' . \$scopeId") + && str_contains($deletion, "['scope_snapshot_json' => \$snapshot]"), + 'Retained single-instance compensation audit is not anonymized.'); $assert(str_contains($deletion, "'cloud_service_compensations as compensation_batch'") && str_contains($deletion, diff --git a/web/tests/CloudServiceCompensationPlannerTest.php b/web/tests/CloudServiceCompensationPlannerTest.php index 4bf6fff9..dae4b3fb 100644 --- a/web/tests/CloudServiceCompensationPlannerTest.php +++ b/web/tests/CloudServiceCompensationPlannerTest.php @@ -311,9 +311,15 @@ $assert($preview['recipients'][0]['user_id'] === 1 $instancePreview = $planner->preview('instance', 101); $assert($instancePreview['scope']['type'] === 'instance' && $instancePreview['scope']['instance_id'] === 101 + && $instancePreview['scope']['label'] === '单实例 #101' + && !array_key_exists('user_id', $instancePreview['scope']) + && !str_contains( + json_encode($instancePreview['scope'], JSON_THROW_ON_ERROR), + 'vm-101' + ) && $instancePreview['instance_count'] === 1 && $instancePreview['eligible_count'] === 1, - 'Instance scope must produce one eligible preview.'); + 'Instance scope must produce one eligible preview without tenant PII.'); $deletedPreview = $planner->preview('instance', 111); $assert($deletedPreview['scope']['instance_id'] === 111 && $deletedPreview['instance_count'] === 0 diff --git a/web/tests/ManualAccountDeletionSqliteTest.php b/web/tests/ManualAccountDeletionSqliteTest.php index 3041fc7b..74a1ffe5 100644 --- a/web/tests/ManualAccountDeletionSqliteTest.php +++ b/web/tests/ManualAccountDeletionSqliteTest.php @@ -126,7 +126,9 @@ $schema->create('cloud_mail_outbox', static function (Blueprint $table) { }); $schema->create('cloud_service_compensations', static function (Blueprint $table) { $table->increments('id'); $table->integer('requested_by_user_id'); - $table->string('state'); + $table->string('state'); $table->string('scope_type')->nullable(); + $table->integer('scope_id')->nullable(); + $table->text('scope_snapshot_json')->nullable(); }); $schema->create('cloud_service_compensation_items', static function (Blueprint $table) { $table->increments('id'); $table->integer('batch_id'); @@ -251,6 +253,11 @@ Capsule::table('cloud_mail_outbox')->insert([ ]); Capsule::table('cloud_service_compensations')->insert([ 'id' => 1, 'requested_by_user_id' => 2, 'state' => 'completed', + 'scope_type' => 'instance', 'scope_id' => 501, + 'scope_snapshot_json' => json_encode([ + 'type' => 'instance', 'id' => 501, 'label' => 'tenant-secret-vm', + 'instance_id' => 501, 'user_id' => 1, 'host_id' => 5, + ], JSON_THROW_ON_ERROR), ]); Capsule::table('cloud_service_compensation_items')->insert([ ['id' => 1, 'batch_id' => 1, 'user_id' => 1, 'state' => 'applied'], @@ -308,6 +315,14 @@ $assert(Capsule::table('cloud_mail_outbox')->pluck('invoice_id')->all() === [303 'Cloud invoice mail history deletion was not isolated.'); $assert(Capsule::table('cloud_service_compensations')->pluck('id')->all() === [1], 'Account deletion removed the immutable compensation batch audit.'); +$retainedCompensationScope = json_decode((string) Capsule::table( + 'cloud_service_compensations' +)->where('id', 1)->value('scope_snapshot_json'), true, 16, + JSON_THROW_ON_ERROR); +$assert($retainedCompensationScope === [ + 'type' => 'instance', 'id' => 501, 'label' => '单实例 #501', + 'instance_id' => 501, + ], 'Account deletion retained tenant PII in compensation batch audit.'); $assert(Capsule::table('cloud_service_compensation_items')->pluck('user_id')->all() === [3] && Capsule::table('cloud_service_compensation_recipients')