Skip to content

armgit / ente

Public Imported from GitHub
0

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

Fix proton import issues

Prateek Sunal0250ad3698ff
diff --git a/mobile/apps/auth/lib/ui/settings/data/import/proton_import.dart b/mobile/apps/auth/lib/ui/settings/data/import/proton_import.dart
index 92b93c2a24..e05db3f8d3 100644
--- a/mobile/apps/auth/lib/ui/settings/data/import/proton_import.dart
+++ b/mobile/apps/auth/lib/ui/settings/data/import/proton_import.dart
@@ -100,30 +100,42 @@ Future<int?> _processProtonExportFile(
if (isEncryptedProtonExport(decodedJson)) {
await dialog.hide();
- final password = await _promptForProtonExportPassword(context);
- if (password == null) {
- return null;
- }
+ while (true) {
+ final password = await _promptForProtonExportPassword(context);
+ if (password == null) {
+ return null;
+ }
- await dialog.show();
- try {
- final decryptedJsonString = await compute(
- _decryptProtonExportInBackground,
- {
- 'jsonString': jsonString,
- 'password': password,
- },
- );
- decodedJson = decodeProtonExportJson(decryptedJsonString);
- } catch (e, s) {
- Logger('ProtonImport').warning('Failed to decrypt Proton export', e, s);
- await dialog.hide();
- await showErrorDialog(
- context,
- context.l10n.incorrectPasswordTitle,
- context.l10n.pleaseCheckPasswordAndTryAgain,
- );
- return null;
+ await dialog.show();
+ try {
+ final decryptedJsonResult = await compute(
+ _decryptProtonExportInBackground,
+ {
+ 'jsonString': jsonString,
+ 'password': password,
+ },
+ );
+ switch (decryptedJsonResult['status']) {
+ case 'incorrect_password':
+ await dialog.hide();
+ await showErrorDialog(
+ context,
+ context.l10n.incorrectPasswordTitle,
+ context.l10n.pleaseCheckPasswordAndTryAgain,
+ );
+ continue;
+ case 'ok':
+ decodedJson =
+ decodeProtonExportJson(decryptedJsonResult['jsonString']!);
+ break;
+ default:
+ throw StateError('Unexpected Proton decrypt result status');
+ }
+ break;
+ } catch (e, s) {
+ Logger('ProtonImport').warning('Failed to decrypt Proton export', e, s);
+ rethrow;
+ }
}
}
@@ -136,7 +148,9 @@ Future<int?> _processProtonExportFile(
return parsedCodes.length;
}
-String _decryptProtonExportInBackground(Map<String, String> params) {
+Map<String, String> _decryptProtonExportInBackground(
+ Map<String, String> params,
+) {
final jsonString = params['jsonString'];
final password = params['password'];
if (jsonString == null || password == null) {
@@ -144,24 +158,26 @@ String _decryptProtonExportInBackground(Map<String, String> params) {
}
final decodedJson = decodeProtonExportJson(jsonString);
- return decryptProtonExport(decodedJson, password: password);
+ try {
+ return {
+ 'status': 'ok',
+ 'jsonString': decryptProtonExport(decodedJson, password: password),
+ };
+ } on IncorrectProtonExportPasswordException {
+ return {'status': 'incorrect_password'};
+ }
}
Future<String?> _promptForProtonExportPassword(BuildContext context) async {
- final password = Completer<String?>();
+ String? password;
await showTextInputDialog(
context,
title: context.l10n.passwordForDecryptingExport,
submitButtonLabel: context.l10n.submit,
isPasswordInput: true,
onSubmit: (value) async {
- if (!password.isCompleted) {
- password.complete(value);
- }
+ password = value;
},
);
- if (!password.isCompleted) {
- return null;
- }
- return password.future;
+ return password;
}
diff --git a/mobile/apps/auth/lib/ui/settings/data/import/proton_import_parser.dart b/mobile/apps/auth/lib/ui/settings/data/import/proton_import_parser.dart
index abce75763e..b2f0b7433f 100644
--- a/mobile/apps/auth/lib/ui/settings/data/import/proton_import_parser.dart
+++ b/mobile/apps/auth/lib/ui/settings/data/import/proton_import_parser.dart
@@ -14,12 +14,22 @@ const _protonExportAad = 'proton.authenticator.export.v1';
final _logger = Logger('ProtonImportParser');
+class IncorrectProtonExportPasswordException implements Exception {
+ const IncorrectProtonExportPasswordException();
+
+ @override
+ String toString() => 'Incorrect password';
+}
+
Map<String, dynamic> decodeProtonExportJson(String jsonString) {
final decodedJson = jsonDecode(jsonString);
if (decodedJson is! Map) {
throw const FormatException('Invalid Proton export');
}
- return Map<String, dynamic>.from(decodedJson);
+
+ final exportJson = Map<String, dynamic>.from(decodedJson);
+ _validateProtonExportVersion(exportJson);
+ return exportJson;
}
bool isEncryptedProtonExport(Map<String, dynamic> decodedJson) {
@@ -105,6 +115,7 @@ String decryptProtonExport(
Map<String, dynamic> decodedJson, {
required String password,
}) {
+ _validateProtonExportVersion(decodedJson);
if (!isEncryptedProtonExport(decodedJson)) {
throw const FormatException('Invalid Proton export');
}
@@ -140,7 +151,12 @@ String decryptProtonExport(
),
);
- final decryptedBytes = cipher.process(cipherText);
+ late final Uint8List decryptedBytes;
+ try {
+ decryptedBytes = cipher.process(cipherText);
+ } on InvalidCipherTextException {
+ throw const IncorrectProtonExportPasswordException();
+ }
return utf8.decode(decryptedBytes);
}
@@ -168,3 +184,9 @@ Uint8List _deriveProtonPasswordKey(String password, Uint8List salt) {
return generator.process(Uint8List.fromList(utf8.encode(password)));
}
+
+void _validateProtonExportVersion(Map<String, dynamic> decodedJson) {
+ if (decodedJson['version'] != _protonExportVersion) {
+ throw const FormatException('Invalid Proton export');
+ }
+}
diff --git a/mobile/apps/auth/test/ui/settings/data/import/fixtures/proton_authenticator_backup.json b/mobile/apps/auth/test/ui/settings/data/import/fixtures/proton_authenticator_backup.json
index 7149cd7bbe..90ea90c00f 100644
--- a/mobile/apps/auth/test/ui/settings/data/import/fixtures/proton_authenticator_backup.json
+++ b/mobile/apps/auth/test/ui/settings/data/import/fixtures/proton_authenticator_backup.json
@@ -1 +1 @@
-{"version":1,"entries":[{"id":"31122360-ffdc-49ec-a1e1-696cd67564b6","content":{"uri":"otpauth://totp/sean.hickey%2B2fa%40userlane.com?secret=J4YFC3TZKVYWCVCNIVBWIV2GHFAXUNDF&issuer=Userlane+Realm&algorithm=SHA1&digits=6&period=30","entry_type":"Totp","name":"[email protected]"},"note":null},{"id":"defcaa63-c551-41f1-82b7-810279c201fb","content":{"uri":"otpauth://totp/john.appleseed%40example.org?secret=554vbdogjrldg2jv&algorithm=SHA1&digits=6&period=30","entry_type":"Totp","name":"[email protected]"},"note":null},{"id":"ca7e81f3-8959-4b25-9714-150a01a0971e","content":{"uri":"otpauth://totp/ravin%40prontomarketing.com?secret=PIBTGMBYQYVSLN5PVZYQUTKYHOBRYYT7&issuer=Simplesat&algorithm=SHA1&digits=6&period=30","entry_type":"Totp","name":"[email protected]"},"note":null},{"id":"61cb558f-eca9-4c77-9347-c8e461df54e5","content":{"uri":"otpauth://totp/dele%40countersoft.com?secret=MI4GCOJSGIZTKLJSGMZWKLJUGEZDQLJYG5STILJSMUYDIYTCMQ3TSOJRGI&issuer=Gemini&algorithm=SHA1&digits=6&period=30","entry_type":"Totp","name":"[email protected]"},"note":null}]}
+{"version":1,"entries":[{"id":"1ccf0766-1121-4443-8cd2-25ce991384c3","content":{"uri":"otpauth://totp/example%40ente.io?secret=MI4GCOJSGIZTKLJSGMZWKLJUGEZDQLJYG5STILJSMUYDIYTCMQ3TSOJRGI&issuer=.env&algorithm=SHA1&digits=6&period=30","entry_type":"Totp","name":"[email protected]"},"note":null},{"id":"66b886aa-a764-427a-acde-8c5368d63482","content":{"uri":"otpauth://totp/%3Acool%40ente.com?secret=554VBDOGJRLDG2JV&issuer=%2Fe%2F&algorithm=SHA1&digits=6&period=30","entry_type":"Totp","name":":[email protected]"},"note":null},{"id":"d8a85eb1-2e84-418d-b764-e2d0868ee66a","content":{"uri":"otpauth://totp/simple%40ente.sh?secret=PIBTGMBYQYVSLN5PVZYQUTKYHOBRYYT7&issuer=ente&algorithm=SHA1&digits=6&period=30","entry_type":"Totp","name":"[email protected]"},"note":null},{"id":"3a481750-197b-4c02-8d1e-086068f55a54","content":{"uri":"otpauth://totp/r%40ente.com?secret=J4YFC3TZKVYWCVCNIVBWIV2GHFAXUNDF&issuer=reddit&algorithm=SHA1&digits=6&period=30","entry_type":"Totp","name":"[email protected]"},"note":null}]}
\ No newline at end of file
diff --git a/mobile/apps/auth/test/ui/settings/data/import/fixtures/proton_authenticator_backup_encrypted_1231246.json b/mobile/apps/auth/test/ui/settings/data/import/fixtures/proton_authenticator_backup_encrypted_1231246.json
index d32e65a4fe..78161468e9 100644
--- a/mobile/apps/auth/test/ui/settings/data/import/fixtures/proton_authenticator_backup_encrypted_1231246.json
+++ b/mobile/apps/auth/test/ui/settings/data/import/fixtures/proton_authenticator_backup_encrypted_1231246.json
@@ -1 +1 @@
-{"version":1,"salt":"ahTn0MTM/OG3vGs+nkLGVg==","content":"YYQI+yGhcpoGRGZYxrhN17UQquHgy0veJAW3DlhFFLnnAAFBKK5D0esMcw03I268WauJPszVI++EV3m2okNidzQKS2/zTBBFVxdtJEyiD7t7zPukRRgt9wvjO3R7z56/RhZaWAgJzF8Rw1ZtB9nRYByrGEpSJ6YI+YfT9l7bT3dQOw/r45Ec+I2rEhGN4/0kVRyhPXCAy4roztBJogxNZs+0wIx6rTHYK2Mlsx37YFejWaDHorCz1ukYoBYIV2HP5WswMyyvYdVsXULKePvhrBSLmy3un1s/T/qcMHndFR86nMXRsCXtIFL0BZDbvaOCJkAQh9rF0Zf9E8SdsM33H2qjTxWBVafJoqLOTjbsuhITszHc+UCLcgS/A0GPyqmTXvEH/AwM5UHQs7Ts+6lqnzVZnFOHEzKQzRj1u4VBdc+XGGyOOszbnMASUb2kKVHSisP/VhqO3NxHDoeSbiJxBxii7bSWC1l82sjxtSu41pmKB9ZhFDji5n6ABO2flIc943W4Jfg8gN0KYy6q2rpL0K9krg+m4ckz+/bZ76dD5l55hOLdBzXOEtlrUKWxw8+STdrCGAtXmceEHXtfC7F3cwCuyvfvg2TD4tgrR5o+YqHBMqZwEINaQxQWLw7Q5pI5QS3m3QgUpBs/nQ82ZkM+594+GoLTmumrn1GbZ5gCu4VqDrPpdP/mRf0Ib7b+wASEPs7IITeqvrUxMi5czyhrEAqt6F+/YpFeGc7vvmoOCMWyLqpDIKueRKE1HZp6QabPHbnjfYrFonLzwNjrmkaLMoyXU7QM0CoKzuxkTiv0hwG908rUDPgqY7ZWiOpBP6EXmhu6lTSEw3seC7w2dubtMBTzqcebLHjH1Csiac+7xYDevPOvp+8y18mgf10FfVL1GOZ+/dGvUOcK/0y71OxDRqVn2Q8ILxBSvjf73edPiI/uk0U3Nqe1WbEGLwn4bkb9Bn8D5rQZag2fotpJDn7905ZhdWrUv585sUf19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sJEU0OGM11WR66IjDx+U8Fr4cxrk0tvHA/iEwxEnhAMCrcpWJFmyHZiSGqdkodYB2ahe6Wi19hzo4ddXC2I8liBWvBQLLnWk8HSwXn80XfPWLt08zqD8BPfbOTtpGHwxIVJ3fPZKlJECLc9YKjDLW7MttqNjCKvgcQrkKh6U3NYHUwykHX6m2P+1O1DwE/B15f1Sy0l/yL+YrVX5Ijz8WIixZKlCNsjOa7WCPTfDNPZuD30G5n6UHHrSbN/7FdQKdOT149JJf1iD6uLae8YnpYS68PEevURq7UuGTd7sEkGBrPgHPaT6wB+Tj1DZx4FxAgu3GIIbMjbng3KywjlzYbp3NehawDzStoc0iFA6rSzQuZn2e5GqUgNlQXpNwMg4i2CHQ2smx8MLddKEVT7Ho9llqZvFoIl+hI4MHXhySpNHH1qLgVE0QVdziUVQ0MBzW4JnFA0Vzvk8tCTG0lai766vrTrB619kpdwNi9qpUEX1JPnD2nzAkUVHhfzvxf9VpFlwhAgMyuKnDUdNUxTuhPIJhjfB4GOvjrsO8QFh5uuiIe65o3XdyfttFUM5nxusKKPyQEXILFFtRyshwxQU0bdzxd9jNFC8xp0wkhGgWiBsF2WoUOw2Vq6HEhudJ3LlVv263hFpcYEsNO3iN0vZpBgzECXOeXIwUGb7ntC+IgYisX35jEtxiQO6M592REybEom0pe2nPcCvU9QCjIP+VDtu5fYSVHNZGI57DMPu5TsCrkH02b8uZ98IiPyLS35hVW7s2XcAIwul8rbCc6AKzHfuwh+N3JnmDyidn8ERrWw=="}
\ No newline at end of file
diff --git a/mobile/apps/auth/test/ui/settings/data/import/proton_import_parser_test.dart b/mobile/apps/auth/test/ui/settings/data/import/proton_import_parser_test.dart
index 0939bc1acf..cd442008f7 100644
--- a/mobile/apps/auth/test/ui/settings/data/import/proton_import_parser_test.dart
+++ b/mobile/apps/auth/test/ui/settings/data/import/proton_import_parser_test.dart
@@ -7,26 +7,49 @@ import 'package:flutter_test/flutter_test.dart';
void main() {
group('Proton import parser', () {
test('parses plaintext Proton exports from fixture', () async {
- final exportJson = decodeProtonExportJson(
- await _fixture('proton_authenticator_backup.json').readAsString(),
- );
-
- final codes = parseProtonExport(exportJson);
+ final codes = await _loadPlaintextFixtureCodes();
expect(codes, hasLength(4));
-
- expect(codes.first.type, Type.totp);
- expect(codes.first.issuer, 'Userlane Realm');
- expect(codes.first.account, '[email protected]');
-
- expect(codes[1].issuer, '[email protected]');
- expect(codes[1].account, '[email protected]');
-
- expect(codes[2].issuer, 'Simplesat');
- expect(codes[2].account, '[email protected]');
-
- expect(codes[3].issuer, 'Gemini');
- expect(codes[3].account, '[email protected]');
+ _assertCode(
+ codes[0],
+ type: Type.totp,
+ issuer: '.env',
+ account: '[email protected]',
+ secret: 'MI4GCOJSGIZTKLJSGMZWKLJUGEZDQLJYG5STILJSMUYDIYTCMQ3TSOJRGI',
+ algorithm: Algorithm.sha1,
+ digits: 6,
+ period: 30,
+ );
+ _assertCode(
+ codes[1],
+ type: Type.totp,
+ issuer: '/e/',
+ account: '[email protected]',
+ secret: '554VBDOGJRLDG2JV',
+ algorithm: Algorithm.sha1,
+ digits: 6,
+ period: 30,
+ );
+ _assertCode(
+ codes[2],
+ type: Type.totp,
+ issuer: 'ente',
+ account: '[email protected]',
+ secret: 'PIBTGMBYQYVSLN5PVZYQUTKYHOBRYYT7',
+ algorithm: Algorithm.sha1,
+ digits: 6,
+ period: 30,
+ );
+ _assertCode(
+ codes[3],
+ type: Type.totp,
+ issuer: 'reddit',
+ account: '[email protected]',
+ secret: 'J4YFC3TZKVYWCVCNIVBWIV2GHFAXUNDF',
+ algorithm: Algorithm.sha1,
+ digits: 6,
+ period: 30,
+ );
});
test('decrypts password protected Proton exports from fixture', () async {
@@ -42,11 +65,15 @@ void main() {
encryptedJson,
password: '1231246',
);
- final codes = parseProtonExport(decodeProtonExportJson(decryptedJson));
+ final decryptedCodes =
+ parseProtonExport(decodeProtonExportJson(decryptedJson));
+ final plaintextCodes = await _loadPlaintextFixtureCodes();
- expect(codes, hasLength(4));
- expect(codes.first.issuer, 'Userlane Realm');
- expect(codes.last.issuer, 'Gemini');
+ expect(decryptedCodes, hasLength(4));
+ expect(decryptedCodes, hasLength(plaintextCodes.length));
+ for (var index = 0; index < plaintextCodes.length; index++) {
+ expect(decryptedCodes[index], plaintextCodes[index]);
+ }
});
test('fails to decrypt password protected Proton exports with bad password',
@@ -59,12 +86,38 @@ void main() {
expect(
() => decryptProtonExport(encryptedJson, password: 'wrong-password'),
- throwsA(isA<Exception>()),
+ throwsA(isA<IncorrectProtonExportPasswordException>()),
);
});
});
}
+Future<List<Code>> _loadPlaintextFixtureCodes() async {
+ final exportJson = decodeProtonExportJson(
+ await _fixture('proton_authenticator_backup.json').readAsString(),
+ );
+ return parseProtonExport(exportJson);
+}
+
+void _assertCode(
+ Code code, {
+ required Type type,
+ required String issuer,
+ required String account,
+ required String secret,
+ required Algorithm algorithm,
+ required int digits,
+ required int period,
+}) {
+ expect(code.type, type);
+ expect(code.issuer, issuer);
+ expect(code.account, account);
+ expect(code.secret, secret);
+ expect(code.algorithm, algorithm);
+ expect(code.digits, digits);
+ expect(code.period, period);
+}
+
File _fixture(String name) => File(
'test/ui/settings/data/import/fixtures/$name',
);