π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Merge main and unify decryption failure handling
ashilknc472ed4e9776
diff --cc mobile/apps/photos/lib/module/download/decrypt.dartindex a5e444d413,33817fe035..1759626003--- a/mobile/apps/photos/lib/module/download/decrypt.dart+++ b/mobile/apps/photos/lib/module/download/decrypt.dart@@@ -94,17 -114,11 +93,17 @@@ Future<File?> _downloadAndDecryptPublicerror,stackTrace,);- if (throwOnDecryptionFailure) {- throw DownloadDecryptionFailedError();++ if (error is StreamPullErr && metadata.encryptedFileSha1 != null) {++ throw DownloadDecryptionError(metadata.encryptedFileSha1!);+ }return null;}return File(decryptedFilePath);} catch (error, stackTrace) {- _logger.severe('$logPrefix failed to download', error, stackTrace);- if (throwOnDecryptionFailure && error is DownloadDecryptionFailedError) {++ if (error is DownloadDecryptionError) {+ rethrow;+ }+ _logger.severe('$logPrefix failed to download', error, stackTrace);return null;}}diff --cc mobile/apps/photos/lib/module/download/download_error.dartindex c997641db7,0000000000..465e9df1a9mode 100644,000000..100644--- a/mobile/apps/photos/lib/module/download/download_error.dart+++ b/mobile/apps/photos/lib/module/download/download_error.dart@@@ -1,34 -1,0 +1,37 @@@+class DownloadFailedError implements Exception {+ final String message;++ DownloadFailedError(this.message);++ @override+ String toString() => message;+}++class DownloadNoConnectionError extends DownloadFailedError {+ DownloadNoConnectionError() : super('No connection');+}++class DownloadUnavailableError extends DownloadFailedError {+ DownloadUnavailableError() : super('Unavailable');+}+- class DownloadDecryptionFailedError extends DownloadFailedError {- DownloadDecryptionFailedError() : super('Failed to decrypt downloaded file');++class DownloadDecryptionError extends DownloadFailedError {++ final String encryptedFileSha1;++++ DownloadDecryptionError(this.encryptedFileSha1)++ : super('Failed to decrypt downloaded file');+}++Future<T?> handleDownloadDecryptionFailureForCaller<T>(+ Future<T?> download, {+ required bool rethrowDecryptionFailure,+}) async {+ try {+ return await download;- } on DownloadDecryptionFailedError {++ } on DownloadDecryptionError {+ if (rethrowDecryptionFailure) {+ rethrow;+ }+ return null;+ }+}diff --cc mobile/apps/photos/lib/module/download/file.dartindex c21466235e,bcd1638df3..b5badaa1b2--- a/mobile/apps/photos/lib/module/download/file.dart+++ b/mobile/apps/photos/lib/module/download/file.dart@@@ -175,37 -178,42 +178,38 @@@ Future<File?> getFileFromServer_progressCallbacks[downloadID] = progressCallback;}+ final download = _runOncePerKey(+ _fileDownloadsInProgress,+ downloadID,+ () {+ Future<File?> downloadFuture;+ if (file.fileType == FileType.livePhoto) {+ downloadFuture = _getLivePhotoFromServer(+ file,+ progressCallback: (count, total) {+ _progressCallbacks[downloadID]?.call(count, total);+ },+ needLiveVideo: liveVideo,+ forGalleryDownload: forGalleryDownload,+ );+ } else {+ downloadFuture = _downloadAndCache(+ file,+ cacheManager,+ progressCallback: (count, total) {+ _progressCallbacks[downloadID]?.call(count, total);+ },+ forGalleryDownload: forGalleryDownload,+ );+ }+ return downloadFuture;+ },+ onComplete: () => _progressCallbacks.remove(downloadID),+ );- try {- return await download;- } on DownloadDecryptionError {- if (forGalleryDownload || throwOnDecryptionFailure) {- rethrow;- }- return null;- }+ return handleDownloadDecryptionFailureForCaller(- _runOncePerKey(- _fileDownloadsInProgress,- downloadID,- () {- Future<File?> downloadFuture;- if (file.fileType == FileType.livePhoto) {- downloadFuture = _getLivePhotoFromServer(- file,- progressCallback: (count, total) {- _progressCallbacks[downloadID]?.call(count, total);- },- needLiveVideo: liveVideo,- forGalleryDownload: forGalleryDownload,- );- } else {- downloadFuture = _downloadAndCache(- file,- cacheManager,- progressCallback: (count, total) {- _progressCallbacks[downloadID]?.call(count, total);- },- forGalleryDownload: forGalleryDownload,- );- }- return downloadFuture;- },- onComplete: () => _progressCallbacks.remove(downloadID),- ),++ download,+ rethrowDecryptionFailure: forGalleryDownload || throwOnDecryptionFailure,+ );}Future<bool> isFileCached(EnteFile file, {bool liveVideo = false}) async {diff --cc mobile/apps/photos/lib/services/machine_learning/ml_file_retrieval.dartindex 0000000000,aca75df5b1..d5dc4ca023mode 000000,100644..100644--- a/mobile/apps/photos/lib/services/machine_learning/ml_file_retrieval.dart+++ b/mobile/apps/photos/lib/services/machine_learning/ml_file_retrieval.dart@@@ -1,0 -1,26 +1,26 @@@+ import "dart:io";+-import "package:photos/module/download/decrypt.dart";++import "package:photos/module/download/download_error.dart";+ import "package:photos/services/machine_learning/ml_exceptions.dart";++ typedef MlFileDownloadAndLoader = Future<File?> Function();++ Future<File?> downloadAndLoadFileForMlWithDecryptionRetry(+ MlFileDownloadAndLoader downloadAndLoad,+ ) async {+ const retryCount = 3;+ DownloadDecryptionError? firstFailure;+ for (var attempt = 0; ; attempt++) {+ try {+ return await downloadAndLoad();+ } on DownloadDecryptionError catch (error) {+ firstFailure ??= error;+ if (error.encryptedFileSha1 != firstFailure.encryptedFileSha1) {+ rethrow;+ }+ if (attempt == retryCount) {+ throw RepeatedFileDecryptionError();+ }+ }+ }+ }diff --cc mobile/apps/photos/lib/ui/viewer/file/video_widget_media_kit.dartindex dfbcd41066,d4751cddd4..4e77470d05--- a/mobile/apps/photos/lib/ui/viewer/file/video_widget_media_kit.dart+++ b/mobile/apps/photos/lib/ui/viewer/file/video_widget_media_kit.dart@@@ -323,15 -321,11 +323,15 @@@ class _VideoWidgetMediaKitState extend}).onError((error, stackTrace) {if (!mounted) return;- if (error is DownloadDecryptionFailedError) {- showErrorDialog(- context,- context.strings.error,- context.strings.failedToDownloadVideo,- );++ if (error is DownloadDecryptionError) {+ showDownloadDecryptionFailedDialog(context: context);+ } else {+ showErrorDialog(+ context,+ context.strings.error,+ context.strings.failedToDownloadVideo,+ );+ }});}diff --cc mobile/apps/photos/lib/ui/viewer/file/video_widget_native.dartindex 30c3a3b481,ae683b4418..07cbefe286--- a/mobile/apps/photos/lib/ui/viewer/file/video_widget_native.dart+++ b/mobile/apps/photos/lib/ui/viewer/file/video_widget_native.dart@@@ -718,15 -716,11 +718,15 @@@ class _VideoWidgetNativeState extends S}).onError((error, stackTrace) {if (!mounted) return;- if (error is DownloadDecryptionFailedError) {- showErrorDialog(- context,- context.strings.error,- context.strings.failedToDownloadVideo,- );++ if (error is DownloadDecryptionError) {+ showDownloadDecryptionFailedDialog(context: context);+ } else {+ showErrorDialog(+ context,+ context.strings.error,+ context.strings.failedToDownloadVideo,+ );+ }});}diff --cc mobile/apps/photos/lib/ui/viewer/file/zoomable_image.dartindex 4987f297a9,984272afe7..e781483b78--- a/mobile/apps/photos/lib/ui/viewer/file/zoomable_image.dart+++ b/mobile/apps/photos/lib/ui/viewer/file/zoomable_image.dart@@@ -318,11 -312,7 +318,11 @@@ class _ZoomableImageState extends Statee,s,);- if (e is DownloadDecryptionFailedError) {- _onFinalImageFetchFailed();++ if (e is DownloadDecryptionError) {+ _onFinalImageDecryptionFailed();+ } else {+ _onFinalImageFetchFailed();+ }});}}diff --cc mobile/apps/photos/test/module/download/download_error_test.dartindex 35be3cc9d1,0000000000..43c61d4e0bmode 100644,000000..100644--- a/mobile/apps/photos/test/module/download/download_error_test.dart+++ b/mobile/apps/photos/test/module/download/download_error_test.dart@@@ -1,40 -1,0 +1,50 @@@+import 'package:flutter_test/flutter_test.dart';+import 'package:photos/module/download/download_error.dart';++void main() {+ group('download decryption failures', () {+ test('are distinguishable from other download failures', () {- final error = DownloadDecryptionFailedError();++ final error = DownloadDecryptionError('encrypted-sha1');++ expect(error, isA<DownloadFailedError>());+ expect(error, isNot(isA<DownloadNoConnectionError>()));++ expect(error.encryptedFileSha1, 'encrypted-sha1');+ expect(error.toString(), 'Failed to decrypt downloaded file');+ });++ test('can be suppressed for an existing caller', () async {+ final result = await handleDownloadDecryptionFailureForCaller<int>(- Future<int?>.error(DownloadDecryptionFailedError()),++ Future<int?>.error(DownloadDecryptionError('encrypted-sha1')),+ rethrowDecryptionFailure: false,+ );++ expect(result, isNull);+ });++ test('can be surfaced to a viewer sharing the same download', () async {+ final sharedDownload = Future<int?>.error(- DownloadDecryptionFailedError(),++ DownloadDecryptionError('encrypted-sha1'),+ );+ final existingCaller = handleDownloadDecryptionFailureForCaller<int>(+ sharedDownload,+ rethrowDecryptionFailure: false,+ );+ final viewer = handleDownloadDecryptionFailureForCaller<int>(+ sharedDownload,+ rethrowDecryptionFailure: true,+ );++ expect(await existingCaller, isNull);- await expectLater(viewer, throwsA(isA<DownloadDecryptionFailedError>()));++ await expectLater(++ viewer,++ throwsA(++ isA<DownloadDecryptionError>().having(++ (error) => error.encryptedFileSha1,++ 'encryptedFileSha1',++ 'encrypted-sha1',++ ),++ ),++ );+ });+ });+}diff --cc mobile/apps/photos/test/services/machine_learning/ml_file_retrieval_test.dartindex 0000000000,0000000000..4e8a8aee98new file mode 100644--- /dev/null+++ b/mobile/apps/photos/test/services/machine_learning/ml_file_retrieval_test.dart@@@ -1,0 -1,0 +1,62 @@@++import 'dart:io';++++import 'package:flutter_test/flutter_test.dart';++import 'package:photos/module/download/download_error.dart';++import 'package:photos/services/machine_learning/ml_exceptions.dart';++import 'package:photos/services/machine_learning/ml_file_retrieval.dart';++++void main() {++ group('ML file decryption retries', () {++ test('returns a successful retry for identical ciphertext', () async {++ var attempts = 0;++++ final result = await downloadAndLoadFileForMlWithDecryptionRetry(++ () async {++ attempts++;++ if (attempts < 3) {++ throw DownloadDecryptionError('encrypted-sha1');++ }++ return File('decrypted-file');++ },++ );++++ expect(attempts, 3);++ expect(result?.path, 'decrypted-file');++ });++++ test('turns four identical failures into an expected ML skip', () async {++ var attempts = 0;++++ final result = downloadAndLoadFileForMlWithDecryptionRetry(() async {++ attempts++;++ throw DownloadDecryptionError('encrypted-sha1');++ });++++ await expectLater(result, throwsA(isA<RepeatedFileDecryptionError>()));++ expect(attempts, 4);++ });++++ test('stops retrying when the ciphertext changes', () async {++ var attempts = 0;++++ final result = downloadAndLoadFileForMlWithDecryptionRetry(() async {++ attempts++;++ throw DownloadDecryptionError(++ attempts == 1 ? 'first-sha1' : 'second-sha1',++ );++ });++++ await expectLater(++ result,++ throwsA(++ isA<DownloadDecryptionError>().having(++ (error) => error.encryptedFileSha1,++ 'encryptedFileSha1',++ 'second-sha1',++ ),++ ),++ );++ expect(attempts, 2);++ });++ });++}