From c4fa46c9f357fd36b0bef9007f7c79e76781e32a Mon Sep 17 00:00:00 2001 From: Krille Fear Date: Wed, 17 Nov 2021 19:29:17 +0100 Subject: [PATCH] fix: Use sqlcipher for FluffyBox --- .../fluffybox_database.dart | 31 ++++++++++--------- pubspec.lock | 7 ----- pubspec.yaml | 1 - 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/lib/utils/matrix_sdk_extensions.dart/fluffybox_database.dart b/lib/utils/matrix_sdk_extensions.dart/fluffybox_database.dart index f5eb46cc..06f11f00 100644 --- a/lib/utils/matrix_sdk_extensions.dart/fluffybox_database.dart +++ b/lib/utils/matrix_sdk_extensions.dart/fluffybox_database.dart @@ -1,3 +1,5 @@ +//@dart=2.12 + import 'dart:io'; import 'dart:typed_data'; @@ -8,15 +10,12 @@ import 'package:encrypt/encrypt.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:matrix/matrix.dart'; import 'package:path_provider/path_provider.dart'; -import 'package:sqflite/sqflite.dart' as sqflite; - -import '../platform_infos.dart'; +import 'package:sqflite_sqlcipher/sqflite.dart' as sqflite; class FlutterFluffyBoxDatabase extends FluffyBoxDatabase { FlutterFluffyBoxDatabase( String name, { - String path, - Future Function() openSqlDatabase, + Future Function()? openSqlDatabase, }) : super( name, openSqlDatabase: openSqlDatabase, @@ -27,6 +26,7 @@ class FlutterFluffyBoxDatabase extends FluffyBoxDatabase { static Future databaseBuilder(Client client) async { Logs().d('Open FluffyBox...'); + String? password; try { // Workaround for secure storage is calling Platform.operatingSystem on web if (kIsWeb) throw MissingPluginException(); @@ -43,24 +43,27 @@ class FlutterFluffyBoxDatabase extends FluffyBoxDatabase { } // workaround for if we just wrote to the key and it still doesn't exist - final rawEncryptionKey = await secureStorage.read(key: _cipherStorageKey); - if (rawEncryptionKey == null) throw MissingPluginException(); + password = await secureStorage.read(key: _cipherStorageKey); + if (password == null) throw MissingPluginException(); } on MissingPluginException catch (_) { Logs().i('FluffyBox encryption is not supported on this platform'); } final db = FluffyBoxDatabase( 'fluffybox_${client.clientName.replaceAll(' ', '_').toLowerCase()}', - openSqlDatabase: () => _openSqlDatabase(client), + openSqlDatabase: () => _openSqlDatabase(client, password), ); await db.open(); Logs().d('FluffyBox is ready'); return db; } - static Future _openSqlDatabase(Client client) async { + static Future _openSqlDatabase( + Client client, + String? password, + ) async { final path = await _findDatabasePath(client); - return await sqflite.openDatabase(path); + return await sqflite.openDatabase(path, password: password); } static Future _findDatabasePath(Client client) async { @@ -85,9 +88,7 @@ class FlutterFluffyBoxDatabase extends FluffyBoxDatabase { @override int get maxFileSize => supportsFileStoring ? 100 * 1024 * 1024 : 0; @override - bool get supportsFileStoring => (PlatformInfos.isIOS || - PlatformInfos.isAndroid || - PlatformInfos.isDesktop); + bool get supportsFileStoring => !kIsWeb; Future _getFileStoreDirectory() async { try { @@ -97,12 +98,12 @@ class FlutterFluffyBoxDatabase extends FluffyBoxDatabase { return (await getApplicationDocumentsDirectory()).path; } } catch (_) { - return (await getDownloadsDirectory()).path; + return (await getDownloadsDirectory())!.path; } } @override - Future getFile(Uri mxcUri) async { + Future getFile(Uri mxcUri) async { if (!supportsFileStoring) return null; final tempDirectory = await _getFileStoreDirectory(); final file = diff --git a/pubspec.lock b/pubspec.lock index 5f6fe622..efd594ef 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -658,13 +658,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.1" - idb_sqflite: - dependency: "direct main" - description: - name: idb_sqflite - url: "https://pub.dartlang.org" - source: hosted - version: "1.0.1" image: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index cb10e97c..e0d2234f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -42,7 +42,6 @@ dependencies: future_loading_dialog: ^0.2.1 geolocator: ^7.6.2 hive_flutter: ^1.1.0 - idb_sqflite: ^1.0.1 image_picker: ^0.8.4+2 intl: any localstorage: ^4.0.0+1