Skip to content

armgit / ente

Public Imported from GitHub
0

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

Limit OTP Auth import file size

Neeraj Gupta30f84fda662c
diff --git a/mobile/apps/auth/lib/core/errors.dart b/mobile/apps/auth/lib/core/errors.dart
index 34e2d55c68..e764853ebc 100644
--- a/mobile/apps/auth/lib/core/errors.dart
+++ b/mobile/apps/auth/lib/core/errors.dart
@@ -18,6 +18,15 @@ class StorageLimitExceededError extends Error {}
class FileTooLargeForPlanError extends Error {}
+class ImportFileTooLargeException implements Exception {
+ final int maxBytes;
+
+ const ImportFileTooLargeException(this.maxBytes);
+
+ @override
+ String toString() => 'Import file exceeds the $maxBytes byte limit';
+}
+
class SilentlyCancelUploadsError extends Error {}
class UserCancelledUploadError extends Error {}
diff --git a/mobile/apps/auth/lib/ui/settings/data/import/import_file_cleanup.dart b/mobile/apps/auth/lib/ui/settings/data/import/import_file_cleanup.dart
index f85dda3193..832363e363 100644
--- a/mobile/apps/auth/lib/ui/settings/data/import/import_file_cleanup.dart
+++ b/mobile/apps/auth/lib/ui/settings/data/import/import_file_cleanup.dart
@@ -1,6 +1,7 @@
import 'dart:io';
import 'dart:typed_data';
+import 'package:ente_auth/core/errors.dart';
import 'package:logging/logging.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
@@ -15,9 +16,22 @@ Future<String> readPickedImportFileAsString(String path) async {
}
}
-Future<Uint8List> readPickedImportFileAsBytes(String path) async {
+Future<Uint8List> readPickedImportFileAsBytes(
+ String path, {
+ int? maxBytes,
+}) async {
try {
- return await File(path).readAsBytes();
+ final file = File(path);
+ if (maxBytes == null) return await file.readAsBytes();
+
+ final bytes = BytesBuilder(copy: false);
+ await for (final chunk in file.openRead(0, maxBytes + 1)) {
+ if (bytes.length + chunk.length > maxBytes) {
+ throw ImportFileTooLargeException(maxBytes);
+ }
+ bytes.add(chunk);
+ }
+ return bytes.takeBytes();
} finally {
await deletePickedImportFileIfAppOwned(path);
}
diff --git a/mobile/apps/auth/lib/ui/settings/data/import/otp_auth_import.dart b/mobile/apps/auth/lib/ui/settings/data/import/otp_auth_import.dart
index 4c71c867ce..b0202e2605 100644
--- a/mobile/apps/auth/lib/ui/settings/data/import/otp_auth_import.dart
+++ b/mobile/apps/auth/lib/ui/settings/data/import/otp_auth_import.dart
@@ -1,3 +1,4 @@
+import 'package:ente_auth/core/errors.dart';
import 'package:ente_auth/models/code.dart';
import 'package:ente_auth/ui/settings/data/import/import_file_cleanup.dart';
import 'package:ente_auth/ui/settings/data/import/import_flow.dart';
@@ -33,6 +34,11 @@ Future<void> _pickOtpAuthFile(BuildContext context) async {
showProgressBeforeProcessing: false,
logger: _logger,
logMessage: 'Exception while processing OTP Auth import',
+ errorMessage: (context, error) => error is ImportFileTooLargeException
+ ? context.strings.importFileTooLarge(
+ size: '${error.maxBytes ~/ (1024 * 1024)} MiB',
+ )
+ : '${context.strings.importFailureDesc}\n Error: $error',
process: (path, progressDialog) =>
_processOtpAuthFile(context, path, progressDialog),
);
@@ -43,7 +49,10 @@ Future<int?> _processOtpAuthFile(
String path,
ProgressDialog dialog,
) async {
- final fileBytes = await readPickedImportFileAsBytes(path);
+ final fileBytes = await readPickedImportFileAsBytes(
+ path,
+ maxBytes: otpAuthImportMaxBytes,
+ );
if (!context.mounted) return null;
final l10n = context.strings;
while (true) {
diff --git a/mobile/apps/auth/lib/ui/settings/data/import/otp_auth_import_parser.dart b/mobile/apps/auth/lib/ui/settings/data/import/otp_auth_import_parser.dart
index 7c8e016925..7bee91c054 100644
--- a/mobile/apps/auth/lib/ui/settings/data/import/otp_auth_import_parser.dart
+++ b/mobile/apps/auth/lib/ui/settings/data/import/otp_auth_import_parser.dart
@@ -2,6 +2,7 @@ import 'dart:convert';
import 'dart:typed_data';
import 'package:base32/base32.dart';
+import 'package:ente_auth/core/errors.dart';
import 'package:ente_auth/models/code.dart';
import 'package:ente_auth/ui/settings/data/import/import_flow.dart';
import 'package:logging/logging.dart';
@@ -9,11 +10,16 @@ import 'package:pointycastle/export.dart' hide Algorithm;
final _logger = Logger('OtpAuthImportParser');
+const otpAuthImportMaxBytes = 10 * 1024 * 1024;
+
class IncorrectOtpAuthPasswordException implements Exception {
const IncorrectOtpAuthPasswordException();
}
List<Code> parseOtpAuthExport(Uint8List fileBytes, {required String password}) {
+ if (fileBytes.length > otpAuthImportMaxBytes) {
+ throw const ImportFileTooLargeException(otpAuthImportMaxBytes);
+ }
final outer = _decryptOuterArchive(fileBytes);
final isBackup = outer.containsKey('WrappedData');
final encryptedData = _bytes(outer[isBackup ? 'WrappedData' : 'Data']);
diff --git a/mobile/apps/auth/test/ui/settings/data/import/import_file_cleanup_test.dart b/mobile/apps/auth/test/ui/settings/data/import/import_file_cleanup_test.dart
new file mode 100644
index 0000000000..9ce8a8679f
--- /dev/null
+++ b/mobile/apps/auth/test/ui/settings/data/import/import_file_cleanup_test.dart
@@ -0,0 +1,27 @@
+import 'dart:io';
+
+import 'package:ente_auth/core/errors.dart';
+import 'package:ente_auth/ui/settings/data/import/import_file_cleanup.dart';
+import 'package:flutter_test/flutter_test.dart';
+
+void main() {
+ test('enforces an optional byte limit without truncating imports', () async {
+ final directory = await Directory.systemTemp.createTemp('auth-import-');
+ addTearDown(() => directory.delete(recursive: true));
+ final file = File('${directory.path}/backup');
+ await file.writeAsBytes([1, 2, 3, 4]);
+
+ expect(await readPickedImportFileAsBytes(file.path, maxBytes: 4), [
+ 1,
+ 2,
+ 3,
+ 4,
+ ]);
+ await file.writeAsBytes([5], mode: FileMode.append);
+ await expectLater(
+ readPickedImportFileAsBytes(file.path, maxBytes: 4),
+ throwsA(isA<ImportFileTooLargeException>()),
+ );
+ expect(await readPickedImportFileAsBytes(file.path), [1, 2, 3, 4, 5]);
+ });
+}
diff --git a/mobile/apps/auth/test/ui/settings/data/import/otp_auth_import_parser_test.dart b/mobile/apps/auth/test/ui/settings/data/import/otp_auth_import_parser_test.dart
index 4156e2baf1..f1c9f596ad 100644
--- a/mobile/apps/auth/test/ui/settings/data/import/otp_auth_import_parser_test.dart
+++ b/mobile/apps/auth/test/ui/settings/data/import/otp_auth_import_parser_test.dart
@@ -2,12 +2,23 @@ import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
+import 'package:ente_auth/core/errors.dart';
import 'package:ente_auth/models/code.dart';
import 'package:ente_auth/ui/settings/data/import/otp_auth_import_parser.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('OTP Auth import parser', () {
+ test('rejects oversized files before attempting decryption', () {
+ expect(
+ () => parseOtpAuthExport(
+ Uint8List(otpAuthImportMaxBytes + 1),
+ password: '',
+ ),
+ throwsA(isA<ImportFileTooLargeException>()),
+ );
+ });
+
for (final version in ['1.0', '1.1']) {
test('decrypts backup version $version', () async {
final codes = parseOtpAuthExport(
diff --git a/mobile/packages/strings/lib/l10n/arb/strings_en.arb b/mobile/packages/strings/lib/l10n/arb/strings_en.arb
index 9c710fd00f..8451cb881e 100644
--- a/mobile/packages/strings/lib/l10n/arb/strings_en.arb
+++ b/mobile/packages/strings/lib/l10n/arb/strings_en.arb
@@ -1978,6 +1978,15 @@
"importEnteEncGuide": "Select the encrypted JSON file exported from Ente",
"importEnterSetupKey": "Enter a setup key",
"importFailureDesc": "Could not parse the selected file.",
+ "importFileTooLarge": "The selected file is too large. Maximum size: {size}.",
+ "@importFileTooLarge": {
+ "placeholders": {
+ "size": {
+ "type": "String",
+ "example": "10 MiB"
+ }
+ }
+ },
"importFromGallery": "Import from gallery",
"importGoogleAuthConfirmation": "{codeCount, plural, one{Import 1 code from Google Authenticator?} other{Import {codeCount} codes from Google Authenticator?}}",
"@importGoogleAuthConfirmation": {