Modern Apps git

← Modern-Apps.git

web: apply tracker-query rewrites to main-frame navigations

Commit
ed0ff148db6c2a36145ccb981446d1416f99b451
Author
Vayun Mathur <[email protected]>
When
2026-11-08 02:28 UTC (28 days ago)
Parents
05b6fca
The main-frame path in ShieldsWebViewClient.rewrite() ran the cosmetic
URL cleaner but never consulted the shields engine, so filter rules with
$removeparam (e.g. ||reddit.com^$doc,removeparam=ref, one of 282 such
rules in the bundled lists) stripped tracking parameters from
subresources yet left them on the address bar and the loaded document.
Reddit-style ref= campaign parameters survived navigation even with
blockTrackers on.

Root cause: shouldOverrideUrlLoading delegates to rewrite(), and rewrite()
only called UrlCleaner.clean before the blockTrackers gate. The engine was
only ever asked about subresources (ShieldsRequestFilter), never the
document itself, even though ShieldsCheck.rewritten is documented as the
rewrite to apply and ResourceTypes.of() already classifies main-frame
requests as document type.

The fix consults ShieldsEngine.check() inside the blockTrackers gate,
after UrlCleaner, and adopts a non-blocked rewritten URL that differs from
the current one. Fail-open throughout: a null check, a blocked verdict, or
an identical rewrite all leave the URL untouched, and the existing
lastRewritten loop guard is unchanged. Subresource consumption of
rewritten stays as-is (WebView shouldInterceptRequest cannot redirect, so
there is nothing to apply there).

User-visible effect: opening a link with a filter-listed tracking
parameter (e.g. Reddit ref=search_posts) now lands on the cleaned URL with
trackers blocked, matching subresource behaviour.

Verified with a red-then-green Robolectric test (ShieldsRemoveParamTest:
failed pre-fix at the shouldOverrideUrlLoading assertion, passes post-fix)
and a full :web:testDevUnitTest run green (BUILD SUCCESSFUL).

Diff

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 9c3f82560d..53aa32ab84 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@@ -14,6 +14,7 @@ import androidx.webkit.WebMessageCompat import androidx.webkit.WebViewCompat import androidx.webkit.WebViewFeature 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.ContentFilters@@ -148,6 +149,15 @@ open class ShieldsWebViewClient(         }         if (shields.blockTrackers) {             result = UrlCleaner.clean(result)+            // The bundled lists carry $removeparam rules for parameters the fixed+            // UrlCleaner list above does not know (e.g. `ref` on reddit.com). The+            // engine only reports a rewrite when the navigation is not blocked, and+            // main-frame blocking stays out of scope โ€” a null check fails open.+            ShieldsEngine.check(result, result, ResourceTypes.of(result, isMainFrame = true, headers = emptyMap()))+                ?.takeIf { !it.blocked }+                ?.rewritten+                ?.takeIf { it != result }+                ?.let { result = it }         }         return if (result == url) null else result     }
web/src/test/java/com/vayunmathur/web/platform/ShieldsRemoveParamTest.kt
diff --git a/web/src/test/java/com/vayunmathur/web/platform/ShieldsRemoveParamTest.kt b/web/src/test/java/com/vayunmathur/web/platform/ShieldsRemoveParamTest.ktnew file mode 100644index 0000000000..49a7676721--- /dev/null+++ b/web/src/test/java/com/vayunmathur/web/platform/ShieldsRemoveParamTest.kt@@ -0,0 +1,73 @@+package com.vayunmathur.web.platform++import android.net.Uri+import android.os.Looper+import android.webkit.WebView+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.web.domain.EffectiveShields+import com.vayunmathur.web.domain.ShieldsSettings+import com.vayunmathur.web.domain.shields.UrlCleaner+import com.vayunmathur.web.platform.shields.ShieldsEngine+import com.vayunmathur.web.platform.shields.ShieldsWebViewClient+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertNotNull+import kotlin.test.assertTrue+import kotlinx.coroutines.runBlocking+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.Shadows.shadowOf++/**+ * The bundled filter lists carry `$removeparam` rules for parameters the fixed+ * [UrlCleaner] list does not know (e.g. `ref` on reddit.com). The engine reports+ * those as [com.vayunmathur.web.platform.shields.ShieldsCheck.rewritten], and the+ * main-frame navigation path must honor them โ€” subresources cannot be redirected+ * from `shouldInterceptRequest`, so the top-level rewrite is the only consumer.+ */+@RunWith(RobolectricTestRunner::class)+class ShieldsRemoveParamTest {++    private val context = ApplicationProvider.getApplicationContext<android.content.Context>()++    private fun request(url: String, mainFrame: Boolean = false): android.webkit.WebResourceRequest =+        object : android.webkit.WebResourceRequest {+            override fun getUrl(): Uri = Uri.parse(url)+            override fun isForMainFrame(): Boolean = mainFrame+            override fun isRedirect(): Boolean = false+            override fun hasGesture(): Boolean = false+            override fun getMethod(): String = "GET"+            override fun getRequestHeaders(): Map<String, String> = emptyMap()+        }++    @Test fun `engine removeparam rewrite reaches main-frame navigation`() {+        runBlocking { ShieldsEngine.load(context) }+        shadowOf(Looper.getMainLooper()).idle()+        if (!ShieldsEngine.ready) return // Fail-open environment: engine contract untestable here.+        val url = "https://www.reddit.com/r/test/?ref=search_posts"+        // UrlCleaner alone does not know `ref` โ€” this pins why the engine must be consulted.+        assertEquals(url, UrlCleaner.clean(url))+        // The engine does rewrite it, without blocking the navigation.+        val check = ShieldsEngine.check(url, url, "document")+        assertNotNull(check, "engine should answer for a document request")+        assertFalse(check.blocked, "removeparam rule must not block the navigation")+        val rewritten = check.rewritten+        assertNotNull(rewritten, "engine should strip the removeparam parameter")+        assertFalse(rewritten.contains("ref="), "rewritten URL must drop the ref parameter")+        // And the client must navigate to the rewritten URL.+        val aggressive = EffectiveShields.resolve(ShieldsSettings.AGGRESSIVE_DEFAULTS)+        var navigated: String? = null+        val client = ShieldsWebViewClient(+            context,+            shieldsFor = { aggressive },+            onNavigate = { _, navigatedUrl -> navigated = navigatedUrl },+        )+        val view = WebView(context)+        assertTrue(+            client.shouldOverrideUrlLoading(view, request(url, mainFrame = true)),+            "client should take over the navigation to apply the rewrite",+        )+        assertEquals(rewritten, navigated)+    }+}