open the wallet file that was validated, rejecting a wallet file with an empty name
What changed, and why it matters
This commit fixes a security bug in how Sparrow Wallet opens its H2 database files. Previously, the wallet validated one file but then opened a different file for the database connection, and it allowed wallet files with an empty name. A specially crafted wallet file with an empty name could trick the app into connecting to an attacker-controlled external H2 database, potentially leaking or manipulating wallet data. The patch makes sure the validated file is the one actually opened and rejects empty-named wallet files.
Treat this as a security fix and include it in the next release. Users should upgrade to a version containing this commit and avoid opening untrusted wallet files. Review the drongo submodule change for related security fixes and consider requesting a CVE from the vendor or a security researcher.
Security signals we found
Validation/opening mismatch between validateStore() and getUrl()
Empty wallet file name allowed H2 database name to resolve to parent directory
Potential H2 linked table / INIT JDBC URL injection via crafted wallet file
Regression test demonstrates arbitrary external CSV write was possible before fix
Subproject drongo updated, suggesting related library-level fix
Evidence from the diff
In DbPersistence.java, createDataSource() previously called validateStore(walletFile, password) but then passed walletFile directly to getUrl(), which internally stripped the file extension to compute the H2 database name. This created a validation/opening mismatch. Additionally, getWalletName() could return an empty string for a file named ‘.mv.db’, producing a database name that resolved to the parent directory and allowing an attacker-supplied JDBC URL to be injected via H2’s linked table feature. The patch introduces getDbName() to compute and validate the database name before validation, rejects empty wallet names, and passes the already-derived dbName to both validateStore() and getUrl(). A new regression test demonstrates that a file named ‘.mv.db’ can no longer trigger external CSV write via a forced linked table.
Changed components
src/main/java/com/sparrowwallet/sparrow/io/db/DbPersistence.javadrongo subprojectH2 database persistence layerWallet file loading/validation pathInspect captured patch +44 / −5
### drongo
@@ -1 +1 @@
-Subproject commit f5a0b36b1ff953cf40b32d5fc2a8083ae69fec3f
+Subproject commit 90d9385133a42f31877e74c019d1f01ed002e9d9
### src/main/java/com/sparrowwallet/sparrow/io/db/DbPersistence.java
@@ -1044,10 +1044,11 @@ private HikariDataSource getDataSource(Storage storage, String password) throws
private HikariDataSource createDataSource(File walletFile, String password) throws StorageException {
try {
- validateStore(walletFile, password);
+ File dbName = getDbName(walletFile);
+ validateStore(new File(dbName.getPath() + "." + getType().getExtension()), password);
Class.forName("org.h2.Driver");
HikariConfig config = new HikariConfig();
- config.setJdbcUrl(getUrl(walletFile, password));
+ config.setJdbcUrl(getUrl(dbName, password));
config.setUsername(H2_USER);
config.setPassword(password == null ? H2_PASSWORD : password + " " + H2_PASSWORD);
return new HikariDataSource(config);
@@ -1067,15 +1068,23 @@ private HikariDataSource createDataSource(File walletFile, String password) thro
}
}
- private String getUrl(File walletFile, String password) throws StorageException {
+ private File getDbName(File walletFile) throws StorageException {
File dbFile = walletFile.getAbsoluteFile();
if(JDBC_URL_INJECTION_PATTERN.matcher(dbFile.getPath()).find()) {
throw new StorageException("Wallet file path contains invalid characters");
}
//H2 appends the extension to the database name in the URL, so only a trailing extension can be removed - removing every occurrence would
//open a different file to the one tracked here, as would removing one from a directory name
- File dbName = new File(dbFile.getParentFile(), getWalletName(dbFile, null));
+ String walletName = getWalletName(dbFile, null);
+ if(walletName.isEmpty()) {
+ throw new StorageException("Invalid wallet file name " + dbFile.getName());
+ }
+
+ return new File(dbFile.getParentFile(), walletName);
+ }
+
+ private String getUrl(File dbName, String password) {
return "jdbc:h2:" + dbName.getPath() + ";INIT=SET TRACE_LEVEL_FILE=4;TRACE_LEVEL_FILE=4;DEFRAG_ALWAYS=true;MAX_COMPACT_TIME=5000;DATABASE_TO_UPPER=false" + (password == null ? "" : ";CIPHER=AES");
}
### src/test/java/com/sparrowwallet/sparrow/io/DbPersistenceTest.java
@@ -264,6 +264,36 @@ public void walletNameContainingExtensionUsesItsOwnFile() throws Exception {
Assertions.assertEquals(otherHash, getFileHash(otherFile), "another wallet file was written by a wallet name containing the extension");
}
+ @Test
+ public void newWalletFileWithoutExtensionSaves() throws Exception {
+ Storage storage = new Storage(tempDir.resolve("newwallet").toFile());
+ storage.setKeyDeriver(new Argon2KeyDeriver());
+ storage.setEncryptionPubKey(Storage.NO_PASSWORD_KEY);
+ storage.saveWallet(createWallet("newwallet"));
+ storage.closeAndWait();
+
+ File walletFile = tempDir.resolve("newwallet." + PersistenceType.DB.getExtension()).toFile();
+ Assertions.assertTrue(walletFile.exists());
+ Assertions.assertTrue(isWalletValid(walletFile, null));
+ }
+
+ @Test
+ public void walletFileWithEmptyNameRejected() throws Exception {
+ File dir = tempDir.resolve("wallet").toFile();
+ Assertions.assertTrue(dir.mkdir());
+ File emptyNameFile = new File(dir, "." + PersistenceType.DB.getExtension());
+ File cleanFile = buildWalletFile();
+ Files.move(cleanFile.toPath(), emptyNameFile.toPath());
+
+ File marker = tempDir.resolve("output.csv").toFile();
+ String target = "jdbc:h2:" + tempDir.resolve("external") + ";INIT=CREATE TABLE IF NOT EXISTS PUB(ID INT)\\;CALL CSVWRITE('" + marker.getAbsolutePath().replace('\\', '/') + "','SELECT 1')";
+ buildWalletFile("create force linked table wallet_master.remote('','" + target.replace("'", "''") + "','sa','','PUB')");
+ marker.delete();
+
+ assertRejected(emptyNameFile);
+ Assertions.assertFalse(marker.exists());
+ }
+
/**
* An update is queued with the key the storage held at the time, and can still be waiting when the wallet is saved with a password added. Run
* after that save, it must leave the file as the save left it.Why this scored 70/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.