From e6f90af011913d42deab6e86e4e73cb9c0db5be6 Mon Sep 17 00:00:00 2001 From: ggurdin Date: Fri, 23 Aug 2024 11:56:34 -0400 Subject: [PATCH] added potential fix and better logging for flutter_secure_storage returning null for database cipher on iOS --- .../cipher.dart | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) 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 bfe1251dc..0134b50b3 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 @@ -14,9 +14,25 @@ Future getDatabaseCipher() async { String? password; try { - const secureStorage = FlutterSecureStorage(); + // #Pangea + // mogol/flutter_secure_storage#532 + // mogol/flutter_secure_storage#524 + // Pangea# + const secureStorage = FlutterSecureStorage( + // #Pangea + iOptions: IOSOptions(accessibility: KeychainAccessibility.first_unlock), + // Pangea# + ); + // #Pangea + await secureStorage.read(key: _passwordStorageKey); + // Pangea# final containsEncryptionKey = await secureStorage.read(key: _passwordStorageKey) != null; + // #Pangea + Sentry.addBreadcrumb( + Breadcrumb(message: 'containsEncryptionKey: $containsEncryptionKey'), + ); + // Pangea# if (!containsEncryptionKey) { final rng = Random.secure(); final list = Uint8List(32); @@ -29,18 +45,41 @@ 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(); + if (password == null) { + throw MissingPluginException( + // #Pangea + "password is null after storing new password", + // Pangea# + ); + } } on MissingPluginException catch (e) { const FlutterSecureStorage() .delete(key: _passwordStorageKey) .catchError((_) {}); Logs().w('Database encryption is not supported on this platform', e); + // #Pangea + Sentry.addBreadcrumb( + Breadcrumb( + message: + 'Database encryption is not supported on this platform. Error message: ${e.message}', + data: {'exception': e}, + ), + ); + // Pangea# _sendNoEncryptionWarning(e); } catch (e, s) { const FlutterSecureStorage() .delete(key: _passwordStorageKey) .catchError((_) {}); Logs().w('Unable to init database encryption', e, s); + // #Pangea + Sentry.addBreadcrumb( + Breadcrumb( + message: 'Unable to init database encryption', + data: {'exception': e, 'stackTrace': s}, + ), + ); + // Pangea# _sendNoEncryptionWarning(e); }