diff --git a/assets/l10n/intl_en.arb b/assets/l10n/intl_en.arb index 6d087c5b9..baa741ad5 100644 --- a/assets/l10n/intl_en.arb +++ b/assets/l10n/intl_en.arb @@ -2601,5 +2601,6 @@ "appname": {}, "unread": {} } - } + }, + "noDatabaseEncryption": "Database encryption is not supported on this platform" } diff --git a/lib/config/setting_keys.dart b/lib/config/setting_keys.dart index 0766249bf..5fbccc4aa 100644 --- a/lib/config/setting_keys.dart +++ b/lib/config/setting_keys.dart @@ -30,4 +30,6 @@ abstract class SettingKeys { static const String showPresences = 'chat.fluffy.show_presences'; static const String displayChatDetailsColumn = 'chat.fluffy.display_chat_details_column'; + static const String noEncryptionWarningShown = + 'chat.fluffy.no_encryption_warning_shown'; } diff --git a/lib/utils/matrix_sdk_extensions/flutter_matrix_dart_sdk_database/builder.dart b/lib/utils/matrix_sdk_extensions/flutter_matrix_dart_sdk_database/builder.dart index 8c182716e..2679ff72a 100644 --- a/lib/utils/matrix_sdk_extensions/flutter_matrix_dart_sdk_database/builder.dart +++ b/lib/utils/matrix_sdk_extensions/flutter_matrix_dart_sdk_database/builder.dart @@ -81,21 +81,23 @@ Future _constructDatabase(Client client) async { // in case we got a cipher, we use the encryption helper // to manage SQLite encryption - final helper = SQfLiteEncryptionHelper( - factory: factory, - path: path, - cipher: cipher, - ); + final helper = cipher == null + ? null + : SQfLiteEncryptionHelper( + factory: factory, + path: path, + cipher: cipher, + ); // check whether the DB is already encrypted and otherwise do so - await helper.ensureDatabaseFileEncrypted(); + await helper?.ensureDatabaseFileEncrypted(); final database = await factory.openDatabase( path, options: OpenDatabaseOptions( version: 1, // most important : apply encryption when opening the DB - onConfigure: helper.applyPragmaKey, + onConfigure: helper?.applyPragmaKey, ), ); diff --git a/lib/utils/matrix_sdk_extensions/flutter_matrix_dart_sdk_database/cipher.dart b/lib/utils/matrix_sdk_extensions/flutter_matrix_dart_sdk_database/cipher.dart index 0c1163c4a..a1903018d 100644 --- a/lib/utils/matrix_sdk_extensions/flutter_matrix_dart_sdk_database/cipher.dart +++ b/lib/utils/matrix_sdk_extensions/flutter_matrix_dart_sdk_database/cipher.dart @@ -1,14 +1,20 @@ import 'dart:convert'; import 'dart:math'; +import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; +import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:matrix/matrix.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +import 'package:fluffychat/config/setting_keys.dart'; +import 'package:fluffychat/utils/client_manager.dart'; const _passwordStorageKey = 'database_password'; -Future getDatabaseCipher() async { +Future getDatabaseCipher() async { String? password; try { @@ -28,21 +34,34 @@ Future getDatabaseCipher() async { // workaround for if we just wrote to the key and it still doesn't exist password = await secureStorage.read(key: _passwordStorageKey); if (password == null) throw MissingPluginException(); - } on MissingPluginException catch (_) { + } on MissingPluginException catch (e) { const FlutterSecureStorage() .delete(key: _passwordStorageKey) .catchError((_) {}); - Logs().i('Database encryption is not supported on this platform'); + Logs().w('Database encryption is not supported on this platform', e); + _sendNoEncryptionWarning(e); } catch (e, s) { const FlutterSecureStorage() .delete(key: _passwordStorageKey) .catchError((_) {}); Logs().w('Unable to init database encryption', e, s); + _sendNoEncryptionWarning(e); } - // with the new database, we should no longer allow unencrypted storage - // secure_storage now supports all platforms we support - assert(password != null); + return password; +} + +void _sendNoEncryptionWarning(Object exception) async { + final store = await SharedPreferences.getInstance(); + final isStored = store.getBool(SettingKeys.noEncryptionWarningShown); + + if (isStored == true) return; + + final l10n = lookupL10n(PlatformDispatcher.instance.locale); + ClientManager.sendInitNotification( + l10n.noDatabaseEncryption, + exception.toString(), + ); - return password!; + await store.setBool(SettingKeys.noEncryptionWarningShown, true); }