passwords: strip UTF-8 BOM when matching CSV import headers
- Commit
e4a576681cd92ff75ca5c47ecda40a4291540a03- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-08 02:24 UTC (28 days ago)
- Parents
e9ca13b
Excel's 'CSV UTF-8' export and spreadsheet round-trips emit a BOM that lands on the first header cell. Kotlin trim() does not remove U+FEFF, so the first column silently matched nothing: for 1Password-family exports (Title first) every entry fell back to its URL as the name. resolveColumns now strips a leading BOM before matching. resolveColumns/ImportColumns are internal so unit tests can pin the mapping (an end-to-end import cannot run under unit tests: the SQLCipher native library is unavailable there). Regression test bomPrefixedFirstHeaderStillMapsToTitle failed pre-fix (name=-1), full :passwords:testDevUnitTest green post-fix.
Diff
passwords/src/main/java/com/vayunmathur/passwords/platform/PasswordsViewModel.kt
diff --git a/passwords/src/main/java/com/vayunmathur/passwords/platform/PasswordsViewModel.kt b/passwords/src/main/java/com/vayunmathur/passwords/platform/PasswordsViewModel.ktindex 6932224915..6beed4d463 100644--- a/passwords/src/main/java/com/vayunmathur/passwords/platform/PasswordsViewModel.kt+++ b/passwords/src/main/java/com/vayunmathur/passwords/platform/PasswordsViewModel.kt@@ -267,7 +267,8 @@ class PasswordsViewModel( private data class ImportResult(val inserted: Int, val skipped: Int) - private data class ImportColumns(+ // Visible for testing (return type of [resolveColumns]).+ internal data class ImportColumns( val name: Int, val username: Int, val password: Int,@@ -278,9 +279,16 @@ class PasswordsViewModel( val type: Int, ) - private fun resolveColumns(header: List<String>, source: ImportSource): ImportColumns {+ // internal (not private) so unit tests can pin the header mapping: an+ // end-to-end import cannot run under unit tests (the SQLCipher native+ // library is unavailable there), and the BOM regression lives in this mapping.+ internal fun resolveColumns(header: List<String>, source: ImportSource): ImportColumns {+ // A UTF-8 BOM on the file lands on the first header cell, and trim()+ // does not remove it — so strip it before matching, or the first+ // column of BOM'd exports silently maps to nothing.+ val clean = header.map { it.removePrefix("\uFEFF") } fun findCol(vararg names: String): Int =- names.firstNotNullOfOrNull { n -> header.indexOf(n).takeIf { it >= 0 } } ?: -1+ names.firstNotNullOfOrNull { n -> clean.indexOf(n).takeIf { it >= 0 } } ?: -1 return ImportColumns( name = findCol(*source.nameHeaders), username = findCol(*source.usernameHeaders),
passwords/src/test/java/com/vayunmathur/passwords/platform/PasswordsViewModelImportTest.kt
diff --git a/passwords/src/test/java/com/vayunmathur/passwords/platform/PasswordsViewModelImportTest.kt b/passwords/src/test/java/com/vayunmathur/passwords/platform/PasswordsViewModelImportTest.ktindex 6767d63569..6c738b72ba 100644--- a/passwords/src/test/java/com/vayunmathur/passwords/platform/PasswordsViewModelImportTest.kt+++ b/passwords/src/test/java/com/vayunmathur/passwords/platform/PasswordsViewModelImportTest.kt@@ -5,6 +5,7 @@ import android.net.Uri import androidx.test.core.app.ApplicationProvider import com.vayunmathur.passwords.FakeAndroidKeyStore import com.vayunmathur.passwords.data.PasswordRepository+import com.vayunmathur.passwords.domain.ImportSource import org.junit.Before import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner@@ -68,4 +69,21 @@ class PasswordsViewModelImportTest { awaitImportDone(viewModel) assertEquals("Imported 0 rows, skipped 0 rows", viewModel.importMessage.value) }++ @Test fun bomPrefixedFirstHeaderStillMapsToTitle() {+ // Exporters and spreadsheet round-trips (e.g. Excel's "CSV UTF-8")+ // emit a BOM, which lands on the first header cell. 1Password-family+ // exports put Title first: losing it silently renames every entry+ // to its URL. Pinned at the mapping layer because an end-to-end+ // import cannot run under unit tests (SQLCipher native unavailable).+ val viewModel = vm()+ val columns = viewModel.resolveColumns(+ listOf("\uFEFFtitle", "url", "username", "password"),+ ImportSource.ONE_PASSWORD,+ )+ assertEquals(0, columns.name)+ assertEquals(1, columns.url)+ assertEquals(2, columns.username)+ assertEquals(3, columns.password)+ } }