delete backups made before a wallet was migrated from json when deleting a wallet or its backups
What changed, and why it matters
This commit fixes a cleanup bug in Sparrow Wallet. When a user deletes a wallet or its backups, the app now also finds and removes old backups that were created before the wallet was migrated from the older JSON format to the newer database format. Previously, those pre-migration backups could be left behind on disk after deletion, meaning a wallet the user thought was gone might still have recoverable files sitting in the backup folder.
Treat as a low-to-moderate privacy fix. Users who deleted migrated wallets before this patch should be advised that old JSON/no-extension backups may still exist in their backup directory and can be manually removed. No immediate incident response is required; review whether the deletion UI should notify users of all files being removed.
Security signals we found
data-retention bug: sensitive wallet backups left on disk after user-initiated deletion
backup cleanup bypass due to extension mismatch after wallet format migration
privacy/data-remnance issue rather than remote code execution or cryptographic flaw
Evidence from the diff
Storage.getBackups() previously matched backup files only by the wallet’s current file extension. After a JSON wallet is migrated to H2 (.mv.db), old backups still have .json or no-extension names, so they were not returned by getBackups() and therefore not deleted when the wallet or its backups were removed. The patch extends the regex to also include the legacy JSON extension and the no-extension variant, but only when the corresponding JSON wallet no longer exists alongside the DB wallet, preventing accidental inclusion of a separate current JSON wallet’s backups. Tests confirm inclusion of pre-migration backups and exclusion when a JSON wallet still exists.
Changed components
com.sparrowwallet.sparrow.io.Storage.getBackups(File, String)wallet deletion / backup deletion flows that rely on getBackups()migrated wallets: JSON -> H2 (.mv.db) persistence formatInspect captured patch +46 / −3
### src/main/java/com/sparrowwallet/sparrow/io/Storage.java
@@ -299,7 +299,18 @@ File[] getBackups(String prefix) {
File[] getBackups(File backupDir, String prefix) {
String walletName = persistence.getWalletName(walletFile, null);
String extension = walletFile.getName().substring(walletName.length());
- Pattern backupPattern = Pattern.compile(Pattern.quote((prefix == null ? "" : prefix + "_") + walletName + "-") + "[0-9]{14}" + Pattern.quote(extension));
+ List<String> extensions = new ArrayList<>(List.of(extension));
+ if(getType() == PersistenceType.DB) {
+ //Backups made before migration from JSON keep the JSON file name, unless that JSON wallet still exists alongside
+ for(String jsonExtension : List.of("." + PersistenceType.JSON.getExtension(), "")) {
+ if(!new File(walletFile.getParentFile(), walletName + jsonExtension).isFile()) {
+ extensions.add(jsonExtension);
+ }
+ }
+ }
+
+ String extensionPattern = extensions.stream().map(Pattern::quote).collect(Collectors.joining("|", "(?:", ")"));
+ Pattern backupPattern = Pattern.compile(Pattern.quote((prefix == null ? "" : prefix + "_") + walletName + "-") + "[0-9]{14}" + extensionPattern);
File[] backups = backupDir.listFiles((dir, name) -> backupPattern.matcher(name).matches());
backups = backups == null ? new File[0] : backups;
### src/test/java/com/sparrowwallet/sparrow/io/StorageTest.java
@@ -131,11 +131,43 @@ public void getBackupsRequiresAWholeDateAndAMatchingExtension() throws IOExcepti
File backupDir = createBackupDir("Savings-20250101120000.mv.db", "Savings-2025010112000.mv.db", "Savings-202501011200000.mv.db",
"Savings-20250101120000.json", "Savings-20250101120000", "Savings.mv.db", "Savings-notes.txt");
- assertBackups(backupDir, PersistenceType.DB, "Savings.mv.db", "Savings-20250101120000.mv.db");
+ assertBackups(backupDir, PersistenceType.DB, "Savings.mv.db", "Savings-20250101120000.mv.db", "Savings-20250101120000.json", "Savings-20250101120000");
assertBackups(backupDir, PersistenceType.JSON, "Savings.json", "Savings-20250101120000.json");
assertBackups(backupDir, PersistenceType.JSON, "Savings", "Savings-20250101120000");
}
+ @Test
+ public void getBackupsIncludesBackupsMadeBeforeMigration() throws Exception {
+ File walletsDir = Files.createDirectory(tempDir.resolve("wallets")).toFile();
+ File backupDir = createBackupDir("Migrated-20250101120000.mv.db", "Migrated-20210101120000.json", "Migrated-20200101120000",
+ "Migrated-20210101120000.txt", "Migrated-2021-20210101120000.json", "MigratedX-20210101120000.json");
+ File json = writeJsonWallet(new File(walletsDir, "Migrated.json"), watchOnly("Migrated", existingXpub));
+
+ Storage storage = new Storage(json);
+ try {
+ Assertions.assertArrayEquals(new String[] {"Migrated-20210101120000.json"}, Arrays.stream(storage.getBackups(backupDir, null)).map(File::getName).toArray(String[]::new));
+ storage.loadUnencryptedWallet();
+ Assertions.assertEquals(PersistenceType.DB, storage.getType());
+ Assertions.assertArrayEquals(new String[] {"Migrated-20250101120000.mv.db", "Migrated-20210101120000.json", "Migrated-20200101120000"},
+ Arrays.stream(storage.getBackups(backupDir, null)).map(File::getName).toArray(String[]::new));
+ } finally {
+ storage.closeAndWait();
+ }
+ }
+
+ @Test
+ public void getBackupsExcludesBackupsOfAnExistingJsonWallet() throws IOException {
+ File backupDir = createBackupDir("Separate-20250101120000.mv.db", "Separate-20210101120000.json", "Separate-20200101120000");
+
+ Assertions.assertTrue(new File(tempDir.toFile(), "Separate.json").createNewFile());
+ assertBackups(backupDir, PersistenceType.DB, "Separate.mv.db", "Separate-20250101120000.mv.db", "Separate-20200101120000");
+ assertBackups(backupDir, PersistenceType.JSON, "Separate.json", "Separate-20210101120000.json");
+
+ Assertions.assertTrue(new File(tempDir.toFile(), "Separate").createNewFile());
+ assertBackups(backupDir, PersistenceType.DB, "Separate.mv.db", "Separate-20250101120000.mv.db");
+ assertBackups(backupDir, PersistenceType.JSON, "Separate", "Separate-20200101120000");
+ }
+
@Test
public void getBackupsTreatsAWalletNameLiterally() throws IOException {
File backupDir = createBackupDir("SavingsXold-20250101120000.mv.db");
@@ -241,7 +273,7 @@ private File createBackupDir(String... backupNames) throws IOException {
}
private void assertBackups(File backupDir, PersistenceType persistenceType, String walletFileName, String... expectedBackupNames) {
- Storage storage = new Storage(persistenceType, new File(backupDir.getParentFile(), walletFileName));
+ Storage storage = new Storage(persistenceType, new File(tempDir.toFile(), walletFileName));
File[] backups = storage.getBackups(backupDir, null);
Assertions.assertArrayEquals(expectedBackupNames, Arrays.stream(backups).map(File::getName).toArray(String[]::new));
}Why this scored 47/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.