Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Limit Android image attachment input size

laurenspriem180898170b54
diff --git a/mobile/native/android/apps/ensu/app/build.gradle.kts b/mobile/native/android/apps/ensu/app/build.gradle.kts
index 6f44385b1e..3dffcc9b63 100644
--- a/mobile/native/android/apps/ensu/app/build.gradle.kts
+++ b/mobile/native/android/apps/ensu/app/build.gradle.kts
@@ -160,6 +160,8 @@ dependencies {
exclude(group = "com.google.guava", module = "listenablefuture")
}
+ testImplementation("junit:junit:4.13.2")
+
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")
}
diff --git a/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/AttachmentInput.kt b/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/AttachmentInput.kt
new file mode 100644
index 0000000000..288e0f294f
--- /dev/null
+++ b/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/AttachmentInput.kt
@@ -0,0 +1,30 @@
+package io.ente.ensu
+
+import java.io.ByteArrayOutputStream
+import java.io.IOException
+import java.io.InputStream
+
+internal const val MaxAttachmentImageInputBytes = 32 * 1024 * 1024
+
+internal fun InputStream.readAttachmentImageBytes(
+ maxBytes: Int = MaxAttachmentImageInputBytes
+): ByteArray {
+ require(maxBytes >= 0) { "maxBytes must not be negative" }
+
+ val output = ByteArrayOutputStream(minOf(DEFAULT_BUFFER_SIZE, maxBytes))
+ val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
+ var totalBytes = 0
+
+ while (true) {
+ val bytesRead = read(buffer)
+ if (bytesRead < 0) break
+ if (bytesRead == 0) continue
+ if (bytesRead > maxBytes - totalBytes) {
+ throw IOException("Image attachment exceeds the $maxBytes-byte limit")
+ }
+ output.write(buffer, 0, bytesRead)
+ totalBytes += bytesRead
+ }
+
+ return output.toByteArray()
+}
diff --git a/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/HomeView.kt b/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/HomeView.kt
index 58f8cec99e..e445dac765 100644
--- a/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/HomeView.kt
+++ b/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/HomeView.kt
@@ -411,7 +411,7 @@ private fun buildAttachmentFromUri(
if (type == AttachmentType.Image) {
val inputStream = resolver.openInputStream(uri) ?: return@runCatching null
- val originalBytes = inputStream.use { input -> input.readBytes() }
+ val originalBytes = inputStream.use { input -> input.readAttachmentImageBytes() }
val compressedBytes = compressAttachmentImage(originalBytes)
FileOutputStream(destination).use { output ->
output.write(compressedBytes)
diff --git a/mobile/native/android/apps/ensu/app/src/test/java/io/ente/ensu/AttachmentInputTest.kt b/mobile/native/android/apps/ensu/app/src/test/java/io/ente/ensu/AttachmentInputTest.kt
new file mode 100644
index 0000000000..965da64994
--- /dev/null
+++ b/mobile/native/android/apps/ensu/app/src/test/java/io/ente/ensu/AttachmentInputTest.kt
@@ -0,0 +1,44 @@
+package io.ente.ensu
+
+import java.io.ByteArrayInputStream
+import java.io.IOException
+import java.io.InputStream
+import org.junit.Assert.assertArrayEquals
+import org.junit.Assert.assertThrows
+import org.junit.Test
+
+class AttachmentInputTest {
+ @Test
+ fun acceptsInputAtTheLimit() {
+ val input = byteArrayOf(1, 2, 3, 4)
+
+ val result = ByteArrayInputStream(input).readAttachmentImageBytes(maxBytes = input.size)
+
+ assertArrayEquals(input, result)
+ }
+
+ @Test
+ fun rejectsInputPastTheLimit() {
+ val input = ByteArrayInputStream(byteArrayOf(1, 2, 3, 4, 5))
+
+ assertThrows(IOException::class.java) {
+ input.readAttachmentImageBytes(maxBytes = 4)
+ }
+ }
+
+ @Test
+ fun stopsReadingAStreamThatDoesNotEnd() {
+ val input = object : InputStream() {
+ override fun read(): Int = 0
+
+ override fun read(buffer: ByteArray, offset: Int, length: Int): Int {
+ buffer.fill(0, offset, offset + length)
+ return length
+ }
+ }
+
+ assertThrows(IOException::class.java) {
+ input.readAttachmentImageBytes(maxBytes = 16)
+ }
+ }
+}