parse a multisig setup file line by line on newlines only, with case-insensitive labels, a strict policy value and no repeated policy or format
What changed, and why it matters
This commit tightens how Sparrow reads Coldcard multisig setup files. It now splits files only on newline characters, rejects hidden control characters, makes labels case-insensitive, enforces a strict 'N of M' policy format, and refuses files that repeat the policy or format lines. These changes reduce the chance that a maliciously crafted file could trick Sparrow into importing the wrong wallet configuration, but the commit itself is a hardening fix rather than a confirmed exploit patch.
Treat as a defensive security improvement. Review whether other file importers in Sparrow use similar permissive parsing and apply comparable hardening. No immediate incident response is indicated unless a specific advisory is published.
Security signals we found
Input parsing hardening for wallet import
Rejection of control characters in imported files
Strict regex validation of multisig policy string
Prevention of repeated Policy/Format declarations
Case-insensitive label handling to avoid bypasses
Evidence from the diff
The ColdcardMultisig importer was rewritten to parse the setup file manually instead of using Guava’s CharStreams.readLines, which splits on any line terminator. The new parser splits only on ‘\n’, strips a trailing ‘\r’, rejects control characters other than tab, parses key:value pairs with a single colon separator, lower-cases keys, validates the Policy value against a regex ‘([0-9]+)\s+of\s+([0-9]+)’, and throws if Policy or Format appears more than once. Tests were added for case-insensitive policy, CRLF line endings, duplicate/repeated policy, malformed policy, non-ASCII digits, and duplicate format.
Changed components
ColdcardMultisig wallet import parserSparrow Wallet Coldcard multisig setup file importInspect captured patch +70 / −41
### src/main/java/com/sparrowwallet/sparrow/io/ColdcardMultisig.java
@@ -1,6 +1,5 @@
package com.sparrowwallet.sparrow.io;
-import com.google.common.io.CharStreams;
import com.sparrowwallet.drongo.ExtendedKey;
import com.sparrowwallet.drongo.KeyDerivation;
import com.sparrowwallet.drongo.Utils;
@@ -14,12 +13,14 @@
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
-import java.util.List;
import java.util.Locale;
import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
public class ColdcardMultisig implements WalletImport, KeystoreFileImport, WalletExport {
private static final Logger log = LoggerFactory.getLogger(ColdcardMultisig.class);
+ private static final Pattern POLICY_PATTERN = Pattern.compile("([0-9]+)\\s+of\\s+([0-9]+)", Pattern.CASE_INSENSITIVE);
@Override
public String getName() {
@@ -135,52 +136,62 @@ public Wallet importWallet(InputStream inputStream, String password) throws Impo
String derivation = null;
try {
- List<String> lines = CharStreams.readLines(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
- for (String line : lines) {
+ String text = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
+ boolean policyFound = false;
+ boolean formatFound = false;
+ for(String line : text.split("\n")) {
+ if(line.endsWith("\r")) {
+ line = line.substring(0, line.length() - 1);
+ }
+ if(line.chars().anyMatch(c -> (c < 0x20 && c != '\t') || c == 0x7f)) {
+ throw new IllegalStateException("This file contains unexpected control characters");
+ }
+
line = line.trim();
- if (line.isEmpty()) {
+ int separator = line.indexOf(':');
+ if(line.isEmpty() || separator < 0) {
continue;
}
- String[] keyValue = line.split(":");
- if (keyValue.length == 2) {
- String key = keyValue[0].trim();
- String value = keyValue[1].trim();
-
- switch (key) {
- case "Name":
- wallet.setName(value.trim());
- break;
- case "Policy":
- String[] policy = value.split("\\s+");
- if(policy.length != 3) {
- throw new IllegalStateException("Could not determine the multisig policy from \"" + line + "\"");
- }
- threshold = Integer.parseInt(policy[0]);
- cosigners = Integer.parseInt(policy[2]);
- break;
- case "Derivation":
- case "# derivation":
- derivation = value;
- break;
- case "Format":
- scriptType = ScriptType.valueOf(value.replace("P2WSH-P2SH", "P2SH_P2WSH"));
- break;
- default:
- if (key.length() == 8 && Utils.isHex(key)) {
- Keystore keystore = new Keystore("Coldcard");
- keystore.setSource(KeystoreSource.HW_AIRGAPPED);
- keystore.setWalletModel(WalletModel.COLDCARD);
- keystore.setKeyDerivation(new KeyDerivation(key, derivation, true));
- keystore.setExtendedPublicKey(ExtendedKey.fromDescriptor(value));
- wallet.makeLabelsUnique(keystore);
- wallet.getKeystores().add(keystore);
- }
- }
+ String key = line.substring(0, separator).trim();
+ String value = line.substring(separator + 1).trim();
+ switch(key.toLowerCase(Locale.ROOT)) {
+ case "name":
+ wallet.setName(value);
+ break;
+ case "policy":
+ Matcher policyMatcher = POLICY_PATTERN.matcher(value);
+ if(policyFound || !policyMatcher.matches()) {
+ throw new IllegalStateException("Could not determine the multisig policy from \"" + line + "\"");
+ }
+ policyFound = true;
+ threshold = Integer.parseInt(policyMatcher.group(1));
+ cosigners = Integer.parseInt(policyMatcher.group(2));
+ break;
+ case "derivation":
+ case "# derivation":
+ derivation = value;
+ break;
+ case "format":
+ if(formatFound) {
+ throw new IllegalStateException("This file specifies more than one script format");
+ }
+ formatFound = true;
+ scriptType = ScriptType.valueOf(value.toUpperCase(Locale.ROOT).replace("P2WSH-P2SH", "P2SH_P2WSH"));
+ break;
+ default:
+ if(key.length() == 8 && Utils.isHex(key)) {
+ Keystore keystore = new Keystore("Coldcard");
+ keystore.setSource(KeystoreSource.HW_AIRGAPPED);
+ keystore.setWalletModel(WalletModel.COLDCARD);
+ keystore.setKeyDerivation(new KeyDerivation(key, derivation, true));
+ keystore.setExtendedPublicKey(ExtendedKey.fromDescriptor(value));
+ wallet.makeLabelsUnique(keystore);
+ wallet.getKeystores().add(keystore);
+ }
}
}
-
if(threshold == 0) {
throw new IllegalStateException("This file does not specify the multisig policy");
}
### src/test/java/com/sparrowwallet/sparrow/io/ColdcardMultisigTest.java
@@ -12,6 +12,7 @@
import org.junit.jupiter.api.Test;
import java.io.*;
+import java.nio.charset.StandardCharsets;
import java.util.Locale;
public class ColdcardMultisigTest extends IoTest {
@@ -77,6 +78,23 @@ public void importWallet1() throws ImportException {
Assertions.assertTrue(wallet.isValid());
}
+ private Wallet importEdited(String find, String replace) throws Exception {
+ String original = new String(ByteStreams.toByteArray(getInputStream("cc-multisig-export-1.txt")), StandardCharsets.UTF_8);
+ Assertions.assertTrue(original.contains(find));
+ return new ColdcardMultisig().importWallet(new ByteArrayInputStream(original.replace(find, replace).getBytes(StandardCharsets.UTF_8)), null);
+ }
+
+ @Test
+ public void importWalletPolicyVariants() throws Exception {
+ Assertions.assertEquals(2, importEdited("Policy: 2 of 4", "policy: 2 of 4").getDefaultPolicy().getNumSignaturesRequired());
+ Assertions.assertEquals(2, importEdited("\n", "\r\n").getDefaultPolicy().getNumSignaturesRequired());
+ Assertions.assertThrows(ImportException.class, () -> importEdited("Policy: 2 of 4", "Policy: 1 of 4\npolicy: 2 of 4"));
+ Assertions.assertThrows(ImportException.class, () -> importEdited("Policy: 2 of 4", "Policy: 2 of 4\n# note\rPolicy: 1 of 4"));
+ Assertions.assertThrows(ImportException.class, () -> importEdited("Policy: 2 of 4", "Policy: 1 2of 4"));
+ Assertions.assertThrows(ImportException.class, () -> importEdited("Policy: 2 of 4", "Policy: \uFF11 of 4"));
+ Assertions.assertThrows(ImportException.class, () -> importEdited("Format: P2WSH", "Format: P2WSH\nFormat: P2SH"));
+ }
+
@Test
public void importWallet2() throws ImportException {
Network.set(Network.TESTNET);Why this scored 40/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.