From 756ebc79b5e5ad439092b6f737038881fff59594 Mon Sep 17 00:00:00 2001 From: andrew gonzalez <80482198+bruhdev1290@users.noreply.github.com> Date: Wed, 4 Nov 2026 14:55:02 -0500 Subject: [PATCH] fix(data): bound bitmap decoding resources --- .../maps/android/data/parser/kml/KmzParser.kt | 38 +++-- .../data/renderer/SafeBitmapDecoder.kt | 83 ++++++++++ .../android/data/renderer/UrlIconProvider.kt | 32 ++-- .../android/data/parser/kml/KmzParserTest.kt | 47 +++++- .../data/renderer/SafeBitmapDecoderTest.kt | 146 ++++++++++++++++++ .../data/renderer/UrlIconProviderTest.kt | 59 ++++++- 6 files changed, 376 insertions(+), 29 deletions(-) create mode 100644 data/src/main/java/com/google/maps/android/data/renderer/SafeBitmapDecoder.kt create mode 100644 data/src/test/java/com/google/maps/android/data/renderer/SafeBitmapDecoderTest.kt diff --git a/data/src/main/java/com/google/maps/android/data/parser/kml/KmzParser.kt b/data/src/main/java/com/google/maps/android/data/parser/kml/KmzParser.kt index ac1d95d63..713205206 100644 --- a/data/src/main/java/com/google/maps/android/data/parser/kml/KmzParser.kt +++ b/data/src/main/java/com/google/maps/android/data/parser/kml/KmzParser.kt @@ -16,7 +16,8 @@ package com.google.maps.android.data.parser.kml import android.graphics.Bitmap -import android.graphics.BitmapFactory +import com.google.maps.android.data.renderer.SafeBitmapDecoder +import com.google.maps.android.data.renderer.SafeBitmapDecoder.takeIfWithinAllocationLimit import java.io.BufferedInputStream import java.io.ByteArrayInputStream import java.io.IOException @@ -35,7 +36,7 @@ public interface ImageDecoder { * Default implementation using Android's BitmapFactory. */ public class AndroidImageDecoder public constructor() : ImageDecoder { - public override fun decode(bytes: ByteArray): Bitmap? = BitmapFactory.decodeByteArray(bytes, 0, bytes.size) + public override fun decode(bytes: ByteArray): Bitmap? = SafeBitmapDecoder.decode(bytes) } /** @@ -101,6 +102,7 @@ public class KmzParser @JvmOverloads public constructor( */ public fun parse(inputStream: InputStream): Kml { val images = mutableMapOf() + var decodedImageBytes = 0L var kml: Kml? = null val zipInputStream = ZipInputStream(BufferedInputStream(inputStream)) val countingStream = CountingInputStream(zipInputStream, maxKmzUncompressedTotalSize) @@ -130,9 +132,20 @@ public class KmzParser @JvmOverloads public constructor( } else { // Try to decode as image val bytes = countingStream.readBytes() - val bitmap = imageDecoder.decode(bytes) + val bitmap = imageDecoder.decode(bytes)?.takeIfWithinAllocationLimit() if (bitmap != null) { - images[name] = bitmap + val bitmapBytes = bitmap.allocationByteCount.toLong() + if (bitmapBytes > MAX_DECODED_IMAGE_TOTAL_BYTES - decodedImageBytes) { + bitmap.recycle() + throw IOException( + "KMZ decoded image memory exceeds limit of $MAX_DECODED_IMAGE_TOTAL_BYTES bytes.", + ) + } + decodedImageBytes += bitmapBytes + images.put(name, bitmap)?.let { replaced -> + decodedImageBytes -= replaced.allocationByteCount.toLong() + replaced.recycle() + } } } // Drain any unread bytes through countingStream before closing entry to ensure @@ -141,15 +154,19 @@ public class KmzParser @JvmOverloads public constructor( zipInputStream.closeEntry() entry = zipInputStream.nextEntry } + if (kml == null) { + throw IllegalArgumentException("No KML file found in KMZ stream") + } + + return kml.copy(images = images) + } catch (exception: Exception) { + images.values.forEach { bitmap -> + if (!bitmap.isRecycled) bitmap.recycle() + } + throw exception } finally { zipInputStream.close() } - - if (kml == null) { - throw IllegalArgumentException("No KML file found in KMZ stream") - } - - return kml.copy(images = images) } /** @@ -166,6 +183,7 @@ public class KmzParser @JvmOverloads public constructor( public companion object { private const val DRAIN_BUFFER_SIZE = 8192 + private const val MAX_DECODED_IMAGE_TOTAL_BYTES = 32L * 1024 * 1024 public val SUPPORTED_EXTENSIONS: Set = setOf("kmz") public fun canParse(header: String): Boolean { diff --git a/data/src/main/java/com/google/maps/android/data/renderer/SafeBitmapDecoder.kt b/data/src/main/java/com/google/maps/android/data/renderer/SafeBitmapDecoder.kt new file mode 100644 index 000000000..83c0984ea --- /dev/null +++ b/data/src/main/java/com/google/maps/android/data/renderer/SafeBitmapDecoder.kt @@ -0,0 +1,83 @@ +/* + * Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.maps.android.data.renderer + +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import java.io.ByteArrayOutputStream +import java.io.InputStream + +/** + * Central security boundary for bitmap data originating in KML, GeoJSON, or KMZ input. + * + * Encoded byte limits do not bound bitmap memory: a small, highly compressible image can require + * several bytes per pixel after decoding. Always inspect dimensions before allocating pixels, then + * verify the actual allocation because row stride and decoder configuration can exceed estimates. + */ +internal object SafeBitmapDecoder { + internal const val MAX_ENCODED_BYTES: Int = 10 * 1024 * 1024 + internal const val MAX_DECODED_BYTES: Long = 16L * 1024 * 1024 + + private const val ARGB_8888_BYTES_PER_PIXEL = 4L + private const val READ_BUFFER_SIZE = 8192 + + internal fun decode(bytes: ByteArray): Bitmap? { + if (bytes.isEmpty() || bytes.size > MAX_ENCODED_BYTES) return null + + val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true } + BitmapFactory.decodeByteArray(bytes, 0, bytes.size, bounds) + if (!dimensionsFitBudget(bounds.outWidth, bounds.outHeight)) return null + + val options = BitmapFactory.Options().apply { inPreferredConfig = Bitmap.Config.ARGB_8888 } + val bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.size, options) ?: return null + return bitmap.takeIfWithinAllocationLimit() + } + + internal fun decode(input: InputStream): Bitmap? { + val encoded = readBounded(input) ?: return null + return decode(encoded) + } + + internal fun Bitmap.takeIfWithinAllocationLimit(): Bitmap? { + if (allocationByteCount.toLong() <= MAX_DECODED_BYTES) return this + recycle() + return null + } + + internal fun Bitmap.allocationKilobytes(): Int = maxOf(1, (allocationByteCount + 1023) / 1024) + + internal fun dimensionsFitBudget(width: Int, height: Int): Boolean { + if (width <= 0 || height <= 0) return false + val pixelCount = width.toLong() * height.toLong() + return pixelCount <= MAX_DECODED_BYTES / ARGB_8888_BYTES_PER_PIXEL + } + + private fun readBounded(input: InputStream): ByteArray? { + val output = ByteArrayOutputStream(READ_BUFFER_SIZE) + val buffer = ByteArray(READ_BUFFER_SIZE) + var total = 0 + while (true) { + val remainingWithSentinel = MAX_ENCODED_BYTES - total + 1 + val read = input.read(buffer, 0, minOf(buffer.size, remainingWithSentinel)) + if (read == -1) break + if (read == 0) continue + total += read + if (total > MAX_ENCODED_BYTES) return null + output.write(buffer, 0, read) + } + return output.toByteArray() + } +} diff --git a/data/src/main/java/com/google/maps/android/data/renderer/UrlIconProvider.kt b/data/src/main/java/com/google/maps/android/data/renderer/UrlIconProvider.kt index e4bc069df..1d5d8c16f 100644 --- a/data/src/main/java/com/google/maps/android/data/renderer/UrlIconProvider.kt +++ b/data/src/main/java/com/google/maps/android/data/renderer/UrlIconProvider.kt @@ -16,16 +16,18 @@ package com.google.maps.android.data.renderer import android.graphics.Bitmap -import android.graphics.BitmapFactory import android.util.LruCache import com.google.maps.android.data.kml.DefaultKmlUrlSanitizer import com.google.maps.android.data.kml.KmlUrlSanitizer +import com.google.maps.android.data.renderer.SafeBitmapDecoder.allocationKilobytes import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Deferred import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.async +import kotlinx.coroutines.sync.Semaphore +import kotlinx.coroutines.sync.withPermit import kotlinx.coroutines.withContext import java.net.HttpURLConnection import java.net.URL @@ -46,6 +48,7 @@ public open class UrlIconProvider @JvmOverloads public constructor( private val memoryCache: LruCache private val inFlight = ConcurrentHashMap>() private val downloadScope = CoroutineScope(SupervisorJob() + dispatcher) + private val downloadSemaphore = Semaphore(MAX_CONCURRENT_DOWNLOADS) init { // Get max available VM memory, exceeding this amount will throw an @@ -61,7 +64,7 @@ public open class UrlIconProvider @JvmOverloads public constructor( override fun sizeOf(key: String, bitmap: Bitmap): Int { // The cache size will be measured in kilobytes rather than // number of items. - return bitmap.byteCount / 1024 + return bitmap.allocationKilobytes() } } } @@ -76,15 +79,19 @@ public open class UrlIconProvider @JvmOverloads public constructor( synchronized(inFlight) { inFlight.getOrPut(url) { downloadScope.async { - try { - val bitmap = loadBitmapFromUrl(url) - if (bitmap != null) { - memoryCache.put(url, bitmap) + downloadSemaphore.withPermit { + try { + val bitmap = loadBitmapFromUrl(url)?.let { + SafeBitmapDecoder.run { it.takeIfWithinAllocationLimit() } + } + if (bitmap != null) { + memoryCache.put(url, bitmap) + } + bitmap + } finally { + // Always remove from in-flight map when done, success or failure + inFlight.remove(url) } - bitmap - } finally { - // Always remove from in-flight map when done, success or failure - inFlight.remove(url) } } } @@ -120,7 +127,7 @@ public open class UrlIconProvider @JvmOverloads public constructor( when { code in 200..299 -> { return@withContext connection.inputStream.use { - BitmapFactory.decodeStream(it) + SafeBitmapDecoder.decode(it) } } @@ -151,5 +158,8 @@ public open class UrlIconProvider @JvmOverloads public constructor( private companion object { /** Maximum number of redirects to follow before giving up. */ private const val MAX_REDIRECTS = 5 + + /** Limits simultaneous response buffering and bitmap allocation for distinct URLs. */ + private const val MAX_CONCURRENT_DOWNLOADS = 2 } } diff --git a/data/src/test/java/com/google/maps/android/data/parser/kml/KmzParserTest.kt b/data/src/test/java/com/google/maps/android/data/parser/kml/KmzParserTest.kt index 0b3aaee9a..2dc5778a0 100644 --- a/data/src/test/java/com/google/maps/android/data/parser/kml/KmzParserTest.kt +++ b/data/src/test/java/com/google/maps/android/data/parser/kml/KmzParserTest.kt @@ -17,12 +17,12 @@ package com.google.maps.android.data.parser.kml import android.graphics.Bitmap import io.mockk.every -import io.mockk.mockkStatic +import io.mockk.mockk +import io.mockk.verify import org.junit.Assert.assertEquals import org.junit.Assert.assertNotNull import org.junit.Assert.assertThrows import org.junit.Assert.assertTrue -import org.junit.Before import org.junit.Test import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream @@ -144,6 +144,49 @@ class KmzParserTest { ) } + @Test + fun `parse rejects cumulative decoded bitmap memory above limit`() { + val bitmapBytes = 16 * 1024 * 1024 + val bitmaps = List(3) { + mockk(relaxed = true) { + every { allocationByteCount } returns bitmapBytes + } + } + var nextBitmap = 0 + val decoder = + object : ImageDecoder { + override fun decode(bytes: ByteArray): Bitmap = bitmaps[nextBitmap++] + } + val kmzStream = + createKmzStream( + "doc.kml" to "".toByteArray(), + "one.png" to byteArrayOf(1), + "two.png" to byteArrayOf(2), + "three.png" to byteArrayOf(3), + ) + + assertThrows(IOException::class.java) { + KmzParser(decoder).parse(kmzStream) + } + verify(exactly = 1) { bitmaps[0].recycle() } + verify(exactly = 1) { bitmaps[1].recycle() } + verify(exactly = 1) { bitmaps[2].recycle() } + } + + @Test + fun `parse recycles decoded images when KMZ has no KML entry`() { + val bitmap = mockk(relaxed = true) + val decoder = + object : ImageDecoder { + override fun decode(bytes: ByteArray): Bitmap = bitmap + } + + assertThrows(IllegalArgumentException::class.java) { + KmzParser(decoder).parse(createKmzStream("image.png" to byteArrayOf(1))) + } + verify(exactly = 1) { bitmap.recycle() } + } + private fun createKmzStream(vararg entries: kotlin.Pair): ByteArrayInputStream { val baos = ByteArrayOutputStream() val zos = ZipOutputStream(baos) diff --git a/data/src/test/java/com/google/maps/android/data/renderer/SafeBitmapDecoderTest.kt b/data/src/test/java/com/google/maps/android/data/renderer/SafeBitmapDecoderTest.kt new file mode 100644 index 000000000..524c8fffb --- /dev/null +++ b/data/src/test/java/com/google/maps/android/data/renderer/SafeBitmapDecoderTest.kt @@ -0,0 +1,146 @@ +/* + * Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.maps.android.data.renderer + +import android.graphics.Bitmap +import com.google.maps.android.data.parser.kml.KmzParser +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.GraphicsMode +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream +import java.nio.ByteBuffer +import java.util.zip.CRC32 +import java.util.zip.DeflaterOutputStream +import java.util.zip.ZipEntry +import java.util.zip.ZipOutputStream + +@RunWith(RobolectricTestRunner::class) +@GraphicsMode(GraphicsMode.Mode.NATIVE) +class SafeBitmapDecoderTest { + @Test + fun `decode accepts a normal image`() { + val bitmap = SafeBitmapDecoder.decode(makeUniformRgbaPng(width = 128, height = 128)) + + assertNotNull(bitmap) + assertEquals(128, bitmap!!.width) + assertEquals(128, bitmap.height) + } + + @Test + fun `decode rejects image whose pixels exceed decoded memory budget`() { + val encoded = makeUniformRgbaPng(width = 4096, height = 4096) + + assertTrue(encoded.size < 256 * 1024) + assertNull(SafeBitmapDecoder.decode(encoded)) + } + + @Test + fun `KMZ parser does not retain image whose decoded size exceeds limit`() { + val encoded = makeUniformRgbaPng(width = 4096, height = 4096) + val kmz = + ByteArrayOutputStream().use { output -> + ZipOutputStream(output).use { zip -> + zip.putNextEntry(ZipEntry("doc.kml")) + zip.write("".toByteArray()) + zip.closeEntry() + zip.putNextEntry(ZipEntry("images/oversized.png")) + zip.write(encoded) + zip.closeEntry() + } + output.toByteArray() + } + + val parsed = KmzParser().parse(ByteArrayInputStream(kmz)) + + assertFalse(parsed.images.containsKey("images/oversized.png")) + } + + @Test + fun `decode stream stops after encoded byte limit`() { + val input = CountingInputStream(SafeBitmapDecoder.MAX_ENCODED_BYTES + 1) + + assertNull(SafeBitmapDecoder.decode(input)) + assertEquals(SafeBitmapDecoder.MAX_ENCODED_BYTES + 1, input.bytesRead) + } + + @Test + fun `dimension arithmetic rejects invalid and excessive sizes without overflow`() { + assertFalse(SafeBitmapDecoder.dimensionsFitBudget(0, 1)) + assertFalse(SafeBitmapDecoder.dimensionsFitBudget(Int.MAX_VALUE, Int.MAX_VALUE)) + assertTrue(SafeBitmapDecoder.dimensionsFitBudget(2048, 2048)) + } + + @Test + fun `cache accounting rounds small allocations up instead of treating them as free`() { + val bitmap = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888) + + assertEquals(1, SafeBitmapDecoder.run { bitmap.allocationKilobytes() }) + } + + private class CountingInputStream(private val size: Int) : ByteArrayInputStream(ByteArray(size)) { + var bytesRead: Int = 0 + private set + + override fun read(buffer: ByteArray, offset: Int, length: Int): Int { + val read = super.read(buffer, offset, length) + if (read > 0) bytesRead += read + return read + } + } + + private fun makeUniformRgbaPng(width: Int, height: Int): ByteArray { + val compressed = ByteArrayOutputStream() + DeflaterOutputStream(compressed).use { deflater -> + val row = ByteArray(1 + width * 4) + repeat(height) { deflater.write(row) } + } + + return ByteArrayOutputStream().use { png -> + png.write(byteArrayOf(0x89.toByte(), 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a)) + val header = ByteBuffer.allocate(13) + .putInt(width) + .putInt(height) + .put(8) + .put(6) + .put(0) + .put(0) + .put(0) + .array() + writeChunk(png, "IHDR", header) + writeChunk(png, "IDAT", compressed.toByteArray()) + writeChunk(png, "IEND", ByteArray(0)) + png.toByteArray() + } + } + + private fun writeChunk(output: ByteArrayOutputStream, type: String, data: ByteArray) { + output.write(ByteBuffer.allocate(4).putInt(data.size).array()) + val typeBytes = type.toByteArray(Charsets.US_ASCII) + output.write(typeBytes) + output.write(data) + val crc = CRC32() + crc.update(typeBytes) + crc.update(data) + output.write(ByteBuffer.allocate(4).putInt(crc.value.toInt()).array()) + } +} diff --git a/data/src/test/java/com/google/maps/android/data/renderer/UrlIconProviderTest.kt b/data/src/test/java/com/google/maps/android/data/renderer/UrlIconProviderTest.kt index 6fda9b4b8..b6ac484d9 100644 --- a/data/src/test/java/com/google/maps/android/data/renderer/UrlIconProviderTest.kt +++ b/data/src/test/java/com/google/maps/android/data/renderer/UrlIconProviderTest.kt @@ -16,15 +16,20 @@ package com.google.maps.android.data.renderer import android.graphics.Bitmap +import io.mockk.every +import io.mockk.mockk +import io.mockk.verify import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.async import kotlinx.coroutines.awaitAll import kotlinx.coroutines.delay import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.advanceTimeBy +import kotlinx.coroutines.test.advanceUntilIdle import kotlinx.coroutines.test.runTest import org.junit.Assert.assertEquals import org.junit.Assert.assertNotNull +import org.junit.Assert.assertNull import org.junit.Assert.assertSame import org.junit.Test import org.junit.runner.RunWith @@ -103,6 +108,40 @@ class UrlIconProviderTest { assertEquals(2, provider.loadCount.get()) } + @Test + fun loadIcon_rejectsOversizedBitmapReturnedBySubclass() = runTest { + val testDispatcher = StandardTestDispatcher(testScheduler) + val oversized = + mockk(relaxed = true) { + every { allocationByteCount } returns (SafeBitmapDecoder.MAX_DECODED_BYTES + 1).toInt() + } + val provider = + object : UrlIconProvider(testDispatcher) { + override suspend fun loadBitmapFromUrl(urlString: String): Bitmap = oversized + } + + assertNull(provider.loadIcon("http://example.com/oversized.png")) + verify(exactly = 1) { oversized.recycle() } + } + + @Test + fun loadIcon_limitsConcurrentDistinctDownloads() = runTest { + val testDispatcher = StandardTestDispatcher(testScheduler) + val provider = TestableUrlIconProvider(testDispatcher, delayMillis = 1000) + + val results = + listOf( + async { provider.loadIcon("http://example.com/one.png") }, + async { provider.loadIcon("http://example.com/two.png") }, + async { provider.loadIcon("http://example.com/three.png") }, + ) + advanceUntilIdle() + results.awaitAll() + + assertEquals(2, provider.maxActiveLoads.get()) + assertEquals(3, provider.loadCount.get()) + } + // Subclass to mock network calls private class TestableUrlIconProvider( dispatcher: kotlinx.coroutines.CoroutineDispatcher, @@ -110,16 +149,24 @@ class UrlIconProviderTest { var shouldFail: Boolean = false, ) : UrlIconProvider(dispatcher) { val loadCount = AtomicInteger(0) + val activeLoads = AtomicInteger(0) + val maxActiveLoads = AtomicInteger(0) override suspend fun loadBitmapFromUrl(urlString: String): Bitmap? { loadCount.incrementAndGet() - if (delayMillis > 0) { - delay(delayMillis) - } - if (shouldFail) { - return null + val active = activeLoads.incrementAndGet() + maxActiveLoads.updateAndGet { previous -> maxOf(previous, active) } + try { + if (delayMillis > 0) { + delay(delayMillis) + } + if (shouldFail) { + return null + } + return Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888) + } finally { + activeLoads.decrementAndGet() } - return Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888) } } }