Fix autofill overwriting user data with empty field values (#5649)

This commit is contained in:
Igorro 2025-08-19 16:47:31 +03:00 committed by GitHub
parent 44410efe56
commit 20dea9b5ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 20 deletions

View File

@ -11,29 +11,34 @@ import com.x8bit.bitwarden.data.autofill.model.FilledItem
fun AutofillView.buildFilledItemOrNull(
value: String,
): FilledItem? =
when (this.data.autofillType) {
View.AUTOFILL_TYPE_DATE -> {
value
.toLongOrNull()
?.let { AutofillValue.forDate(it) }
}
// Do not try to autofill fields that are empty in the vault
if (value.isEmpty()) {
null
} else {
when (this.data.autofillType) {
View.AUTOFILL_TYPE_DATE -> {
value
.toLongOrNull()
?.let { AutofillValue.forDate(it) }
}
View.AUTOFILL_TYPE_LIST -> this.buildListAutofillValueOrNull(value = value)
View.AUTOFILL_TYPE_TEXT -> AutofillValue.forText(value)
View.AUTOFILL_TYPE_TOGGLE -> {
value
.toBooleanStrictOrNull()
?.let { AutofillValue.forToggle(it) }
}
View.AUTOFILL_TYPE_LIST -> this.buildListAutofillValueOrNull(value = value)
View.AUTOFILL_TYPE_TEXT -> AutofillValue.forText(value)
View.AUTOFILL_TYPE_TOGGLE -> {
value
.toBooleanStrictOrNull()
?.let { AutofillValue.forToggle(it) }
}
else -> null
else -> null
}
?.let { autofillValue ->
FilledItem(
autofillId = this.data.autofillId,
value = autofillValue,
)
}
}
?.let { autofillValue ->
FilledItem(
autofillId = this.data.autofillId,
value = autofillValue,
)
}
/**
* Build a list [AutofillValue] out of [value] or return null if not possible.

View File

@ -329,4 +329,24 @@ class AutofillViewExtensionsTest {
// Verify
assertNull(actual)
}
@Test
fun `buildFilledItemOrNull should return null when value is empty`() {
// Setup
val value = ""
val autofillViewData = autofillViewData.copy(
autofillType = View.AUTOFILL_TYPE_TEXT,
)
val autofillView = AutofillView.Login.Username(
data = autofillViewData,
)
// Test
val actual = autofillView.buildFilledItemOrNull(
value = value,
)
// Verify
assertNull(actual)
}
}