Skip to content

Commit 76d8a3c

Browse files
romtsnclaude
andauthored
fix(replay): Mask Coil 3.1+ AsyncImage in Compose (#6759)
* fix(replay): Mask Coil 3.1+ AsyncImage in Compose Coil 3.1 moved the painter from ContentPainterElement to its Modifier.Node, so the reflective painter lookup failed and the node was not treated as an image. Treat a Painter-named modifier without a readable painter as a maskable image, keep *PainterElement names under R8, and run the replay unit tests across Coil 3 versions in CI. Refs VULN-3400 Co-Authored-By: Claude <noreply@anthropic.com> * changelog * ref(replay): Find the painter field by name instead of catching getDeclaredField threw NoSuchFieldException for every Coil 3.1+ image on every frame, and the catch failed detekt. Search declaredFields instead, so no exception is created and the findPainter baseline entry is no longer needed. Refs VULN-3400 Co-Authored-By: Claude <noreply@anthropic.com> * test(replay): Render Coil 3 next to Coil 2 in the masking test activity Drop the useCoil3 flag and its two tests. The existing image tests now expect both images. Refs VULN-3400 Co-Authored-By: Claude <noreply@anthropic.com> * test(replay): Check Coil 3 image masking in the minified UI test app The AGP matrix runs sentry-uitest-android as an R8 release build, but its Compose screen had no image. Add a Coil 3 AsyncImage and a ReplayTest that checks its area is one color in a replay frame. The app now uses Compose UI 1.10.2, the first version that lets R8 rename modifier elements, so the test fails without the PainterElement keep rule. Compose 1.10 needs minSdk 23. Refs VULN-3400 Co-Authored-By: Claude <noreply@anthropic.com> * test(replay): Keep UI test app on minSdk 21 with manifest overrides Override the minSdk 23 of the Compose 1.10 libraries in the manifest instead of raising the minSdk of the app. The UI tests run on API 30+ only. Refs VULN-3400 Co-Authored-By: Claude <noreply@anthropic.com> * test(replay): Use minSdk 23 for the UI test app Compose UI 1.10 needs minSdk 23, and the UI tests run on API 30+ only. This replaces the manifest overrides for 12 libraries. Refs VULN-3400 Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 9a7574b commit 76d8a3c

15 files changed

Lines changed: 158 additions & 28 deletions

File tree

.github/workflows/compose-ui-matrix.yml renamed to .github/workflows/replay-dependency-matrix.yml

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Compose UI Matrix
1+
name: Session Replay Dependency Matrix
22

33
on:
44
push:
@@ -11,10 +11,20 @@ concurrency:
1111
cancel-in-progress: true
1212

1313
jobs:
14-
compose-ui-matrix:
14+
replay-dependency-matrix:
1515
timeout-minutes: 30
1616
runs-on: ubuntu-latest
17-
name: Compose UI Matrix
17+
name: ${{ matrix.name }} Matrix
18+
strategy:
19+
fail-fast: false
20+
matrix:
21+
include:
22+
# Runs every version in gradle/compose-ui-versions.json
23+
- name: Compose UI
24+
task: composeUiTest
25+
# Runs every version in gradle/coil3-versions.json
26+
- name: Coil 3
27+
task: coil3Test
1828

1929
steps:
2030
- name: Checkout Repo
@@ -29,15 +39,15 @@ jobs:
2939
- name: Setup Gradle
3040
uses: gradle/actions/setup-gradle@8da46b539cd8fe6ab27ca75128919a18d757604c # v6.4.1
3141

32-
# Runs every version in gradle/compose-ui-versions.json; --continue reports each failing one.
42+
# --continue reports each failing version.
3343
- name: Run Session Replay unit tests
34-
run: ./gradlew :sentry-android-replay:composeUiTest --continue
44+
run: ./gradlew :sentry-android-replay:${{ matrix.task }} --continue
3545

3646
- name: Upload test results
3747
if: always()
3848
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7
3949
with:
40-
name: test-results-compose-ui
50+
name: test-results-${{ matrix.task }}
4151
path: |
4252
sentry-android-replay/build/reports/*
4353
sentry-android-replay/build/test-results/**/*.xml
@@ -46,7 +56,7 @@ jobs:
4656
uses: phoenix-actions/test-reporting@7317eea6e13c47348dd0bb318669485157c518d6 # pin@v16
4757
if: always()
4858
with:
49-
name: JUnit Compose UI
59+
name: JUnit ${{ matrix.name }}
5060
path: |
5161
sentry-android-replay/build/test-results/**/*.xml
5262
reporter: java-junit

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
### Fixes
1111

12+
- Session Replay: Mask Compose images loaded with Coil 3.1+ `AsyncImage` when `maskAllImages` is enabled ([#6759](https://github.com/getsentry/sentry-java/pull/6759))
1213
- Profiling: No longer crash on API 35+ devices whose framework can't load `ProfilingManager`. Perfetto profiling is turned off on those devices instead ([#6742](https://github.com/getsentry/sentry-java/pull/6742))
1314
- Android: Out-of-process crash reporting now supports Android 5.0 (API level 21) and later, previously required Android 11 (API level 30) ([#6614](https://github.com/getsentry/sentry-java/pull/6614))
1415
- Android profiling: Uploading a Perfetto profile chunk now keeps the trace in memory once instead of up to three times, lowering peak memory use for traces up to 50 MB ([#6743](https://github.com/getsentry/sentry-java/pull/6743))

‎gradle/coil3-versions.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
"3.0.4",
3+
"3.1.0",
4+
"3.6.3"
5+
]

‎gradle/compose-ui-versions.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[
2-
{ "version": "1.8.3", "pin": { "androidx.compose.material3": "1.3.2" } },
2+
{ "version": "1.8.3", "pin": { "androidx.compose.material3": "1.3.2", "io.coil-kt.coil3": "3.3.0" } },
33
"1.13.0",
44
"1.14.0-alpha01"
55
]

‎gradle/libs.versions.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ async-profiler-jfr-converter = { module = "tools.profiler:jfr-converter", versio
141141
caffeine = { module = "com.github.ben-manes.caffeine:caffeine" }
142142
caffeine-jcache = { module = "com.github.ben-manes.caffeine:jcache", version = "3.3.1" }
143143
coil-compose = { module = "io.coil-kt:coil-compose", version = "2.7.0" }
144+
coil3-compose = { module = "io.coil-kt.coil3:coil-compose", version = "3.4.0" }
144145
commons-compress = {module = "org.apache.commons:commons-compress", version = "1.28.0"}
145146
context-propagation = { module = "io.micrometer:context-propagation", version = "1.2.1" }
146147
errorprone-core = { module = "com.google.errorprone:error_prone_core", version = "2.52.0" }

‎sentry-android-integration-tests/sentry-uitest-android/build.gradle.kts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ android {
2121
namespace = "io.sentry.uitest.android"
2222

2323
defaultConfig {
24-
minSdk = libs.versions.minSdk.get().toInt()
24+
// Compose UI 1.10+ requires minSdk 23; the Sentry SDK still supports 21.
25+
minSdk = 23
2526
targetSdk = libs.versions.compileSdk.get().toInt()
2627
versionCode = 1
2728
versionName = "1.0.0"
@@ -109,6 +110,9 @@ dependencies {
109110
implementation(libs.androidx.activity.compose)
110111
implementation(libs.androidx.compose.foundation)
111112
implementation(libs.androidx.compose.material3)
113+
implementation(libs.coil3.compose)
114+
// Compose UI 1.10+ lets R8 rename modifier elements, so the replay masking keep rules get tested
115+
implementation(libs.androidx.compose.ui.replay)
112116
implementation(libs.androidx.constraintlayout)
113117
implementation(libs.androidx.core)
114118
implementation(libs.androidx.recyclerview)

‎sentry-android-integration-tests/sentry-uitest-android/src/androidTest/java/io/sentry/uitest/android/ReplayTest.kt‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
package io.sentry.uitest.android
22

3+
import android.graphics.Bitmap
34
import androidx.lifecycle.Lifecycle
45
import androidx.test.core.app.launchActivity
56
import io.sentry.SentryOptions
7+
import io.sentry.SentryReplayOptions
8+
import io.sentry.TypeCheckHint
9+
import java.util.concurrent.CountDownLatch
10+
import java.util.concurrent.TimeUnit
611
import java.util.concurrent.atomic.AtomicBoolean
12+
import java.util.concurrent.atomic.AtomicReference
713
import kotlin.test.Test
14+
import kotlin.test.assertEquals
15+
import kotlin.test.assertTrue
816
import leakcanary.LeakAssertions
917
import leakcanary.LeakCanary
1018
import org.awaitility.kotlin.await
@@ -81,4 +89,52 @@ class ReplayTest : BaseUiTest() {
8189

8290
LeakAssertions.assertNoLeaks()
8391
}
92+
93+
@Test
94+
fun composeCoilImageIsMasked() {
95+
val frame = AtomicReference<Bitmap>()
96+
val frameReceived = CountDownLatch(1)
97+
98+
val activityScenario = launchActivity<ComposeActivity>()
99+
activityScenario.moveToState(Lifecycle.State.RESUMED)
100+
// an image that hasn't loaded yet is empty, and would look masked
101+
assertTrue(ComposeActivity.imageLoaded.await(10, TimeUnit.SECONDS), "Coil image didn't load")
102+
103+
initSentry {
104+
it.sessionReplay.sessionSampleRate = 1.0
105+
it.sessionReplay.frameObserver = SentryReplayOptions.ReplayFrameObserver { hint, _, _ ->
106+
val bitmap = hint.getAs(TypeCheckHint.REPLAY_FRAME_BITMAP, Bitmap::class.java)
107+
if (bitmap != null && frame.compareAndSet(null, bitmap)) {
108+
frameReceived.countDown()
109+
} else {
110+
bitmap?.recycle()
111+
}
112+
}
113+
}
114+
assertTrue(frameReceived.await(10, TimeUnit.SECONDS), "Expected a replay frame")
115+
116+
var rootWidth = 0
117+
var rootHeight = 0
118+
activityScenario.onActivity {
119+
rootWidth = it.window.decorView.width
120+
rootHeight = it.window.decorView.height
121+
}
122+
val bitmap = frame.get()
123+
val scaleX = bitmap.width / rootWidth.toFloat()
124+
val scaleY = bitmap.height / rootHeight.toFloat()
125+
val bounds = ComposeActivity.imageBounds
126+
// the inner half of the image, away from the edges of the scaled mask
127+
val colors = mutableSetOf<Int>()
128+
for (i in 1..9) {
129+
for (j in 1..9) {
130+
val x = (bounds.left + bounds.width * (0.25f + i * 0.05f)) * scaleX
131+
val y = (bounds.top + bounds.height * (0.25f + j * 0.05f)) * scaleY
132+
colors += bitmap.getPixel(x.toInt(), y.toInt())
133+
}
134+
}
135+
bitmap.recycle()
136+
activityScenario.moveToState(Lifecycle.State.DESTROYED)
137+
138+
assertEquals(1, colors.size, "Coil image isn't masked in the replay frame")
139+
}
84140
}

‎sentry-android-integration-tests/sentry-uitest-android/src/main/AndroidManifest.xml‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
android:label="Sentry End2End Tests"
1010
android:largeHeap="true"
1111
android:icon="@android:mipmap/sym_def_app_icon"
12-
android:usesCleartextTraffic="true"
13-
tools:targetApi="m">
12+
android:usesCleartextTraffic="true">
1413
<!-- android:usesCleartextTraffic="true" is needed to send data to http://localhost... (the mock relay server) -->
1514

1615
<activity android:name=".EmptyActivity" />

‎sentry-android-integration-tests/sentry-uitest-android/src/main/java/io/sentry/uitest/android/ComposeActivity.kt‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,38 @@ import androidx.compose.foundation.lazy.LazyColumn
1313
import androidx.compose.material3.MaterialTheme
1414
import androidx.compose.material3.Surface
1515
import androidx.compose.material3.Text
16+
import androidx.compose.ui.Alignment
1617
import androidx.compose.ui.Modifier
18+
import androidx.compose.ui.geometry.Rect
1719
import androidx.compose.ui.graphics.Color
20+
import androidx.compose.ui.layout.ContentScale
21+
import androidx.compose.ui.layout.boundsInWindow
22+
import androidx.compose.ui.layout.onGloballyPositioned
1823
import androidx.compose.ui.platform.testTag
1924
import androidx.compose.ui.unit.dp
25+
import androidx.core.graphics.createBitmap
26+
import androidx.core.graphics.set
27+
import coil3.compose.AsyncImage
28+
import java.util.concurrent.CountDownLatch
2029

2130
class ComposeActivity : AppCompatActivity() {
2231
companion object {
2332
private const val ITEM_COUNT: Int = 100
33+
34+
@Volatile var imageLoaded = CountDownLatch(1)
35+
@Volatile var imageBounds = Rect.Zero
2436
}
2537

38+
// two colors, so a replay frame shows whether the image is masked: a mask is one color
39+
private val image =
40+
createBitmap(2, 1).apply {
41+
this[0, 0] = android.graphics.Color.BLACK
42+
this[1, 0] = android.graphics.Color.WHITE
43+
}
44+
2645
override fun onCreate(savedInstanceState: Bundle?) {
2746
super.onCreate(savedInstanceState)
47+
imageLoaded = CountDownLatch(1)
2848

2949
setContent {
3050
MaterialTheme {
@@ -42,6 +62,18 @@ class ComposeActivity : AppCompatActivity() {
4262
.testTag("button_login"),
4363
) {
4464
Text("Login")
65+
// at the top, away from the center that the UI tests tap
66+
AsyncImage(
67+
model = image,
68+
contentDescription = null,
69+
contentScale = ContentScale.FillBounds,
70+
onSuccess = { imageLoaded.countDown() },
71+
modifier =
72+
Modifier.align(Alignment.TopCenter)
73+
.padding(top = 48.dp)
74+
.size(96.dp)
75+
.onGloballyPositioned { imageBounds = it.boundsInWindow() },
76+
)
4577
}
4678
}
4779
items(ITEM_COUNT) {

‎sentry-android-replay/build.gradle.kts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,13 @@ dependencies {
101101
testImplementation(libs.androidx.compose.foundation.layout)
102102
testImplementation(libs.androidx.compose.material3)
103103
testImplementation(libs.coil.compose)
104+
testImplementation(libs.coil3.compose)
104105
}
105106

106107
// Session Replay calls Compose internals, whose mangled JVM names change between Compose versions.
107-
// .github/workflows/compose-ui-matrix.yml runs each version's task. The oldest entry pins material3
108-
// to a version that runs on that Compose UI, because the catalog's material3 needs a newer one.
108+
// .github/workflows/replay-dependency-matrix.yml runs each version's task. The oldest entry pins
109+
// material3 and Coil 3 to versions that run on that Compose UI, because the catalog's need a newer
110+
// one.
109111
dependencyMatrix {
110112
matrix(
111113
name = "composeUi",
@@ -118,6 +120,9 @@ dependencyMatrix {
118120
),
119121
versionsFile = "compose-ui-versions.json",
120122
)
123+
// Compose image masking detects Coil's painter modifier by reflection, and Coil 3.1 changed it.
124+
// Coil 2 is a different artifact and package, so it's covered by the default unit tests instead.
125+
matrix(name = "coil3", groups = listOf("io.coil-kt.coil3"), versionsFile = "coil3-versions.json")
121126
}
122127

123128
tasks.withType<Detekt>().configureEach {

0 commit comments

Comments
 (0)