Skip to content

Commit 843181f

Browse files
committed
fix: Add size limit for bimi
1 parent f28a86b commit 843181f

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

Ui/Compose/ContactCard/src/main/kotlin/com/infomaniak/core/ui/compose/contactcard/ContactCardShareExt.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import kotlinx.coroutines.withContext
3030
import okhttp3.Request
3131
import java.io.File
3232

33+
private const val MAX_AVATAR_SIZE = 5 * 1024 * 1024 // 5 MB
3334
private const val CONTACT_CARD_DIRECTORY = "attachments_cache"
3435
private const val CONTACT_CARD_FILE_PREFIX = "contact_card_"
3536
private const val CONTACT_CARD_FILE_SUFFIX = ".vcf"
@@ -57,12 +58,19 @@ private suspend fun Card.getAvatarDataOrNull(): Pair<String?, String?> {
5758
val request = Request.Builder().url(url).build()
5859

5960
HttpClient.okHttpClient.newCall(request).await().use { response ->
60-
val body = response.body
61+
val body = response.body ?: return@runCatching null to null
6162

6263
if (!response.isSuccessful) return@runCatching null to null
6364

65+
val contentLength = body.contentLength()
66+
if (contentLength > MAX_AVATAR_SIZE) return@runCatching null to null
67+
6468
val mimeType = body.contentType()?.subtype?.uppercase()
65-
val base64 = Base64.encodeToString(body.bytes(), Base64.NO_WRAP)
69+
val bytes = body.bytes()
70+
71+
if (bytes.size > MAX_AVATAR_SIZE) return@runCatching null to null
72+
73+
val base64 = Base64.encodeToString(bytes, Base64.NO_WRAP)
6674

6775
base64 to mimeType
6876
}

0 commit comments

Comments
 (0)