Modern Apps git

← Modern-Apps.git

web: leave fragment query strings alone in UrlCleaner

Commit
c628781bde7b3ab9ebf5ed5a7557e75a6a2c3469
Author
Vayun Mathur <[email protected]>
When
2026-11-08 03:22 UTC (28 days ago)
Parents
c8a5b68
UrlCleaner.clean located the query with url.indexOf('?') and the fragment
with url.indexOf('#', queryStart), so a '?' that lives inside the fragment
was treated as the start of a real query. On hash-routed sites the fragment
carries app state (e.g. app.example/#/item/123?utm_source=x&tab=reviews):
clean stripped the tracking-looking names out of the fragment and, when only
tracking-looking names were present, deleted the fragment query entirely
(app.example/#/p?utm_medium=x became app.example/#/p). Because clean feeds
the main-frame rewrite path in ShieldsWebViewClient, every navigation to
such a URL silently corrupted the page's own state.

Fix: locate the fragment first and return the URL untouched when the first
'?' sits at or after the '#'. Pure early return; the normal query-before-
fragment path is byte-identical to before.

Verified with two red-then-green tests (fragment with mixed params returned
by identity, fragment-only tracking params kept) plus the full
:web:testDevUnitTest suite: 328 tests, 0 failures, 0 errors. (One full-suite
run showed a single unrelated WebPagesTest flake, 'connection pool has been
closed'; WebPagesTest passes 14/14 in isolation and the suite re-run is
fully green.)

Diff

web/src/main/java/com/vayunmathur/web/domain/shields/UrlCleaner.kt
diff --git a/web/src/main/java/com/vayunmathur/web/domain/shields/UrlCleaner.kt b/web/src/main/java/com/vayunmathur/web/domain/shields/UrlCleaner.ktindex d5f29806cb..a06cf294ec 100644--- a/web/src/main/java/com/vayunmathur/web/domain/shields/UrlCleaner.kt+++ b/web/src/main/java/com/vayunmathur/web/domain/shields/UrlCleaner.kt@@ -43,10 +43,12 @@ object UrlCleaner {      * can compare by identity to decide whether a reload is warranted.      */     fun clean(url: String): String {+        val fragmentStart = url.indexOf('#')         val queryStart = url.indexOf('?')-        if (queryStart < 0) return url+        // A '?' at or after the '#' is fragment content (hash-routed app state), not a+        // query: stripping it would corrupt the page's own state on every navigation.+        if (queryStart < 0 || (fragmentStart >= 0 && queryStart > fragmentStart)) return url -        val fragmentStart = url.indexOf('#', queryStart)         val query = if (fragmentStart < 0) {             url.substring(queryStart + 1)         } else {
web/src/test/java/com/vayunmathur/web/domain/shields/UrlCleanerTest.kt
diff --git a/web/src/test/java/com/vayunmathur/web/domain/shields/UrlCleanerTest.kt b/web/src/test/java/com/vayunmathur/web/domain/shields/UrlCleanerTest.ktindex 2e0c091e29..a467d4eb4f 100644--- a/web/src/test/java/com/vayunmathur/web/domain/shields/UrlCleanerTest.kt+++ b/web/src/test/java/com/vayunmathur/web/domain/shields/UrlCleanerTest.kt@@ -31,6 +31,20 @@ class UrlCleanerTest {         )     } +    @Test+    fun `leaves a question mark inside the fragment alone`() {+        // Hash-routed app state: the '?' is fragment content, not a query, so even+        // tracking-looking names in it must survive.+        val url = "https://app.example/#/item/123?utm_source=x&tab=reviews"+        assertSame(url, UrlCleaner.clean(url))+    }++    @Test+    fun `keeps a fragment that is only tracking-looking parameters`() {+        val url = "https://app.example/#/p?utm_medium=x"+        assertSame(url, UrlCleaner.clean(url))+    }+     @Test     fun `preserves order and repeated keys`() {         assertEquals(