import a descriptor wrapped over several lines or followed by other text in a pdf, and refuse ec or duplicate keys in a scanned multisig
What changed, and why it matters
This commit fixes two separate issues in Sparrow Wallet. First, it improves importing of Bitcoin wallet descriptors from PDF files so that descriptors split across multiple lines, or followed by extra text, are read correctly. Second, it makes QR code scanning of multisig wallet setups safer by rejecting raw EC (single-key) keys and duplicate extended public keys, which could otherwise lead to malformed or insecure wallet configurations.
Review the drongo subproject update separately to confirm its relevance. Treat this commit as a hardening/fix patch and include it in the next release. Users importing descriptors from PDFs or scanning multisig QR codes should upgrade.
Security signals we found
Rejection of unsupported EC keys in scanned multisig descriptors
Rejection of duplicate extended public keys in multisig setup
Regex replacement now uses Matcher.quoteReplacement to prevent injection/malformation
Descriptor extraction from PDF now bounded to valid descriptor syntax
Subproject drongo updated (security relevance not verifiable from diff)
Evidence from the diff
The patch modifies descriptor parsing in Descriptor.java to concatenate lines when parentheses are unbalanced, allowing wrapped descriptors. PdfUtils.java now extracts only the descriptor text up to the matching closing parenthesis plus an optional checksum/suffix. QRScanDialog.java now throws IllegalArgumentException if a scanned multi-key contains EC keys or duplicate extended keys, and fixes a regex replacement bug by adding a negative lookahead and using Matcher.quoteReplacement to avoid issues with replacement strings containing special characters.
Changed components
PDF descriptor import (PdfUtils.java, Descriptor.java)QR code scanning dialog (QRScanDialog.java)drongo subprojectInspect captured patch +44 / −6
### drongo
@@ -1 +1 @@
-Subproject commit 90d9385133a42f31877e74c019d1f01ed002e9d9
+Subproject commit d81ff1a5c370e31dd8e205d6a4e3c9778c4111b3
### src/main/java/com/sparrowwallet/sparrow/control/QRScanDialog.java
@@ -612,12 +612,17 @@ private OutputDescriptor getOutputDescriptor(CryptoOutput cryptoOutput) {
if(cryptoOutput.getMultiKey() != null) {
MultiKey multiKey = cryptoOutput.getMultiKey();
+ if(multiKey.getEcKeys() != null && !multiKey.getEcKeys().isEmpty()) {
+ throw new IllegalArgumentException("EC keys are currently unsupported");
+ }
Map<ExtendedKey, KeyDerivation> extendedPublicKeys = new LinkedHashMap<>();
Map<ExtendedKey, String> extendedPublicKeyLabels = new LinkedHashMap<>();
for(CryptoHDKey cryptoHDKey : multiKey.getHdKeys()) {
ExtendedKey extendedKey = getExtendedKey(cryptoHDKey);
KeyDerivation keyDerivation = getKeyDerivation(cryptoHDKey.getOrigin());
- extendedPublicKeys.put(extendedKey, keyDerivation);
+ if(extendedPublicKeys.put(extendedKey, keyDerivation) != null) {
+ throw new IllegalArgumentException("Duplicate extended keys are not supported");
+ }
if(cryptoHDKey.getName() != null) {
extendedPublicKeyLabels.put(extendedKey, cryptoHDKey.getName());
}
@@ -707,10 +712,10 @@ private OutputDescriptor getOutputDescriptor(UROutputDescriptor urOutputDescript
KeyDerivation keyDerivation = getKeyDerivation(urhdKey.getOrigin());
if(urhdKey.getChainCode() == null) {
ECKey ecKey = getKey(urhdKey);
- source = source.replaceAll("@" + i, OutputDescriptor.writeKey(ecKey, keyDerivation, true, true));
+ source = source.replaceAll("@" + i + "(?!\\d)", Matcher.quoteReplacement(OutputDescriptor.writeKey(ecKey, keyDerivation, true, true)));
} else {
ExtendedKey extendedKey = getExtendedKey(urhdKey);
- source = source.replaceAll("@" + i, OutputDescriptor.writeKey(extendedKey, keyDerivation, null, true, true));
+ source = source.replaceAll("@" + i + "(?!\\d)", Matcher.quoteReplacement(OutputDescriptor.writeKey(extendedKey, keyDerivation, null, true, true)));
if(urhdKey.getName() != null) {
mapExtendedPublicKeyLabels.put(extendedKey, urhdKey.getName());
}
### src/main/java/com/sparrowwallet/sparrow/io/Descriptor.java
@@ -136,7 +136,12 @@ private static List<String> getParagraphs(InputStream inputStream) {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
for(String line : reader.lines().map(String::trim).toArray(String[]::new)) {
if(!line.isEmpty() && !line.startsWith("#")) {
- paragraphs.add(line.replaceFirst("^.+:", "").trim());
+ String last = paragraphs.isEmpty() ? null : paragraphs.getLast();
+ if(last != null && last.chars().filter(c -> c == '(').count() > last.chars().filter(c -> c == ')').count()) {
+ paragraphs.set(paragraphs.size() - 1, last + line);
+ } else {
+ paragraphs.add(line.replaceFirst("^.+:", "").trim());
+ }
}
}
### src/main/java/com/sparrowwallet/sparrow/io/PdfUtils.java
@@ -35,9 +35,12 @@
import java.io.*;
import java.util.*;
import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
public class PdfUtils {
private static final Logger log = LoggerFactory.getLogger(PdfUtils.class);
+ private static final Pattern DESCRIPTOR_SUFFIX_PATTERN = Pattern.compile("(?:\\?[a-zA-Z]+=[0-9]+(?:&[a-zA-Z]+=[0-9]+)*)?(?:#[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{8})?");
private static final int QR_WIDTH = 480;
private static final int QR_HEIGHT = 480;
@@ -89,6 +92,20 @@ public static void saveOutputDescriptor(String walletName, String outputDescript
}
}
+ private static String getDescriptorText(String text) {
+ int depth = 0;
+ for(int i = 0; i < text.length(); i++) {
+ if(text.charAt(i) == '(') {
+ depth++;
+ } else if(text.charAt(i) == ')' && --depth == 0) {
+ Matcher suffixMatcher = DESCRIPTOR_SUFFIX_PATTERN.matcher(text).region(i + 1, text.length());
+ return text.substring(0, suffixMatcher.lookingAt() ? suffixMatcher.end() : i + 1);
+ }
+ }
+
+ return text;
+ }
+
public static OutputDescriptor getOutputDescriptor(InputStream inputStream) throws IOException {
try {
PdfReader pdfReader = new PdfReader(inputStream);
@@ -110,7 +127,7 @@ public static OutputDescriptor getOutputDescriptor(InputStream inputStream) thro
}
if(descriptor != null) {
- return OutputDescriptor.getOutputDescriptor(descriptor);
+ return OutputDescriptor.getOutputDescriptor(getDescriptorText(descriptor));
}
} catch(Exception e) {
throw new IllegalArgumentException("Not a valid PDF or output descriptor");
### src/test/java/com/sparrowwallet/sparrow/io/DescriptorTest.java
@@ -108,4 +108,15 @@ public void testImportSeparateDescriptorsNoBlankLine() throws ImportException {
Assertions.assertEquals(ExtendedKey.fromDescriptor("xpub6DM7CYgaTMdMbhTcLTUWmNUE5WLXK5hx8ZMa4sRw8qYJPqtqKYiKnwsmT8A6AijDVAUZRivdBnXdR8QE7Y9vVnqvzPL3fXCmu1WtCRLdAoz"), keystore.getExtendedPublicKey());
Assertions.assertTrue(keystore.isValid());
}
+
+ @Test
+ public void testImportWrappedMultisigDescriptor() throws ImportException {
+ String text = "wsh(sortedmulti(2,[04fefef0/48h/0h/0h/2h]xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB/0/*,\n"
+ + "[04ba1ef0/48h/0h/0h/2h]xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH/0/*,\n"
+ + "[04fefef1/48h/0h/0h/2h]xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL/0/*))\n";
+ Wallet wallet = new Descriptor().importWallet(new ByteArrayInputStream(text.getBytes()), null);
+
+ Assertions.assertEquals(3, wallet.getKeystores().size());
+ Assertions.assertEquals(2, wallet.getDefaultPolicy().getNumSignaturesRequired());
+ }
}Why this scored 46/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.