Modern Apps git

← Modern-Apps.git

web: fail closed on non-web URLs reaching WebView.loadUrl

Commit
1f4a0f08312f71feced8370c7296b6985adc7084
Author
Vayun Mathur <[email protected]>
When
2026-11-08 04:31 UTC (28 days ago)
Parents
aeb8694
Tab URLs reach the pooled WebView unchecked. Several feeds mirror
arbitrary strings into tab state: search-result and fallback URLs come
from the search-backend JSON untouched, history and bookmark rows are
tapped verbatim, and long-press links carry page-supplied hrefs. Both
programmatic sinks in WebViewBrowser - the pool-factory load and the
tab-sync update block - then handed that string straight to
WebView.loadUrl with no scheme check.

A javascript: URL there executes in the pooled WebView stale page
origin: on the native search-results screen the visible UI is Compose
while the pooled WebView still holds the last real page, so tapping a
server-supplied javascript: fallback runs script in another site
context with its cookies and DOM. The omnibox path was already safe
(everything funnels through toNavigationUrl, which only yields http,
https or engine search URLs) and in-page javascript: link clicks are
ordinary user-gesture navigation, so the hole is programmatic loads
only. External intents were already guarded by extractHttpUrl.

The fix is a pure fail-closed gatekeeper, BrowserUtils.canLoadInWebView:
only http(s) and about:blank may be handed to loadUrl, everything else
(javascript:, data:, blob:, file:, content:, intent:, tel:, and the
internal search:// pseudo-URL, which must never reach a WebView either)
is refused. Both sinks enforce it - the factory parks on about:blank
and the update block skips the load - so every feed is covered at once
since both sinks read tab state. Guarding the model funnels instead was
rejected: dropping non-web URLs from the tab mirror would desync the
omnibox after legitimate in-page data:/blob: navigations and make the
update block fight the WebView.

Verified red-then-green: the new BrowserUtilsTest cases failed against
an always-allow stub, then passed with the gate. Full
:web:testDevUnitTest green, 338 tests, 0 failures.

Diff

web/src/main/java/com/vayunmathur/web/platform/BrowserState.kt
diff --git a/web/src/main/java/com/vayunmathur/web/platform/BrowserState.kt b/web/src/main/java/com/vayunmathur/web/platform/BrowserState.ktindex 76675611e1..3259b9739e 100644--- a/web/src/main/java/com/vayunmathur/web/platform/BrowserState.kt+++ b/web/src/main/java/com/vayunmathur/web/platform/BrowserState.kt@@ -131,6 +131,26 @@ object BrowserUtils {     fun isWebUrl(url: String): Boolean =         url.startsWith("http://", ignoreCase = true) || url.startsWith("https://", ignoreCase = true) +    /**+     * Fail-closed gate for programmatic `WebView.loadUrl` calls (the tab-sync update block+     * and the pool factory in `WebViewBrowser`).+     *+     * Only `http(s)` and `about:blank` may be handed to `loadUrl`. Anything else — `javascript:`+     * above all — must never reach it: `loadUrl("javascript:…")` executes in the pooled WebView's+     * current (stale) page origin, so a server-supplied search result or fallback URL, a poisoned+     * history/bookmark row, or a page-supplied long-press link would run script in another site's+     * context. In-page `javascript:` links are unaffected: those never pass through this gate.+     *+     * `search://` pseudo-URLs return false on purpose — they render natively in+     * `SearchResultsPage` and must never be handed to a WebView either.+     */+    fun canLoadInWebView(url: String): Boolean {+        val trimmed = url.trim()+        if (trimmed.isEmpty()) return false+        if (trimmed.equals("about:blank", ignoreCase = true)) return true+        return isWebUrl(trimmed)+    }+     /** Kept for migration only — new tabs use "" and search uses selected engine. */     const val HOMEPAGE = "https://duckduckgo.com" 
web/src/main/java/com/vayunmathur/web/ui/WebViewBrowser.kt
diff --git a/web/src/main/java/com/vayunmathur/web/ui/WebViewBrowser.kt b/web/src/main/java/com/vayunmathur/web/ui/WebViewBrowser.ktindex 790afb1250..3040ada3ed 100644--- a/web/src/main/java/com/vayunmathur/web/ui/WebViewBrowser.kt+++ b/web/src/main/java/com/vayunmathur/web/ui/WebViewBrowser.kt@@ -30,6 +30,7 @@ import com.vayunmathur.library.ui.rememberMultiplePermissionRequest import com.vayunmathur.library.ui.rememberPermissionRequest import com.vayunmathur.web.platform.shields.FarblingConfig import com.vayunmathur.web.platform.shields.ShieldsWebViewClient+import com.vayunmathur.web.platform.BrowserUtils import com.vayunmathur.web.platform.WebViewModel import com.vayunmathur.web.platform.addDownload import com.vayunmathur.web.platform.denyGeolocation@@ -224,7 +225,10 @@ fun WebViewBrowser(                 val toLoad = if (initialUrl.isBlank()) "about:blank" else initialUrl                 // Before the first load: document-start scripts do not apply retroactively.                 (webViewClient as ShieldsWebViewClient).installFarbling(this, farblingConfig)-                loadUrl(toLoad)+                // Fail closed: a non-web initial URL (e.g. a javascript: string mirrored from a+                // server-supplied result or a stale tab row) must never execute in this pooled+                // WebView's origin. Park on a blank page instead of loading it.+                loadUrl(if (BrowserUtils.canLoadInWebView(toLoad)) toLoad else "about:blank")             }.also {                 webViewPool[tabId] = it                 applyWebViewSettings(it, viewModel)@@ -233,7 +237,11 @@ fun WebViewBrowser(         update = { webView ->             val current = webView.url ?: ""             val desired = viewModel.getCurrentUrl(tabId)-            if (desired.isNotBlank() && desired != current && !viewModel.omniboxFocused) {+            // canLoadInWebView fails closed: server- or page-supplied non-web strings mirrored+            // into tab state (search results, history rows, long-press links) are never handed+            // to loadUrl, where javascript: would execute in this WebView's stale origin.+            if (desired.isNotBlank() && desired != current && !viewModel.omniboxFocused &&+                BrowserUtils.canLoadInWebView(desired)) {                 if (viewModel.activeTabId == tabId) {                     // Always load when desired differs — the previous prog >= 1f guard prevented                     // external intents from loading while the current page was still loading,
web/src/test/java/com/vayunmathur/web/platform/BrowserUtilsTest.kt
diff --git a/web/src/test/java/com/vayunmathur/web/platform/BrowserUtilsTest.kt b/web/src/test/java/com/vayunmathur/web/platform/BrowserUtilsTest.ktindex 806e87a007..bc9b8e2c0f 100644--- a/web/src/test/java/com/vayunmathur/web/platform/BrowserUtilsTest.kt+++ b/web/src/test/java/com/vayunmathur/web/platform/BrowserUtilsTest.kt@@ -50,4 +50,28 @@ class BrowserUtilsTest {         assertEquals("HTTP://example.com/", BrowserUtils.toNavigationUrl("HTTP://example.com/"))         assertEquals("HTTPS://example.com/a", BrowserUtils.toNavigationUrl("HTTPS://example.com/a"))     }++    @Test+    fun `only http, https and about blank may reach WebView loadUrl`() {+        assertTrue(BrowserUtils.canLoadInWebView("https://example.com/"))+        assertTrue(BrowserUtils.canLoadInWebView("http://nas.local/"))+        assertTrue(BrowserUtils.canLoadInWebView("HTTP://example.com/"))+        assertTrue(BrowserUtils.canLoadInWebView("about:blank"))+        assertTrue(BrowserUtils.canLoadInWebView("ABOUT:BLANK"))+    }++    @Test+    fun `programmatic javascript and exotic schemes never reach WebView loadUrl`() {+        assertFalse(BrowserUtils.canLoadInWebView("javascript:alert(1)"))+        assertFalse(BrowserUtils.canLoadInWebView("JaVaScRiPt:alert(1)"))+        assertFalse(BrowserUtils.canLoadInWebView("   javascript:alert(1)"))+        assertFalse(BrowserUtils.canLoadInWebView("data:text/html,<h1>x</h1>"))+        assertFalse(BrowserUtils.canLoadInWebView("blob:https://example.com/uuid"))+        assertFalse(BrowserUtils.canLoadInWebView("file:///etc/hosts"))+        assertFalse(BrowserUtils.canLoadInWebView("content://media/external/images/1"))+        assertFalse(BrowserUtils.canLoadInWebView("intent://scan/#Intent;scheme=zxing;end"))+        assertFalse(BrowserUtils.canLoadInWebView("tel:+15551234567"))+        assertFalse(BrowserUtils.canLoadInWebView("search://results?q=weather"))+        assertFalse(BrowserUtils.canLoadInWebView(""))+    } }