@dynamic-labs/legacy-react-native-extension 5.9.1 → 5.9.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/EmbeddedWebViewController.kt +292 -8
- package/android/EmbeddedWebViewModule.kt +4 -0
- package/android/build.gradle +5 -0
- package/android/dynamic/embeddedwebview/EmbeddedWebViewController.kt +292 -8
- package/android/dynamic/embeddedwebview/EmbeddedWebViewModule.kt +4 -0
- package/android/embeddedwebview/EmbeddedWebViewController.kt +292 -8
- package/android/embeddedwebview/EmbeddedWebViewModule.kt +4 -0
- package/android/java/xyz/dynamic/embeddedwebview/EmbeddedWebViewController.kt +292 -8
- package/android/java/xyz/dynamic/embeddedwebview/EmbeddedWebViewModule.kt +4 -0
- package/android/main/java/xyz/dynamic/embeddedwebview/EmbeddedWebViewController.kt +292 -8
- package/android/main/java/xyz/dynamic/embeddedwebview/EmbeddedWebViewModule.kt +4 -0
- package/android/src/main/java/xyz/dynamic/embeddedwebview/EmbeddedWebViewController.kt +292 -8
- package/android/src/main/java/xyz/dynamic/embeddedwebview/EmbeddedWebViewModule.kt +4 -0
- package/android/xyz/dynamic/embeddedwebview/EmbeddedWebViewController.kt +292 -8
- package/android/xyz/dynamic/embeddedwebview/EmbeddedWebViewModule.kt +4 -0
- package/index.cjs +22 -4
- package/index.js +22 -4
- package/ios/EmbeddedWebViewController.swift +235 -0
- package/ios/EmbeddedWebViewModule.swift +6 -0
- package/package.json +6 -6
- package/src/ReactNativeExtension/ReactNativeExtension.d.ts +10 -1
- package/src/components/WebView/EmbeddedWebView/EmbeddedWebView.d.ts +8 -1
- package/src/nativeModules/EmbeddedWebView.d.ts +8 -0
|
@@ -7,6 +7,9 @@ import android.graphics.Color
|
|
|
7
7
|
import android.net.http.SslError
|
|
8
8
|
import android.os.Handler
|
|
9
9
|
import android.os.Looper
|
|
10
|
+
import android.text.TextUtils
|
|
11
|
+
import android.util.Log
|
|
12
|
+
import android.view.Gravity
|
|
10
13
|
import android.view.View
|
|
11
14
|
import android.view.ViewGroup
|
|
12
15
|
import android.webkit.JavascriptInterface
|
|
@@ -19,6 +22,10 @@ import android.webkit.WebResourceResponse
|
|
|
19
22
|
import android.webkit.WebView
|
|
20
23
|
import android.webkit.WebViewClient
|
|
21
24
|
import android.widget.FrameLayout
|
|
25
|
+
import android.widget.LinearLayout
|
|
26
|
+
import android.widget.TextView
|
|
27
|
+
import androidx.webkit.WebSettingsCompat
|
|
28
|
+
import androidx.webkit.WebViewFeature
|
|
22
29
|
import expo.modules.kotlin.AppContext
|
|
23
30
|
import org.json.JSONObject
|
|
24
31
|
import java.util.UUID
|
|
@@ -44,6 +51,21 @@ object EmbeddedWebViewController {
|
|
|
44
51
|
private var webView: WebView? = null
|
|
45
52
|
private var debuggingEnabled = false
|
|
46
53
|
|
|
54
|
+
// Opt-in for window.open() popups, set via setAllowWebViewPopups before
|
|
55
|
+
// the first setUrl call (ensureWebView reads it once, at creation time —
|
|
56
|
+
// WebSettings.setSupportMultipleWindows/javaScriptCanOpenWindowsAutomatically
|
|
57
|
+
// only take effect for the WebChromeClient wired up below, not
|
|
58
|
+
// retroactively on an existing WebView). Defaults to false: consumers who
|
|
59
|
+
// never call the setter get the pre-Coinbase-popup behavior unchanged.
|
|
60
|
+
private var allowPopups = false
|
|
61
|
+
|
|
62
|
+
// The popup opened via window.open() (e.g. Coinbase's keys.coinbase.com
|
|
63
|
+
// connect flow) — a separate WebView/container from the main overlay, torn
|
|
64
|
+
// down on window.close() (see PopupWebChromeClient.onCloseWindow), the
|
|
65
|
+
// dismiss button, or destroy().
|
|
66
|
+
private var popupContainer: LinearLayout? = null
|
|
67
|
+
private var popupWebView: WebView? = null
|
|
68
|
+
|
|
47
69
|
private val pendingNavigationUrls = mutableMapOf<String, String>()
|
|
48
70
|
private val pendingTimeouts = mutableMapOf<String, Runnable>()
|
|
49
71
|
|
|
@@ -160,6 +182,12 @@ object EmbeddedWebViewController {
|
|
|
160
182
|
}
|
|
161
183
|
}
|
|
162
184
|
|
|
185
|
+
fun setAllowWebViewPopups(enabled: Boolean) {
|
|
186
|
+
runOnMain {
|
|
187
|
+
allowPopups = enabled
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
163
191
|
fun destroy() {
|
|
164
192
|
runOnMain { teardown() }
|
|
165
193
|
}
|
|
@@ -226,13 +254,19 @@ object EmbeddedWebViewController {
|
|
|
226
254
|
settings.mediaPlaybackRequiresUserGesture = false
|
|
227
255
|
settings.allowFileAccess = false
|
|
228
256
|
settings.allowContentAccess = false
|
|
229
|
-
//
|
|
230
|
-
//
|
|
231
|
-
//
|
|
232
|
-
|
|
257
|
+
// Some wallet-connect flows (e.g. Coinbase's keys.coinbase.com Smart
|
|
258
|
+
// Wallet flow) only work through a real window.open() popup, with the
|
|
259
|
+
// result posted back via `postMessage` to `window.opener` — there's no
|
|
260
|
+
// redirect-based fallback. PopupWebChromeClient below hosts that popup
|
|
261
|
+
// in its own overlay, separate from this WebView's own content.
|
|
262
|
+
// Opt-in via allowPopups (see setAllowWebViewPopups): everyone else
|
|
263
|
+
// keeps the original refuse-popups behavior below.
|
|
264
|
+
settings.setSupportMultipleWindows(allowPopups)
|
|
265
|
+
settings.javaScriptCanOpenWindowsAutomatically = allowPopups
|
|
233
266
|
addJavascriptInterface(JsBridge(), SCRIPT_HANDLER_NAME)
|
|
234
267
|
webViewClient = NavigationClient()
|
|
235
|
-
webChromeClient =
|
|
268
|
+
webChromeClient =
|
|
269
|
+
if (allowPopups) PopupWebChromeClient(activity) else NoPopupWebChromeClient()
|
|
236
270
|
}
|
|
237
271
|
|
|
238
272
|
val container = FrameLayout(activity).apply {
|
|
@@ -288,6 +322,7 @@ object EmbeddedWebViewController {
|
|
|
288
322
|
}
|
|
289
323
|
|
|
290
324
|
private fun teardown() {
|
|
325
|
+
closePopup()
|
|
291
326
|
overlayContainer?.let { container ->
|
|
292
327
|
(container.parent as? ViewGroup)?.removeView(container)
|
|
293
328
|
container.removeAllViews()
|
|
@@ -311,6 +346,143 @@ object EmbeddedWebViewController {
|
|
|
311
346
|
preApprovedOrigin = null
|
|
312
347
|
}
|
|
313
348
|
|
|
349
|
+
// ---- Popup window (window.open) -------------------------------------------
|
|
350
|
+
|
|
351
|
+
// Builds the popup's overlay: its own visible, non-zero-size container and
|
|
352
|
+
// WebView, attached above the main overlay so a passkey/connect prompt is
|
|
353
|
+
// actually reachable by the user. Settings mirror the main WebView's
|
|
354
|
+
// hardening (no file/content access, no host JsBridge — the popup only
|
|
355
|
+
// ever talks to the wallet's own hosted page, never our RN bridge).
|
|
356
|
+
@SuppressLint("SetJavaScriptEnabled")
|
|
357
|
+
private fun createPopupWebView(activity: android.app.Activity): WebView {
|
|
358
|
+
val density = activity.resources.displayMetrics.density
|
|
359
|
+
|
|
360
|
+
// Shows the popup's current origin (e.g. "🔒 keys.coinbase.com") so the
|
|
361
|
+
// overlay reads as a real browser chrome bar rather than a bare content
|
|
362
|
+
// frame — the wallet page is running on a domain the user didn't
|
|
363
|
+
// navigate to themselves, so this is also a legitimate trust signal, not
|
|
364
|
+
// just cosmetic.
|
|
365
|
+
val addressBarText = TextView(activity).apply {
|
|
366
|
+
setTextColor(Color.parseColor("#E0E0E0"))
|
|
367
|
+
textSize = 14f
|
|
368
|
+
maxLines = 1
|
|
369
|
+
ellipsize = TextUtils.TruncateAt.MIDDLE
|
|
370
|
+
layoutParams = LinearLayout.LayoutParams(
|
|
371
|
+
0,
|
|
372
|
+
ViewGroup.LayoutParams.WRAP_CONTENT,
|
|
373
|
+
1f,
|
|
374
|
+
)
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
val webView = WebView(activity).apply {
|
|
378
|
+
setBackgroundColor(Color.WHITE)
|
|
379
|
+
settings.javaScriptEnabled = true
|
|
380
|
+
settings.domStorageEnabled = true
|
|
381
|
+
settings.databaseEnabled = true
|
|
382
|
+
settings.mediaPlaybackRequiresUserGesture = false
|
|
383
|
+
settings.allowFileAccess = false
|
|
384
|
+
settings.allowContentAccess = false
|
|
385
|
+
settings.setSupportMultipleWindows(false)
|
|
386
|
+
// Coinbase's keys.coinbase.com page refuses to proceed when it detects
|
|
387
|
+
// an embedded WebView — it reads the "wv" and "Version/x.x" markers
|
|
388
|
+
// Android adds to the default WebView user-agent (a real Chrome tab
|
|
389
|
+
// has neither). Stripping them makes this popup present as a stock
|
|
390
|
+
// Chrome tab, which is otherwise indistinguishable from one: it's a
|
|
391
|
+
// real window opened via WebChromeClient#onCreateWindow, not a
|
|
392
|
+
// same-document overlay.
|
|
393
|
+
settings.userAgentString =
|
|
394
|
+
settings.userAgentString
|
|
395
|
+
.replace("; wv", "")
|
|
396
|
+
.replace(Regex("Version/[\\d.]+ "), "")
|
|
397
|
+
webViewClient = PopupNavigationClient { origin ->
|
|
398
|
+
val host = origin.removePrefix("https://").removePrefix("http://")
|
|
399
|
+
runOnMain { addressBarText.text = "🔒 $host" }
|
|
400
|
+
}
|
|
401
|
+
webChromeClient = object : WebChromeClient() {
|
|
402
|
+
override fun onCloseWindow(window: WebView) {
|
|
403
|
+
// The wallet's own page calls window.close() once it has posted
|
|
404
|
+
// the connect result back via postMessage.
|
|
405
|
+
runOnMain { closePopup() }
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_AUTHENTICATION)) {
|
|
409
|
+
WebSettingsCompat.setWebAuthenticationSupport(
|
|
410
|
+
settings,
|
|
411
|
+
WebSettingsCompat.WEB_AUTHENTICATION_SUPPORT_FOR_APP,
|
|
412
|
+
)
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
val dismissButton = TextView(activity).apply {
|
|
417
|
+
text = "✕"
|
|
418
|
+
setTextColor(Color.WHITE)
|
|
419
|
+
textSize = 18f
|
|
420
|
+
val padding = (12 * density).toInt()
|
|
421
|
+
setPadding(padding, 0, padding, 0)
|
|
422
|
+
setOnClickListener { closePopup() }
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
val addressBar = LinearLayout(activity).apply {
|
|
426
|
+
orientation = LinearLayout.HORIZONTAL
|
|
427
|
+
gravity = Gravity.CENTER_VERTICAL
|
|
428
|
+
setBackgroundColor(Color.parseColor("#F2202124"))
|
|
429
|
+
val horizontalPadding = (12 * density).toInt()
|
|
430
|
+
val verticalPadding = (10 * density).toInt()
|
|
431
|
+
setPadding(horizontalPadding, verticalPadding, horizontalPadding, verticalPadding)
|
|
432
|
+
addView(addressBarText)
|
|
433
|
+
addView(dismissButton)
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
val container = LinearLayout(activity).apply {
|
|
437
|
+
orientation = LinearLayout.VERTICAL
|
|
438
|
+
setBackgroundColor(Color.WHITE)
|
|
439
|
+
addView(
|
|
440
|
+
addressBar,
|
|
441
|
+
LinearLayout.LayoutParams(
|
|
442
|
+
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
443
|
+
ViewGroup.LayoutParams.WRAP_CONTENT,
|
|
444
|
+
).apply { topMargin = (24 * density).toInt() },
|
|
445
|
+
)
|
|
446
|
+
addView(
|
|
447
|
+
webView,
|
|
448
|
+
LinearLayout.LayoutParams(
|
|
449
|
+
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
450
|
+
0,
|
|
451
|
+
1f,
|
|
452
|
+
),
|
|
453
|
+
)
|
|
454
|
+
visibility = View.VISIBLE
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
val decor = activity.window.decorView as ViewGroup
|
|
458
|
+
decor.addView(
|
|
459
|
+
container,
|
|
460
|
+
ViewGroup.LayoutParams(
|
|
461
|
+
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
462
|
+
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
463
|
+
),
|
|
464
|
+
)
|
|
465
|
+
container.requestFocus()
|
|
466
|
+
|
|
467
|
+
popupContainer = container
|
|
468
|
+
popupWebView = webView
|
|
469
|
+
return webView
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
private fun closePopup() {
|
|
473
|
+
popupContainer?.let { container ->
|
|
474
|
+
(container.parent as? ViewGroup)?.removeView(container)
|
|
475
|
+
container.removeAllViews()
|
|
476
|
+
}
|
|
477
|
+
popupWebView?.let {
|
|
478
|
+
it.stopLoading()
|
|
479
|
+
it.webChromeClient = null
|
|
480
|
+
it.destroy()
|
|
481
|
+
}
|
|
482
|
+
popupContainer = null
|
|
483
|
+
popupWebView = null
|
|
484
|
+
}
|
|
485
|
+
|
|
314
486
|
private fun runOnMain(action: () -> Unit) {
|
|
315
487
|
if (Looper.myLooper() == Looper.getMainLooper()) {
|
|
316
488
|
action()
|
|
@@ -543,9 +715,10 @@ object EmbeddedWebViewController {
|
|
|
543
715
|
return if (shouldIncludePort) "$scheme://$host:$port" else "$scheme://$host"
|
|
544
716
|
}
|
|
545
717
|
|
|
546
|
-
//
|
|
547
|
-
// `setSupportMultipleWindows={false}`
|
|
548
|
-
// to open the new window inside
|
|
718
|
+
// Default when allowPopups is false: refuse target=_blank / window.open
|
|
719
|
+
// popups (matches react-native-webview's `setSupportMultipleWindows={false}`
|
|
720
|
+
// semantics) — there's nowhere reasonable to open the new window inside
|
|
721
|
+
// the overlay unless the consumer has opted in via setAllowWebViewPopups.
|
|
549
722
|
private class NoPopupWebChromeClient : WebChromeClient() {
|
|
550
723
|
override fun onCreateWindow(
|
|
551
724
|
view: WebView,
|
|
@@ -556,4 +729,115 @@ object EmbeddedWebViewController {
|
|
|
556
729
|
return false
|
|
557
730
|
}
|
|
558
731
|
}
|
|
732
|
+
|
|
733
|
+
// Hosts window.open() popups (e.g. Coinbase's keys.coinbase.com Smart
|
|
734
|
+
// Wallet connect flow) in their own overlay — see createPopupWebView.
|
|
735
|
+
// Only one popup at a time: a second onCreateWindow while one is already
|
|
736
|
+
// open closes the first, matching a page replacing its own popup.
|
|
737
|
+
private class PopupWebChromeClient(
|
|
738
|
+
private val activity: android.app.Activity,
|
|
739
|
+
) : WebChromeClient() {
|
|
740
|
+
override fun onCreateWindow(
|
|
741
|
+
view: WebView,
|
|
742
|
+
isDialog: Boolean,
|
|
743
|
+
isUserGesture: Boolean,
|
|
744
|
+
resultMsg: android.os.Message?,
|
|
745
|
+
): Boolean {
|
|
746
|
+
val transport = resultMsg?.obj as? WebView.WebViewTransport ?: return false
|
|
747
|
+
|
|
748
|
+
closePopup()
|
|
749
|
+
val popup = createPopupWebView(activity)
|
|
750
|
+
transport.webView = popup
|
|
751
|
+
resultMsg.sendToTarget()
|
|
752
|
+
return true
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
// The popup only ever needs to stay within the origin it started on (e.g.
|
|
757
|
+
// keys.coinbase.com) — that first navigation is trusted because it's the
|
|
758
|
+
// trusted top-frame's own JS that called window.open() with the target
|
|
759
|
+
// URL. Anything the popup subsequently tries to navigate to outside that
|
|
760
|
+
// origin is refused, closing off a mid-flow redirect to an attacker page.
|
|
761
|
+
private class PopupNavigationClient(
|
|
762
|
+
private val onUrlChanged: (String) -> Unit,
|
|
763
|
+
) : WebViewClient() {
|
|
764
|
+
private var popupOrigin: String? = null
|
|
765
|
+
|
|
766
|
+
// Tracks whether the trusted first navigation has already happened,
|
|
767
|
+
// separately from popupOrigin itself. An opaque first load (about:blank,
|
|
768
|
+
// javascript:, etc.) has no host and so no determinable origin — without
|
|
769
|
+
// this flag, popupOrigin would stay null and the "first navigation is
|
|
770
|
+
// trusted" branch below would keep re-triggering on every subsequent
|
|
771
|
+
// navigation, letting a page open the popup at about:blank then redirect
|
|
772
|
+
// it to an attacker origin as if that were the trusted first load.
|
|
773
|
+
private var hasApprovedFirstNavigation = false
|
|
774
|
+
|
|
775
|
+
// Sonar flags any shouldOverrideUrlLoading override as a security
|
|
776
|
+
// hotspot (S6363, "unrestricted navigation") purely for existing, since
|
|
777
|
+
// it's the choke point that could let a WebView navigate anywhere.
|
|
778
|
+
// This one is the actual control, not a gap in it: it locks onto the
|
|
779
|
+
// popup's first-navigated origin below and rejects every subsequent
|
|
780
|
+
// navigation outside that origin. Removing or loosening this override
|
|
781
|
+
// would remove the security control the hotspot is asking about.
|
|
782
|
+
override fun shouldOverrideUrlLoading( // NOSONAR
|
|
783
|
+
view: WebView,
|
|
784
|
+
request: WebResourceRequest,
|
|
785
|
+
): Boolean {
|
|
786
|
+
if (!request.isForMainFrame) return false
|
|
787
|
+
|
|
788
|
+
val origin = originString(request.url)
|
|
789
|
+
|
|
790
|
+
if (!hasApprovedFirstNavigation) {
|
|
791
|
+
hasApprovedFirstNavigation = true
|
|
792
|
+
// Blocked navigations are not reported through emitLoadError: that
|
|
793
|
+
// channel is shared with the main webview's onLoadError, which feeds
|
|
794
|
+
// the SDK's reload/recovery budget — a benign popup-side block (e.g.
|
|
795
|
+
// the wallet page navigating off-origin) would otherwise count
|
|
796
|
+
// against and can exhaust the main webview's retry attempts, even
|
|
797
|
+
// though it never actually failed to load. Mirrors iOS's
|
|
798
|
+
// PopupNavigationDelegate, which is silent here too.
|
|
799
|
+
popupOrigin = origin
|
|
800
|
+
return false
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
if (origin != null && origin == popupOrigin) return false
|
|
804
|
+
|
|
805
|
+
return true
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
override fun onPageStarted(view: WebView, url: String?, favicon: Bitmap?) {
|
|
809
|
+
super.onPageStarted(view, url, favicon)
|
|
810
|
+
val origin = originString(url?.let(android.net.Uri::parse))
|
|
811
|
+
// Defensive fallback: some WebView versions don't route the very first,
|
|
812
|
+
// window.open()-triggered navigation through shouldOverrideUrlLoading.
|
|
813
|
+
// Capture the origin here too if it hasn't been captured yet, so the
|
|
814
|
+
// allowlist above is never left empty for the popup's whole lifetime.
|
|
815
|
+
if (!hasApprovedFirstNavigation) {
|
|
816
|
+
hasApprovedFirstNavigation = true
|
|
817
|
+
popupOrigin = origin
|
|
818
|
+
}
|
|
819
|
+
// Only ever report the approved origin, never a navigation that
|
|
820
|
+
// shouldOverrideUrlLoading is about to block (or already tried to) —
|
|
821
|
+
// otherwise the address bar could momentarily show an attacker's
|
|
822
|
+
// origin while the popup's actual (trusted) content stays on screen.
|
|
823
|
+
if (origin != null && origin == popupOrigin) onUrlChanged(origin)
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
override fun onReceivedSslError(
|
|
827
|
+
view: WebView,
|
|
828
|
+
handler: SslErrorHandler,
|
|
829
|
+
error: SslError,
|
|
830
|
+
) {
|
|
831
|
+
// Not routed through emitLoadError (see shouldOverrideUrlLoading's
|
|
832
|
+
// comment above) — but still worth a native log line, unlike a benign
|
|
833
|
+
// off-origin block, since a cert error here could mean a
|
|
834
|
+
// man-in-the-middle attack against the wallet connection. Origin
|
|
835
|
+
// only, not the full URL, which can carry session tokens.
|
|
836
|
+
Log.w(
|
|
837
|
+
"EmbeddedWebViewController",
|
|
838
|
+
"Popup SSL error ${error.primaryError} for ${originString(error.url?.let(android.net.Uri::parse)) ?: "unknown origin"}",
|
|
839
|
+
)
|
|
840
|
+
handler.cancel()
|
|
841
|
+
}
|
|
842
|
+
}
|
|
559
843
|
}
|
|
@@ -47,6 +47,10 @@ class EmbeddedWebViewModule : Module() {
|
|
|
47
47
|
EmbeddedWebViewController.setDebuggingEnabled(enabled)
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
+
AsyncFunction("setAllowWebViewPopups") { enabled: Boolean ->
|
|
51
|
+
EmbeddedWebViewController.setAllowWebViewPopups(enabled)
|
|
52
|
+
}
|
|
53
|
+
|
|
50
54
|
AsyncFunction("destroy") {
|
|
51
55
|
EmbeddedWebViewController.destroy()
|
|
52
56
|
}
|