1package com.peachpasswords.autofill23import android.app.assist.AssistStructure4import android.text.InputType5import android.view.View6import android.view.autofill.AutofillId7891011121314151617data class ParsedStructure(18 val usernameId: AutofillId? = null,19 val emailId: AutofillId? = null,20 val passwordId: AutofillId? = null,21 val otpId: AutofillId? = null,22 23 val otpDigitBoxIds: List<AutofillId> = emptyList(),24 val usernameValue: String? = null,25 val emailValue: String? = null,26 val passwordValue: String? = null,27 val otpValue: String? = null,28 val passwordPolicy: PasswordPolicy? = null,29 val isNewPassword: Boolean = false,30 val webDomain: String? = null,31 val packageName: String? = null,32 val appTitle: String? = null,33 val originDomains: List<String> = emptyList(),34 val cardNumberId: AutofillId? = null,35 val cardExpMonthId: AutofillId? = null,36 val cardExpYearId: AutofillId? = null,37 val cardExpDateId: AutofillId? = null,38 val cardCvcId: AutofillId? = null,39 val cardNameId: AutofillId? = null,40) {41 42 val identityId: AutofillId? get() = emailId ?: usernameId43 444546474849 val hasCredentialField: Boolean get() = passwordId != null || otpId != null || otpDigitBoxIds.isNotEmpty() || hasCardField50 val hasCardField: Boolean get() = cardNumberId != null || cardExpMonthId != null ||51 cardExpYearId != null || cardExpDateId != null || cardCvcId != null || cardNameId != null52 53 val origin: String? get() = webDomain ?: packageName54}5556575859606162object AssistStructureParser {6364 fun parse(structure: AssistStructure): ParsedStructure {65 66 67 var result = ParsedStructure(68 packageName = authenticatedPackageName(structure.activityComponent?.packageName),69 )70 val windows = structure.windowNodeCount71 for (i in 0 until windows) {72 val root = structure.getWindowNodeAt(i).rootViewNode73 result = traverse(root, result)74 }75 76 if (result.otpDigitBoxIds.size !in 4..10) {77 result = result.copy(otpDigitBoxIds = emptyList())78 }79 return result80 }8182 private fun traverse(node: AssistStructure.ViewNode, acc: ParsedStructure): ParsedStructure {83 var result = acc84 if (result.webDomain == null && !node.webDomain.isNullOrBlank()) {85 result = result.copy(webDomain = node.webDomain)86 }8788 val id = node.autofillId89 if (id != null && isFillable(node)) {90 if (isDigitBoxCandidate(node)) {91 result = result.copy(otpDigitBoxIds = result.otpDigitBoxIds + id)92 } else {93 val value = node.autofillValue?.takeIf { it.isText }?.textValue?.toString()94 ?: node.text?.toString()95 result = when (classify(node)) {96 FieldKind.PASSWORD -> if (result.passwordId == null) {97 val attrs = htmlAttributes(node)98 result.copy(99 passwordId = id,100 passwordValue = value,101 passwordPolicy = PasswordPolicy.fromHtmlAttributes(attrs)102 .takeIf { it.minLength != null || it.maxLength != null },103 isNewPassword = attrs["autocomplete"]104 ?.contains("new-password", ignoreCase = true) == true,105 )106 } else result107 FieldKind.USERNAME -> if (result.usernameId == null) result.copy(usernameId = id, usernameValue = value) else result108 FieldKind.EMAIL -> if (result.emailId == null) result.copy(emailId = id, emailValue = value) else result109 FieldKind.OTP -> if (result.otpId == null) result.copy(otpId = id, otpValue = value) else result110 FieldKind.CARD_NUMBER -> if (result.cardNumberId == null) result.copy(cardNumberId = id) else result111 FieldKind.CARD_EXP_MONTH -> if (result.cardExpMonthId == null) result.copy(cardExpMonthId = id) else result112 FieldKind.CARD_EXP_YEAR -> if (result.cardExpYearId == null) result.copy(cardExpYearId = id) else result113 FieldKind.CARD_EXP_DATE -> if (result.cardExpDateId == null) result.copy(cardExpDateId = id) else result114 FieldKind.CARD_CVC -> if (result.cardCvcId == null) result.copy(cardCvcId = id) else result115 FieldKind.CARD_NAME -> if (result.cardNameId == null) result.copy(cardNameId = id) else result116 FieldKind.NONE -> result117 }118 }119 }120121 for (i in 0 until node.childCount) {122 result = traverse(node.getChildAt(i), result)123 }124 return result125 }126127 private fun isFillable(node: AssistStructure.ViewNode): Boolean =128 node.autofillType == View.AUTOFILL_TYPE_TEXT &&129 node.visibility == View.VISIBLE130131 132133134135136 private fun htmlAttributes(node: AssistStructure.ViewNode): Map<String, String> {137 val info = node.htmlInfo ?: return emptyMap()138 val out = mutableMapOf<String, String>()139 for (attr in info.attributes.orEmpty()) {140 val key = attr.first?.lowercase() ?: continue141 val value = attr.second ?: continue142 out[key] = value143 }144 return out145 }146147 148149150151152153154 private fun isDigitBoxCandidate(node: AssistStructure.ViewNode): Boolean {155 val attrs = htmlAttributes(node)156 if (attrs.isNotEmpty()) {157 if (attrs["maxlength"] != "1") return false158 return (attrs["type"]?.lowercase() ?: "") in setOf("", "text", "number", "tel")159 }160 if (node.autofillHints?.any { it.equals("smsOTPCode", true) || it.equals("oneTimeCode", true) } == true) {161 return true162 }163 val type = node.inputType164 if (type and InputType.TYPE_MASK_VARIATION == InputType.TYPE_NUMBER_VARIATION_PASSWORD) return false165 if (type and InputType.TYPE_MASK_CLASS != InputType.TYPE_CLASS_NUMBER) return false166 val haystack = listOfNotNull(node.idEntry, node.hint, node.text?.toString())167 .joinToString(" ").lowercase()168 return haystack.isBlank() ||169 Regex("\b(otp|2fa|mfa|one[- ]?time|verification code|auth code|pin)\b").containsMatchIn(haystack)170 }171172 private enum class FieldKind { USERNAME, EMAIL, PASSWORD, OTP, CARD_NUMBER, CARD_EXP_MONTH, CARD_EXP_YEAR, CARD_EXP_DATE, CARD_CVC, CARD_NAME, NONE }173174 private fun classify(node: AssistStructure.ViewNode): FieldKind {175 node.autofillHints?.forEach { hint ->176 when (hint.lowercase()) {177 View.AUTOFILL_HINT_PASSWORD, "password" -> return FieldKind.PASSWORD178 View.AUTOFILL_HINT_USERNAME, "username" -> return FieldKind.USERNAME179 View.AUTOFILL_HINT_EMAIL_ADDRESS, "email", "emailaddress" -> return FieldKind.EMAIL180 "otp", "smsotpcode", "2facode",181 "one-time-password", "one_time_password", "totp" -> return FieldKind.OTP182 View.AUTOFILL_HINT_CREDIT_CARD_NUMBER, "creditcardnumber", "ccnumber" -> return FieldKind.CARD_NUMBER183 View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH, "creditcardexpirationmonth" -> return FieldKind.CARD_EXP_MONTH184 View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR, "creditcardexpirationyear" -> return FieldKind.CARD_EXP_YEAR185 View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE, "creditcardexpirationdate" -> return FieldKind.CARD_EXP_DATE186 View.AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE, "creditcardsecuritycode", "ccsecuritycode" -> return FieldKind.CARD_CVC187 }188 }189190 val type = node.inputType191 val isPasswordType = type and InputType.TYPE_MASK_VARIATION == InputType.TYPE_TEXT_VARIATION_PASSWORD ||192 type and InputType.TYPE_MASK_VARIATION == InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD ||193 type and InputType.TYPE_MASK_VARIATION == InputType.TYPE_NUMBER_VARIATION_PASSWORD194 if (isPasswordType) return FieldKind.PASSWORD195196 val haystack = listOfNotNull(node.idEntry, node.hint, node.text?.toString())197 .joinToString(" ").lowercase()198 return when {199 haystack.isBlank() -> FieldKind.NONE200 201 202 203 204 205 206 OTP_HAYSTACK_RX.containsMatchIn(haystack) -> FieldKind.OTP207 haystack.contains("password") || haystack.contains("passwd") || haystack.contains("pwd") -> FieldKind.PASSWORD208 haystack.contains("email") || haystack.contains("e-mail") -> FieldKind.EMAIL209 haystack.contains("user") || haystack.contains("login") || haystack.contains("account") -> FieldKind.USERNAME210 CARD_NUMBER_RX.containsMatchIn(haystack) -> FieldKind.CARD_NUMBER211 CARD_EXP_MONTH_RX.containsMatchIn(haystack) -> FieldKind.CARD_EXP_MONTH212 CARD_EXP_YEAR_RX.containsMatchIn(haystack) -> FieldKind.CARD_EXP_YEAR213 CARD_EXP_DATE_RX.containsMatchIn(haystack) -> FieldKind.CARD_EXP_DATE214 CARD_CVC_RX.containsMatchIn(haystack) -> FieldKind.CARD_CVC215 CARD_NAME_RX.containsMatchIn(haystack) -> FieldKind.CARD_NAME216 else -> FieldKind.NONE217 }218 }219220 221222223224225 internal val OTP_HAYSTACK_RX = Regex(226 "\\b(otp|2fa|mfa|one[- ]?time|verification|auth(entication)?|security code|token|confirm)\\b",227 )228229 private val CARD_NUMBER_RX = Regex("\\b(card\\s*number|credit\\s*card|card\\s*no|pan|cc\\s*num)\\b")230 private val CARD_EXP_MONTH_RX = Regex("\\b(exp\\s*month|expiration\\s*month|expiry\\s*month)\\b")231 private val CARD_EXP_YEAR_RX = Regex("\\b(exp\\s*year|expiration\\s*year|expiry\\s*year)\\b")232 private val CARD_EXP_DATE_RX = Regex("\\b(exp\\s*date|expiration\\s*date|expiry\\s*date|exp\\b)")233 private val CARD_CVC_RX = Regex("\\b(cvc|cvv|security\\s*code|card\\s*code)\\b")234 private val CARD_NAME_RX = Regex("\\b(cardholder|card\\s*holder|name\\s*on\\s*card|card\\s*name)\\b")235}236237238239240241242internal fun authenticatedPackageName(packageName: String?): String? =243 packageName?.takeIf { it.isNotBlank() }