web: handle uppercase URL schemes case-insensitively
- Commit
2bd2af912c6129b73f6f0e311f482925be066ec8- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-08 02:45 UTC (28 days ago)
- Parents
ed0ff14
Every scheme gate in the browser compared raw strings case-sensitively,
so an uppercase-scheme URL (HTTP://example.com from a pasted link, a
redirect, or another app's share intent) fell through to the wrong path:
the omnibox sent it to search instead of navigating, shouldOverrideUrlLoading
handed it to the external-app opener, the shields panel showed no host,
favicons were never stored, and the entry-point URL extractor dropped it.
RFC 3986 schemes are case-insensitive, and Android's Uri preserves the
sender's casing, so the browser has to normalize at each comparison.
Root cause: a dozen independent case-sensitive checks - startsWith("http"),
a lowercase-only ^(https?://)? regex prefix, Uri scheme set membership -
with no shared helper. Two files (LanPolicy, UrlCleaner) already used
ignoreCase = true, proving case-insensitive was always the intent.
The fix adds BrowserUtils.isWebScheme (for Uri.getScheme values) and
isWebUrl (for raw strings), makes the URL_LIKE regex's scheme prefix
case-insensitive, and routes every gate through them: omnibox
classification and navigation, both WebView clients' scheme sets, the
storage-footprint probe, the shields document/cosmetic gates, the shields
panel host, favicon put, the image pipeline's scheme checks, PWA install
gates, and both entry-point extractHttpUrl implementations (whose match
regex also gains IGNORE_CASE). Replacements are behaviour-identical for
lowercase input; the stricter :// form only excludes strings that were
never valid page URLs.
User-visible effect: pasting or opening an uppercase-scheme link now loads
the page in-browser with shields, favicon and storage tracking intact,
instead of opening a search or an external app.
Verified red-then-green (BrowserUtilsTest uppercase looksLikeUrl/
toNavigationUrl plus a FaviconStore uppercase-scheme put assertion failed
pre-fix, pass post-fix) and a full :web:testDevUnitTest run green (BUILD
SUCCESSFUL).Diff
web/src/main/java/com/vayunmathur/web/MainActivity.kt
diff --git a/web/src/main/java/com/vayunmathur/web/MainActivity.kt b/web/src/main/java/com/vayunmathur/web/MainActivity.ktindex f6ae15a149..67dc76296b 100644--- a/web/src/main/java/com/vayunmathur/web/MainActivity.kt+++ b/web/src/main/java/com/vayunmathur/web/MainActivity.kt@@ -22,6 +22,7 @@ import com.vayunmathur.web.data.RemoteImageFetchers import com.vayunmathur.web.data.TabThumbnailStore import com.vayunmathur.web.data.WebRepository import com.vayunmathur.web.platform.BrowserTab+import com.vayunmathur.web.platform.BrowserUtils import com.vayunmathur.web.platform.shields.ShieldsEngine import com.vayunmathur.web.platform.shields.ShieldsServiceWorkerClient import com.vayunmathur.web.platform.WebViewModel@@ -129,11 +130,11 @@ class MainActivity : MaActivity() { private fun extractHttpUrl(text: String): String? { val trimmed = text.trim()- if (trimmed.startsWith("http://") || trimmed.startsWith("https://")) {- val match = Regex("https?://\\S+").find(trimmed)+ if (BrowserUtils.isWebUrl(trimmed)) {+ val match = Regex("https?://\\S+", RegexOption.IGNORE_CASE).find(trimmed) return match?.value ?: trimmed.substringBefore(" ") }- Regex("https?://\\S+").find(trimmed)?.let { return it.value }+ Regex("https?://\\S+", RegexOption.IGNORE_CASE).find(trimmed)?.let { return it.value } return null }
web/src/main/java/com/vayunmathur/web/data/FaviconStore.kt
diff --git a/web/src/main/java/com/vayunmathur/web/data/FaviconStore.kt b/web/src/main/java/com/vayunmathur/web/data/FaviconStore.ktindex ad27c22e13..75f6c78565 100644--- a/web/src/main/java/com/vayunmathur/web/data/FaviconStore.kt+++ b/web/src/main/java/com/vayunmathur/web/data/FaviconStore.kt@@ -75,7 +75,7 @@ object FaviconStore { * current session sees it, then downscaled and written off the main thread. */ fun put(url: String, icon: Bitmap, isPrivate: Boolean) {- if (!url.startsWith("http")) return+ if (!BrowserUtils.isWebUrl(url)) return val host = BrowserUtils.hostFromUrl(url).lowercase() if (host.isBlank()) return cache[host] = icon
web/src/main/java/com/vayunmathur/web/data/RemoteImageFetchers.kt
diff --git a/web/src/main/java/com/vayunmathur/web/data/RemoteImageFetchers.kt b/web/src/main/java/com/vayunmathur/web/data/RemoteImageFetchers.ktindex 0d32d53b30..0ee4d2d5a9 100644--- a/web/src/main/java/com/vayunmathur/web/data/RemoteImageFetchers.kt+++ b/web/src/main/java/com/vayunmathur/web/data/RemoteImageFetchers.kt@@ -4,6 +4,7 @@ import android.content.Context import android.net.Uri import com.vayunmathur.library.image.fetchers.FetchResult import com.vayunmathur.library.network.NetworkClient+import com.vayunmathur.web.platform.BrowserUtils import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext @@ -24,11 +25,11 @@ object RemoteImageFetchers { is String -> data is Uri -> { val s = data.scheme- if (s == "http" || s == "https") data.toString() else return null+ if (BrowserUtils.isWebScheme(s)) data.toString() else return null } else -> return null }- if (!url.startsWith("http://") && !url.startsWith("https://")) return null+ if (!BrowserUtils.isWebUrl(url)) return null return withContext(Dispatchers.IO) { val resp = NetworkClient.execute(url) if (!resp.isSuccess) {
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 9a23c6ffa9..76675611e1 100644--- a/web/src/main/java/com/vayunmathur/web/platform/BrowserState.kt+++ b/web/src/main/java/com/vayunmathur/web/platform/BrowserState.kt@@ -114,9 +114,23 @@ object BrowserUtils { // `kotlin` - into a failed navigation. `nas.local` and `192.168.1.1:8080` already match, // and `http://router` works through the explicit-scheme path below. private val URL_LIKE = Regex(- "^(https?://)?([a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}|localhost|\\d{1,3}(\\.\\d{1,3}){3})(:\\d+)?(/.*)?$"+ // (?i): schemes are case-insensitive per RFC 3986, and pasted links, intents and+ // redirects can carry `HTTP://`. Host labels were already A–Z-tolerant; this also+ // lets `LOCALHOST` match, which DNS treats the same as `localhost`.+ "^(?i)(https?://)?([a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}|localhost|\\d{1,3}(\\.\\d{1,3}){3})(:\\d+)?(/.*)?$" ) + /**+ * Case-insensitive `http(s)` scheme test for [Uri.getScheme] values: `Uri` preserves the+ * sender's casing, so `HTTP://` links must not fall through to the external-app path.+ */+ fun isWebScheme(scheme: String?): Boolean =+ scheme.equals("http", ignoreCase = true) || scheme.equals("https", ignoreCase = true)++ /** Case-insensitive `http(s)://` prefix test for raw URL strings. */+ fun isWebUrl(url: String): Boolean =+ url.startsWith("http://", ignoreCase = true) || url.startsWith("https://", ignoreCase = true)+ /** Kept for migration only — new tabs use "" and search uses selected engine. */ const val HOMEPAGE = "https://duckduckgo.com" @@ -126,7 +140,7 @@ object BrowserUtils { fun looksLikeUrl(text: String): Boolean { if (text.contains(" ")) return false val trimmed = text.trim()- if (trimmed.startsWith("http://") || trimmed.startsWith("https://")) return true+ if (isWebUrl(trimmed)) return true return URL_LIKE.matches(trimmed) } @@ -144,7 +158,7 @@ object BrowserUtils { fun toNavigationUrl(input: String, searchEngine: SearchEngine, safeSearch: Boolean): String { val trimmed = input.trim() if (trimmed.isEmpty()) return searchEngine.homepage- if (trimmed.startsWith("http://") || trimmed.startsWith("https://")) return trimmed+ if (isWebUrl(trimmed)) return trimmed if (looksLikeUrl(trimmed)) { // LAN targets are nearly all plain http and have no publicly trusted certificate, // so defaulting them to https would fail before the page ever loaded.
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 377819db3f..0e3bb5d4fd 100644--- a/web/src/main/java/com/vayunmathur/web/platform/PwaActivity.kt+++ b/web/src/main/java/com/vayunmathur/web/platform/PwaActivity.kt@@ -100,11 +100,11 @@ class PwaActivity : ComponentActivity() { private fun extractHttpUrl(text: String): String? { val trimmed = text.trim()- if (trimmed.startsWith("http://") || trimmed.startsWith("https://")) {- val match = Regex("https?://\\S+").find(trimmed)+ if (BrowserUtils.isWebUrl(trimmed)) {+ val match = Regex("https?://\\S+", RegexOption.IGNORE_CASE).find(trimmed) return match?.value ?: trimmed.substringBefore(" ") }- Regex("https?://\\S+").find(trimmed)?.let { return it.value }+ Regex("https?://\\S+", RegexOption.IGNORE_CASE).find(trimmed)?.let { return it.value } return null } }
web/src/main/java/com/vayunmathur/web/platform/PwaBrowser.kt
diff --git a/web/src/main/java/com/vayunmathur/web/platform/PwaBrowser.kt b/web/src/main/java/com/vayunmathur/web/platform/PwaBrowser.ktindex b14f491418..d779cbacff 100644--- a/web/src/main/java/com/vayunmathur/web/platform/PwaBrowser.kt+++ b/web/src/main/java/com/vayunmathur/web/platform/PwaBrowser.kt@@ -363,7 +363,7 @@ private fun createPwaWebViewClient( ) { override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean { val scheme = request.url.scheme ?: return false- if (scheme !in setOf("http", "https", "about", "data", "blob", "javascript")) {+ if (scheme !in setOf("about", "data", "blob", "javascript") && !BrowserUtils.isWebScheme(scheme)) { return openExternalUri(ctx, request.url.toString()) { fallback -> view.loadUrl(fallback) } } // For PWA: stay inside same origin; external origins still load but that is okay.
web/src/main/java/com/vayunmathur/web/platform/WebViewModelTabs.kt
diff --git a/web/src/main/java/com/vayunmathur/web/platform/WebViewModelTabs.kt b/web/src/main/java/com/vayunmathur/web/platform/WebViewModelTabs.ktindex a413092109..3ff5784d22 100644--- a/web/src/main/java/com/vayunmathur/web/platform/WebViewModelTabs.kt+++ b/web/src/main/java/com/vayunmathur/web/platform/WebViewModelTabs.kt@@ -25,7 +25,7 @@ fun WebViewModel.onTabUrlChange(tabId: String, url: String) { // A native search tab shows its query, not its search:// pseudo-URL. omniboxText = displayTextForUrl(url) }- if (url.startsWith("http")) {+ if (BrowserUtils.isWebUrl(url)) { val origin = BrowserUtils.originFromUrl(url) val host = BrowserUtils.hostFromUrl(url) scope.launch {@@ -148,7 +148,7 @@ fun WebViewModel.navigateActiveTab(input: String) { val engine = SearchEngine.effective(searchEngine) if (engine == SearchEngine.SELF_HOSTED) { val trimmed = input.trim()- val isUrl = trimmed.startsWith("http://") || trimmed.startsWith("https://") ||+ val isUrl = BrowserUtils.isWebUrl(trimmed) || BrowserUtils.looksLikeUrl(trimmed) if (trimmed.isNotEmpty() && !isUrl) { val dest = nativeSearchUrl(trimmed)
web/src/main/java/com/vayunmathur/web/platform/shields/ShieldsWebViewClient.kt
diff --git a/web/src/main/java/com/vayunmathur/web/platform/shields/ShieldsWebViewClient.kt b/web/src/main/java/com/vayunmathur/web/platform/shields/ShieldsWebViewClient.ktindex 53aa32ab84..9e7125d134 100644--- a/web/src/main/java/com/vayunmathur/web/platform/shields/ShieldsWebViewClient.kt+++ b/web/src/main/java/com/vayunmathur/web/platform/shields/ShieldsWebViewClient.kt@@ -17,6 +17,7 @@ import com.vayunmathur.web.domain.EffectiveShields import com.vayunmathur.web.domain.shields.ResourceTypes import com.vayunmathur.web.domain.shields.UrlCleaner import com.vayunmathur.web.platform.AdultSites+import com.vayunmathur.web.platform.BrowserUtils import com.vayunmathur.web.platform.ContentFilters import kotlinx.serialization.Serializable import kotlinx.serialization.json.Json@@ -179,7 +180,7 @@ open class ShieldsWebViewClient( * only cosmetic, so evaluating them at page start is soon enough. */ private fun applyShields(view: WebView, url: String) {- if (!url.startsWith("http")) return+ if (!BrowserUtils.isWebUrl(url)) return val shields = shieldsFor(hostOf(url)) if (!shields.cosmeticFiltering) return
web/src/main/java/com/vayunmathur/web/ui/BrowserMenu.kt
diff --git a/web/src/main/java/com/vayunmathur/web/ui/BrowserMenu.kt b/web/src/main/java/com/vayunmathur/web/ui/BrowserMenu.ktindex aac63c3bc7..2f24f04f52 100644--- a/web/src/main/java/com/vayunmathur/web/ui/BrowserMenu.kt+++ b/web/src/main/java/com/vayunmathur/web/ui/BrowserMenu.kt@@ -120,7 +120,7 @@ internal fun BrowserMenu( onDismiss() onShowInstallDialog() },- enabled = activeTab.url.isNotBlank() && activeTab.url.startsWith("http") && pinSupported+ enabled = activeTab.url.isNotBlank() && BrowserUtils.isWebUrl(activeTab.url) && pinSupported ) } DropdownMenuItem(@@ -212,7 +212,7 @@ internal fun InstallAppDialog( onClick = { val finalTitle = draftTitle.ifBlank { defaultTitle } onDismiss()- if (tabId != null && url.startsWith("http")) {+ if (tabId != null && BrowserUtils.isWebUrl(url)) { viewModel.installAsPwa( tabId = tabId, url = url,
web/src/main/java/com/vayunmathur/web/ui/BrowserPage.kt
diff --git a/web/src/main/java/com/vayunmathur/web/ui/BrowserPage.kt b/web/src/main/java/com/vayunmathur/web/ui/BrowserPage.ktindex 58a256be82..6c5d4798cd 100644--- a/web/src/main/java/com/vayunmathur/web/ui/BrowserPage.kt+++ b/web/src/main/java/com/vayunmathur/web/ui/BrowserPage.kt@@ -72,7 +72,7 @@ fun BrowserPage( val history by viewModel.history.collectAsStateWithLifecycle() val isCurrentBookmarked = activeTab?.url?.let { url -> url.isNotBlank() && bookmarks.any { it.url == url } } ?: false - val shieldHost = activeTab?.url?.takeIf { it.startsWith("http") }?.let { BrowserUtils.hostFromUrl(it) }+ val shieldHost = activeTab?.url?.takeIf { BrowserUtils.isWebUrl(it) }?.let { BrowserUtils.hostFromUrl(it) } // Navigating away from the site the panel describes has to close it, otherwise it would // silently start editing a different host's settings. LaunchedEffect(shieldHost) { viewModel.showShieldsPanel = false }
web/src/main/java/com/vayunmathur/web/ui/WebViewClients.kt
diff --git a/web/src/main/java/com/vayunmathur/web/ui/WebViewClients.kt b/web/src/main/java/com/vayunmathur/web/ui/WebViewClients.ktindex 834137fb3e..b35b702bbe 100644--- a/web/src/main/java/com/vayunmathur/web/ui/WebViewClients.kt+++ b/web/src/main/java/com/vayunmathur/web/ui/WebViewClients.kt@@ -70,7 +70,7 @@ internal fun createBrowserWebViewClient( ) { override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean { val scheme = request.url.scheme ?: return false- if (scheme !in setOf("http", "https", "about", "data", "blob", "javascript")) {+ if (scheme !in setOf("about", "data", "blob", "javascript") && !BrowserUtils.isWebScheme(scheme)) { // Swallowed rather than passed on when the tab is only being // restored: the page still renders, it just doesn't throw the // user back into the app they last left.@@ -107,7 +107,7 @@ internal fun createBrowserWebViewClient( viewModel.recordHistoryVisit(url ?: "", title) } url?.let { u ->- if (u.startsWith("http")) {+ if (BrowserUtils.isWebUrl(u)) { runCatching { val origin = BrowserUtils.originFromUrl(u) val cookies = CookieManager.getInstance().getCookie(u)
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 131b75b9b0..806e87a007 100644--- a/web/src/test/java/com/vayunmathur/web/platform/BrowserUtilsTest.kt+++ b/web/src/test/java/com/vayunmathur/web/platform/BrowserUtilsTest.kt@@ -38,4 +38,16 @@ class BrowserUtilsTest { assertTrue(BrowserUtils.looksLikeUrl("nas.local")) assertTrue(BrowserUtils.looksLikeUrl("192.168.1.1")) }++ @Test+ fun `uppercase scheme is still a URL`() {+ assertTrue(BrowserUtils.looksLikeUrl("HTTP://example.com/"))+ assertTrue(BrowserUtils.looksLikeUrl("Https://Example.COM/a?b=1"))+ }++ @Test+ fun `uppercase explicit scheme navigates verbatim`() {+ assertEquals("HTTP://example.com/", BrowserUtils.toNavigationUrl("HTTP://example.com/"))+ assertEquals("HTTPS://example.com/a", BrowserUtils.toNavigationUrl("HTTPS://example.com/a"))+ } }
web/src/test/java/com/vayunmathur/web/platform/StoresAndSettingsHelpersTest.kt
diff --git a/web/src/test/java/com/vayunmathur/web/platform/StoresAndSettingsHelpersTest.kt b/web/src/test/java/com/vayunmathur/web/platform/StoresAndSettingsHelpersTest.ktindex 896520dae1..35899936d9 100644--- a/web/src/test/java/com/vayunmathur/web/platform/StoresAndSettingsHelpersTest.kt+++ b/web/src/test/java/com/vayunmathur/web/platform/StoresAndSettingsHelpersTest.kt@@ -63,6 +63,13 @@ class StoresAndSettingsHelpersTest { assertNull(FaviconStore.forHost("data")) } + @Test fun faviconStoreAcceptsUppercaseHttpScheme() {+ FaviconStore.init(context)+ val bmp = Bitmap.createBitmap(16, 16, Bitmap.Config.ARGB_8888)+ FaviconStore.put("HTTP://upper-scheme.example/page", bmp, isPrivate = true)+ assertEquals(bmp, FaviconStore.forUrl("HTTP://upper-scheme.example/other"))+ }+ @Test fun thumbnailStoreRejectsUnsafeIdsAndMisses() { TabThumbnailStore.init(context) assertNull(TabThumbnailStore.get("tab-missing-1"))