1package com.peachpasswords.autofill23import android.app.assist.AssistStructure4import android.text.InputType5import android.view.View6import android.view.autofill.AutofillId78910111213141516data class ParsedStructure(17 val usernameId: AutofillId? = null,18 val emailId: AutofillId? = null,19 val passwordId: AutofillId? = null,20 val otpId: AutofillId? = null,21 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 41 val identityId: AutofillId? get() = emailId ?: usernameId42 434445464748 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 52 val origin: String? get() = webDomain ?: packageName53}5455565758596061object AssistStructureParser {6263 fun parse(structure: AssistStructure): ParsedStructure {64 65 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 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 131132133134135 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 147148149150151152153 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 174175176177178 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 207 208 209 210 211 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 227228229230231 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}242243244245246247248internal fun authenticatedPackageName(packageName: String?): String? =249 packageName?.takeIf { it.isNotBlank() }