fix(mobile): withhold a screenshot when a secure field moves during the capture
MA
6h ago+204+204+012345678901234567890123456789-15-15-01234567890123456789
What
- On a device, an observation's pixels and an
app.screenshot()file are released only when a read of the screen after the capture places every secure field where the read before it did, on a screen of the same size. - Otherwise the observation goes without pixels (
maskedRegionCount: 0), andapp.screenshot()reads and captures once more, then fails withPOLICY_DENIED.
Why
- The masks came from the accessibility read, and the screenshot was a second device call. A scroll, the keyboard, or a screen push in between left the masks where the field used to be, while the image still counted as masked. A secure field that appeared after the read got no mask and was not counted.
- The desktop engine already reads again after its capture and withholds a changed window; this does the same on mobile.
How it works
securePlacementreads the screen after the capture and takes the secure rects and the tree's size from it, projecting without minting the session's ids or replacing the screen it last observed (packages/mobile/src/surface.ts:2135).samePlacementcompares the two reads: same number of secure rects at the same bounds, and the same tree size when both report one (surface.ts:2208).capturePixelswithholds the image when the reads differ or the second read fails, reportingmasked: 0when either read saw a secure field, and no pixels otherwise (surface.ts:2153).maskedScreenshotreads, captures, and reads again; on a difference it does that once more, then refuses withPOLICY_DENIED(surface.ts:2104). A second read that fails fails the screenshot with the device's own error, and nothing is written. Masks still scale by the screen size the session learned, not the tree's, since a tree can be an app drawn smaller than the screen.
Every pixel capture now takes one more accessibility read. I kept it on screens with no secure field too, since that read is what catches a field that appears during the capture.
Issue
Fixes #2432
Verified
Ran it locally: yes, unit level; no device run - this Mac has no CocoaPods to build the mobile benchmark app, and no prebuilt .app. The mobile.yml jobs run the benchmark's deterministic suite on an iOS simulator and an Android emulator.
packages/mobile/tests/unit/engine.test.ts, describe "a secure field that moves, comes, or goes during the capture": nine tests, one per case, where the fake device answers each read with the next screen in line.
| case | main | this PR |
|---|---|---|
| field moves from y=270 to y=500 during an observation | pixels ship, mask at y=270 | no pixels, maskedRegionCount: 0 |
| field appears after the read | pixels ship, nothing masked | no pixels, maskedRegionCount: 0 |
app.screenshot(), field moves once then settles | written, mask at the old spot | written after a second capture, mask where it settled |
app.screenshot(), field keeps moving | written | POLICY_DENIED, nothing written |
main (surface.ts from upstream/main, new test):
× withholds pixels and refuses a screenshot when a secure field moves, comes, or goes during the capture
AssertionError: expected { …(5) } to be undefined
this PR:
Test Files 28 passed (28)
Tests 717 passed (717)
- Could not measure the cost of the extra read with
pnpm bench:ab: it needs a simulator or emulator with the benchmark app.
Checklist
- Changeset added (
pnpm changeset) for any change to a published package, or the change is testbed, docs, or CI only. - Breaking change: title carries
!, the changeset body names what breaks and what replaces it, and the deprecation warning landed a window earlier (stability policy in CONTRIBUTING.md). - Docs updated in the same PR: the
docs/**/*.mdxpage for the behavior, andskills/e2e/if the skill describes it. - Contract change: emitted
.d.tsreviewed,tests/types/sdk-types.tsupdated; wire change edits the schema and both fixtures. - Engine contract change (
e2e/engine): changesets fore2e,@e2e-dev/web, and@e2e-dev/mobile.
Summary by CodeRabbit
-
Bug Fixes
- Screenshots now recheck secure-field placement and screen size after capture. If secure fields move, appear, or disappear—or the screen changes size—images are withheld from observations. Screenshot requests retry once and are denied if the screen remains unstable.
- If the follow-up screen read fails, observations omit image pixels, while screenshot requests return the device error without saving an image.
-
Documentation
- Clarified screenshot behavior for changing secure fields, screen dimensions, and read failures.