fix(backup): pastikan data tong sampah (soft deletes) tidak bocor ke arsip backup .kmh

This commit is contained in:
Ray1111-aa
2026-10-28 09:52:06 +07:00
parent 926683fd34
commit 539cdbce86
2 changed files with 51 additions and 2 deletions
+9 -2
View File
@@ -5,6 +5,7 @@ namespace App\Services;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Schema;
use PDO; use PDO;
use RuntimeException; use RuntimeException;
use ZipArchive; use ZipArchive;
@@ -571,9 +572,9 @@ class DatabaseBackupService
], ],
'system' => [ 'system' => [
'name' => 'Sistem & Internal Database', '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, [ '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' 'jobs', 'job_batches', 'failed_jobs', 'password_reset_tokens'
])), ])),
], ],
@@ -674,6 +675,12 @@ class DatabaseBackupService
fwrite($handle, "-- Dumping Data untuk `{$tableName}`\n"); fwrite($handle, "-- Dumping Data untuk `{$tableName}`\n");
$query = DB::table($tableName); $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(); $totalRows = $query->count();
if ($totalRows === 0) { if ($totalRows === 0) {
+42
View File
@@ -227,4 +227,46 @@ class BackupAndRestoreTest extends TestCase
$content = file_get_contents($fullPath); $content = file_get_contents($fullPath);
$this->assertStringStartsWith(DatabaseBackupService::KMH_MAGIC_HEADER, $content); $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' => '[email protected]',
]);
$trashedUser = User::factory()->create([
'username' => 'user_sampah_backup',
'email' => '[email protected]',
]);
$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('[email protected]', $sqlContent);
$this->assertStringNotContainsString('[email protected]', $sqlContent);
}
} }