web: gate history on the owning tab's privacy flag
- Commit
902d2dc23f1e8db53bf70e5ef71adb3875959a1a- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-08 03:28 UTC (28 days ago)
- Parents
c628781
recordHistoryVisit decided privacy from activeTab, but its only caller is a per-tab WebViewClient that already knows whether its own tab is private. Background tabs finish loading while another tab is active, so the guard answered for the wrong tab in both directions: a private tab that finished while a normal tab was active had its URL written to history (private- browsing leak, reachable on every session restore with mixed tabs), and a normal background tab that finished while a private tab was active had its visit silently dropped. Fix: recordHistoryVisit takes the owning tab's isPrivateTab flag, which the call site in createBrowserWebViewClient passes through. Single caller, so no other call sites to migrate; the existing history test passes false. Verified red-then-green with two tests (private background visit stays out of history once the public visit lands; normal background visit is recorded while a private tab is active) plus the full :web:testDevUnitTest suite: 330 tests, 0 failures, 0 errors.
Diff
web/src/main/java/com/vayunmathur/web/platform/WebViewModelLibrary.kt
diff --git a/web/src/main/java/com/vayunmathur/web/platform/WebViewModelLibrary.kt b/web/src/main/java/com/vayunmathur/web/platform/WebViewModelLibrary.ktindex e19058ffb5..03c0ee3723 100644--- a/web/src/main/java/com/vayunmathur/web/platform/WebViewModelLibrary.kt+++ b/web/src/main/java/com/vayunmathur/web/platform/WebViewModelLibrary.kt@@ -10,9 +10,11 @@ private const val TAG = "WebViewModel" // ---- History and bookmarks ---- -fun WebViewModel.recordHistoryVisit(url: String, title: String) {+fun WebViewModel.recordHistoryVisit(url: String, title: String, isPrivateTab: Boolean) { if (url.isBlank() || url == "about:blank" || url.startsWith("data:")) return- if (activeTab?.isPrivate == true) return+ // The owning tab's privacy flag, not the active tab's: background tabs finish+ // loading while another tab is active, and the active tab may differ.+ if (isPrivateTab) return scope.launch { runCatching { repository.history.upsert(HistoryEntry(url = url, title = title)) } .onFailure { Log.error(TAG, "recordHistory", it) }
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 b35b702bbe..93c24ada94 100644--- a/web/src/main/java/com/vayunmathur/web/ui/WebViewClients.kt+++ b/web/src/main/java/com/vayunmathur/web/ui/WebViewClients.kt@@ -104,7 +104,7 @@ internal fun createBrowserWebViewClient( val title = view.title?.takeIf { it.isNotBlank() } ?: url ?: "" if (title.isNotBlank()) { viewModel.onTabTitleChange(tabId, title)- viewModel.recordHistoryVisit(url ?: "", title)+ viewModel.recordHistoryVisit(url ?: "", title, isPrivateTab) } url?.let { u -> if (BrowserUtils.isWebUrl(u)) {
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 8a5bfe38e0..e72ffca9e5 100644--- a/web/src/test/java/com/vayunmathur/web/platform/WebViewModelTest.kt+++ b/web/src/test/java/com/vayunmathur/web/platform/WebViewModelTest.kt@@ -242,9 +242,9 @@ class WebViewModelTest { @Test fun historyBookmarkAndDownloadFlowsReachRepository() { val v = vm(incognito = false) v.newTab(url = "https://hist.example")- v.recordHistoryVisit("https://hist.example/page", "Hist")- v.recordHistoryVisit("", "blank")- v.recordHistoryVisit("about:blank", "blank")+ v.recordHistoryVisit("https://hist.example/page", "Hist", isPrivateTab = false)+ v.recordHistoryVisit("", "blank", isPrivateTab = false)+ v.recordHistoryVisit("about:blank", "blank", isPrivateTab = false) v.addBookmark("https://bm.example", "BM") v.createFolder("Folder1") v.addDownload("https://dl.example/f.txt", "f.txt", "text/plain", 12)@@ -261,6 +261,32 @@ class WebViewModelTest { awaitCondition("downloads cleared") { v.downloads.value.isEmpty() } } + @Test fun historyVisitUsesOwningTabPrivacyNotActiveTab() {+ val v = vm(incognito = false)+ v.newTab(url = "https://public.example/")+ v.newTab(url = "https://secret.example/", isPrivate = true, makeActive = false)+ // The private tab finishes loading while the normal tab stays active.+ assertEquals("https://public.example/", v.activeTab?.url)+ v.recordHistoryVisit("https://secret.example/", "Secret", isPrivateTab = true)+ v.recordHistoryVisit("https://public.example/", "Public", isPrivateTab = false)+ awaitCondition("public visit recorded") { v.history.value.any { it.url == "https://public.example/" } }+ assertTrue(+ v.history.value.none { it.url == "https://secret.example/" },+ "private tab visit leaked into history while another tab was active",+ )+ }++ @Test fun historyVisitRecordsBackgroundNormalTabWhilePrivateTabActive() {+ val v = vm(incognito = false)+ v.newTab(url = "https://public.example/", makeActive = false)+ v.newTab(url = "https://secret.example/", isPrivate = true)+ assertTrue(v.activeTab?.isPrivate == true)+ v.recordHistoryVisit("https://public.example/", "Public", isPrivateTab = false)+ awaitCondition("background normal visit recorded") {+ v.history.value.any { it.url == "https://public.example/" }+ }+ }+ @Test fun folderDeletionRemovesItsBookmarks() { val v = vm(incognito = false) v.createFolder("ToDelete")