Skip to content

armgit / ente

Public Imported from GitHub
0

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

Santize errors from computer

Neeraj Guptaa249fa928ee4
diff --git a/mobile/apps/photos/plugins/ente_crypto/lib/src/crypto.dart b/mobile/apps/photos/plugins/ente_crypto/lib/src/crypto.dart
index 2e06cf0b95..3f4ceb875e 100644
--- a/mobile/apps/photos/plugins/ente_crypto/lib/src/crypto.dart
+++ b/mobile/apps/photos/plugins/ente_crypto/lib/src/crypto.dart
@@ -123,7 +123,7 @@ Future<EncryptionResult> chachaEncryptFileV2(Map<String, dynamic> args) async {
bytesRead += bufferBytes.length;
if (bufferBytes.length != chunkSize) {
throw Exception(
- "Expected to read $chunkSize bytes, but got ${bufferBytes.length} bytes",
+ "V2 $kPartialReadErrorTag $chunkSize bytes, but got ${bufferBytes.length} bytes, sourceFileLength: $sourceFileLength",
);
}
// Set final tag only after confirming we've read all expected data
@@ -177,6 +177,11 @@ Future<EncryptionResult> chachaEncryptFile(Map<String, dynamic> args) async {
tag = Sodium.cryptoSecretstreamXchacha20poly1305TagFinal;
}
final buffer = await inputFile.read(chunkSize);
+ if (buffer.length != chunkSize) {
+ throw Exception(
+ "$kPartialReadErrorTag to read $chunkSize bytes, but got ${buffer.length} bytes, sourceFileLength: $sourceFileLength",
+ );
+ }
bytesRead += chunkSize;
final encryptedData = Sodium.cryptoSecretstreamXchacha20poly1305Push(
initPushResult.state,
@@ -398,11 +403,13 @@ class CryptoUtil {
args["sourceFilePath"] = sourceFilePath;
args["destinationFilePath"] = destinationFilePath;
args["key"] = key;
- return _computer.compute(
- chachaEncryptFile,
- param: args,
- taskName: "encryptFile",
- );
+ return _computer
+ .compute<Map<String, dynamic>, EncryptionResult>(
+ chachaEncryptFile,
+ param: args,
+ taskName: "encryptFile",
+ )
+ .unwrapExceptionInComputer();
}
// Encrypts the file at sourceFilePath, with the key (if provided) and a
@@ -440,11 +447,17 @@ class CryptoUtil {
args["key"] = key;
return _computer
.compute(
- chachaDecryptFile,
- param: args,
- taskName: "decryptFile",
- )
- .catchError((e) => throw CryptoErr("decryptFile", e));
+ chachaDecryptFile,
+ param: args,
+ taskName: "decryptFile",
+ )
+ .catchError((e) {
+ if (e.toString().contains(kLibSodiumErrorTag)) {
+ throw CryptoErr("decryptFile", e);
+ } else {
+ throw e;
+ }
+ });
}
// Generates and returns a 256-bit key.
diff --git a/mobile/apps/photos/plugins/ente_crypto/lib/src/models/errors.dart b/mobile/apps/photos/plugins/ente_crypto/lib/src/models/errors.dart
index 9893b989c8..9f02c4f47d 100644
--- a/mobile/apps/photos/plugins/ente_crypto/lib/src/models/errors.dart
+++ b/mobile/apps/photos/plugins/ente_crypto/lib/src/models/errors.dart
@@ -16,3 +16,27 @@ class CryptoErr implements Exception {
return 'CryptoErr: $message';
}
}
+
+extension FutureErrorHandling<T> on Future<T> {
+ Future<T> unwrapExceptionInComputer() {
+ return catchError((e) {
+ final msg = e.toString();
+ if (msg.contains(kPartialReadErrorTag)) {
+ throw PartialReadException(msg);
+ }
+ throw e;
+ });
+ }
+}
+
+const kPartialReadErrorTag = 'PartialRead';
+const kLibSodiumErrorTag = 'crypto_secretstream_xchacha20poly1305_pull';
+
+// Exception counterpart for upstream handling via catchError chains
+class PartialReadException implements Exception {
+ final String message;
+ PartialReadException(this.message);
+
+ @override
+ String toString() => 'PartialReadException: $message';
+}