Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mob] Safeguards against concurrent multipart uploads

Neeraj Gupta376dbbc5761f
diff --git a/mobile/apps/photos/lib/core/errors.dart b/mobile/apps/photos/lib/core/errors.dart
index f2bfad2f6d..b22be2a194 100644
--- a/mobile/apps/photos/lib/core/errors.dart
+++ b/mobile/apps/photos/lib/core/errors.dart
@@ -83,3 +83,11 @@ class NoMediaLocationAccessError extends Error {}
class PassKeySessionNotVerifiedError extends Error {}
class PassKeySessionExpiredError extends Error {}
+
+class MultiPartUploadError implements Exception {
+ final String message;
+ MultiPartUploadError(this.message);
+
+ @override
+ String toString() => "MultiPartUploadError: $message";
+}
diff --git a/mobile/apps/photos/lib/db/upload_locks_db.dart b/mobile/apps/photos/lib/db/upload_locks_db.dart
index 25575fb9d3..53a2ac14cd 100644
--- a/mobile/apps/photos/lib/db/upload_locks_db.dart
+++ b/mobile/apps/photos/lib/db/upload_locks_db.dart
@@ -3,6 +3,7 @@ import 'dart:io';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
+import "package:photos/core/errors.dart";
import "package:photos/module/upload/model/multipart.dart";
import 'package:sqflite/sqflite.dart';
import "package:sqflite_migration/sqflite_migration.dart";
@@ -235,6 +236,7 @@ class UploadLocksDB {
String localId,
String fileHash,
int collectionID,
+ String encryptedFileName,
) async {
final db = await database;
@@ -246,9 +248,7 @@ class UploadLocksDB {
whereArgs: [localId, fileHash, collectionID],
);
- if (rows.isEmpty) {
- throw Exception("No cached links found for $localId and $fileHash");
- }
+ _validateResume(rows, localId, encryptedFileName);
final row = rows.first;
return (
@@ -285,6 +285,7 @@ class UploadLocksDB {
String localId,
String fileHash,
int collectionID,
+ String encryptedFileName,
) async {
final db = await database;
final rows = await db.query(
@@ -294,10 +295,9 @@ class UploadLocksDB {
' AND ${_trackUploadTable.columnCollectionID} = ?',
whereArgs: [localId, fileHash, collectionID],
);
- if (rows.isEmpty) {
- throw Exception("No cached links found for $localId and $fileHash");
- }
+ _validateResume(rows, localId, encryptedFileName);
final row = rows.first;
+
final objectKey = row[_trackUploadTable.columnObjectKey] as String;
final encFileSize = row[_trackUploadTable.columnEncryptedFileSize] as int;
final partsStatus = await db.query(
@@ -340,6 +340,28 @@ class UploadLocksDB {
);
}
+// validateResume checks if the cached links are valid for resuming upload
+ void _validateResume(
+ List<Map<String, Object?>> rows,
+ String localId,
+ String encryptedFileName,
+ ) {
+ if (rows.isEmpty) {
+ throw MultiPartUploadError("No cached links found for $localId");
+ }
+ if (rows.length > 1) {
+ throw MultiPartUploadError(
+ "Multiple entries found for localID: $localId",
+ );
+ }
+ if (encryptedFileName !=
+ rows.first[_trackUploadTable.columnEncryptedFileName]) {
+ throw MultiPartUploadError(
+ "Encrypted file name mismatch for localID: $localId",
+ );
+ }
+ }
+
Future<void> appendStreamEntry(
int uploadedFileID,
String errorMessage,
@@ -531,6 +553,16 @@ class UploadLocksDB {
if (rows.isEmpty) {
return null;
}
+ if (rows.length > 1) {
+ await db.delete(
+ _trackUploadTable.table,
+ where: '${_trackUploadTable.columnLocalID} = ?',
+ whereArgs: [localId],
+ );
+ throw MultiPartUploadError(
+ "Multiple entries found for localID: $localId",
+ );
+ }
final row = rows.first;
return row[_trackUploadTable.columnEncryptedFileName] as String;
});
diff --git a/mobile/apps/photos/lib/module/upload/service/multipart.dart b/mobile/apps/photos/lib/module/upload/service/multipart.dart
index ad429e5e77..dafe2125e8 100644
--- a/mobile/apps/photos/lib/module/upload/service/multipart.dart
+++ b/mobile/apps/photos/lib/module/upload/service/multipart.dart
@@ -30,11 +30,12 @@ class MultiPartUploader {
String localId,
String fileHash,
int collectionID,
+ String encFileName,
) async {
final collectionKey =
CollectionsService.instance.getCollectionKey(collectionID);
- final result =
- await _db.getFileEncryptionData(localId, fileHash, collectionID);
+ final result = await _db.getFileEncryptionData(
+ localId, fileHash, collectionID, encFileName);
final encryptedFileKey = CryptoUtil.base642bin(result.encryptedFileKey);
final fileNonce = CryptoUtil.base642bin(result.fileNonce);
@@ -115,9 +116,10 @@ class MultiPartUploader {
String localId,
String fileHash,
int collectionID,
+ String encryptedFileName,
) async {
final multipartInfo =
- await _db.getCachedLinks(localId, fileHash, collectionID);
+ await _db.getCachedLinks(localId, fileHash, collectionID, encryptedFileName);
await _db.updateLastAttempted(localId, fileHash, collectionID);
Map<int, String> etags = multipartInfo.partETags ?? {};
diff --git a/mobile/apps/photos/lib/utils/file_uploader.dart b/mobile/apps/photos/lib/utils/file_uploader.dart
index e8bc6de704..991a1a1c98 100644
--- a/mobile/apps/photos/lib/utils/file_uploader.dart
+++ b/mobile/apps/photos/lib/utils/file_uploader.dart
@@ -547,12 +547,12 @@ class FileUploader {
collectionID,
)
: null;
- bool multipartEntryExists = existingMultipartEncFileName != null;
+ final bool hasExistingMultiPart = existingMultipartEncFileName != null;
final tempDirectory = Configuration.instance.getTempDirectory();
final String uniqueID =
'${const Uuid().v4().toString()}_${file.generatedID}';
- final encryptedFilePath = multipartEntryExists
+ final encryptedFilePath = hasExistingMultiPart
? '$tempDirectory$existingMultipartEncFileName'
: '$tempDirectory$uploadTempFilePrefix${uniqueID}_file.encrypted';
final encryptedThumbnailPath =
@@ -574,11 +574,12 @@ class FileUploader {
);
Uint8List? key;
- EncryptionResult? multiPartFileEncResult = multipartEntryExists
+ final EncryptionResult? multiPartFileEncResult = hasExistingMultiPart
? await _multiPartUploader.getEncryptionResult(
lockKey,
mediaUploadData.hashData!.fileHash!,
collectionID,
+ existingMultipartEncFileName,
)
: null;
if (isUpdatedFile) {
@@ -607,21 +608,19 @@ class FileUploader {
// If the multipart entry exists but the encrypted file doesn't, it means
// that we'll have to re-upload as the nonce is lost
- if (multipartEntryExists) {
+ if (hasExistingMultiPart) {
+ if (!encryptedFileExists) {
+ throw MultiPartUploadError(
+ 'multiPartResume: encryptedFile missing',
+ );
+ }
final bool updateWithDiffKey = isUpdatedFile &&
multiPartFileEncResult != null &&
!listEquals(key, multiPartFileEncResult.key);
- if (!encryptedFileExists || updateWithDiffKey) {
- if (updateWithDiffKey) {
- _logger.severe('multiPart update resumed with differentKey');
- } else {
- _logger.warning(
- 'multiPart EncryptedFile missing, discard multipart entry',
- );
- }
- await _uploadLocks.deleteMultipartTrack(lockKey);
- multipartEntryExists = false;
- multiPartFileEncResult = null;
+ if (updateWithDiffKey) {
+ throw MultiPartUploadError(
+ 'multiPart update resumed with differentKey',
+ );
}
} else if (encryptedFileExists) {
// otherwise just delete the file for singlepart upload
@@ -678,14 +677,15 @@ class FileUploader {
} else {
isMultipartUpload = true;
_logger.info(
- "Init multipartUpload $multipartEntryExists, isUpdate $isUpdatedFile",
+ "Init multipartUpload $hasExistingMultiPart, isUpdate $isUpdatedFile",
);
- if (multipartEntryExists) {
+ if (hasExistingMultiPart) {
fileObjectKey = await _multiPartUploader.putExistingMultipartFile(
encryptedFile,
lockKey,
mediaUploadData.hashData!.fileHash!,
collectionID,
+ existingMultipartEncFileName,
);
} else {
final fileUploadURLs =
@@ -882,6 +882,9 @@ class FileUploader {
}
bool isPutOrUpdateFileError(Object e) {
+ if (e is MultiPartUploadError) {
+ return true;
+ }
if (e is DioException) {
return e.requestOptions.path.contains("/files") ||
e.requestOptions.path.contains("/files/update");