Modern Apps git

← Modern-Apps.git

web: guard the PWA launch extra like any other external input

Commit
cc8374eb0167b8b60a53d6f30d4ff7415606d318
Author
Vayun Mathur <[email protected]>
When
2026-11-08 04:37 UTC (28 days ago)
Parents
1f4a0f0
PwaActivity is exported (pinned shortcuts need it), so any app on the
device can fire it with the pwa_url extra set to anything. handleIntent
ran that extra through no validation at all - urlFromExtra was used
verbatim as the PWA window initial URL - while the sibling intent-data
feed went through the fail-closed extractHttpUrl guard. A foreign
javascript:, file: or content: string therefore reached PwaBrowser and
its unguarded initial loadUrl with zero user involvement. Our own
shortcuts always carry clean https URLs, so nothing legitimate changes.

The fix hoists extractHttpUrl into BrowserUtils with identical
semantics and routes the extra through it: only strings containing an
http(s) URL launch, everything else drops the intent. The private copy
in PwaActivity is removed in favour of the shared guard. MainActivity
keeps its own identical private copy for now - behaviour is unchanged
there, and unifying the two call sites is left as cleanup.

Verified red-then-green: the new BrowserUtilsTest extractor cases
failed against a null stub, then passed with the hoisted guard. Full
:web:testDevUnitTest green, 340 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 3259b9739e..e0ec99aff3 100644--- a/web/src/main/java/com/vayunmathur/web/platform/BrowserState.kt+++ b/web/src/main/java/com/vayunmathur/web/platform/BrowserState.kt@@ -151,6 +151,22 @@ object BrowserUtils {         return isWebUrl(trimmed)     } +    /**+     * Fail-closed extractor for externally supplied launch strings: share sheets, shortcut+     * extras, and intent data/text. Returns the first `http(s)` URL in the text, or null when+     * there is none — `javascript:`, `file:`, `data:` and friends never come back out.+     * Hoisted from duplicated private copies so every exported entry point shares one guard.+     */+    fun extractHttpUrl(text: String): String? {+        val trimmed = text.trim()+        if (isWebUrl(trimmed)) {+            val match = Regex("https?://\\S+", RegexOption.IGNORE_CASE).find(trimmed)+            return match?.value ?: trimmed.substringBefore(" ")+        }+        Regex("https?://\\S+", RegexOption.IGNORE_CASE).find(trimmed)?.let { return it.value }+        return null+    }+     /** 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/platform/PwaActivity.kt
diff --git a/web/src/main/java/com/vayunmathur/web/platform/PwaActivity.kt b/web/src/main/java/com/vayunmathur/web/platform/PwaActivity.ktindex 0e3bb5d4fd..7913945ab6 100644--- a/web/src/main/java/com/vayunmathur/web/platform/PwaActivity.kt+++ b/web/src/main/java/com/vayunmathur/web/platform/PwaActivity.kt@@ -74,7 +74,10 @@ class PwaActivity : ComponentActivity() {         intent ?: return         val urlFromExtra = intent.getStringExtra(EXTRA_URL)         val urlFromData = intent.dataString ?: intent.getStringExtra(Intent.EXTRA_TEXT)-        val url = urlFromExtra ?: extractHttpUrl(urlFromData ?: "") ?: return+        // This activity is exported (pinned shortcuts), so the extra is sender-controlled:+        // run it through the same fail-closed extractor as intent data instead of trusting+        // it verbatim. Our own shortcuts carry clean https URLs, which pass through unchanged.+        val url = BrowserUtils.extractHttpUrl(urlFromExtra ?: urlFromData ?: "") ?: return         initialUrlState.value = url         titleState.value = intent.getStringExtra(EXTRA_TITLE)         labelRecentsEntry(url, titleState.value)@@ -97,14 +100,4 @@ class PwaActivity : ComponentActivity() {             }         )     }--    private fun extractHttpUrl(text: String): String? {-        val trimmed = text.trim()-        if (BrowserUtils.isWebUrl(trimmed)) {-            val match = Regex("https?://\\S+", RegexOption.IGNORE_CASE).find(trimmed)-            return match?.value ?: trimmed.substringBefore(" ")-        }-        Regex("https?://\\S+", RegexOption.IGNORE_CASE).find(trimmed)?.let { return it.value }-        return null-    } }
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 bc9b8e2c0f..7fdd63710d 100644--- a/web/src/test/java/com/vayunmathur/web/platform/BrowserUtilsTest.kt+++ b/web/src/test/java/com/vayunmathur/web/platform/BrowserUtilsTest.kt@@ -3,6 +3,7 @@ package com.vayunmathur.web.platform import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFalse+import kotlin.test.assertNull import kotlin.test.assertTrue  /**@@ -74,4 +75,25 @@ class BrowserUtilsTest {         assertFalse(BrowserUtils.canLoadInWebView("search://results?q=weather"))         assertFalse(BrowserUtils.canLoadInWebView(""))     }++    @Test+    fun `launch extractor passes clean web URLs through`() {+        assertEquals("https://example.com/app", BrowserUtils.extractHttpUrl("https://example.com/app"))+        assertEquals("HTTP://example.com/", BrowserUtils.extractHttpUrl("  HTTP://example.com/  "))+        assertEquals(+            "https://example.com/page",+            BrowserUtils.extractHttpUrl("check this out https://example.com/page"),+        )+    }++    @Test+    fun `launch extractor rejects non-web launch strings`() {+        assertNull(BrowserUtils.extractHttpUrl(""))+        assertNull(BrowserUtils.extractHttpUrl("   "))+        assertNull(BrowserUtils.extractHttpUrl("javascript:alert(1)"))+        assertNull(BrowserUtils.extractHttpUrl("JaVaScRiPt:alert(1)"))+        assertNull(BrowserUtils.extractHttpUrl("file:///etc/hosts"))+        assertNull(BrowserUtils.extractHttpUrl("data:text/html,<h1>x</h1>"))+        assertNull(BrowserUtils.extractHttpUrl("just some words"))+    } }