fix: defer ticket attachment deletion until commit

go
codex 2 weeks ago
parent 3ea294b64b
commit de636b02ca

@ -129,6 +129,30 @@ final class TicketAttachmentService
}
}
/**
* Remove committed attachment files without accepting arbitrary paths.
*/
public static function discardStorageKeys(array $storageKeys): void
{
if ($storageKeys === []) {
return;
}
$directory = rtrim(
BASE_PATH . '/storage/ticket-attachments',
DIRECTORY_SEPARATOR
);
$paths = [];
foreach (array_unique($storageKeys) as $storageKey) {
if (!is_string($storageKey) || $storageKey === ''
|| basename($storageKey) !== $storageKey) {
error_log('Ticket attachment cleanup skipped unsafe storage key');
continue;
}
$paths[] = $directory . DIRECTORY_SEPARATOR . $storageKey;
}
self::discardPaths($paths);
}
public static function path(TicketAttachment $attachment): string
{
return self::directory() . DIRECTORY_SEPARATOR . basename((string) $attachment->storage_key);

@ -3,7 +3,6 @@
namespace App\Services;
use App\Models\EmailLog;
use App\Models\TicketAttachment;
use App\Services\TicketAttachmentService;
use App\Models\User;
@ -47,7 +46,15 @@ class UserDataDeletionService
return $this->emptyResult();
}
$connection = (new User())->getConnection();
$result = $connection->transaction(function () use ($connection, $requested) {
$ticketAttachmentStorageKeys = [];
$result = $connection->transaction(function () use (
$connection,
$requested,
&$ticketAttachmentStorageKeys
) {
// Laravel may retry the transaction. Only clean files selected by
// 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();
if ($users->isEmpty()) {
@ -126,7 +133,8 @@ class UserDataDeletionService
$relatedRows = $this->deleteRelatedRecords(
$connection,
$ids,
$emails
$emails,
$ticketAttachmentStorageKeys
);
$trafficScanRows = $this->deleteCloudTrafficScanFailures(
$connection,
@ -146,6 +154,12 @@ class UserDataDeletionService
'default_rows_deleted' => $rows, 'log_rows_deleted' => $logRows, 'cache_error' => false,
];
}, 3);
if ($ticketAttachmentStorageKeys !== []) {
// Filesystem deletion is intentionally post-commit. A database
// rollback must never leave an attachment row pointing at a file
// that was already destroyed.
TicketAttachmentService::discardStorageKeys($ticketAttachmentStorageKeys);
}
if ($result['deleted'] > 0 && $cleanupCaches) {
$result['cache_error'] = !$this->cleanupCaches($result['deleted_user_ids']);
}
@ -200,7 +214,12 @@ class UserDataDeletionService
}
}
private function deleteRelatedRecords($connection, array $ids, array $emails)
private function deleteRelatedRecords(
$connection,
array $ids,
array $emails,
array &$ticketAttachmentStorageKeys
)
{
$deleted = 0;
foreach ([
@ -218,13 +237,30 @@ class UserDataDeletionService
if ($this->hasColumns($connection, 'user', ['ref_by'])) {
$connection->table('user')->whereIn('ref_by', $ids)->update(['ref_by' => 0]);
}
$ticketAttachmentStorageKeys = array_values(array_unique(array_merge(
$ticketAttachmentStorageKeys,
$this->pluckByUsers(
$connection,
'ticket_attachments',
'user_id',
$ids,
'storage_key'
)
)));
if ($this->hasColumns($connection, 'ticket', ['id', 'userid', 'rootid'])) {
$roots = $connection->table('ticket')->whereIn('userid', $ids)->where('rootid', 0)->pluck('id')->all();
if ($roots !== [] && $this->hasColumns($connection, 'ticket_attachments', ['id', 'ticket_id'])) {
foreach (TicketAttachment::whereIn('ticket_id', $roots)->get() as $attachment) {
TicketAttachmentService::remove($attachment);
}
}
$ticketAttachmentStorageKeys = array_values(array_unique(array_merge(
$ticketAttachmentStorageKeys,
$this->pluckByUsers(
$connection,
'ticket_attachments',
'ticket_id',
$roots,
'storage_key'
)
)));
$deleted += $this->deleteWhereIn(
$connection, 'ticket_attachments', 'ticket_id', $roots);
$deleted += $this->deleteWhereIn($connection, 'ticket', 'rootid', $roots);
}
$deleted += $this->deleteWhereIn($connection, 'ticket_attachments', 'user_id', $ids);

@ -8,6 +8,13 @@ use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
$System_Config = ['enable_log' => 'false'];
if (!defined('BASE_PATH')) {
define('BASE_PATH', sys_get_temp_dir() . '/virt-cloud-account-delete-test-' . bin2hex(random_bytes(6)));
}
$attachmentDirectory = BASE_PATH . '/storage/ticket-attachments';
if (!mkdir($attachmentDirectory, 0700, true) && !is_dir($attachmentDirectory)) {
throw new RuntimeException('Could not create account deletion attachment fixture directory.');
}
$assert = static function ($condition, $message) {
if (!$condition) {
throw new RuntimeException($message);
@ -30,6 +37,13 @@ $schema->create('ticket', static function (Blueprint $table) {
$table->integer('userid');
$table->integer('rootid')->default(0);
});
$schema->create('ticket_attachments', static function (Blueprint $table) {
$table->increments('id');
$table->integer('ticket_id');
$table->integer('message_id');
$table->integer('user_id');
$table->string('storage_key');
});
$schema->create('yipay_orders', static function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
@ -124,6 +138,24 @@ Capsule::table('ticket')->insert([
['id' => 11, 'userid' => 2, 'rootid' => 10],
['id' => 20, 'userid' => 3, 'rootid' => 0],
]);
$attachmentFiles = [
'deleted-user.png' => 'deleted user attachment',
'deleted-admin-reply.png' => 'administrator reply attachment',
'retained-user.png' => 'retained user attachment',
];
foreach ($attachmentFiles as $storageKey => $contents) {
if (file_put_contents($attachmentDirectory . '/' . $storageKey, $contents) === false) {
throw new RuntimeException('Could not create ticket attachment fixture.');
}
}
Capsule::table('ticket_attachments')->insert([
['id' => 1, 'ticket_id' => 10, 'message_id' => 10, 'user_id' => 1,
'storage_key' => 'deleted-user.png'],
['id' => 2, 'ticket_id' => 10, 'message_id' => 11, 'user_id' => 2,
'storage_key' => 'deleted-admin-reply.png'],
['id' => 3, 'ticket_id' => 20, 'message_id' => 20, 'user_id' => 3,
'storage_key' => 'retained-user.png'],
]);
Capsule::table('yipay_orders')->insert([
['user_id' => 1, 'order_no' => 'deleted-order'],
['user_id' => 3, 'order_no' => 'retained-order'],
@ -209,6 +241,12 @@ $assert($result['skipped_user_ids'] === [2, 999], 'Administrator or missing acco
$assert(User::find(1) === null && User::find(2) !== null && User::find(3) !== null, 'User deletion was not isolated.');
$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],
'Ticket attachment deletion was not isolated.');
$assert(!is_file($attachmentDirectory . '/deleted-user.png')
&& !is_file($attachmentDirectory . '/deleted-admin-reply.png')
&& is_file($attachmentDirectory . '/retained-user.png'),
'Ticket attachment files were not cleaned after a committed account deletion.');
foreach (['yipay_orders', 'yipay_callback_events', 'yipay_refunds'] as $tableName) {
$assert(Capsule::table($tableName)->pluck('order_no')->all() === ['retained-order'], 'Payment relation deletion was not isolated.');
}
@ -312,4 +350,44 @@ $assert(User::find(6) !== null
&& $fencedJob->sealed_payload === 'unknown-effect-create-audit',
'Unknown-effect terminal provisioning fence lost its order or job audit.');
Capsule::table('user')->insert([
'id' => 7, 'email' => 'rollback-attachment@example.com',
'is_admin' => 0, 'ref_by' => 0,
]);
Capsule::table('ticket')->insert([
'id' => 70, 'userid' => 7, 'rootid' => 0,
]);
Capsule::table('ticket_attachments')->insert([
'id' => 7, 'ticket_id' => 70, 'message_id' => 70, 'user_id' => 7,
'storage_key' => 'rollback-user.png',
]);
$rollbackAttachment = $attachmentDirectory . '/rollback-user.png';
if (file_put_contents($rollbackAttachment, 'rollback attachment') === false) {
throw new RuntimeException('Could not create rollback attachment fixture.');
}
Capsule::statement(
"CREATE TRIGGER prevent_user_7_delete BEFORE DELETE ON user "
. "WHEN OLD.id = 7 BEGIN SELECT RAISE(ABORT, 'forced user deletion failure'); END"
);
try {
(new UserDataDeletionService())->deleteUsers([7], false);
throw new RuntimeException('Forced account deletion failure did not abort.');
} catch (Throwable $error) {
$assert(str_contains($error->getMessage(), 'forced user deletion failure'),
'Forced account deletion returned the wrong error.');
}
$assert(User::find(7) !== null
&& Capsule::table('ticket')->where('id', 70)->exists()
&& Capsule::table('ticket_attachments')->where('id', 7)->exists(),
'Database rows did not roll back after the forced account deletion failure.');
$assert(is_file($rollbackAttachment),
'Ticket attachment file was deleted before the account transaction committed.');
Capsule::statement('DROP TRIGGER prevent_user_7_delete');
@unlink($rollbackAttachment);
@unlink($attachmentDirectory . '/retained-user.png');
@rmdir($attachmentDirectory);
@rmdir(BASE_PATH . '/storage');
@rmdir(BASE_PATH);
echo 'Manual account deletion SQLite checks passed.' . PHP_EOL;

Loading…
Cancel
Save