Commit 5406044
authored
fix: Links Redirected to Chrome App Bug (Android bug) (MetaMask#25969)
## **Description**
On Android, tapping a `target="_blank"` link (e.g. the "Stake" button on
lido.fi) in the in-app browser opens the URL in Chrome instead of
loading it within the MetaMask browser. This does not happen on iOS.
**Root cause:** The `@metamask/react-native-webview` library handles
`target="_blank"` differently per platform when the `onOpenWindow` prop
is not provided:
- **iOS** has a built-in fallback in `createWebViewWithConfiguration`
that loads the URL in the same WKWebView.
- **Android** has no such fallback. With `setSupportMultipleWindows`
defaulting to `true`, the native `RNCWebChromeClient.onCreateWindow`
creates a bare `WebView` with no custom `WebViewClient`. Since
`mHasOnOpenWindowEvent` is `false`, the URL is loaded in this bare
WebView which has no UI container, causing Android to delegate to the
system browser (Chrome).
**Fix:** Added an `onOpenWindow` handler to the WebView in `BrowserTab`
that intercepts `target="_blank"` navigations and loads the URL in the
current tab via `window.location.href`. This matches the existing iOS
fallback behavior and makes both platforms consistent. The handler
reuses the same `sanitizeUrlInput` + `injectJavaScript` pattern already
used elsewhere in the file for deeplink browser callbacks.
## **Changelog**
CHANGELOG entry: Fixed an Android-only bug where `target="_blank"` links
in the in-app browser opened in Chrome instead of loading within
MetaMask
## **Related issues**
Fixes: <!-- Add issue number if applicable -->
Jira Ticket: https://consensyssoftware.atlassian.net/browse/MCWP-321
## **Manual testing steps**
```gherkin
Feature: In-app browser target="_blank" link handling
Scenario: user taps a target="_blank" link on Android
Given the user has the MetaMask app open on Android
And the user navigates to https://lido.fi in the in-app browser
When user taps the "Stake" button (which is an <a target="_blank"> link to https://stake.lido.fi)
Then the URL loads within the MetaMask in-app browser
And the user is NOT redirected to Chrome or any external browser
Scenario: user taps a target="_blank" link on iOS
Given the user has the MetaMask app open on iOS
And the user navigates to https://lido.fi in the in-app browser
When user taps the "Stake" button (which is an <a target="_blank"> link to https://stake.lido.fi)
Then the URL loads within the MetaMask in-app browser (same behavior as before the fix)
Scenario: user taps a regular link (no target="_blank")
Given the user has the MetaMask app open on either platform
And the user navigates to any dApp in the in-app browser
When user taps a regular link without target="_blank"
Then the link navigates normally within the in-app browser (no regression)
```
## **Screenshots/Recordings**
### **Before**
<!-- [screenshots/recordings showing Chrome opening on Android when
tapping target="_blank" link] -->
### **After**
<!-- [screenshots/recordings showing URL loading within in-app browser
on Android] -->
## **Pre-merge author checklist**
- [x] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile
Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I've included tests if applicable
- [x] I've documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I've applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.
## **Pre-merge reviewer checklist**
- [x] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [x] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Low Risk**
> Localized to in-app browser WebView navigation; behavior change is
straightforward and covered by new unit tests, with minimal impact
outside `target="_blank"` handling.
>
> **Overview**
> Fixes Android in-app browser behavior where `target="_blank"` links /
`window.open()` could escape to the system browser by adding an explicit
`onOpenWindow` handler that redirects the current WebView via injected
`window.location.href` (with `sanitizeUrlInput`).
>
> Updates tests to mock the WebView ref `injectJavaScript`, adds
coverage for `onOpenWindow` behavior (including quote sanitization and
empty URL), and refreshes the BrowserTab snapshot to include the new
prop.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
744973c. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent 8fad90c commit 5406044
3 files changed
Lines changed: 122 additions & 0 deletions
File tree
- app/components/Views/BrowserTab
- __snapshots__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| |||
751 | 752 | | |
752 | 753 | | |
753 | 754 | | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
754 | 771 | | |
755 | 772 | | |
756 | 773 | | |
| |||
1493 | 1510 | | |
1494 | 1511 | | |
1495 | 1512 | | |
| 1513 | + | |
1496 | 1514 | | |
1497 | 1515 | | |
1498 | 1516 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
568 | 568 | | |
569 | 569 | | |
570 | 570 | | |
| 571 | + | |
571 | 572 | | |
572 | 573 | | |
573 | 574 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
9 | 32 | | |
10 | 33 | | |
11 | 34 | | |
| |||
90 | 113 | | |
91 | 114 | | |
92 | 115 | | |
| 116 | + | |
93 | 117 | | |
94 | 118 | | |
95 | 119 | | |
| |||
215 | 239 | | |
216 | 240 | | |
217 | 241 | | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
218 | 321 | | |
0 commit comments