leave qr codes that zbar can only return as transcoded text to the other scanners
What changed, and why it matters
This commit fixes a bug in how Sparrow Wallet reads QR codes using the ZBar scanner. ZBar converts binary QR code data into text and silently stops at the first null byte, which can corrupt non-text data. The fix makes Sparrow only trust ZBar's result when the content is plain printable text; otherwise it lets another scanner try. This could prevent wallet data from being misread when scanning QR codes containing binary or non-ASCII data.
Review whether other scanners in the fallback chain correctly handle binary QR payloads, and verify that the printable-ASCII guard covers all QR data types used by Sparrow (e.g., crypto-PSBT URIs, animated QR fragments). Consider adding integration tests with binary/non-ASCII QR codes.
Security signals we found
Data integrity issue in QR code parsing
Reliance on lossy text conversion for binary data
Silent truncation at NUL byte
Fallback to alternative scanners for non-printable content
No explicit security framing in commit message
Evidence from the diff
ZBar’s Java wrapper returns QR content as a Java String, which is a lossy representation for binary payloads: it transcodes bytes and truncates at NUL. The previous code accepted that string and reconstructed raw bytes via getRawBytes(), potentially producing incorrect data for binary or extended-ASCII QR codes. The patch adds isPrintableAscii() and only returns a ZBar scan when every character is printable ASCII (or CR/LF/TAB). Non-printable or non-ASCII content is left to fallback scanners. A unit test validates the printable-ASCII check.
Changed components
src/main/java/com/sparrowwallet/sparrow/io/ZBar.javaQR code scanning pipelineZBar scanner integrationInspect captured patch +25 / −1
### src/main/java/com/sparrowwallet/sparrow/io/ZBar.java
@@ -59,7 +59,11 @@ public static Scan scan(BufferedImage bufferedImage) {
int result = scanner.scanImage(image);
if(result != 0) {
String symbolData = image.getFirstSymbol().getData();
- return new Scan(getRawBytes(symbolData), symbolData);
+ //ZBar returns the data transcoded to text and cut at the first NUL, so the bytes of binary data cannot be recovered from it.
+ //Only printable ASCII is certain to match its bytes, and anything else is left to the other scanners
+ if(isPrintableAscii(symbolData)) {
+ return new Scan(getRawBytes(symbolData), symbolData);
+ }
}
}
}
@@ -103,6 +107,10 @@ private static byte[] convertToY800(BufferedImage image) {
return outputData;
}
+ static boolean isPrintableAscii(String str) {
+ return str != null && !str.isEmpty() && str.chars().allMatch(c -> (c >= 0x20 && c < 0x7F) || c == '\t' || c == '\n' || c == '\r');
+ }
+
private static byte[] getRawBytes(String str) {
char[] chars = str.toCharArray();
byte[] bytes = new byte[chars.length];
### src/test/java/com/sparrowwallet/sparrow/io/ZBarTest.java
@@ -0,0 +1,16 @@
+package com.sparrowwallet.sparrow.io;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class ZBarTest {
+ @Test
+ public void onlyPrintableAsciiIsTakenAsTheRawData() {
+ Assertions.assertTrue(ZBar.isPrintableAscii("UR:CRYPTO-PSBT/HDCXLKAHSSQZWFVSLOFZOXWKRE"));
+ Assertions.assertTrue(ZBar.isPrintableAscii("bitcoin:bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq?amount=0.01\n"));
+
+ Assertions.assertFalse(ZBar.isPrintableAscii(""));
+ Assertions.assertFalse(ZBar.isPrintableAscii("\u0001ç\u0094\u001b"));
+ Assertions.assertFalse(ZBar.isPrintableAscii("café"));
+ }
+}Why this scored 43/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.