camera: return UNKNOWN for truncated FIDO CBOR instead of throwing
- Commit
cb615083c429615f425e59420c8adab1cf69f522- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-19 01:53 UTC (17 days ago)
- Parents
d5066c8
CborCursor.byte() threw IndexOutOfBoundsException past the end of the decoded bytes. argument() calls it for a long-form (minor 24) argument without first checking a following byte exists, so a scanned FIDO QR whose digit payload decodes to a lone 0x18 (or a map header followed by a truncated long-form key) threw out of fidoRequestType(). Throw path: PhotoAnalyzer scans a crafted QR such as FIDO:/024, CameraSettingsColumn shows FidoQrCard for any FIDO: prefix, and FidoQrCard calls fidoRequestType(text) during composition to pick its label. The exception escaped composition and crashed the app. Fix: byte() now returns null past the end (matching the cursor's other accessors, which already return null on truncation), and argument()/uInt() propagate that null. Malformed payloads degrade to FidoRequestType.UNKNOWN as the existing truncated-payload tests expect. Verification: ./gradlew :camera:testDevUnitTest and ./check camera (run after this commit; see session report).
Diff
camera/src/main/java/com/vayunmathur/camera/domain/QrClassification.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/domain/QrClassification.kt b/camera/src/main/java/com/vayunmathur/camera/domain/QrClassification.ktindex 801427b8a3..e1834427ae 100644--- a/camera/src/main/java/com/vayunmathur/camera/domain/QrClassification.kt+++ b/camera/src/main/java/com/vayunmathur/camera/domain/QrClassification.kt@@ -155,7 +155,7 @@ private class CborCursor(private val bytes: ByteArray) { val minor = bytes[pos++].toInt() and 0x1F return when (minor) { in 0..23 -> minor.toLong()- 24 -> byte().toLong() and BYTE_MASK.toLong()+ 24 -> byte()?.let { it.toLong() and BYTE_MASK.toLong() } 25 -> uInt(2) 26 -> uInt(4) 27 -> uInt(8)@@ -195,15 +195,18 @@ private class CborCursor(private val bytes: ByteArray) { return true } - private fun byte(): Byte {- if (pos >= bytes.size) throw IndexOutOfBoundsException("CBOR cursor past end at $pos of ${bytes.size}")+ // Null past the end rather than throwing: the bytes come from a scanned+ // QR, so a truncated long-form argument (e.g. a lone 0x18) is malformed+ // input to report as UNKNOWN, not a crash in the composable reading it.+ private fun byte(): Byte? {+ if (pos >= bytes.size) return null return bytes[pos++] } private fun uInt(n: Int): Long? { if (pos + n > bytes.size) return null var v = 0L- repeat(n) { v = (v shl BYTE_SHIFT) or (byte().toLong() and BYTE_MASK.toLong()) }+ repeat(n) { v = (v shl BYTE_SHIFT) or ((byte() ?: return null).toLong() and BYTE_MASK.toLong()) } return v } }