PM-41853: bug: Fix account keys migration for keystore encrypted shared preferences (#7273)

This commit is contained in:
David Perez authored and GitHub committed 2026-08-18 14:24:26 +00:00
1 parent 75dfcfd903
commit caf108deda
3 files changed
+15 -11

No files matched your search

@@ -726,7 +726,7 @@ class AuthDiskSourceImpl(
val userId = account.profile.userId
val accountKeysKey = PROFILE_ACCOUNT_KEYS_KEY.appendIdentifier(userId)
val privateKeyKey = MASTER_KEY_ENCRYPTION_PRIVATE_KEY.appendIdentifier(userId)
val accountKeys = getEncryptedString(key = accountKeysKey)
val accountKeys = getLegacyEncryptedString(key = accountKeysKey)
?.let { json.decodeFromStringOrNull<AccountKeysJson>(it) }
val privateKey = accountKeys
?.publicKeyEncryptionKeyPair
@@ -738,7 +738,7 @@ class AuthDiskSourceImpl(
accountCryptographicState = accountKeys.toAccountCryptographicState(it),
)
// Remove the Account Keys and Private Key
putEncryptedString(key = accountKeysKey, value = null)
clearLegacyEncryptedString(key = accountKeysKey)
putString(key = privateKeyKey, value = null)
}
}
@@ -25,16 +25,12 @@ internal class DiskSourceMigrationLoggerImpl(
init {
flightRecorderManager
.isLoggingReadyFlow
.filter { it }
.onEach {
// The older encrypted shared preferences has its own values written to disk,
// so it is normal for there to be 2 values still present.
if (keystoreEncryptedPreferences.all.isNotEmpty() &&
encryptedPreferences.all.size <= 2
) {
Timber.d("Encrypted data has been migrated to Keystore encryption.")
}
.filter { isLoggingReady ->
isLoggingReady &&
keystoreEncryptedPreferences.all.isNotEmpty() &&
encryptedPreferences.all.isEmpty()
}
.onEach { Timber.d("Encrypted data has been migrated to Keystore encryption.") }
.launchIn(unconfinedScope)
}
}
@@ -16,6 +16,14 @@ abstract class BaseEncryptedDiskSource(
private val encryptedSharedPreferences: SharedPreferences,
private val keystoreEncryptedPreferences: SharedPreferences,
) : BaseDiskSource(sharedPreferences = sharedPreferences) {
protected fun getLegacyEncryptedString(
key: String,
): String? = encryptedSharedPreferences.getString("$LEGACY_PREFIX$key", null)
protected fun clearLegacyEncryptedString(key: String) {
encryptedSharedPreferences.edit { remove("$LEGACY_PREFIX$key") }
}
protected fun getEncryptedString(
key: String,
): String? = keystoreEncryptedPreferences.getString(key, null)