From 615d016dd5b00afb6fd2792640cf3c83947a6c41 Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Mon, 27 Nov 2023 08:28:34 -0500 Subject: [PATCH] be absolutely sure we delete secure storage keys before writing them (#1182) * be absolutely sure we delete secure storage keys before writing them * sync with other PR --------- Co-authored-by: fossephate Co-authored-by: Omar Hatem --- lib/core/backup_service.dart | 4 ++++ lib/core/key_service.dart | 1 + lib/entities/default_settings_migration.dart | 1 + lib/entities/fs_migration.dart | 1 + lib/entities/get_encryption_key.dart | 4 +++- lib/src/screens/support_chat/widgets/chatwoot_widget.dart | 6 ++++-- lib/view_model/edit_backup_password_view_model.dart | 1 + 7 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/core/backup_service.dart b/lib/core/backup_service.dart index b13a01889..7dd0b50f3 100644 --- a/lib/core/backup_service.dart +++ b/lib/core/backup_service.dart @@ -436,6 +436,7 @@ class BackupService { final backupPasswordKey = generateStoreKeyFor(key: SecretStoreKey.backupPassword); final backupPassword = keychainJSON[backupPasswordKey] as String; + await _flutterSecureStorage.delete(key: backupPasswordKey); await _flutterSecureStorage.write(key: backupPasswordKey, value: backupPassword); keychainWalletsInfo.forEach((dynamic rawInfo) async { @@ -443,6 +444,7 @@ class BackupService { await importWalletKeychainInfo(info); }); + await _flutterSecureStorage.delete(key: pinCodeKey); await _flutterSecureStorage.write(key: pinCodeKey, value: encodedPinCode(pin: decodedPin)); keychainDumpFile.deleteSync(); @@ -462,6 +464,7 @@ class BackupService { final backupPasswordKey = generateStoreKeyFor(key: SecretStoreKey.backupPassword); final backupPassword = keychainJSON[backupPasswordKey] as String; + await _flutterSecureStorage.delete(key: backupPasswordKey); await _flutterSecureStorage.write(key: backupPasswordKey, value: backupPassword); keychainWalletsInfo.forEach((dynamic rawInfo) async { @@ -469,6 +472,7 @@ class BackupService { await importWalletKeychainInfo(info); }); + await _flutterSecureStorage.delete(key: pinCodeKey); await _flutterSecureStorage.write(key: pinCodeKey, value: encodedPinCode(pin: decodedPin)); keychainDumpFile.deleteSync(); diff --git a/lib/core/key_service.dart b/lib/core/key_service.dart index 337f1ef21..fce254ea2 100644 --- a/lib/core/key_service.dart +++ b/lib/core/key_service.dart @@ -19,6 +19,7 @@ class KeyService { key: SecretStoreKey.moneroWalletPassword, walletName: walletName); final encodedPassword = encodeWalletPassword(password: password); + await _secureStorage.delete(key: key); await _secureStorage.write(key: key, value: encodedPassword); } diff --git a/lib/entities/default_settings_migration.dart b/lib/entities/default_settings_migration.dart index 47092c22f..a65742e99 100644 --- a/lib/entities/default_settings_migration.dart +++ b/lib/entities/default_settings_migration.dart @@ -496,6 +496,7 @@ Future generateBackupPassword(FlutterSecureStorage secureStorage) async { } final password = encrypt.Key.fromSecureRandom(32).base16; + await secureStorage.delete(key: key); await secureStorage.write(key: key, value: password); } diff --git a/lib/entities/fs_migration.dart b/lib/entities/fs_migration.dart index e3088ff54..4280949cd 100644 --- a/lib/entities/fs_migration.dart +++ b/lib/entities/fs_migration.dart @@ -147,6 +147,7 @@ Future ios_migrate_pin() async { final key = generateStoreKeyFor(key: SecretStoreKey.pinCodePassword); final encodedPassword = encodedPinCode(pin: pinPassword); + await flutterSecureStorage.delete(key: key); await flutterSecureStorage.write(key: key, value: encodedPassword); await prefs.setBool('ios_migration_pin_completed', true); } diff --git a/lib/entities/get_encryption_key.dart b/lib/entities/get_encryption_key.dart index e67380bb8..a32d4e311 100644 --- a/lib/entities/get_encryption_key.dart +++ b/lib/entities/get_encryption_key.dart @@ -9,7 +9,9 @@ Future> getEncryptionKey( if (stringifiedKey == null) { key = CakeHive.generateSecureKey(); final keyStringified = key.join(','); - await secureStorage.write(key: 'transactionDescriptionsBoxKey', value: keyStringified); + String storageKey = 'transactionDescriptionsBoxKey'; + await secureStorage.delete(key: storageKey); + await secureStorage.write(key: storageKey, value: keyStringified); } else { key = stringifiedKey.split(',').map((i) => int.parse(i)).toList(); } diff --git a/lib/src/screens/support_chat/widgets/chatwoot_widget.dart b/lib/src/screens/support_chat/widgets/chatwoot_widget.dart index d9a5f9bb8..73403d667 100644 --- a/lib/src/screens/support_chat/widgets/chatwoot_widget.dart +++ b/lib/src/screens/support_chat/widgets/chatwoot_widget.dart @@ -57,6 +57,8 @@ class ChatwootWidgetState extends State { return true; } - Future storeCookie(String value) async => - await widget.secureStorage.write(key: COOKIE_KEY, value: value); + Future storeCookie(String value) async { + await widget.secureStorage.delete(key: COOKIE_KEY); + await widget.secureStorage.write(key: COOKIE_KEY, value: value); + } } diff --git a/lib/view_model/edit_backup_password_view_model.dart b/lib/view_model/edit_backup_password_view_model.dart index e7b8ee017..aca76502a 100644 --- a/lib/view_model/edit_backup_password_view_model.dart +++ b/lib/view_model/edit_backup_password_view_model.dart @@ -37,6 +37,7 @@ abstract class EditBackupPasswordViewModelBase with Store { @action Future save() async { final key = generateStoreKeyFor(key: SecretStoreKey.backupPassword); + await secureStorage.delete(key: key); await secureStorage.write(key: key, value: backupPassword); secretStore.write(key: key, value: backupPassword); }