AssistStructureParser.kt

13.0 KB · raw

1package com.peachpasswords.autofill23import android.app.assist.AssistStructure4import android.text.InputType5import android.view.View6import android.view.autofill.AutofillId78/**9* The autofillable fields discovered in one [AssistStructure], plus the origin10* Android authenticated for ranking vault entries.11 *12 * [webDomain] is untrusted form metadata. The service may use it only after13 * verifying the framework-authenticated package and its signing identity.14 * [originDomains]/[appTitle] are populated by the service, not the parser.15*/16data class ParsedStructure(17    val usernameId: AutofillId? = null,18    val emailId: AutofillId? = null,19    val passwordId: AutofillId? = null,20    val otpId: AutofillId? = null,21    /** Split single-character OTP boxes in traversal order (browser A6 parity). */22    val otpDigitBoxIds: List<AutofillId> = emptyList(),23    val usernameValue: String? = null,24    val emailValue: String? = null,25    val passwordValue: String? = null,26    val otpValue: String? = null,27    val passwordPolicy: PasswordPolicy? = null,28    val isNewPassword: Boolean = false,29    val webDomain: String? = null,30    val packageName: String? = null,31    val appTitle: String? = null,32    val originDomains: List<String> = emptyList(),33    val cardNumberId: AutofillId? = null,34    val cardExpMonthId: AutofillId? = null,35    val cardExpYearId: AutofillId? = null,36    val cardExpDateId: AutofillId? = null,37    val cardCvcId: AutofillId? = null,38    val cardNameId: AutofillId? = null,39) {40    /** The id we put the account identifier into — email preferred, else username. */41    val identityId: AutofillId? get() = emailId ?: usernameId42    /**43     * Forms that earn an autofill response: a password field (login/signup),44     * an OTP field (2FA entry), or card fields. Bare text/identity fields —45     * chat composers, search boxes — never get a suggestion (the Discord46     * message-input false positive).47     */48    val hasCredentialField: Boolean get() = passwordId != null || otpId != null || otpDigitBoxIds.isNotEmpty() || hasCardField49    val hasCardField: Boolean get() = cardNumberId != null || cardExpMonthId != null ||50        cardExpYearId != null || cardExpDateId != null || cardCvcId != null || cardNameId != null51    /** Best label to rank entries against. */52    val origin: String? get() = webDomain ?: packageName53}5455/**56 * Walks an [AssistStructure] to classify username/email/password/OTP/card fields.57 * Mirrors the browser content-script's field detection: trust explicit autofill58 * hints first, then fall back to input-type and id/hint keyword heuristics so59 * unannotated login forms still work.60 */61object AssistStructureParser {6263    fun parse(structure: AssistStructure): ParsedStructure {64        // View-node package ids are supplied by the foreground app; the framework-owned65        // activity component is the authenticated caller identity.66        var result = ParsedStructure(67            packageName = authenticatedPackageName(structure.activityComponent?.packageName),68        )69        val windows = structure.windowNodeCount70        for (i in 0 until windows) {71            val root = structure.getWindowNodeAt(i).rootViewNode72            result = traverse(root, result)73        }74        // Browser parity: a split-OTP group is 4-10 single-character boxes.75        if (result.otpDigitBoxIds.size !in 4..10) {76            result = result.copy(otpDigitBoxIds = emptyList())77        }78        return result79    }8081    private fun traverse(node: AssistStructure.ViewNode, acc: ParsedStructure): ParsedStructure {82        var result = acc83        if (result.webDomain == null && !node.webDomain.isNullOrBlank()) {84            result = result.copy(webDomain = node.webDomain)85        }8687        val id = node.autofillId88        if (id != null && isFillable(node)) {89            if (isDigitBoxCandidate(node)) {90                result = result.copy(otpDigitBoxIds = result.otpDigitBoxIds + id)91            } else {92            val value = node.autofillValue?.takeIf { it.isText }?.textValue?.toString()93                ?: node.text?.toString()94            result = when (classify(node)) {95                FieldKind.PASSWORD -> if (result.passwordId == null) {96                    val attrs = htmlAttributes(node)97                    result.copy(98                        passwordId = id,99                        passwordValue = value,100                        passwordPolicy = PasswordPolicy.fromHtmlAttributes(attrs)101                            .takeIf { it.minLength != null || it.maxLength != null },102                        isNewPassword = attrs["autocomplete"]103                            ?.contains("new-password", ignoreCase = true) == true,104                    )105                } else result106                FieldKind.USERNAME -> if (result.usernameId == null) result.copy(usernameId = id, usernameValue = value) else result107                FieldKind.EMAIL -> if (result.emailId == null) result.copy(emailId = id, emailValue = value) else result108                FieldKind.OTP -> if (result.otpId == null) result.copy(otpId = id, otpValue = value) else result109                FieldKind.CARD_NUMBER -> if (result.cardNumberId == null) result.copy(cardNumberId = id) else result110                FieldKind.CARD_EXP_MONTH -> if (result.cardExpMonthId == null) result.copy(cardExpMonthId = id) else result111                FieldKind.CARD_EXP_YEAR -> if (result.cardExpYearId == null) result.copy(cardExpYearId = id) else result112                FieldKind.CARD_EXP_DATE -> if (result.cardExpDateId == null) result.copy(cardExpDateId = id) else result113                FieldKind.CARD_CVC -> if (result.cardCvcId == null) result.copy(cardCvcId = id) else result114                FieldKind.CARD_NAME -> if (result.cardNameId == null) result.copy(cardNameId = id) else result115                FieldKind.NONE -> result116            }117            }118        }119120        for (i in 0 until node.childCount) {121            result = traverse(node.getChildAt(i), result)122        }123        return result124    }125126    private fun isFillable(node: AssistStructure.ViewNode): Boolean =127        node.autofillType == View.AUTOFILL_TYPE_TEXT &&128            node.visibility == View.VISIBLE129130    /**131     * A web field's HTML attributes (minlength/maxlength/pattern/autocomplete…),132     * lower-cased by key. Empty for native views — [ViewNode.getHtmlInfo] is only133     * populated for web content, so policy detection degrades to "no constraint".134     */135    private fun htmlAttributes(node: AssistStructure.ViewNode): Map<String, String> {136        val info = node.htmlInfo ?: return emptyMap()137        val out = mutableMapOf<String, String>()138        for (attr in info.attributes.orEmpty()) {139            val key = attr.first?.lowercase() ?: continue140            val value = attr.second ?: continue141            out[key] = value142        }143        return out144    }145146    /**147     * A single-character input in a split-OTP group (browser's148     * findOTPDigitBoxGroup). Web: maxlength=1 with a text/number/tel type.149     * Native: numeric input type, but only when unlabeled or OTP-labeled —150     * labeled numeric fields (card number, expiry) still go through [classify].151     * The 4..10 group gate in [parse] keeps stray singles out.152     */153    private fun isDigitBoxCandidate(node: AssistStructure.ViewNode): Boolean {154        val attrs = htmlAttributes(node)155        if (attrs.isNotEmpty()) {156            if (attrs["maxlength"] != "1") return false157            return (attrs["type"]?.lowercase() ?: "") in setOf("", "text", "number", "tel")158        }159        if (nonNullHints(node.autofillHints).any { it.equals("smsOTPCode", true) || it.equals("oneTimeCode", true) }) {160            return true161        }162        val type = node.inputType163        if (type and InputType.TYPE_MASK_VARIATION == InputType.TYPE_NUMBER_VARIATION_PASSWORD) return false164        if (type and InputType.TYPE_MASK_CLASS != InputType.TYPE_CLASS_NUMBER) return false165        val haystack = listOfNotNull(node.idEntry, node.hint, node.text?.toString())166            .joinToString(" ").lowercase()167        return haystack.isBlank() ||168            Regex("\b(otp|2fa|mfa|one[- ]?time|verification code|auth code|pin)\b").containsMatchIn(haystack)169    }170171    private enum class FieldKind { USERNAME, EMAIL, PASSWORD, OTP, CARD_NUMBER, CARD_EXP_MONTH, CARD_EXP_YEAR, CARD_EXP_DATE, CARD_CVC, CARD_NAME, NONE }172173    /**174     * Third-party apps supply the autofillHints array and some ship null175     * entries (Facebook Messenger, 2026-08-07 Fold 7 crash loop). Hints are176     * untrusted input: drop nulls so classification never dereferences one.177     */178    internal fun nonNullHints(hints: Array<String?>?): List<String> = hints?.filterNotNull().orEmpty()179180    private fun classify(node: AssistStructure.ViewNode): FieldKind {181        nonNullHints(node.autofillHints).forEach { hint ->182            when (hint.lowercase()) {183                View.AUTOFILL_HINT_PASSWORD, "password" -> return FieldKind.PASSWORD184                View.AUTOFILL_HINT_USERNAME, "username" -> return FieldKind.USERNAME185                View.AUTOFILL_HINT_EMAIL_ADDRESS, "email", "emailaddress" -> return FieldKind.EMAIL186                "otp", "smsotpcode", "2facode",187                "one-time-password", "one_time_password", "totp" -> return FieldKind.OTP188                View.AUTOFILL_HINT_CREDIT_CARD_NUMBER, "creditcardnumber", "ccnumber" -> return FieldKind.CARD_NUMBER189                View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH, "creditcardexpirationmonth" -> return FieldKind.CARD_EXP_MONTH190                View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR, "creditcardexpirationyear" -> return FieldKind.CARD_EXP_YEAR191                View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE, "creditcardexpirationdate" -> return FieldKind.CARD_EXP_DATE192                View.AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE, "creditcardsecuritycode", "ccsecuritycode" -> return FieldKind.CARD_CVC193            }194        }195196        val type = node.inputType197        val isPasswordType = type and InputType.TYPE_MASK_VARIATION == InputType.TYPE_TEXT_VARIATION_PASSWORD ||198            type and InputType.TYPE_MASK_VARIATION == InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD ||199            type and InputType.TYPE_MASK_VARIATION == InputType.TYPE_NUMBER_VARIATION_PASSWORD200        if (isPasswordType) return FieldKind.PASSWORD201202        val haystack = listOfNotNull(node.idEntry, node.hint, node.text?.toString())203            .joinToString(" ").lowercase()204        return when {205            haystack.isBlank() -> FieldKind.NONE206            // OTP/2FA haystack: cover the labels real 2FA forms actually use,207            // not just the textbook ones. The framework only sets "otp" hint208            // when the developer opts in to autofill; most don't, so the field209            // ends up with a generic label and a missed detection. Standalone210            // "code" is intentionally absent -- promo / coupon / discount codes211            // match it too and would mis-fill with a TOTP.212            OTP_HAYSTACK_RX.containsMatchIn(haystack) -> FieldKind.OTP213            haystack.contains("password") || haystack.contains("passwd") || haystack.contains("pwd") -> FieldKind.PASSWORD214            haystack.contains("email") || haystack.contains("e-mail") -> FieldKind.EMAIL215            haystack.contains("user") || haystack.contains("login") || haystack.contains("account") -> FieldKind.USERNAME216            CARD_NUMBER_RX.containsMatchIn(haystack) -> FieldKind.CARD_NUMBER217            CARD_EXP_MONTH_RX.containsMatchIn(haystack) -> FieldKind.CARD_EXP_MONTH218            CARD_EXP_YEAR_RX.containsMatchIn(haystack) -> FieldKind.CARD_EXP_YEAR219            CARD_EXP_DATE_RX.containsMatchIn(haystack) -> FieldKind.CARD_EXP_DATE220            CARD_CVC_RX.containsMatchIn(haystack) -> FieldKind.CARD_CVC221            CARD_NAME_RX.containsMatchIn(haystack) -> FieldKind.CARD_NAME222            else -> FieldKind.NONE223        }224    }225226    /**227     * Heuristic for OTP / 2FA fields. Covers the labels real 2FA forms228     * actually use -- not just the textbook ones. Standalone "code" is229     * intentionally absent (promo / coupon / discount codes would mis-match).230     */231    internal val OTP_HAYSTACK_RX = Regex(232        "\\b(otp|2fa|mfa|one[- ]?time|verification|auth(entication)?|security code|token|confirm)\\b",233    )234235    private val CARD_NUMBER_RX = Regex("\\b(card\\s*number|credit\\s*card|card\\s*no|pan|cc\\s*num)\\b")236    private val CARD_EXP_MONTH_RX = Regex("\\b(exp\\s*month|expiration\\s*month|expiry\\s*month)\\b")237    private val CARD_EXP_YEAR_RX = Regex("\\b(exp\\s*year|expiration\\s*year|expiry\\s*year)\\b")238    private val CARD_EXP_DATE_RX = Regex("\\b(exp\\s*date|expiration\\s*date|expiry\\s*date|exp\\b)")239    private val CARD_CVC_RX = Regex("\\b(cvc|cvv|security\\s*code|card\\s*code)\\b")240    private val CARD_NAME_RX = Regex("\\b(cardholder|card\\s*holder|name\\s*on\\s*card|card\\s*name)\\b")241}242243/**244 * The framework-owned activity component is the caller identity. Never infer245 * the package from a view node or an autofill hint: those are host-controlled246 * form metadata and cannot authenticate an origin.247 */248internal fun authenticatedPackageName(packageName: String?): String? =249    packageName?.takeIf { it.isNotBlank() }