Skip to content

Commit 6184a63

Browse files
authored
Ensure aspect ratio of images in the timeline is restricted (#6168)
* Ensure aspect ratio of images in the timeline is restricted Otherwise, this could cause a crash in Compose since the width and height values could become way too large.
1 parent ef04e06 commit 6184a63

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/components/event/TimelineItemAspectRatioBox.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import androidx.compose.ui.unit.dp
2020
const val MIN_HEIGHT_IN_DP = 100
2121
const val MAX_HEIGHT_IN_DP = 360
2222
const val DEFAULT_ASPECT_RATIO = 1.33f
23+
const val MIN_ASPECT_RATIO = 0.001f
24+
const val MAX_ASPECT_RATIO = 10f
2325

2426
@Composable
2527
fun TimelineItemAspectRatioBox(
@@ -30,7 +32,8 @@ fun TimelineItemAspectRatioBox(
3032
maxHeight: Int = MAX_HEIGHT_IN_DP,
3133
content: @Composable (BoxScope.() -> Unit),
3234
) {
33-
val safeAspectRatio = aspectRatio ?: DEFAULT_ASPECT_RATIO
35+
// Make sure the aspect ratio is not extremely large, otherwise the resulting size can crash Compose
36+
val safeAspectRatio = aspectRatio?.coerceIn(MIN_ASPECT_RATIO, MAX_ASPECT_RATIO) ?: DEFAULT_ASPECT_RATIO
3437
Box(
3538
modifier = modifier
3639
.heightIn(min = minHeight.dp, max = maxHeight.dp)

features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/factories/event/TimelineItemContentMessageFactory.kt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ import kotlinx.collections.immutable.toImmutableList
5050
import org.jsoup.nodes.Document
5151
import kotlin.time.Duration
5252

53+
private const val MIN_IMAGE_SIZE = 1L
54+
private const val MAX_IMAGE_SIZE = 10_000L
55+
private const val MIN_ASPECT_RATIO = 0.001f
56+
private const val MAX_ASPECT_RATIO = 10f
57+
5358
@Inject
5459
class TimelineItemContentMessageFactory(
5560
private val fileSizeFormatter: FileSizeFormatter,
@@ -83,7 +88,10 @@ class TimelineItemContentMessageFactory(
8388
val dom = messageType.formattedCaption?.toHtmlDocument(permalinkParser = permalinkParser)
8489
val formattedCaption = dom?.let(::parseHtml)
8590
?: messageType.caption?.withLinks()
86-
val aspectRatio = aspectRatioOf(messageType.info?.width, messageType.info?.height)
91+
// Coerce the image sizes and prevent invalid aspect ratios, which can cause crashes
92+
val width = messageType.info?.width?.coerceIn(MIN_IMAGE_SIZE, MAX_IMAGE_SIZE)
93+
val height = messageType.info?.height?.coerceIn(MIN_IMAGE_SIZE, MAX_IMAGE_SIZE)
94+
val aspectRatio = aspectRatioOf(width, height)?.coerceIn(MIN_ASPECT_RATIO, MAX_ASPECT_RATIO)
8795
TimelineItemImageContent(
8896
filename = messageType.filename,
8997
fileSize = messageType.info?.size ?: 0,
@@ -94,10 +102,10 @@ class TimelineItemContentMessageFactory(
94102
thumbnailSource = messageType.info?.thumbnailSource,
95103
mimeType = messageType.info?.mimetype ?: MimeTypes.OctetStream,
96104
blurhash = messageType.info?.blurhash,
97-
width = messageType.info?.width?.toInt(),
98-
height = messageType.info?.height?.toInt(),
99-
thumbnailWidth = messageType.info?.thumbnailInfo?.width?.toInt(),
100-
thumbnailHeight = messageType.info?.thumbnailInfo?.height?.toInt(),
105+
width = width?.toInt(),
106+
height = height?.toInt(),
107+
thumbnailWidth = messageType.info?.thumbnailInfo?.width?.coerceIn(MIN_IMAGE_SIZE, MAX_IMAGE_SIZE)?.toInt(),
108+
thumbnailHeight = messageType.info?.thumbnailInfo?.height?.coerceIn(MIN_IMAGE_SIZE, MAX_IMAGE_SIZE)?.toInt(),
101109
aspectRatio = aspectRatio,
102110
formattedFileSize = fileSizeFormatter.format(messageType.info?.size ?: 0),
103111
fileExtension = fileExtensionExtractor.extractFromName(messageType.filename)

0 commit comments

Comments
 (0)