π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Bind shared hashes in Photos file lookups
Neeraj Gupta7c41999ee90a
diff --git a/mobile/apps/photos/lib/db/files_db.dart b/mobile/apps/photos/lib/db/files_db.dartindex 13be8f8f05..9846a01dc1 100644--- a/mobile/apps/photos/lib/db/files_db.dart+++ b/mobile/apps/photos/lib/db/files_db.dart@@ -1293,7 +1293,7 @@ class FilesDB with SqlDbBase {int userID,) async {final db = await sqliteAsyncDB;- final List<String> hashes = [];+ final hashes = <String>{};for (final file in files) {if (file.hash != null && file.hash != '') {hashes.add(file.hash!);@@ -1302,11 +1302,23 @@ class FilesDB with SqlDbBase {if (hashes.isEmpty) {return {};}- final inParam = hashes.map((e) => "'$e'").join(',');- final rows = await db.getAll('''- SELECT * FROM $filesTable WHERE $columnHash IN ($inParam) AND $columnOwnerID = $userID;- ''');- final matchedFiles = convertToFiles(rows);++ const maxInParams = 900;+ final hashList = hashes.toList(growable: false);+ final matchedFiles = <EnteFile>[];+ for (var i = 0; i < hashList.length; i += maxInParams) {+ final end = min(i + maxInParams, hashList.length);+ final chunk = hashList.sublist(i, end);+ final placeholders = List.filled(chunk.length, '?').join(',');+ final rows = await db.getAll(+ '''+ SELECT * FROM $filesTable+ WHERE $columnHash IN ($placeholders) AND $columnOwnerID = ?;+ ''',+ [...chunk, userID],+ );+ matchedFiles.addAll(convertToFiles(rows));+ }return Map.fromIterable(matchedFiles, key: (e) => e.hash);}diff --git a/mobile/apps/photos/test/db/files_db_shared_hash_test.dart b/mobile/apps/photos/test/db/files_db_shared_hash_test.dartnew file mode 100644index 0000000000..0e62a786ee--- /dev/null+++ b/mobile/apps/photos/test/db/files_db_shared_hash_test.dart@@ -0,0 +1,119 @@+import "dart:io";++import "package:flutter_test/flutter_test.dart";+import "package:path_provider_platform_interface/path_provider_platform_interface.dart";+import "package:photos/db/files_db.dart";+import "package:photos/models/file/file.dart";+import "package:photos/models/file/file_type.dart";++void main() {+ TestWidgetsFlutterBinding.ensureInitialized();+ final files = FilesDB.instance;+ late Directory directory;+ late PathProviderPlatform previousPathProvider;++ setUpAll(() async {+ directory = await Directory.systemTemp.createTemp("files_db_hash_");+ previousPathProvider = PathProviderPlatform.instance;+ PathProviderPlatform.instance = _PathProvider(directory.path);+ });++ tearDownAll(() async {+ await (await files.sqliteAsyncDB).close();+ PathProviderPlatform.instance = previousPathProvider;+ await directory.delete(recursive: true);+ });++ test("matches owned hashes and treats shared hashes as data", () async {+ await files.clearTable();+ await files.insertMultiple([+ _storedFile(uploadedFileID: 1, ownerID: 7, hash: "owned-hash"),+ _storedFile(uploadedFileID: 2, ownerID: 8, hash: "other-owner-hash"),+ _storedFile(uploadedFileID: 3, ownerID: 8, hash: "owned-hash"),+ _storedFile(uploadedFileID: 4, ownerID: 7, hash: "image-hash:video-hash"),+ _storedFile(uploadedFileID: 5, ownerID: 7, hash: "quote'only"),+ ]);++ final ordinary = await files+ .getUserOwnedFilesWithSameHashForGivenListOfFiles([+ _sharedFile("owned-hash"),+ _sharedFile("owned-hash"),+ _sharedFile("other-owner-hash"),+ _sharedFile("image-hash:video-hash"),+ _sharedFile(""),+ EnteFile(),+ ], 7);+ expect(+ ordinary.keys,+ unorderedEquals(["owned-hash", "image-hash:video-hash"]),+ );+ expect(ordinary["owned-hash"]?.uploadedFileID, 1);+ expect(ordinary["owned-hash"]?.ownerID, 7);+ expect(ordinary["image-hash:video-hash"]?.uploadedFileID, 4);++ final quoted = await files.getUserOwnedFilesWithSameHashForGivenListOfFiles(+ [_sharedFile("quote'only")],+ 7,+ );+ expect(quoted.keys, ["quote'only"]);+ expect(quoted["quote'only"]?.uploadedFileID, 5);++ final injected = await files+ .getUserOwnedFilesWithSameHashForGivenListOfFiles([+ _sharedFile("x') OR 1=1 --"),+ ], 7);+ expect(injected, isEmpty);+ });++ test("merges hash lookup batches without duplicate results", () async {+ await files.clearTable();+ const count = 1001;+ final hashes = List.generate(count, (index) => "hash-$index");+ await files.insertMultiple([+ for (var index = 0; index < count; index++)+ _storedFile(uploadedFileID: index + 1, ownerID: 7, hash: hashes[index]),+ ]);++ final matches = await files+ .getUserOwnedFilesWithSameHashForGivenListOfFiles(+ [...hashes, hashes.first].map(_sharedFile).toList(),+ 7,+ );++ expect(matches, hasLength(count));+ expect(matches["hash-1000"]?.uploadedFileID, 1001);+ });+}++EnteFile _storedFile({+ required int uploadedFileID,+ required int ownerID,+ required String hash,+}) => EnteFile()+ ..uploadedFileID = uploadedFileID+ ..ownerID = ownerID+ ..collectionID = 10+ ..title = "photo.jpg"+ ..fileType = FileType.image+ ..creationTime = 100+ ..modificationTime = 100+ ..hash = hash;++EnteFile _sharedFile(String hash) => EnteFile()+ ..uploadedFileID = 100+ ..ownerID = 9+ ..collectionID = 20+ ..title = "shared.jpg"+ ..fileType = FileType.image+ ..creationTime = 100+ ..modificationTime = 100+ ..hash = hash;++class _PathProvider extends PathProviderPlatform {+ _PathProvider(this.path);++ final String path;++ @override+ Future<String?> getApplicationDocumentsPath() async => path;+}