From 539cdbce86f688361c818fadf492fbf8fee3ba18 Mon Sep 17 00:00:00 2001 From: Ray1111-aa Date: Wed, 28 Oct 2026 09:52:06 +0700 Subject: [PATCH] fix(backup): pastikan data tong sampah (soft deletes) tidak bocor ke arsip backup .kmh --- app/Services/DatabaseBackupService.php | 11 +++++-- tests/Feature/BackupAndRestoreTest.php | 42 ++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/app/Services/DatabaseBackupService.php b/app/Services/DatabaseBackupService.php index f8d04ad..7422f17 100644 --- a/app/Services/DatabaseBackupService.php +++ b/app/Services/DatabaseBackupService.php @@ -5,6 +5,7 @@ namespace App\Services; use Carbon\Carbon; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\File; +use Illuminate\Support\Facades\Schema; use PDO; use RuntimeException; use ZipArchive; @@ -571,9 +572,9 @@ class DatabaseBackupService ], 'system' => [ 'name' => 'Sistem & Internal Database', - 'description' => 'Migrasi, sesi, cache, dan antrean background jobs.', + 'description' => 'Migrasi, sesi, cache, pengaturan retensi, dan antrean background jobs.', 'tables' => array_values(array_intersect($allTables, [ - 'migrations', 'sessions', 'cache', 'cache_locks', + 'system_settings', 'migrations', 'sessions', 'cache', 'cache_locks', 'jobs', 'job_batches', 'failed_jobs', 'password_reset_tokens' ])), ], @@ -674,6 +675,12 @@ class DatabaseBackupService fwrite($handle, "-- Dumping Data untuk `{$tableName}`\n"); $query = DB::table($tableName); + + // Pastikan data yang berada di tong sampah (soft-deleted) tidak bocor ke dalam data backup + if (Schema::hasColumn($tableName, 'deleted_at')) { + $query->whereNull('deleted_at'); + } + $totalRows = $query->count(); if ($totalRows === 0) { diff --git a/tests/Feature/BackupAndRestoreTest.php b/tests/Feature/BackupAndRestoreTest.php index 32cb9ba..c3b80c5 100644 --- a/tests/Feature/BackupAndRestoreTest.php +++ b/tests/Feature/BackupAndRestoreTest.php @@ -227,4 +227,46 @@ class BackupAndRestoreTest extends TestCase $content = file_get_contents($fullPath); $this->assertStringStartsWith(DatabaseBackupService::KMH_MAGIC_HEADER, $content); } + + /** + * Memastikan data yang berada di tong sampah (soft-deleted) TIDAK bocor ke dalam file backup .kmh. + */ + public function test_backup_does_not_leak_trashed_records(): void + { + // 1. Buat user aktif dan user yang dihapus (masuk tong sampah) + $activeUser = User::factory()->create([ + 'username' => 'user_aktif_backup', + 'email' => 'aktif_backup@kmh.co.id', + ]); + + $trashedUser = User::factory()->create([ + 'username' => 'user_sampah_backup', + 'email' => 'sampah_backup@kmh.co.id', + ]); + $trashedUser->delete(); + + $this->assertSoftDeleted('users', ['id' => $trashedUser->id]); + + // 2. Buat backup khusus tabel users + $filename = $this->backupService->createKmhBackup(['users'], false, 'test_no_trash_leak.kmh'); + $fullPath = $this->backupService->getBackupDirectory() . DIRECTORY_SEPARATOR . $filename; + $this->assertTrue(File::exists($fullPath)); + + // 3. Dekripsi paket .kmh dan baca file database.sql di dalamnya + $kmhContent = file_get_contents($fullPath); + $zipContent = $this->backupService->decryptFromKmh($kmhContent); + + $tempZip = storage_path('app/temp_verify_leak_' . uniqid() . '.zip'); + file_put_contents($tempZip, $zipContent); + + $zip = new \ZipArchive(); + $this->assertTrue($zip->open($tempZip)); + $sqlContent = $zip->getFromName('database.sql'); + $zip->close(); + @unlink($tempZip); + + // 4. Pastikan data user aktif ada di file SQL, sedangkan data di tong sampah TIDAK ADA SAMA SEKALI + $this->assertStringContainsString('aktif_backup@kmh.co.id', $sqlContent); + $this->assertStringNotContainsString('sampah_backup@kmh.co.id', $sqlContent); + } }