truncate address, purpose node and keystore labels when updating a wallet, as a full save does
What changed, and why it matters
This commit fixes a bug where Sparrow Wallet's database update path failed to shorten (truncate) long labels for wallet addresses, purpose nodes, and keystores before saving them. The full save path already did this truncation, but the incremental update path did not. If a user set an unusually long label, the database update could fail or behave inconsistently, potentially blocking later wallet updates. The fix applies the same truncation routine during updates, and a new test confirms that a 300-character address label is safely reduced to 255 characters and does not prevent a second label from being saved.
No immediate user action required; ensure you are on a version containing this commit. Developers should verify that all other database write paths that accept user-controlled strings apply equivalent truncation or validation.
Security signals we found
Data-integrity bug: inconsistent truncation between full save and incremental update
Potential denial-of-service against local wallet state via overly long labels
Regression test added to prevent future recurrence
Evidence from the diff
In DbPersistence.update(), the code previously inserted wallet nodes and updated keystore labels using raw label strings. The full save path already calls walletNodeDao.truncate() / keystoreDao.truncate() to fit database column limits. This change applies the same truncate() calls in the update() path for purposeNode labels, addressNode labels, and keystore labels. A regression test (longAddressLabelDoesNotBlockLaterUpdates) creates a wallet, adds a 300-character label, triggers an update, then adds a second label and verifies both are persisted (the first truncated to 255 chars, the second intact).
Changed components
src/main/java/com/sparrowwallet/sparrow/io/db/DbPersistence.javasrc/test/java/com/sparrowwallet/sparrow/io/DbPersistenceTest.javaInspect captured patch +50 / −5
### src/main/java/com/sparrowwallet/sparrow/io/db/DbPersistence.java
@@ -353,11 +353,13 @@ private void update(Storage storage, Wallet wallet, String password) throws Stor
if(addressNode.getId() == null) {
WalletNode purposeNode = wallet.getNode(addressNode.getKeyPurpose());
if(purposeNode.getId() == null) {
- long purposeNodeId = walletNodeDao.insertWalletNode(purposeNode.getDerivationPath(), purposeNode.getLabel(), wallet.getId(), null, null, null);
+ long purposeNodeId = walletNodeDao.insertWalletNode(purposeNode.getDerivationPath(), walletNodeDao.truncate(purposeNode.getLabel()),
+ wallet.getId(), null, null, null);
purposeNode.setId(purposeNodeId);
}
- long nodeId = walletNodeDao.insertWalletNode(addressNode.getDerivationPath(), addressNode.getLabel(), wallet.getId(), purposeNode.getId(), addressNode.getAddressData(), addressNode.getSilentPaymentTweak());
+ long nodeId = walletNodeDao.insertWalletNode(addressNode.getDerivationPath(), walletNodeDao.truncate(addressNode.getLabel()), wallet.getId(),
+ purposeNode.getId(), addressNode.getAddressData(), addressNode.getSilentPaymentTweak());
addressNode.setId(nodeId);
} else if(addressNode.getAddress() != null) {
walletNodeDao.updateNodeAddressData(addressNode.getId(), addressNode.getAddressData());
@@ -412,11 +414,13 @@ private void update(Storage storage, Wallet wallet, String password) throws Stor
if(addressNode.getId() == null) {
WalletNode purposeNode = wallet.getNode(addressNode.getKeyPurpose());
if(purposeNode.getId() == null) {
- long purposeNodeId = walletNodeDao.insertWalletNode(purposeNode.getDerivationPath(), purposeNode.getLabel(), wallet.getId(), null, null, null);
+ long purposeNodeId = walletNodeDao.insertWalletNode(purposeNode.getDerivationPath(), walletNodeDao.truncate(purposeNode.getLabel()),
+ wallet.getId(), null, null, null);
purposeNode.setId(purposeNodeId);
}
- long nodeId = walletNodeDao.insertWalletNode(addressNode.getDerivationPath(), addressNode.getLabel(), wallet.getId(), purposeNode.getId(), addressNode.getAddressData(), addressNode.getSilentPaymentTweak());
+ long nodeId = walletNodeDao.insertWalletNode(addressNode.getDerivationPath(), walletNodeDao.truncate(addressNode.getLabel()), wallet.getId(),
+ purposeNode.getId(), addressNode.getAddressData(), addressNode.getSilentPaymentTweak());
addressNode.setId(nodeId);
} else if(addressNode.getAddress() != null) {
walletNodeDao.updateNodeAddressData(addressNode.getId(), addressNode.getAddressData());
@@ -472,7 +476,7 @@ private void update(Storage storage, Wallet wallet, String password) throws Stor
if(!dirtyPersistables.labelKeystores.isEmpty()) {
KeystoreDao keystoreDao = handle.attach(KeystoreDao.class);
for(Keystore keystore : dirtyPersistables.labelKeystores) {
- keystoreDao.updateLabel(keystore.getLabel(), keystore.getId());
+ keystoreDao.updateLabel(keystoreDao.truncate(keystore.getLabel()), keystore.getId());
}
}
### src/test/java/com/sparrowwallet/sparrow/io/DbPersistenceTest.java
@@ -3,6 +3,7 @@
import com.google.common.eventbus.Subscribe;
import com.sparrowwallet.drongo.ExtendedKey;
import com.sparrowwallet.drongo.KeyDerivation;
+import com.sparrowwallet.drongo.KeyPurpose;
import com.sparrowwallet.drongo.crypto.Argon2KeyDeriver;
import com.sparrowwallet.drongo.crypto.ECKey;
import com.sparrowwallet.drongo.policy.Policy;
@@ -14,9 +15,13 @@
import com.sparrowwallet.drongo.wallet.StandardAccount;
import com.sparrowwallet.drongo.wallet.Wallet;
import com.sparrowwallet.drongo.wallet.WalletModel;
+import com.sparrowwallet.drongo.wallet.WalletNode;
import com.sparrowwallet.sparrow.EventManager;
import com.sparrowwallet.sparrow.event.WalletBlockHeightChangedEvent;
+import com.sparrowwallet.sparrow.event.WalletEntryLabelsChangedEvent;
import com.sparrowwallet.sparrow.event.WalletPasswordChangeFailedEvent;
+import com.sparrowwallet.sparrow.io.db.DbPersistence;
+import com.sparrowwallet.sparrow.wallet.NodeEntry;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
@@ -35,9 +40,11 @@
import java.sql.Statement;
import java.util.Comparator;
import java.util.List;
+import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Collectors;
import java.util.stream.Stream;
public class DbPersistenceTest {
@@ -644,6 +651,40 @@ public void passwordChangeKeepsSymlinkedWalletInPlace() throws Exception {
Assertions.assertEquals(0, tempFiles == null ? -1 : tempFiles.length, "temporary files were left alongside the wallet file");
}
+ @Test
+ public void longAddressLabelDoesNotBlockLaterUpdates() throws Exception {
+ Persistence persistence = PersistenceType.DB.getInstance();
+ Storage storage = new Storage(persistence, tempDir.resolve("Labels." + PersistenceType.DB.getExtension()).toFile());
+ storage.setKeyDeriver(new Argon2KeyDeriver());
+ storage.setEncryptionPubKey(Storage.NO_PASSWORD_KEY);
+ Wallet wallet = createWallet("Labels");
+ storage.saveWallet(wallet);
+
+ WalletNode firstNode = new WalletNode(wallet, KeyPurpose.RECEIVE, 0);
+ firstNode.setLabel("a".repeat(300));
+ wallet.getNode(KeyPurpose.RECEIVE).getChildren().add(firstNode);
+ ((DbPersistence)persistence).walletEntryLabelsChanged(new WalletEntryLabelsChangedEvent(wallet, new NodeEntry(wallet, firstNode, false)));
+ persistence.updateWallet(storage, wallet);
+
+ WalletNode secondNode = new WalletNode(wallet, KeyPurpose.RECEIVE, 1);
+ secondNode.setLabel("Second");
+ wallet.getNode(KeyPurpose.RECEIVE).getChildren().add(secondNode);
+ ((DbPersistence)persistence).walletEntryLabelsChanged(new WalletEntryLabelsChangedEvent(wallet, new NodeEntry(wallet, secondNode, false)));
+ persistence.updateWallet(storage, wallet);
+ storage.closeAndWait();
+
+ Storage reopened = new Storage(PersistenceType.DB, storage.getWalletFile());
+ try {
+ Wallet loaded = reopened.loadUnencryptedWallet().getWallet();
+ Map<Integer, String> labels = loaded.getNode(KeyPurpose.RECEIVE).getChildren().stream().filter(node -> node.getLabel() != null)
+ .collect(Collectors.toMap(WalletNode::getIndex, WalletNode::getLabel));
+ Assertions.assertEquals("a".repeat(255), labels.get(0));
+ Assertions.assertEquals("Second", labels.get(1));
+ } finally {
+ reopened.closeAndWait();
+ }
+ }
+
@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.