Skip to content

fix: only dismiss an alert that is on screen - #72

Draft
cryptotavares wants to merge 1 commit into
mainfrom
cryptotavares/safe-dismiss-alert
Draft

cryptotavares wants to merge 1 commit into
mainfrom
cryptotavares/safe-dismiss-alert

Conversation

@cryptotavares

Copy link
Copy Markdown
Contributor

Part of the mm CLI v2 effort (audit item N1).

Summary: dismissAlert now acts only on an alert that is on screen. It looks for buttons only inside that alert, matches their labels exactly, and runs the same hit-test as tapElement before it taps. With no alert on screen, it throws No alert on screen and taps nothing.

Problem

dismissAlert(accept) searched the whole screen for each label in ACCEPT_LABELS / DENY_LABELS. It used the fuzzy match (case-insensitive substring), then tapped the frame centre with no hit-test. With no alert on screen, it tapped an unrelated control. mm dismiss-alert exposes this on devices that hold real funds.

The old code, run on a live Android Settings home snapshot with no alert on screen:

dismissAlert(false): label "No" matched "Notifications" at {"x":221,"y":1214,"width":239,"height":57}  -> tap

OK matches "Tokens" the same way, and Continue or Confirm matches any such button in the app.

Solution

flowchart TD
    A["dismissAlert(accept)"] --> B[snapshot]
    B --> C{"Alert on screen?<br/>iOS: type Alert<br/>Android: window with an alert id"}
    C -->|no| X["Error: No alert on screen. Nothing is tapped."]
    C -->|yes| D{"Button inside the alert<br/>with an exact label?"}
    D -->|no| Y["Error: No alert button found in the alert. Nothing is tapped."]
    D -->|yes| E["tapElement(button): hit-test"]
    E -->|covered| Z["TargetObscuredError. Nothing is tapped."]
    E -->|clear| F[tap]

    classDef new fill:#fff3cd,stroke:#d39e00
    classDef stop fill:#f8d7da,stroke:#c82333
    class C,D,E new
    class X,Y,Z stop
Loading

Yellow boxes are new. Red boxes stop the tap.

Supported alert shapes:

Platform Alert container Where the buttons come from
iOS An element of type Alert, as idb reports it. This covers in-app UIAlertController alerts. SpringBoard alerts (for example "Open in MetaMask?") work only if idb reports them as Alert in the tree. This is not verified live. Descendants of that element
Android The window root that contains <package>:id/alertTitle, android:id/button1/2/3, or any com.android.permissioncontroller / com.google.android.permissioncontroller id Descendants of that window

The label lists and their order are unchanged. A label matches when it equals the element's label or text, ignoring case.

What changes

Screen Before After
No alert; "Tokens", "Notifications", "Continue" on screen Taps the first fuzzy match No alert on screen; nothing was tapped.
Alert with OK / Cancel; "Allow" on the screen behind it accept taps the screen's "Allow" (earlier in the list) accept taps the alert's OK; deny taps its Cancel
Alert with no listed label; a matching button behind it Taps the button behind the alert No alert button found in the alert; nothing was tapped. Tried: …
Alert button covered by an overlay Taps the overlay TargetObscuredError (TARGET_OBSCURED)

The signature is unchanged. Failures still throw Error; the obscured case throws the existing TargetObscuredError.

Decisions for reviewers

Decision Reason
Case-insensitive equality, not case-sensitive The lists already hold ALLOW and DENY, so real buttons vary in case. Equality still rejects "Tokens" for OK.
Android: find the alert by resource ids, not by class name Live dumps report the dialog window as a plain FrameLayout. The class never contains Dialog. The ids are stable framework ids.
Android: search the whole alert window A dialog has its own window, so the screen behind it is not searched. A custom layout can put the buttons outside the title's parent.
Reuse tapElement with the resolved element It is the same hit-test path (describe-point on iOS, the occlusion check on Android) and the same viewport check.
Leave getAlertText as is Out of scope. It still uses its own container rules.

Review guide

# File What to check
1 src/utils/alert-labels.ts findAlertButton: exact match, list order kept, search only inside the alert
2 src/backends/idb-backend.ts dismissAlert: findElement(…, { type: 'Alert', exact: true }), then tapElement
3 src/backends/adb-backend.ts ALERT_MARKER_ID, findAndroidAlert, dismissAlert
4 *.test.ts One dismissAlert block per backend; the rows match the table above
Testing
  • yarn build, yarn lint and yarn test pass (38 files, 1002 tests, 6 skipped). Coverage stays above the floors.
  • Each backend has six tests: no alert (accept and deny), alert accept, alert deny, matching button only outside the alert, and covered button.
  • Mutation check: with the old dismissAlert, 5 of 6 tests fail on each backend. The deny-with-alert test passes on old code too, because the alert's Cancel happened to be the first fuzzy match.
  • Live check on an Android 16 emulator, Settings app only (instrument snapshots):
Screen Call Result
Settings home, no alert dismissAlert(true), dismissAlert(false) Both throw No alert on screen; nothing was tapped. The focused activity and the screen texts are the same before and after.
About → Device name dialog (alertTitle, button1 OK, button2 Cancel) dismissAlert(false) Taps Cancel. The dialog closes; the device name is unchanged.
Same dialog dismissAlert(true) Taps OK. Settings opens a second alert ("Your device name is visible…"). dismissAlert(false) taps its Cancel. The device name is unchanged.
  • Not run live: iOS. The iOS tests use the Alert shape that idb reports.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant