keep a symlinked wallet file in place when adding, changing or removing its password
What changed, and why it matters
This commit fixes a bug in Sparrow Wallet where changing a wallet's password would replace a symlinked wallet file with a regular file, breaking the symlink. The fix makes the password-change process follow the symlink and update the real wallet file instead, while leaving the symlink in place. The included test confirms the symlink survives password changes.
No immediate user action required; this is a defensive fix. Users who store wallet files as symlinks should upgrade to a release containing this commit to ensure password changes do not break their symlink setup. Reviewers should verify that toRealPath() correctly handles nested symlinks and that permissions/ownership of the replacement file remain appropriate.
Security signals we found
Fixes symlink-following behavior during sensitive file replacement
Prevents password-change operation from silently breaking symlinked wallet storage layouts
Uses atomic move with fallback to replace existing
Adds regression test for symlink preservation
Evidence from the diff
In DbPersistence.changeFileEncryption(), the code previously used walletFile.getAbsoluteFile().toPath() as the target for atomic replacement during password add/change/remove operations. If the wallet file was a symbolic link, this replaced the symlink with the newly encrypted file rather than updating the link’s target. The patch resolves the symlink via toRealPath(), creates the temporary encryption directory in the real target’s parent directory, and moves the encrypted file over the real target. A new unit test verifies that a symlinked wallet file remains a symlink after setPassword().
Changed components
src/main/java/com/sparrowwallet/sparrow/io/db/DbPersistence.javaWallet file encryption/password change flowSymbolic-link wallet file setupsInspect captured patch +34 / −3
### src/main/java/com/sparrowwallet/sparrow/io/db/DbPersistence.java
@@ -674,7 +674,9 @@ private void updatePassword(Storage storage, ECKey encryptionPubKey) throws Stor
//created alongside the wallet file so that the replacement is an atomic move, and so that the copy is not written to a shared temp location.
private void changeFileEncryption(File walletFile, String currentPassword, String newPassword) throws IOException, SQLException {
File dbFile = walletFile.getAbsoluteFile();
- Path encryptionDir = Files.createTempDirectory(dbFile.getParentFile().toPath(), "sparrowenc");
+ //A wallet file may be a symlink to a file stored elsewhere, so convert and replace the file it points to rather than the link
+ Path targetFile = dbFile.toPath().toRealPath();
+ Path encryptionDir = Files.createTempDirectory(targetFile.getParent(), "sparrowenc");
try {
Path encryptionFile = encryptionDir.resolve(dbFile.getName());
@@ -700,9 +702,9 @@ private void changeFileEncryption(File walletFile, String currentPassword, Strin
}
try {
- Files.move(encryptionFile, dbFile.toPath(), StandardCopyOption.ATOMIC_MOVE);
+ Files.move(encryptionFile, targetFile, StandardCopyOption.ATOMIC_MOVE);
} catch(AtomicMoveNotSupportedException e) {
- Files.move(encryptionFile, dbFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
+ Files.move(encryptionFile, targetFile, StandardCopyOption.REPLACE_EXISTING);
}
} finally {
File[] remainingFiles = encryptionDir.toFile().listFiles();
### src/test/java/com/sparrowwallet/sparrow/io/DbPersistenceTest.java
@@ -14,10 +14,12 @@
import com.sparrowwallet.drongo.wallet.WalletModel;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.File;
+import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Connection;
@@ -269,6 +271,33 @@ public void updateQueuedWithANewPasswordEncryptsTheFile() throws Exception {
Assertions.assertTrue(isWalletValid(storage.getWalletFile(), "pass"));
}
+ @Test
+ public void passwordChangeKeepsSymlinkedWalletInPlace() throws Exception {
+ Storage targetStorage = createUnencryptedWallet("Volume");
+ targetStorage.closeAndWait();
+ Path walletsDir = Files.createDirectory(tempDir.resolve("wallets"));
+ Path link;
+ try {
+ link = Files.createSymbolicLink(walletsDir.resolve("Savings." + PersistenceType.DB.getExtension()), targetStorage.getWalletFile().toPath());
+ } catch(UnsupportedOperationException | IOException e) {
+ //Windows only allows symbolic links to be created in Developer Mode or when elevated
+ throw Assumptions.<RuntimeException>abort("Symbolic links cannot be created: " + e.getMessage());
+ }
+
+ Storage storage = new Storage(PersistenceType.DB, link.toFile());
+ storage.setKeyDeriver(new Argon2KeyDeriver());
+ storage.loadUnencryptedWallet();
+ setPassword(storage, "pass");
+ storage.closeAndWait();
+
+ Assertions.assertTrue(Files.isSymbolicLink(link), "the symlink was replaced by the wallet file");
+ Assertions.assertTrue(isWalletValid(targetStorage.getWalletFile(), "pass"));
+ String[] walletsDirFiles = walletsDir.toFile().list();
+ Assertions.assertEquals(1, walletsDirFiles == null ? -1 : walletsDirFiles.length, "files were left in the wallets directory");
+ String[] tempFiles = tempDir.toFile().list((dir, name) -> name.equals("temp.db") || name.startsWith("sparrowenc"));
+ Assertions.assertEquals(0, tempFiles == null ? -1 : tempFiles.length, "temporary files were left alongside the wallet file");
+ }
+
@Test
public void passwordRemovalDecryptsWalletFile() throws Exception {
Storage storage = createUnencryptedWallet("Savings");Why this scored 34/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.