6f3c80a1efb3

fix: MF-7 autofill strict eTLD+1 domain match (MatchResult, PSL-backed, no substring false positives) + trusted-browser origin validation (refuse web autofill from non-browser apps); 'No saved logins' picker on no-match; 5 tests (v2.2.2)

time
2026-06-09 18:27 UTC · 5 months ago
machine
koh2
agent
claude-opus-4-8
session
audit fixes autofill matching
parent
c13f308ba9b0
5 files app/src/main/java/com/peachpasswords/autofill/AutofillEntryPickerActivity.kt+1 −1app/src/main/java/com/peachpasswords/autofill/AutofillResponseBuilder.kt+21 −18app/src/main/java/com/peachpasswords/autofill/PeachAutofillService.kt+17 −4app/src/main/java/com/peachpasswords/autofill/TrustedBrowsers.kt+31 −0app/src/test/java/com/peachpasswords/autofill/AutofillMatchTest.kt+31 −12
app/src/main/java/com/peachpasswords/autofill/AutofillEntryPickerActivity.kt+1 −1
@@ -69,7 +69,7 @@6969         )7070         val unlocked = vault.isUnlocked.value7171         val entries = if (unlocked) {72-            AutofillResponseBuilder.matchEntries(vault.getEntries(), parsed.origin).take(MAX_ROWS)72+            AutofillResponseBuilder.matchEntries(vault.getEntries(), parsed.origin).all.take(MAX_ROWS)7373         } else {7474             emptyList()7575         }

view file

app/src/main/java/com/peachpasswords/autofill/AutofillResponseBuilder.kt+21 −18
@@ -23,27 +23,30 @@2323  */2424 object AutofillResponseBuilder {2525 26-    /** Entries whose domain/title plausibly match the form origin, best first. */27-    fun matchEntries(entries: List<VaultEntry>, origin: String?): List<VaultEntry> {28-        if (origin.isNullOrBlank()) return entries29-        val core = coreLabel(origin)30-        val (hits, rest) = entries.partition { entry ->31-            val hay = listOfNotNull(extractDomain(entry.url ?: ""), entry.url, entry.title)32-                .joinToString(" ").lowercase()33-            core.isNotBlank() && (hay.contains(core) || hay.contains(origin.lowercase()))34-        }35-        return hits + rest26+    /** Max entries shown when there is no web origin to match strictly against. */27+    private const val FALLBACK_LIMIT = 628+29+    /**30+     * Strict origin match (MF-7). [hits] are entries whose registrable domain31+     * (eTLD+1, via the public suffix list) exactly equals the request origin's —32+     * never a loose substring, so `github.com` can't elicit a `notgithub.com`33+     * entry. [rest] is a small fallback shown ONLY when the origin has no34+     * registrable domain (a native package or no origin at all); for a real web35+     * origin it is always empty, so unrelated entries are never offered.36+     */37+    data class MatchResult(val hits: List<VaultEntry>, val rest: List<VaultEntry>) {38+        val all: List<VaultEntry> get() = hits + rest3639     }3740 38-    /** The registrable-ish core token of an origin: "login.github.com" → "github". */39-    private fun coreLabel(origin: String): String {40-        val host = origin.substringAfter("://").substringBefore('/').lowercase()41-        val parts = host.removePrefix("www.").split('.')42-        return when {43-            parts.size >= 2 -> parts[parts.size - 2]44-            parts.isNotEmpty() -> parts[0]45-            else -> host41+    fun matchEntries(entries: List<VaultEntry>, origin: String?): MatchResult {42+        if (origin.isNullOrBlank()) return MatchResult(emptyList(), entries.take(FALLBACK_LIMIT))43+        val originDomain = extractDomain(origin)44+            ?: return MatchResult(emptyList(), entries.take(FALLBACK_LIMIT))45+        val hits = entries.filter { entry ->46+            val entryDomain = extractDomain(entry.url ?: "") ?: return@filter false47+            entryDomain == originDomain4648         }49+        return MatchResult(hits = hits, rest = emptyList())4750     }4851 4952     fun menuRow(context: Context, entry: VaultEntry): RemoteViews {

view file

app/src/main/java/com/peachpasswords/autofill/PeachAutofillService.kt+17 −4
@@ -37,6 +37,14 @@3737         val parsed = AssistStructureParser.parse(structure)3838         if (!parsed.hasUsableField) { callback.onSuccess(null); return }3939 40+        // MF-7: a web origin (webDomain) is only trustworthy from a known browser.41+        // A non-browser app claiming a web domain is trying to elicit credentials it42+        // does not own — refuse to offer anything at all.43+        if (parsed.webDomain != null) {44+            val clientPackage = structure.activityComponent?.packageName ?: parsed.packageName45+            if (!TrustedBrowsers.isTrusted(clientPackage)) { callback.onSuccess(null); return }46+        }47+4048         val inlineSpecs = inlineSpecs(request)4149         val response = FillResponse.Builder()4250         var datasetCount = 0@@ -44,7 +52,7 @@4452         if (vault.isUnlocked.value) {4553             val matches = AutofillResponseBuilder4654                 .matchEntries(vault.getEntries(), parsed.origin)47-                .take(MAX_DATASETS)55+                .all.take(MAX_DATASETS)4856             matches.forEachIndexed { index, entry ->4957                 val spec = inlineSpecs.getOrNull(index)5058                 AutofillResponseBuilder.datasetFor(context, parsed, entry, spec)?.let {@@ -54,9 +62,14 @@5462             }5563         }5664 57-        // Always offer the branded picker as the last row (search / unlock).65+        // Always offer the branded picker as the last row (search / unlock / no-match).66+        val pickerLabel = when {67+            !vault.isUnlocked.value -> "Unlock Peach"68+            datasetCount == 0 -> "No saved logins for this site"69+            else -> "Search Peach…"70+        }5871         runCatching {59-            response.addDataset(pickerDataset(context, parsed, inlineSpecs.getOrNull(datasetCount)))72+            response.addDataset(pickerDataset(context, parsed, pickerLabel, inlineSpecs.getOrNull(datasetCount)))6073             datasetCount++6174         }6275 @@ -71,9 +84,9 @@7184     private fun pickerDataset(7285         context: android.content.Context,7386         parsed: ParsedStructure,87+        label: String,7488         inlineSpec: android.widget.inline.InlinePresentationSpec?,7589     ): android.service.autofill.Dataset {76-        val label = if (vault.isUnlocked.value) "Search Peach…" else "Unlock Peach"7790         val menu = AutofillResponseBuilder.simpleRow(context, label, parsed.origin ?: "")7891         val builder = android.service.autofill.Dataset.Builder(menu)7992         // A placeholder value per field keeps the dataset valid; the real values

view file

app/src/main/java/com/peachpasswords/autofill/TrustedBrowsers.kt+31 −0
@@ -0,0 +1,31 @@1+package com.peachpasswords.autofill2+3+/**4+ * Conservative allowlist of apps permitted to request autofill for a *web* origin5+ * (MF-7). When an autofill request carries a webDomain, the providing app must be a6+ * known browser — otherwise a malicious app could claim `bank.com` and harvest the7+ * credentials it does not own. Native-app autofill (no webDomain) is unaffected.8+ *9+ * [NEEDS EXPANSION] — add browsers as needed; an unknown browser simply gets no10+ * web autofill until it is listed (it can still be used via the manual picker).11+ */12+object TrustedBrowsers {13+    private val KNOWN_BROWSERS = setOf(14+        "com.android.chrome",15+        "com.chrome.beta",16+        "com.chrome.dev",17+        "org.mozilla.firefox",18+        "org.mozilla.firefox_beta",19+        "com.brave.browser",20+        "com.microsoft.emmx",        // Edge21+        "com.opera.browser",22+        "com.opera.mini.native",23+        "com.vivaldi.browser",24+        "com.kiwibrowser.browser",25+        "com.duckduckgo.mobile.android",26+        "com.sec.android.app.sbrowser", // Samsung Internet27+        "com.android.browser",28+    )29+30+    fun isTrusted(packageName: String?): Boolean = packageName in KNOWN_BROWSERS31+}

view file

app/src/test/java/com/peachpasswords/autofill/AutofillMatchTest.kt+31 −12
@@ -2,28 +2,47 @@22 33 import com.peachpasswords.vault.VaultEntry44 import org.junit.Assert.assertEquals5+import org.junit.Assert.assertFalse6+import org.junit.Assert.assertTrue57 import org.junit.Test68 79 class AutofillMatchTest {810     private fun entry(title: String, url: String?) =911         VaultEntry(id = title, title = title, url = url, createdAt = 0, modifiedAt = 0)1012 11-    @Test fun domain_match_ranks_first() {12-        val gh = entry("GitHub", "https://github.com/login")13+    @Test fun strict_domain_match_hits_only_the_matching_site() {14+        val peach = entry("Peach", "https://peachpasswords.com/login")1315         val mail = entry("Fastmail", "https://fastmail.com")14-        val ranked = AutofillResponseBuilder.matchEntries(listOf(mail, gh), "login.github.com")15-        assertEquals("GitHub", ranked.first().title)16+        val result = AutofillResponseBuilder.matchEntries(listOf(mail, peach), "peachpasswords.com")17+        assertEquals(listOf(peach), result.hits)18+        assertTrue("no unrelated entries leak for a web origin", result.rest.isEmpty())1619     }1720 18-    @Test fun subdomain_origin_matches_core_label() {19-        val gh = entry("GitHub", "https://github.com")20-        val ranked = AutofillResponseBuilder.matchEntries(listOf(gh), "https://gist.github.com/")21-        assertEquals("GitHub", ranked.first().title)21+    @Test fun no_false_positive_on_substring_lookalike() {22+        val gh = entry("GitHub", "https://github.com/login")23+        val result = AutofillResponseBuilder.matchEntries(listOf(gh), "notgithub.com")24+        assertTrue("github.com must not match notgithub.com", result.hits.isEmpty())25+        assertTrue(result.rest.isEmpty())2226     }2327 24-    @Test fun no_origin_returns_all_unchanged() {25-        val a = entry("A", null)26-        val b = entry("B", null)27-        assertEquals(listOf(a, b), AutofillResponseBuilder.matchEntries(listOf(a, b), null))28+    @Test fun subdomain_origin_matches_registrable_domain() {29+        val peach = entry("Peach", "https://peachpasswords.com")30+        val result = AutofillResponseBuilder.matchEntries(listOf(peach), "app.peachpasswords.com")31+        assertEquals(listOf(peach), result.hits)32+    }33+34+    @Test fun null_origin_yields_no_hits_only_fallback() {35+        val a = entry("A", "https://a.com")36+        val b = entry("B", "https://b.com")37+        val result = AutofillResponseBuilder.matchEntries(listOf(a, b), null)38+        assertTrue(result.hits.isEmpty())39+        assertEquals(listOf(a, b), result.rest)40+    }41+42+    @Test fun trusted_browser_allowlist() {43+        assertTrue(TrustedBrowsers.isTrusted("com.android.chrome"))44+        assertTrue(TrustedBrowsers.isTrusted("org.mozilla.firefox"))45+        assertFalse(TrustedBrowsers.isTrusted("com.evil.phisher"))46+        assertFalse(TrustedBrowsers.isTrusted(null))2847     }2948 }

view file