π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Distinguish malformed recovery keys and reuse mobile guidance
Manav Rathi6c8872293a47
diff --git a/mobile/apps/photos/lib/services/account/user_service.dart b/mobile/apps/photos/lib/services/account/user_service.dartindex 216b9c6058..c3c4c3d5b5 100644--- a/mobile/apps/photos/lib/services/account/user_service.dart+++ b/mobile/apps/photos/lib/services/account/user_service.dart@@ -994,11 +994,14 @@ class UserService {await dialog.hide();_logger.severe("error during recovery", e);if (!context.mounted) return;- if (e == const AccountsError.incorrectRecoveryKey()) {+ if (e == const AccountsError.incorrectRecoveryKey() ||+ e == const AccountsError.malformedRecoveryKey()) {await showAlertBottomSheet(context,title: context.strings.incorrectRecoveryKey,- message: context.strings.theRecoveryKeyYouEnteredIsIncorrect,+ message: e == const AccountsError.malformedRecoveryKey()+ ? context.strings.invalidRecoveryKey+ : context.strings.theRecoveryKeyYouEnteredIsIncorrect,assetPath: 'assets/warning-grey.png',);} else if (e == const AccountsError.sessionExpired()) {diff --git a/mobile/apps/photos/lib/ui/account/recovery_page.dart b/mobile/apps/photos/lib/ui/account/recovery_page.dartindex 2fe9d42dc6..c9dffe79ae 100644--- a/mobile/apps/photos/lib/ui/account/recovery_page.dart+++ b/mobile/apps/photos/lib/ui/account/recovery_page.dart@@ -1,3 +1,4 @@+import 'package:ente_base/models/two_factor.dart';import "package:ente_components/ente_components.dart";import "package:ente_strings/ente_strings.dart";import 'package:flutter/material.dart';@@ -123,10 +124,9 @@ class _RecoveryPageState extends State<RecoveryPage> {} catch (e) {await dialog.hide();if (!mounted) return;- String errMessage = context.strings.incorrectRecoveryKeyBody;- if (e is AssertionError) {- errMessage = '$errMessage : ${e.message}';- }+ final errMessage = e == const AccountsError.malformedRecoveryKey()+ ? context.strings.invalidRecoveryKey+ : context.strings.incorrectRecoveryKeyBody;if (!mounted) return;// ignore: unawaited_futuresshowAlertBottomSheet(diff --git a/mobile/packages/accounts/lib/pages/recovery_page.dart b/mobile/packages/accounts/lib/pages/recovery_page.dartindex b5c125b792..17a24a1408 100644--- a/mobile/packages/accounts/lib/pages/recovery_page.dart+++ b/mobile/packages/accounts/lib/pages/recovery_page.dart@@ -52,15 +52,13 @@ class _RecoveryPageState extends State<RecoveryPage> {);} catch (e) {await dialog.hide();- String errMessage = 'The recovery key you entered is incorrect';- if (e is AssertionError) {- errMessage = '$errMessage : ${e.message}';- }if (mounted) {await showAlertBottomSheet(context,title: context.strings.incorrectRecoveryKey,- message: errMessage,+ message: e == const AccountsError.malformedRecoveryKey()+ ? context.strings.invalidRecoveryKey+ : context.strings.theRecoveryKeyYouEnteredIsIncorrect,assetPath: 'assets/warning-grey.png',);}diff --git a/mobile/packages/accounts/lib/services/user_service.dart b/mobile/packages/accounts/lib/services/user_service.dartindex 4b6e7acd73..977cbc8c39 100644--- a/mobile/packages/accounts/lib/services/user_service.dart+++ b/mobile/packages/accounts/lib/services/user_service.dart@@ -1062,11 +1062,14 @@ class UserService {if (!context.mounted) {return;}- if (e == const AccountsError.incorrectRecoveryKey()) {+ if (e == const AccountsError.incorrectRecoveryKey() ||+ e == const AccountsError.malformedRecoveryKey()) {await showAlertBottomSheet(context,title: context.strings.incorrectRecoveryKey,- message: context.strings.theRecoveryKeyYouEnteredIsIncorrect,+ message: e == const AccountsError.malformedRecoveryKey()+ ? context.strings.invalidRecoveryKey+ : context.strings.theRecoveryKeyYouEnteredIsIncorrect,assetPath: 'assets/warning-grey.png',);} else if (e == const AccountsError.sessionExpired()) {diff --git a/rust/bindings/frb/lib/src/accounts/types.rs b/rust/bindings/frb/lib/src/accounts/types.rsindex 26abd35142..44248abdd0 100644--- a/rust/bindings/frb/lib/src/accounts/types.rs+++ b/rust/bindings/frb/lib/src/accounts/types.rs@@ -8,6 +8,7 @@ use flutter_rust_bridge::frb;#[frb(non_opaque)]pub enum AccountsError {IncorrectRecoveryKey,+ MalformedRecoveryKey,SessionExpired,Other { message: String },}@@ -16,6 +17,7 @@ impl From<Error> for AccountsError {fn from(error: Error) -> Self {match error {Error::IncorrectRecoveryKey => Self::IncorrectRecoveryKey,+ Error::MalformedRecoveryKey => Self::MalformedRecoveryKey,Error::Http(ref http) if http.status_code() == Some(404) => Self::SessionExpired,_ => Self::Other {message: ente_core::error::chain(&error),diff --git a/rust/bindings/frb/lib/src/recovery.rs b/rust/bindings/frb/lib/src/recovery.rsindex bb7c0ae414..e34dd930a0 100644--- a/rust/bindings/frb/lib/src/recovery.rs+++ b/rust/bindings/frb/lib/src/recovery.rs@@ -1,21 +1,9 @@+use crate::accounts::AccountsError;use ente_accounts::auth;use flutter_rust_bridge::frb;-#[frb(non_opaque)]-pub enum RecoveryError {- Other { message: String },-}--impl From<ente_accounts::Error> for RecoveryError {- fn from(error: ente_accounts::Error) -> Self {- Self::Other {- message: ente_core::error::chain(&error),- }- }-}-#[frb(sync)]-pub fn recovery_key_to_mnemonic(recovery_key_hex: String) -> Result<String, RecoveryError> {+pub fn recovery_key_to_mnemonic(recovery_key_hex: String) -> Result<String, AccountsError> {let recovery_key = hex::decode(&recovery_key_hex).map_err(|e| ente_accounts::Error::Decode(format!("recovery_key: {e}")))?;auth::recovery_key_to_mnemonic(&recovery_key).map_err(Into::into)@@ -24,7 +12,7 @@ pub fn recovery_key_to_mnemonic(recovery_key_hex: String) -> Result<String, Reco#[frb(sync)]pub fn recovery_key_from_mnemonic_or_hex(recovery_key_mnemonic_or_hex: String,-) -> Result<String, RecoveryError> {+) -> Result<String, AccountsError> {let recovery_key = auth::recovery_key_from_mnemonic_or_hex(&recovery_key_mnemonic_or_hex)?;Ok(hex::encode(&recovery_key))}diff --git a/rust/bindings/wasm/lib/src/accounts.rs b/rust/bindings/wasm/lib/src/accounts.rsindex 315f97cb23..76629c8285 100644--- a/rust/bindings/wasm/lib/src/accounts.rs+++ b/rust/bindings/wasm/lib/src/accounts.rs@@ -25,6 +25,9 @@ impl Error {Self::Accounts(ente_accounts::Error::IncorrectRecoveryKey) => {Some("incorrect_recovery_key")}+ Self::Accounts(ente_accounts::Error::MalformedRecoveryKey) => {+ Some("malformed_recovery_key")+ }Self::Accounts(ente_accounts::Error::Http(http)) if http.status_code() == Some(404) => {Some("session_expired")}diff --git a/rust/crates/accounts/src/auth/recovery.rs b/rust/crates/accounts/src/auth/recovery.rsindex 7c0b5b8ed9..629bd55786 100644--- a/rust/crates/accounts/src/auth/recovery.rs+++ b/rust/crates/accounts/src/auth/recovery.rs@@ -47,18 +47,18 @@ pub fn recovery_key_from_mnemonic_or_hex(recovery_key_mnemonic_or_hex: &str) ->let recovery_key = SecretVec::new(if trimmed_input.contains(' ') {if trimmed_input.split(' ').count() != 24 {- return Err(Error::IncorrectRecoveryKey);+ return Err(Error::MalformedRecoveryKey);}let mnemonic = Mnemonic::parse_in_normalized(Language::English, &trimmed_input)- .map_err(|_| Error::IncorrectRecoveryKey)?;+ .map_err(|_| Error::MalformedRecoveryKey)?;mnemonic.to_entropy()} else {- hex::decode(&trimmed_input).map_err(|_| Error::IncorrectRecoveryKey)?+ hex::decode(&trimmed_input).map_err(|_| Error::MalformedRecoveryKey)?});if recovery_key.len() != 32 {- return Err(Error::IncorrectRecoveryKey);+ return Err(Error::MalformedRecoveryKey);}Ok(recovery_key)@@ -66,7 +66,10 @@ pub fn recovery_key_from_mnemonic_or_hex(recovery_key_mnemonic_or_hex: &str) ->pub fn recovery_key_to_mnemonic(recovery_key: &[u8]) -> Result<String> {if recovery_key.len() != 32 {- return Err(Error::IncorrectRecoveryKey);+ return Err(Error::InvalidKey(format!(+ "Recovery key must be 32 bytes, got {}",+ recovery_key.len()+ )));}Mnemonic::from_entropy_in(Language::English, recovery_key)diff --git a/rust/crates/accounts/src/error.rs b/rust/crates/accounts/src/error.rsindex 693ceec744..d88242ccb1 100644--- a/rust/crates/accounts/src/error.rs+++ b/rust/crates/accounts/src/error.rs@@ -26,6 +26,9 @@ pub enum Error {#[error("Incorrect recovery key")]IncorrectRecoveryKey,+ #[error("Malformed recovery key")]+ MalformedRecoveryKey,+#[error("Invalid key attributes")]InvalidKeyAttributes,