Skip to content

Commit 77ea4bb

Browse files
author
npub1ux8n2yfs8qfvgd75s7kyhar2mztac355v6vmrz4juc9l3msw4pgstums9e
committed
fix(mobile): reject nested JPEG start markers
Co-authored-by: npub1ux8n2yfs8qfvgd75s7kyhar2mztac355v6vmrz4juc9l3msw4pgstums9e <e18f3511303812c437d487ac4bf46ad897dc46946699b18ab2e60bf8ee0ea851@sprout-oss.stage.blox.sqprod.co> Signed-off-by: npub1ux8n2yfs8qfvgd75s7kyhar2mztac355v6vmrz4juc9l3msw4pgstums9e <e18f3511303812c437d487ac4bf46ad897dc46946699b18ab2e60bf8ee0ea851@sprout-oss.stage.blox.sqprod.co>
1 parent de1a47d commit 77ea4bb

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

‎mobile/android/app/src/main/kotlin/xyz/block/buzz/mobile/AndroidMediaSanitizer.kt‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,16 @@ internal object AndroidMediaSanitizer {
4242
}
4343

4444
fun scrubJpeg(bytes: ByteArray): ByteArray {
45-
require(bytes.size >= 2 && bytes[0] == 0xFF.toByte() && bytes[1] == JPEG_SOI) {
45+
require(
46+
bytes.size >= 2 &&
47+
bytes[0] == 0xFF.toByte() &&
48+
(bytes[1].toInt() and 0xFF) == JPEG_SOI,
49+
) {
4650
"Invalid JPEG signature"
4751
}
4852

4953
val output = ByteArrayOutputStream(bytes.size)
50-
output.write(byteArrayOf(0xFF.toByte(), JPEG_SOI))
54+
output.write(byteArrayOf(0xFF.toByte(), JPEG_SOI.toByte()))
5155
var offset = 2
5256
var inScan = false
5357
while (offset < bytes.size) {
@@ -79,7 +83,7 @@ internal object AndroidMediaSanitizer {
7983
output.write(bytes, markerStart, offset - markerStart)
8084
return output.toByteArray()
8185
}
82-
require(marker != JPEG_SOI.toInt() && bytes.size - offset >= 2) { "Invalid JPEG segment" }
86+
require(marker != JPEG_SOI && bytes.size - offset >= 2) { "Invalid JPEG segment" }
8387

8488
val segmentLength = readUnsignedShortBigEndian(bytes, offset)
8589
require(segmentLength >= 2 && segmentLength <= bytes.size - offset) { "Invalid JPEG segment length" }
@@ -143,7 +147,7 @@ internal object AndroidMediaSanitizer {
143147
}
144148

145149
private const val PNG_CHUNK_OVERHEAD = 12
146-
private const val JPEG_SOI: Byte = 0xD8.toByte()
150+
private const val JPEG_SOI = 0xD8
147151
private const val JPEG_EOI = 0xD9
148152
private const val JPEG_SOS = 0xDA
149153
private const val JPEG_TEMP = 0x01

‎mobile/android/app/src/test/kotlin/xyz/block/buzz/mobile/AndroidMediaSanitizerTest.kt‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ class AndroidMediaSanitizerTest {
4646
assertFailsWith<IllegalArgumentException> {
4747
AndroidMediaSanitizer.scrubJpeg(byteArrayOf(0xFF.toByte(), 0xD8.toByte(), 0xFF.toByte()))
4848
}
49+
assertFailsWith<IllegalArgumentException> {
50+
AndroidMediaSanitizer.scrubJpeg(
51+
byteArrayOf(
52+
0xFF.toByte(),
53+
0xD8.toByte(),
54+
0xFF.toByte(),
55+
0xD8.toByte(),
56+
0x00,
57+
0x02,
58+
0xFF.toByte(),
59+
0xD9.toByte(),
60+
),
61+
)
62+
}
4963
}
5064

5165
private fun fixtureBytes(name: String): ByteArray {

0 commit comments

Comments
 (0)