fix(abuse): align account deletion lock order

go
codex 4 days ago
parent 0991bf9efa
commit 5186d06fbe

@ -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();

@ -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');

Loading…
Cancel
Save