Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(auth): Prevent attempting to read backed up EncryptedSharedPreferences #2113

Merged
merged 2 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,81 @@ import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV
import androidx.security.crypto.EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
import androidx.security.crypto.MasterKeys
import java.io.File
import java.util.UUID

internal class EncryptedKeyValueRepository(
private val context: Context,
private val sharedPreferencesName: String,
) : KeyValueRepository {

@VisibleForTesting
internal val sharedPreferences: SharedPreferences by lazy {
EncryptedSharedPreferences.create(
"$sharedPreferencesName.${getInstallationIdentifier(context, sharedPreferencesName)}",
MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
context,
AES256_SIV,
AES256_GCM
)
}

@VisibleForTesting
internal val editor: SharedPreferences.Editor by lazy {
sharedPreferences.edit()
}

override fun put(dataKey: String, value: String?) {
with(getSharedPreferences().edit()) {
with(editor) {
putString(dataKey, value)
apply()
}
}

override fun get(dataKey: String): String? = getSharedPreferences().getString(dataKey, null)
override fun get(dataKey: String): String? = sharedPreferences.getString(dataKey, null)

override fun remove(dataKey: String) {
with(getSharedPreferences().edit()) {
with(editor) {
remove(dataKey)
apply()
}
}

@VisibleForTesting
fun getSharedPreferences(): SharedPreferences {
return EncryptedSharedPreferences.create(
sharedPreferencesName,
MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
context,
AES256_SIV,
AES256_GCM
)
/**
* EncryptedSharedPreferences may have been backed up by the application, but will be unreadable due to the
* KeyStore record being lost. To prevent an unreadable EncryptedSharedPreferences, we append a suffix to the name
* with a UUID created in the noBackupFilesDir
*/
@Synchronized
private fun getInstallationIdentifier(context: Context, keyValueRepoID: String): String {
val identifierFile = File(context.noBackupFilesDir, "$keyValueRepoID.installationIdentifier")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚢

val previousIdentifier = getExistingInstallationIdentifier(identifierFile)

return previousIdentifier ?: createInstallationIdentifier(identifierFile)
}

/**
* Gets the existing installation identifier (if exists)
*/
private fun getExistingInstallationIdentifier(identifierFile: File): String? {
return if (identifierFile.exists()) {
val identifier = identifierFile.readText()
identifier.ifBlank { null }
} else {
null
}
}

/**
* Creates a new installation identifier for the install
*/
private fun createInstallationIdentifier(identifierFile: File): String {
val newIdentifier = UUID.randomUUID().toString()
try {
identifierFile.writeText(newIdentifier)
} catch (e: Exception) {
// Failed to write identifier to file, session will be forced to be in memory
tylerjroach marked this conversation as resolved.
Show resolved Hide resolved
}
return newIdentifier
tylerjroach marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class EncryptedKeyValueRepositoryTest {

@Before
fun setup() {
Mockito.`when`(repository.getSharedPreferences()).thenReturn(mockPrefs)
Mockito.`when`(mockPrefs.edit()).thenReturn(mockPrefsEditor)
Mockito.`when`(repository.sharedPreferences).thenReturn(mockPrefs)
Mockito.`when`(repository.editor).thenReturn(mockPrefsEditor)
}

@Test
Expand Down