Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mobile][photos] Skip repeated ML decryption failures

laurenspriem27799acaa0c2
diff --git a/mobile/apps/photos/lib/module/download/decrypt.dart b/mobile/apps/photos/lib/module/download/decrypt.dart
index 5fc269b7c7..33817fe035 100644
--- a/mobile/apps/photos/lib/module/download/decrypt.dart
+++ b/mobile/apps/photos/lib/module/download/decrypt.dart
@@ -28,6 +28,13 @@ class DownloadFailedError implements Exception {
String toString() => message;
}
+class DownloadDecryptionError extends DownloadFailedError {
+ final String encryptedFileSha1;
+
+ DownloadDecryptionError(this.encryptedFileSha1)
+ : super('Failed to decrypt downloaded file');
+}
+
class DownloadNoConnectionError extends DownloadFailedError {
DownloadNoConnectionError() : super('No connection');
}
@@ -103,7 +110,7 @@ Future<File?> _downloadAndDecryptPublicFile(
fakeProgress?.stop();
final metadata = await _fileMetadataForLogging(file, encryptedFilePath);
_logger.severe(
- 'Critical: $logPrefix failed to decrypt, $metadata',
+ 'Critical: $logPrefix failed to decrypt, ${metadata.log}',
error,
stackTrace,
);
@@ -220,10 +227,18 @@ Future<File?> downloadAndDecrypt(
fakeProgress?.stop();
final metadata = await _fileMetadataForLogging(file, encryptedFilePath);
_logger.severe(
- 'Critical: $logPrefix failed to decrypt, $metadata',
+ 'Critical: $logPrefix failed to decrypt, ${metadata.log}',
error,
stackTrace,
);
+ await _deleteFailedDecryptionArtifacts(
+ file,
+ encryptedFilePath,
+ decryptedFilePath,
+ );
+ if (error is StreamPullErr && metadata.encryptedFileSha1 != null) {
+ throw DownloadDecryptionError(metadata.encryptedFileSha1!);
+ }
if (throwOnFailure) {
throw DownloadFailedError('Failed to decrypt downloaded file');
}
@@ -232,6 +247,9 @@ Future<File?> downloadAndDecrypt(
await encryptedFile.delete();
return File(decryptedFilePath);
} catch (error, stackTrace) {
+ if (error is DownloadDecryptionError) {
+ rethrow;
+ }
_logger.severe(
'$logPrefix failed to download or decrypt',
error,
@@ -263,20 +281,44 @@ Exception _toDownloadFailure(String? error) {
return DownloadFailedError(error ?? 'Download failed');
}
-Future<String> _fileMetadataForLogging(
+Future<({String log, String? encryptedFileSha1})> _fileMetadataForLogging(
EnteFile file,
String encryptedFilePath,
) async {
final buffer = StringBuffer();
final encryptedFile = File(encryptedFilePath);
+ String? encryptedFileSha1;
if (encryptedFile.existsSync()) {
- final hash = await sha1.bind(encryptedFile.openRead()).first;
- buffer.write('encFileSha1: $hash, ');
+ encryptedFileSha1 = (await sha1.bind(encryptedFile.openRead()).first)
+ .toString();
+ buffer.write('encFileSha1: $encryptedFileSha1, ');
} else {
buffer.write('encFileSha1: file not found, ');
}
buffer.write('metadataVersion: ${file.metadataVersion}, ');
buffer.write('fileSize: ${file.fileSize ?? "null"}, ');
buffer.write('viaMobile: ${(file.deviceFolder ?? '') != ''}');
- return buffer.toString();
+ return (log: buffer.toString(), encryptedFileSha1: encryptedFileSha1);
+}
+
+Future<void> _deleteFailedDecryptionArtifacts(
+ EnteFile file,
+ String encryptedFilePath,
+ String decryptedFilePath,
+) async {
+ try {
+ await downloadManager.cancel(file.uploadedFileID!);
+ for (final path in [encryptedFilePath, decryptedFilePath]) {
+ final artifact = File(path);
+ if (await artifact.exists()) {
+ await artifact.delete();
+ }
+ }
+ } catch (error, stackTrace) {
+ _logger.warning(
+ 'Failed to delete decryption artifacts for File-${file.uploadedFileID}',
+ error,
+ stackTrace,
+ );
+ }
}
diff --git a/mobile/apps/photos/lib/module/download/file.dart b/mobile/apps/photos/lib/module/download/file.dart
index 1bc73c01db..bcd1638df3 100644
--- a/mobile/apps/photos/lib/module/download/file.dart
+++ b/mobile/apps/photos/lib/module/download/file.dart
@@ -34,6 +34,7 @@ Future<File?> getFile(
bool liveVideo = false,
bool isOrigin = false,
bool forGalleryDownload = false, // only relevant for live photos
+ bool throwOnDecryptionFailure = false,
}) async {
try {
if (file.isRemoteOnlyFile) {
@@ -41,6 +42,7 @@ Future<File?> getFile(
file,
liveVideo: liveVideo,
forGalleryDownload: forGalleryDownload,
+ throwOnDecryptionFailure: throwOnDecryptionFailure,
);
} else {
final String key = file.tag + liveVideo.toString() + isOrigin.toString();
@@ -62,7 +64,8 @@ Future<File?> getFile(
}
} catch (e, s) {
_logger.warning("Failed to get file", e, s);
- if (forGalleryDownload) {
+ if (forGalleryDownload ||
+ (throwOnDecryptionFailure && e is DownloadDecryptionError)) {
rethrow;
}
return null;
@@ -160,6 +163,7 @@ Future<File?> getFileFromServer(
ProgressCallback? progressCallback,
bool liveVideo = false, // only needed in case of live photos
bool forGalleryDownload = false,
+ bool throwOnDecryptionFailure = false,
}) async {
final cacheManager = (file.fileType == FileType.video || liveVideo)
? VideoCacheManager.instance
@@ -174,7 +178,7 @@ Future<File?> getFileFromServer(
_progressCallbacks[downloadID] = progressCallback;
}
- return _runOncePerKey(
+ final download = _runOncePerKey(
_fileDownloadsInProgress,
downloadID,
() {
@@ -202,6 +206,14 @@ Future<File?> getFileFromServer(
},
onComplete: () => _progressCallbacks.remove(downloadID),
);
+ try {
+ return await download;
+ } on DownloadDecryptionError {
+ if (forGalleryDownload || throwOnDecryptionFailure) {
+ rethrow;
+ }
+ return null;
+ }
}
Future<bool> isFileCached(EnteFile file, {bool liveVideo = false}) async {
@@ -238,7 +250,7 @@ Future<File?> _getLivePhotoFromServer(
return needLiveVideo ? livePhoto.video : livePhoto.image;
} catch (e, s) {
_logger.warning("live photo get failed", e, s);
- if (forGalleryDownload) {
+ if (forGalleryDownload || e is DownloadDecryptionError) {
rethrow;
}
return null;
diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_exceptions.dart b/mobile/apps/photos/lib/services/machine_learning/ml_exceptions.dart
index 855f7502dc..055553d580 100644
--- a/mobile/apps/photos/lib/services/machine_learning/ml_exceptions.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/ml_exceptions.dart
@@ -13,3 +13,5 @@ class ThumbnailRetrievalException implements Exception {
}
class CouldNotRetrieveAnyFileData implements Exception, LocallyHandledError {}
+
+class RepeatedFileDecryptionError implements Exception, LocallyHandledError {}
diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_file_retrieval.dart b/mobile/apps/photos/lib/services/machine_learning/ml_file_retrieval.dart
new file mode 100644
index 0000000000..e0f3f0b9e9
--- /dev/null
+++ b/mobile/apps/photos/lib/services/machine_learning/ml_file_retrieval.dart
@@ -0,0 +1,24 @@
+import "dart:io";
+
+import "package:photos/module/download/decrypt.dart";
+import "package:photos/services/machine_learning/ml_exceptions.dart";
+
+typedef MlFileLoader = Future<File?> Function();
+
+Future<File?> loadFileForMlWithDecryptionRetry(MlFileLoader load) async {
+ late final DownloadDecryptionError firstFailure;
+ try {
+ return await load();
+ } on DownloadDecryptionError catch (error) {
+ firstFailure = error;
+ }
+
+ try {
+ return await load();
+ } on DownloadDecryptionError catch (error) {
+ if (error.encryptedFileSha1 == firstFailure.encryptedFileSha1) {
+ throw RepeatedFileDecryptionError();
+ }
+ rethrow;
+ }
+}
diff --git a/mobile/apps/photos/lib/utils/ml_util.dart b/mobile/apps/photos/lib/utils/ml_util.dart
index b462505380..0465beaf63 100644
--- a/mobile/apps/photos/lib/utils/ml_util.dart
+++ b/mobile/apps/photos/lib/utils/ml_util.dart
@@ -29,6 +29,7 @@ import "package:photos/services/filedata/model/response.dart";
import "package:photos/services/machine_learning/face_ml/face_alignment/alignment_result.dart";
import "package:photos/services/machine_learning/face_ml/face_detection/detection.dart";
import "package:photos/services/machine_learning/ml_exceptions.dart";
+import "package:photos/services/machine_learning/ml_file_retrieval.dart";
import "package:photos/services/machine_learning/ml_result.dart";
import "package:photos/services/machine_learning/ml_run_control.dart";
import "package:photos/services/search_service.dart";
@@ -739,7 +740,11 @@ Future<String> getImagePathForML(EnteFile enteFile) async {
modificationTime: enteFile.modificationTime,
);
}
- file = await getFile(enteFile, isOrigin: true);
+ file = await loadFileForMlWithDecryptionRetry(
+ () => getFile(enteFile, isOrigin: true, throwOnDecryptionFailure: true),
+ );
+ } on RepeatedFileDecryptionError {
+ rethrow;
} catch (e, s) {
_logger.severe("Could not get file for $enteFile", e, s);
}
@@ -1020,6 +1025,9 @@ Future<MLResult> analyzeImageRust(Map args) async {
}
bool isExpectedMlSkipError(Object error) {
+ if (error is RepeatedFileDecryptionError) {
+ return true;
+ }
final message = _normalizedErrorMessage(error);
const acceptedIssueMarkers = <String>[
"thumbnailretrievalexception",
@@ -1031,6 +1039,9 @@ bool isExpectedMlSkipError(Object error) {
}
String formatExpectedMlSkipReasonForLogs(Object error) {
+ if (error is RepeatedFileDecryptionError) {
+ return "origin decryption failed twice for identical ciphertext";
+ }
final normalized = _normalizedErrorMessage(error);
if (normalized.contains("invalidimageformatexception")) {
return "image decode failed";