-
Notifications
You must be signed in to change notification settings - Fork 118
Disable backup to prevent unintended data restore on Android #4393
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
Open
dab246
wants to merge
2
commits into
master
Choose a base branch
from
bugfix/disable-backup-to-prevent-unintended-data-restore
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import 'package:flutter_secure_storage/flutter_secure_storage.dart'; | ||
| import 'package:tmail_ui_user/main/utils/app_config.dart'; | ||
|
|
||
| class SecureStorageFactory { | ||
| const SecureStorageFactory._(); | ||
|
|
||
| static FlutterSecureStorage create() { | ||
| return const FlutterSecureStorage( | ||
| iOptions: IOSOptions( | ||
| groupId: AppConfig.iOSKeychainSharingGroupId, | ||
| accountName: AppConfig.iOSKeychainSharingService, | ||
| accessibility: KeychainAccessibility.first_unlock_this_device, | ||
| ), | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| class SecureStorageKeys { | ||
| const SecureStorageKeys._(); | ||
|
|
||
| static const String hiveEncryptionKey = 'hive_encryption_key'; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| import 'dart:convert'; | ||
| import 'dart:io'; | ||
| import 'dart:typed_data'; | ||
|
|
||
| import 'package:core/utils/app_logger.dart'; | ||
| import 'package:hive_ce/hive.dart'; | ||
| import 'package:path_provider/path_provider.dart'; | ||
| import 'package:shared_preferences/shared_preferences.dart'; | ||
| import 'package:tmail_ui_user/features/caching/config/secure_storage_factory.dart'; | ||
| import 'package:tmail_ui_user/features/caching/config/secure_storage_keys.dart'; | ||
|
|
||
| class AppSecurityManager { | ||
| static final instance = AppSecurityManager._(); | ||
|
|
||
| AppSecurityManager._(); | ||
|
|
||
| final _storage = SecureStorageFactory.create(); | ||
|
|
||
| Uint8List? _cachedKey; | ||
|
|
||
| Future<void> init() async { | ||
| log('AppSecurityManager::init: Start initialization'); | ||
|
|
||
| try { | ||
| final storedKey = await _readStoredKey(); | ||
| final hiveExists = await _doesHiveExistSafe(); | ||
|
|
||
| await _handleInconsistentState( | ||
| hiveExists: hiveExists, | ||
| storedKey: storedKey, | ||
| ); | ||
|
|
||
| await _ensureKeyExists(storedKey); | ||
|
|
||
| log('AppSecurityManager::init: Initialization completed'); | ||
| } catch (e) { | ||
| logWarning( | ||
| 'AppSecurityManager::init: Initialization failed, Exception $e', | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| Future<Uint8List?> getKey() async { | ||
| if (_cachedKey != null) { | ||
| log('AppSecurityManager::getKey: Returning cached key'); | ||
| return _cachedKey; | ||
| } | ||
|
|
||
| try { | ||
| final key = await _loadKeySafe(); | ||
| _cachedKey = key; | ||
| return key; | ||
| } catch (e, st) { | ||
| logError( | ||
| 'AppSecurityManager::getKey: Failed to load key', | ||
| exception: e, | ||
| stackTrace: st, | ||
| ); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| void clearKey() { | ||
| log('AppSecurityManager::clearKey: Clearing cached key'); | ||
| _cachedKey = null; | ||
| } | ||
|
|
||
| Future<String?> _readStoredKey() async { | ||
| try { | ||
| final key = await _storage.read( | ||
| key: SecureStorageKeys.hiveEncryptionKey, | ||
| ); | ||
| log('AppSecurityManager::_readStoredKey: Key exists: ${key != null}'); | ||
| return key; | ||
| } catch (e) { | ||
| logWarning( | ||
| 'AppSecurityManager::_readStoredKey: Read failed, Exception $e', | ||
| ); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| Future<void> _handleInconsistentState({ | ||
| required bool hiveExists, | ||
| required String? storedKey, | ||
| }) async { | ||
| if (hiveExists && storedKey == null) { | ||
| logWarning( | ||
| 'AppSecurityManager::_handleInconsistentState: ' | ||
| 'Hive exists but key missing → wiping Hive, SharePreference', | ||
| ); | ||
| await _wipeLocalStorageSafe(); | ||
| } | ||
| } | ||
|
|
||
| Future<void> _ensureKeyExists(String? storedKey) async { | ||
| if (storedKey != null) { | ||
| log('AppSecurityManager::_ensureKeyExists: Key already exists'); | ||
| return; | ||
| } | ||
|
|
||
| log('AppSecurityManager::_ensureKeyExists: Generating new key'); | ||
|
|
||
| try { | ||
| final newKey = Hive.generateSecureKey(); | ||
|
|
||
| await _storage.write( | ||
| key: SecureStorageKeys.hiveEncryptionKey, | ||
| value: base64UrlEncode(newKey), | ||
| ); | ||
|
|
||
| log('AppSecurityManager::_ensureKeyExists: Key stored successfully'); | ||
| } catch (e, st) { | ||
| logError( | ||
| 'AppSecurityManager::_ensureKeyExists: Failed to store key', | ||
| exception: e, | ||
| stackTrace: st, | ||
| ); | ||
| rethrow; | ||
| } | ||
| } | ||
|
|
||
| Future<Uint8List> _loadKeySafe() async { | ||
| final key = await _readStoredKey(); | ||
|
|
||
| if (key == null) { | ||
| throw StateError('Encryption key not found'); | ||
| } | ||
|
|
||
| try { | ||
| return base64Url.decode(key); | ||
| } catch (e, st) { | ||
| logError( | ||
| 'AppSecurityManager::_loadKeySafe: Decode failed', | ||
| exception: e, | ||
| stackTrace: st, | ||
| ); | ||
| throw StateError('Invalid encryption key format'); | ||
| } | ||
| } | ||
|
|
||
| Future<bool> _doesHiveExistSafe() async { | ||
| try { | ||
| final dir = await getApplicationDocumentsDirectory(); | ||
| final files = Directory(dir.path).listSync(); | ||
|
|
||
| final exists = files.any((f) => f.path.endsWith('.hive')); | ||
|
|
||
| log('AppSecurityManager::_doesHiveExistSafe: Exists = $exists'); | ||
| return exists; | ||
| } catch (e) { | ||
| logWarning( | ||
| 'AppSecurityManager::_doesHiveExistSafe: Check failed, Exception $e', | ||
| ); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| Future<void> _wipeLocalStorageSafe() async { | ||
| try { | ||
| logWarning('AppSecurityManager::_wipeLocalStorageSafe: Deleting local storage data'); | ||
| await Future.wait([ | ||
| _wipeHiveSafe(), | ||
| _wipeSharePreferenceSafe(), | ||
| ]); | ||
| } catch (e, st) { | ||
| logError( | ||
| 'AppSecurityManager::_wipeLocalStorageSafe: Delete failed', | ||
| exception: e, | ||
| stackTrace: st, | ||
| ); | ||
| rethrow; | ||
| } | ||
| } | ||
|
|
||
| Future<void> _wipeHiveSafe() async { | ||
| try { | ||
| logWarning('AppSecurityManager::_wipeHiveSafe: Deleting Hive data'); | ||
| await Hive.deleteFromDisk(); | ||
| } catch (e) { | ||
| logWarning('AppSecurityManager::_wipeHiveSafe: Delete failed, Exception $e'); | ||
| rethrow; | ||
| } | ||
| } | ||
|
|
||
| Future<void> _wipeSharePreferenceSafe() async { | ||
| try { | ||
| logWarning('AppSecurityManager::_wipeSharePreferenceSafe: Deleting SharePreference data'); | ||
| final sharedPreferences = await SharedPreferences.getInstance(); | ||
| await sharedPreferences.clear(); | ||
| } catch (e) { | ||
| logWarning('AppSecurityManager::_wipeSharePreferenceSafe: Delete failed, Exception $e'); | ||
| rethrow; | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have created an interface :
then 2 implem:
class DefaultEncryptionKeyProvider implements EncryptionKeyProvider {
@OverRide
Future init() => HiveCacheConfig.instance.initializeEncryptionKey();
}
class EncryptionKeyProviderFactory {
const EncryptionKeyProviderFactory._();
}