check the declared cosigner count and reject a malformed wallet type when importing electrum multisig wallets
What changed, and why it matters
This commit tightens the import of Electrum-format multisig wallets in Sparrow Wallet. Previously, if a wallet file declared a policy like '2 of 3' but actually contained a different number of cosigner keys, Sparrow would silently accept it. The patch now rejects such mismatches and malformed wallet-type strings during import. The main risk is that a crafted wallet file could have caused the user to end up with a wallet whose effective multisig policy did not match what the file claimed, potentially leading to unexpected signing requirements or funds being sent to an address controlled by a different key set than the user intended.
Users who import Electrum multisig wallets should upgrade to a version containing this commit. Developers should review whether other wallet import formats perform similar policy/keystore consistency checks and consider adding equivalent validation. No immediate incident response is indicated beyond normal patching.
Security signals we found
Input validation added to wallet import parser
Mismatch between declared multisig policy and actual key material now rejected
Malformed 'wallet_type' string now rejected instead of being parsed with undefined behavior
Unit test added for negative-case malformed wallet import
Evidence from the diff
In Electrum.java, the importWallet() method parses the Electrum ‘wallet_type’ field (e.g., ‘2of3’) to set the wallet’s default multisig policy. Before this commit, it split the string on ‘of’, parsed the threshold from mOfn[0], and called Policy.getPolicy() using the number of loaded keystores as the cosigner count. It did not validate that the string split into exactly two parts, nor that the declared cosigner count matched the number of keystores present. The patch adds two checks: (1) mOfn.length must be 2, otherwise an ImportException is thrown; (2) the integer after ‘of’ must equal wallet.getKeystores().size(), otherwise an ImportException is thrown. A unit test and a test fixture with a ‘2of3’ wallet_type but four cosigner keys are added to verify the rejection.
Changed components
src/main/java/com/sparrowwallet/sparrow/io/Electrum.javaElectrum wallet import functionalityMultisig wallet policy derivationInspect captured patch +16 / −0
### src/main/java/com/sparrowwallet/sparrow/io/Electrum.java
@@ -208,7 +208,14 @@ public Wallet importWallet(InputStream inputStream, String password) throws Impo
} else if(ew.wallet_type.contains("of")) {
wallet.setPolicyType(PolicyType.MULTI_HD);
String[] mOfn = ew.wallet_type.split("of");
+ if(mOfn.length != 2) {
+ throw new ImportException("Could not determine the multisig policy from wallet type " + ew.wallet_type);
+ }
int threshold = Integer.parseInt(mOfn[0]);
+ int cosigners = Integer.parseInt(mOfn[1]);
+ if(cosigners != wallet.getKeystores().size()) {
+ throw new ImportException("This wallet specifies a policy of " + threshold + " of " + cosigners + ", but contains " + wallet.getKeystores().size() + " cosigner key" + (wallet.getKeystores().size() == 1 ? "" : "s"));
+ }
wallet.setDefaultPolicy(Policy.getPolicy(PolicyType.MULTI_HD, scriptType, wallet.getKeystores(), threshold));
} else {
throw new ImportException("Unknown Electrum wallet type of " + ew.wallet_type);
### src/test/java/com/sparrowwallet/sparrow/io/ElectrumTest.java
@@ -70,6 +70,13 @@ public void testMultisigImport() throws ImportException {
Assertions.assertTrue(wallet.isValid());
}
+ @Test
+ public void testMultisigImportExtraCosigner() {
+ Network.set(Network.TESTNET);
+ Electrum electrum = new Electrum();
+ Assertions.assertThrows(ImportException.class, () -> electrum.importWallet(getInputStream("electrum-multisig-wallet-extrakey.json"), null));
+ }
+
@Test
public void testMultisigExport() throws ImportException, ExportException, IOException {
Network.set(Network.TESTNET);
### src/test/resources/com/sparrowwallet/sparrow/io/electrum-multisig-wallet-extrakey.json
@@ -0,0 +1,2 @@
+{"x2/": {"xpub": "Upub5TeXciynXKx4VP1282QDUZ1NvxnBjEuTFVEcB8bRXxESQRqRKuJDqseZzj6bTeFZkMbYi4qo9rsQij79EJZUGywajDod8etFscpgaTSShLd", "hw_type": "coldcard", "ckcc_xfp": 3503269483, "label": "Coldcard 6BA6CFD0", "derivation": "m/48'/1'/0'/1'", "type": "hardware"}, "x3/": {"xpub": "Upub5SzPmFgatRMeLd6ADjvTiG9gnHoXXJy3qu8RNapLymh3GtHDeogzgy2nGtH1P7gPYF4zW9f4R8UYMCoUqUjSpSSZb8NWVKpbesbtP21e6Z4", "hw_type": "coldcard", "ckcc_xfp": 2389277556, "label": "Coldcard 747B698E", "derivation": "m/48'/1'/0'/1'", "type": "hardware"}, "x4/": {"xpub": "Upub5TkZyYLK71ZmBAHM5Gsju74bsoPgzuu4vo3oXo4hjyTNZcbLGpUcKXubdVsDHnLnyevYxdBjt4ZuKnxtVurm5sbEyvHdwpod2eGpubnG9yQ", "hw_type": "coldcard", "ckcc_xfp": 3190206587, "label": "Coldcard 7BB026BE", "derivation": "m/48'/1'/0'/1'", "type": "hardware"}, "x1/": {"xpub": "Upub5T4XUooQzDXL58NCHk8ZCw9BsRSLCtnyHeZEExAq1XdnBFXiXVrHFuvvmh3TnCR7XmKHxkwqdACv68z7QKT1vwru9L1SZSsw8B2fuBvtSa6", "hw_type": "coldcard", "ckcc_xfp": 1130956047, "label": "Coldcard 0F056943", "derivation": "m/48'/1'/0'/1'", "type": "hardware"}, "wallet_type": "2of3", "use_encryption": false, "seed_version": 17}
+Why this scored 56/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.