diff --git a/web/app/Services/UserDataDeletionService.php b/web/app/Services/UserDataDeletionService.php index ce5cf193..cdd5884e 100644 --- a/web/app/Services/UserDataDeletionService.php +++ b/web/app/Services/UserDataDeletionService.php @@ -510,18 +510,20 @@ class UserDataDeletionService )) { return; } - $owned = $connection->table('cloud_network_alert_events as event') + // Match ingestion's instance -> finding/event order. Holding an event + // first and deleting its instance later would create the inverse edge + // when a final Agent window is being committed concurrently. + $ownedInstanceIds = $connection->table('cloud_instances') + ->whereIn('user_id', $userIds)->orderBy('id')->lockForUpdate() + ->pluck('id')->map(static fn ($id): int => (int) $id)->all(); + $owned = $ownedInstanceIds === [] ? [] : $connection + ->table('cloud_network_alert_events as event') ->join( 'cloud_network_findings as finding', 'finding.id', '=', 'event.finding_id' - )->join( - 'cloud_instances as instance', - 'instance.id', - '=', - 'finding.instance_id' - )->whereIn('instance.user_id', $userIds) + )->whereIn('finding.instance_id', $ownedInstanceIds) ->pluck('event.id')->all(); $bound = $connection->table('cloud_network_alert_deliveries') ->whereIn('bound_admin_id', $userIds)->pluck('event_id')->all(); diff --git a/web/tests/ManualAccountDeletionSqliteTest.php b/web/tests/ManualAccountDeletionSqliteTest.php index cf551479..a27df79e 100644 --- a/web/tests/ManualAccountDeletionSqliteTest.php +++ b/web/tests/ManualAccountDeletionSqliteTest.php @@ -859,6 +859,20 @@ $assert(User::find(7) !== null $assert(is_file($rollbackAttachment), 'Ticket attachment file was deleted before the account transaction committed.'); +$deletionSource = file_get_contents(dirname(__DIR__) + . '/app/Services/UserDataDeletionService.php'); +$instanceLockAt = is_string($deletionSource) + ? strpos($deletionSource, '$ownedInstanceIds = $connection->table') + : false; +$eventLockAt = is_string($deletionSource) + ? strpos($deletionSource, '$lockedEventIds = $connection->table') + : false; +$assert($instanceLockAt !== false && $eventLockAt !== false + && $instanceLockAt < $eventLockAt + && str_contains($deletionSource, + "->whereIn('user_id', \$userIds)->orderBy('id')->lockForUpdate()"), + 'Account deletion no longer locks owned instances before abuse events.'); + Capsule::statement('DROP TRIGGER prevent_user_7_delete'); @unlink($rollbackAttachment); @unlink($attachmentDirectory . '/retained-user.png');