-
-
Notifications
You must be signed in to change notification settings - Fork 465
feat(screenshot): Add screenshot masking using view hierarchy #5077
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
romtsn
wants to merge
13
commits into
main
Choose a base branch
from
rz/feat/screenshot-masking
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
469086f
feat(screenshot): Add screenshot masking using view hierarchy
romtsn 17beece
Changelog
romtsn 49f4517
dontwarn about classes we check via reflection at runtime
romtsn ad8d6df
pr id
romtsn 5242ed6
fix(screenshot): Only warn about missing replay module when masking i…
romtsn 115107b
fix(screenshot): Remove sensitive view classes when setMaskAllImages(…
romtsn 83da7df
fix(screenshot): Recycle bitmap copy on masking failure to prevent me…
romtsn b26b448
Merge remote-tracking branch 'origin/main' into rz/feat/screenshot-ma…
romtsn 2b8e4aa
fix: Resolve merge conflicts with main and integrate trackCustomMasking
romtsn 69e5d56
Clean up slop
romtsn 55819a4
fix(test): Implement abstract trackCustomMasking in test stub
romtsn 5084ed2
fix(screenshot): Use peekDecorView instead of getDecorView
romtsn b37b2eb
refactor(screenshot): Per-call MaskRenderer, main-thread VH capture, …
romtsn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
|
|
||
| import android.app.Activity; | ||
| import android.graphics.Bitmap; | ||
| import android.view.View; | ||
| import io.sentry.Attachment; | ||
| import io.sentry.EventProcessor; | ||
| import io.sentry.Hint; | ||
|
|
@@ -14,9 +15,15 @@ | |
| import io.sentry.android.core.internal.util.AndroidCurrentDateProvider; | ||
| import io.sentry.android.core.internal.util.Debouncer; | ||
| import io.sentry.android.core.internal.util.ScreenshotUtils; | ||
| import io.sentry.android.replay.util.MaskRenderer; | ||
| import io.sentry.android.replay.util.ViewsKt; | ||
| import io.sentry.android.replay.viewhierarchy.ViewHierarchyNode; | ||
| import io.sentry.protocol.SentryTransaction; | ||
| import io.sentry.util.HintUtils; | ||
| import io.sentry.util.Objects; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
@@ -34,10 +41,14 @@ public final class ScreenshotEventProcessor implements EventProcessor { | |
| private final @NotNull Debouncer debouncer; | ||
| private static final long DEBOUNCE_WAIT_TIME_MS = 2000; | ||
| private static final int DEBOUNCE_MAX_EXECUTIONS = 3; | ||
| private static final long MASKING_TIMEOUT_MS = 2000; | ||
|
|
||
| private final boolean isReplayAvailable; | ||
|
|
||
| public ScreenshotEventProcessor( | ||
| final @NotNull SentryAndroidOptions options, | ||
| final @NotNull BuildInfoProvider buildInfoProvider) { | ||
| final @NotNull BuildInfoProvider buildInfoProvider, | ||
| final boolean isReplayAvailable) { | ||
| this.options = Objects.requireNonNull(options, "SentryAndroidOptions is required"); | ||
| this.buildInfoProvider = | ||
| Objects.requireNonNull(buildInfoProvider, "BuildInfoProvider is required"); | ||
|
|
@@ -47,11 +58,26 @@ public ScreenshotEventProcessor( | |
| DEBOUNCE_WAIT_TIME_MS, | ||
| DEBOUNCE_MAX_EXECUTIONS); | ||
|
|
||
| this.isReplayAvailable = isReplayAvailable; | ||
|
|
||
| if (options.isAttachScreenshot()) { | ||
| addIntegrationToSdkVersion("Screenshot"); | ||
| } | ||
| } | ||
|
|
||
| private boolean isMaskingEnabled() { | ||
| if (options.getScreenshotOptions().getMaskViewClasses().isEmpty()) { | ||
| return false; | ||
| } | ||
| if (!isReplayAvailable) { | ||
| options | ||
| .getLogger() | ||
| .log(SentryLevel.WARNING, "Screenshot masking requires sentry-android-replay module"); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| public @NotNull SentryTransaction process( | ||
| @NotNull SentryTransaction transaction, @NotNull Hint hint) { | ||
|
|
@@ -89,23 +115,123 @@ public ScreenshotEventProcessor( | |
| return event; | ||
| } | ||
|
|
||
| final Bitmap screenshot = | ||
| Bitmap screenshot = | ||
| captureScreenshot( | ||
| activity, options.getThreadChecker(), options.getLogger(), buildInfoProvider); | ||
| if (screenshot == null) { | ||
| return event; | ||
| } | ||
|
|
||
| // Apply masking if enabled and replay module is available | ||
| if (isMaskingEnabled()) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't this is guaranteed to be always executed on the main thread, right? We might need to enforce this, otherwise random crashes could occur while iterating the VH. |
||
| final @Nullable ViewHierarchyNode rootNode = captureViewHierarchy(activity); | ||
| if (rootNode == null) { | ||
| return event; | ||
| } | ||
| screenshot = applyMasking(screenshot, rootNode); | ||
| if (screenshot == null) { | ||
| return event; | ||
| } | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| final Bitmap finalScreenshot = screenshot; | ||
| hint.setScreenshot( | ||
| Attachment.fromByteProvider( | ||
| () -> ScreenshotUtils.compressBitmapToPng(screenshot, options.getLogger()), | ||
| () -> ScreenshotUtils.compressBitmapToPng(finalScreenshot, options.getLogger()), | ||
| "screenshot.png", | ||
| "image/png", | ||
| false)); | ||
| hint.set(ANDROID_ACTIVITY, activity); | ||
| return event; | ||
| } | ||
|
|
||
| /** | ||
| * Captures the view hierarchy on the main thread, since view traversal requires it. If already on | ||
| * the main thread, captures directly; otherwise posts to the main thread and waits. | ||
| */ | ||
| private @Nullable ViewHierarchyNode captureViewHierarchy(final @NotNull Activity activity) { | ||
| if (options.getThreadChecker().isMainThread()) { | ||
| return buildViewHierarchy(activity); | ||
| } | ||
|
|
||
| final AtomicReference<ViewHierarchyNode> result = new AtomicReference<>(null); | ||
| final CountDownLatch latch = new CountDownLatch(1); | ||
|
|
||
| activity.runOnUiThread( | ||
| () -> { | ||
| try { | ||
| result.set(buildViewHierarchy(activity)); | ||
| } finally { | ||
| latch.countDown(); | ||
| } | ||
| }); | ||
|
|
||
| try { | ||
| if (!latch.await(MASKING_TIMEOUT_MS, TimeUnit.MILLISECONDS)) { | ||
| options | ||
| .getLogger() | ||
| .log( | ||
| SentryLevel.WARNING, "Timed out waiting for view hierarchy capture on main thread"); | ||
| return null; | ||
| } | ||
| } catch (Throwable e) { | ||
| options.getLogger().log(SentryLevel.ERROR, "Failed to capture view hierarchy", e); | ||
| return null; | ||
| } | ||
|
|
||
| return result.get(); | ||
| } | ||
|
|
||
| private @Nullable ViewHierarchyNode buildViewHierarchy(final @NotNull Activity activity) { | ||
| final @Nullable View rootView = | ||
| activity.getWindow() != null | ||
| && activity.getWindow().peekDecorView() != null | ||
| && activity.getWindow().peekDecorView().getRootView() != null | ||
| ? activity.getWindow().peekDecorView().getRootView() | ||
| : null; | ||
| if (rootView == null) { | ||
| return null; | ||
| } | ||
|
|
||
| final ViewHierarchyNode rootNode = | ||
| ViewHierarchyNode.Companion.fromView(rootView, null, 0, options.getScreenshotOptions()); | ||
| ViewsKt.traverse(rootView, rootNode, options.getScreenshotOptions(), options.getLogger()); | ||
| return rootNode; | ||
| } | ||
|
|
||
| private @Nullable Bitmap applyMasking( | ||
| final @NotNull Bitmap screenshot, final @NotNull ViewHierarchyNode rootNode) { | ||
| Bitmap mutableBitmap = screenshot; | ||
| boolean createdCopy = false; | ||
| try (final MaskRenderer maskRenderer = new MaskRenderer()) { | ||
| // Make bitmap mutable if needed | ||
| if (!screenshot.isMutable()) { | ||
| mutableBitmap = screenshot.copy(Bitmap.Config.ARGB_8888, true); | ||
| if (mutableBitmap == null) { | ||
| return screenshot; | ||
| } | ||
| createdCopy = true; | ||
| } | ||
|
|
||
| maskRenderer.renderMasks(mutableBitmap, rootNode, null); | ||
|
|
||
| // Recycle original if we created a copy | ||
| if (createdCopy && !screenshot.isRecycled()) { | ||
| screenshot.recycle(); | ||
| } | ||
|
|
||
| return mutableBitmap; | ||
| } catch (Throwable e) { | ||
| options.getLogger().log(SentryLevel.ERROR, "Failed to mask screenshot", e); | ||
| // Recycle the copy if we created one, to avoid memory leak | ||
| if (createdCopy && !mutableBitmap.isRecycled()) { | ||
| mutableBitmap.recycle(); | ||
| } | ||
| // Don't return unmasked screenshot when masking is configured | ||
| return null; | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Override | ||
| public @Nullable Long getOrder() { | ||
| return 10000L; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ManifestMetadataReader pollutes unmaskViewClasses when keys absent
Medium Severity
When screenshot masking manifest keys are absent,
readBoolreturns the defaultfalse, causingsetMaskAllText(false)andsetMaskAllImages(false)to unconditionally addTEXT_VIEW_CLASS_NAMEandIMAGE_VIEW_CLASS_NAMEtounmaskViewClasses. If a user later callsaddMaskViewClass("android.widget.TextView")programmatically, theshouldMasklogic checksunmaskViewClassesfirst and returns false, silently preventing masking from working for standard view classes. Only callingsetMaskAllText(true)works because it explicitly removes fromunmaskViewClasses. The setters here need to be conditional — only called when the value istrue.Additional Locations (1)
sentry/src/main/java/io/sentry/SentryMaskingOptions.java#L60-L68