web: clear WebView cookies and DOM storage on per-site data clear
- Commit
c8a5b6807b23799801384f98f918a11334a2e30c- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-08 03:09 UTC (28 days ago)
- Parents
2bd2af9
Per-site 'clear site data' only deleted this app's Room rows (storage + permissions) but left the cookies and DOM storage themselves in the WebView profile. The next visit re-reported the same footprint, so the cleared row came straight back and the user stayed logged in: the button was a placebo. clearSiteData now also runs a per-origin WebView-profile clear alongside the row deletes: each of the origin's cookies is overwritten with an expired empty value (the documented WebView cookie-deletion mechanism) followed by a flush, and the origin's WebStorage quota is dropped. Web-side work runs under runCatching so profile failures on odd devices can't break the DB clear. The clearing logic lives behind a small OriginStateClearer seam (injected into WebViewModel, default WebViewCookieClearer) because Robolectric's CookieManager shadow silently drops expired-cookie writes, which makes the real jar unobservable in unit tests; the test substitutes a recorder and asserts clearSiteData invokes it for the origin. Verified with the full :web:testDevUnitTest suite (326 tests, 0 failures).
Diff
web/src/main/java/com/vayunmathur/web/platform/WebViewModel.kt
diff --git a/web/src/main/java/com/vayunmathur/web/platform/WebViewModel.kt b/web/src/main/java/com/vayunmathur/web/platform/WebViewModel.ktindex 497123fea6..2fb674414e 100644--- a/web/src/main/java/com/vayunmathur/web/platform/WebViewModel.kt+++ b/web/src/main/java/com/vayunmathur/web/platform/WebViewModel.kt@@ -67,6 +67,7 @@ class WebViewModel( * start from being farbled on a site the user turned shields off for. */ initialShieldSettings: List<ShieldSetting> = emptyList(),+ val originStateClearer: OriginStateClearer = WebViewCookieClearer, ) : ViewModel() { companion object {
web/src/main/java/com/vayunmathur/web/platform/WebViewModelSiteData.kt
diff --git a/web/src/main/java/com/vayunmathur/web/platform/WebViewModelSiteData.kt b/web/src/main/java/com/vayunmathur/web/platform/WebViewModelSiteData.ktindex 7050009604..f7c72eebce 100644--- a/web/src/main/java/com/vayunmathur/web/platform/WebViewModelSiteData.kt+++ b/web/src/main/java/com/vayunmathur/web/platform/WebViewModelSiteData.kt@@ -1,6 +1,8 @@ package com.vayunmathur.web.platform import android.net.Uri+import android.webkit.CookieManager+import android.webkit.WebStorage import com.vayunmathur.web.data.DownloadEntry import com.vayunmathur.web.data.StorageInfo import kotlinx.coroutines.launch@@ -65,6 +67,35 @@ fun WebViewModel.clearSiteData(origin: String) { scope.launch { repository.storage.deleteOrigin(origin) repository.permissions.deleteOrigin(origin)+ // The rows above are only this app's bookkeeping: the cookies and DOM storage+ // themselves live in the WebView profile and survive a DB-only clear, so the next+ // visit re-reports the same footprint and the row comes back.+ runCatching { originStateClearer.clear(origin) }+ }+}++/**+ * Clears one origin's live WebView-profile state (cookies, DOM storage quota).+ * Split out from [WebViewModel.clearSiteData] so tests can substitute a fake:+ * Robolectric's CookieManager shadow silently drops expired-cookie writes, so the+ * real jar is unobservable in unit tests, while on a device the expired write below+ * is exactly what deletes the cookie.+ */+/** Public only because [WebViewModel]'s public constructor takes one; prefer the default. */+fun interface OriginStateClearer {+ fun clear(origin: String)+}++/** Default [OriginStateClearer]: expires the origin's cookies and drops its DOM storage quota. */+object WebViewCookieClearer : OriginStateClearer {+ override fun clear(origin: String) {+ val cm = CookieManager.getInstance()+ cm.getCookie(origin)?.split(";")?.forEach { pair ->+ val name = pair.substringBefore("=").trim()+ if (name.isNotEmpty()) cm.setCookie(origin, "$name=; Expires=Thu, 01 Jan 1970 00:00:00 GMT")+ }+ cm.flush()+ WebStorage.getInstance().deleteOrigin(origin) } }
web/src/test/java/com/vayunmathur/web/platform/WebViewModelTest.kt
diff --git a/web/src/test/java/com/vayunmathur/web/platform/WebViewModelTest.kt b/web/src/test/java/com/vayunmathur/web/platform/WebViewModelTest.ktindex 5ad370edea..8a5bfe38e0 100644--- a/web/src/test/java/com/vayunmathur/web/platform/WebViewModelTest.kt+++ b/web/src/test/java/com/vayunmathur/web/platform/WebViewModelTest.kt@@ -340,6 +340,19 @@ class WebViewModelTest { awaitCondition("storage cleared") { v.storageInfos.value.none { it.origin == "https://store.example" } } } + @Test fun siteDataClearAlsoClearsWebViewState() {+ val cleared = mutableListOf<String>()+ val v = WebViewModel(stores, context, incognito = false, originStateClearer = OriginStateClearer { cleared += it })+ createdVms += v+ shadowOf(Looper.getMainLooper()).idle()+ val origin = "https://cookie-clear.example"+ v.updateStorageFootprint(origin, cookieCount = 2, hasLocalStorage = true, hasIndexedDb = false, hasServiceWorker = false, estBytes = 100)+ awaitCondition("storage recorded") { v.storageInfos.value.any { it.origin == origin } }+ v.clearSiteData(origin)+ awaitCondition("origin web state cleared") { origin in cleared }+ awaitCondition("storage cleared") { v.storageInfos.value.none { it.origin == origin } }+ }+ @Test fun shieldsPersistThroughRepository() { val v = vm(incognito = false) v.updateShields(ShieldsSettings(level = ShieldLevel.STANDARD, blockTrackers = false, cosmeticFiltering = true, fingerprintProtection = true, httpsUpgrade = true))