only delete wallet backups after a password change or addition has saved, and hide the backup options for wallets outside the wallets folder
What changed, and why it matters
This commit tightens how Sparrow Wallet handles wallet backups. It makes sure old backups are only deleted after a password change or new password has actually been saved, and it hides backup-related options for wallets that live outside the default wallets folder. The change reduces the chance that a user accidentally deletes backups belonging to another wallet, or deletes backups before the new password is safely written.
Review whether any prior release allowed backup deletion before save completion or for out-of-folder wallets; if so, consider a security advisory and patch release. Otherwise treat as a defensive hardening change and include in release notes.
Security signals we found
Deletion of backup files now gated on successful wallet save
Backup options hidden for wallets outside the default wallets folder
Backup deletion restricted to wallets located in the wallets directory
Path comparison uses real/normalized paths to avoid symlink bypasses
Prevents potential cross-wallet backup deletion by name collision
Evidence from the diff
The patch changes backup deletion logic in SettingsController so that deleteBackups is passed as a flag through recursive saveWallet() calls and only executed after a successful save/refresh. It also introduces Storage.isInWalletsDir() (using real/normalized paths, including symlink support) and uses it to gate backupWallet(), deleteBackups(), and the UI warning text in AppController. WalletPasswordDialog now receives a backupSupported flag and only shows backupExisting/deleteBackups checkboxes when the wallet is inside the wallets directory.
Changed components
AppController wallet deletion warningWalletPasswordDialog backup UI controlsStorage backupWallet/deleteBackups/isInWalletsDirSettingsController saveWallet password-change flowInspect captured patch +30 / −19
### src/main/java/com/sparrowwallet/sparrow/AppController.java
@@ -2477,7 +2477,7 @@ private void renameWallet(WalletForm selectedWalletForm) {
}
private void deleteWallet(WalletForm selectedWalletForm) {
- Optional<ButtonType> optButtonType = AppServices.showWarningDialog("Delete " + selectedWalletForm.getWallet().getMasterName() + "?", "The wallet file and any backups will be deleted. Are you sure?", ButtonType.NO, ButtonType.YES);
+ Optional<ButtonType> optButtonType = AppServices.showWarningDialog("Delete " + selectedWalletForm.getWallet().getMasterName() + "?", "The wallet file" + (selectedWalletForm.getStorage().isInWalletsDir() ? " and any backups" : "") + " will be deleted. Are you sure?", ButtonType.NO, ButtonType.YES);
if(optButtonType.isPresent() && optButtonType.get() == ButtonType.YES) {
Storage storage = selectedWalletForm.getStorage();
if(selectedWalletForm.getMasterWallet().isEncrypted()) {
### src/main/java/com/sparrowwallet/sparrow/control/WalletPasswordDialog.java
@@ -25,10 +25,10 @@ public class WalletPasswordDialog extends Dialog<SecureString> {
private boolean addingPassword;
public WalletPasswordDialog(String walletName, PasswordRequirement requirement) {
- this(walletName, requirement, false);
+ this(walletName, requirement, false, false);
}
- public WalletPasswordDialog(String walletName, PasswordRequirement requirement, boolean suggestChangePassword) {
+ public WalletPasswordDialog(String walletName, PasswordRequirement requirement, boolean suggestChangePassword, boolean backupSupported) {
this.requirement = requirement;
this.password = new ViewPasswordField();
this.passwordConfirm = new ViewPasswordField();
@@ -63,7 +63,7 @@ public WalletPasswordDialog(String walletName, PasswordRequirement requirement,
changePassword.setSelected(suggestChangePassword);
}
- if(requirement == PasswordRequirement.UPDATE_EMPTY || requirement == PasswordRequirement.UPDATE_SET) {
+ if(backupSupported && (requirement == PasswordRequirement.UPDATE_EMPTY || requirement == PasswordRequirement.UPDATE_SET)) {
backupExisting.managedProperty().bind(backupExisting.visibleProperty());
deleteBackups.managedProperty().bind(deleteBackups.visibleProperty());
deleteBackups.visibleProperty().bind(backupExisting.visibleProperty().not());
### src/main/java/com/sparrowwallet/sparrow/io/Storage.java
@@ -229,8 +229,18 @@ private void checkWalletNetwork(Wallet wallet) {
}
}
+ public boolean isInWalletsDir() {
+ //Resolve the folder rather than the file, so that a wallet file symlinked into the wallets folder is treated as being in it
+ Path walletDir = walletFile.getAbsoluteFile().getParentFile().toPath();
+ try {
+ return walletDir.toRealPath().startsWith(getWalletsDir().toPath().toRealPath());
+ } catch(IOException e) {
+ return walletDir.normalize().startsWith(getWalletsDir().toPath().toAbsolutePath().normalize());
+ }
+ }
+
public void backupWallet() throws IOException {
- if(walletFile.toPath().startsWith(getWalletsDir().toPath())) {
+ if(isInWalletsDir()) {
backupWallet(null);
}
}
@@ -269,7 +279,10 @@ public boolean delete(boolean deleteBackups) {
}
public void deleteBackups() {
- deleteBackups(null);
+ //Backups are only written for wallets in the wallets folder, so any backups matching the name of a wallet elsewhere belong to another wallet
+ if(isInWalletsDir()) {
+ deleteBackups(null);
+ }
}
private void deleteBackups(String prefix) {
### src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java
@@ -272,7 +272,7 @@ public ScriptType fromString(String string) {
if(walletForm.getWallet().getPolicyType() == PolicyType.SINGLE_SP && walletForm.getWallet().getBirthDate() == null && walletForm.getStorage().getEncryptionPubKey() == null) {
walletForm.getWallet().setBirthDate(new Date());
}
- saveWallet(false, false);
+ saveWallet(false, false, false);
Wallet wallet = walletForm.getWallet();
if(wallet.getPolicyType() == PolicyType.MULTI_HD && wallet.getDefaultPolicy().getNumSignaturesRequired() < wallet.getKeystores().size() && addressChange) {
@@ -649,7 +649,7 @@ public void showAdvanced(ActionEvent event) {
if(optApply.isPresent() && optApply.get() && walletForm.getWallet().isValid()) {
revert.setDisable(true);
apply.setDisable(true);
- saveWallet(false, true);
+ saveWallet(false, true, false);
}
}
@@ -1026,7 +1026,7 @@ private boolean confirmImportedPolicy(Wallet importedWallet, Set<Integer> ownInd
}
//Returns true if the wallet save was initiated, and false if it was abandoned without any change to the wallet or its storage
- private boolean saveWallet(boolean changePassword, boolean suggestChangePassword) {
+ private boolean saveWallet(boolean changePassword, boolean suggestChangePassword, boolean deleteBackups) {
ECKey existingPubKey = walletForm.getStorage().getEncryptionPubKey();
WalletPasswordDialog.PasswordRequirement requirement;
@@ -1049,7 +1049,7 @@ private boolean saveWallet(boolean changePassword, boolean suggestChangePassword
}
}
- WalletPasswordDialog dlg = new WalletPasswordDialog(null, requirement, suggestChangePassword);
+ WalletPasswordDialog dlg = new WalletPasswordDialog(null, requirement, suggestChangePassword, walletForm.getStorage().isInWalletsDir());
dlg.initOwner(apply.getScene().getWindow());
Optional<SecureString> password = walletForm.isLocked() ? Optional.empty() : dlg.showAndWait();
if(password.isPresent() && walletForm.isLocked()) {
@@ -1074,6 +1074,9 @@ private boolean saveWallet(boolean changePassword, boolean suggestChangePassword
try {
walletForm.getStorage().setEncryptionPubKey(Storage.NO_PASSWORD_KEY);
walletForm.saveAndRefresh();
+ if(deleteBackups) {
+ walletForm.deleteBackups();
+ }
EventManager.get().post(new RequestOpenWalletsEvent());
} catch (IOException | StorageException e) {
log.error("Error saving wallet", e);
@@ -1103,10 +1106,6 @@ private boolean saveWallet(boolean changePassword, boolean suggestChangePassword
Wallet masterWallet = walletForm.getWallet().isMasterWallet() ? walletForm.getWallet() : walletForm.getWallet().getMasterWallet();
if(dlg.isChangePassword()) {
- if(dlg.isDeleteBackups()) {
- walletForm.deleteBackups();
- }
-
masterWallet.decrypt(key);
for(Wallet childWallet : masterWallet.getChildWallets()) {
if(!childWallet.isNested()) {
@@ -1115,7 +1114,7 @@ private boolean saveWallet(boolean changePassword, boolean suggestChangePassword
}
//If a new password is not provided, re-encrypt with the existing key rather than leaving the wallet decrypted for the session
- if(!saveWallet(true, false)) {
+ if(!saveWallet(true, false, dlg.isDeleteBackups())) {
masterWallet.encrypt(key);
for(Wallet childWallet : masterWallet.getChildWallets()) {
if(!childWallet.isNested()) {
@@ -1126,10 +1125,6 @@ private boolean saveWallet(boolean changePassword, boolean suggestChangePassword
return;
}
- if(dlg.isDeleteBackups()) {
- walletForm.deleteBackups();
- }
-
masterWallet.encrypt(key);
for(Wallet childWallet : masterWallet.getChildWallets()) {
if(!childWallet.isNested()) {
@@ -1138,6 +1133,9 @@ private boolean saveWallet(boolean changePassword, boolean suggestChangePassword
}
walletForm.getStorage().setEncryptionPubKey(encryptionPubKey);
walletForm.saveAndRefresh();
+ if(deleteBackups || dlg.isDeleteBackups()) {
+ walletForm.deleteBackups();
+ }
EventManager.get().post(new RequestOpenWalletsEvent());
} catch (Exception e) {
log.error("Error saving wallet", e);Why this scored 42/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.