From b96593ed10700e0d1cebca4f0182f08bd83b5ef3 Mon Sep 17 00:00:00 2001 From: Hocuri Date: Tue, 5 Nov 2024 22:13:21 +0100 Subject: [PATCH] fix: Prevent accidental wrong-password-notifications (#6122) Over the past years, it happend two times that a user came to me worried about a false-positive "Cannot login as ***. Please check if the e-mail address and the password are correct." message. I'm not sure why this happened, but this PR makes the logic for showing this notification stricter: - Before: The notification is shown if connection fails two times in a row, and the second error contains the word "authentication". - Now: The notification is shown if the connection fails two times in a row, and _both_ error messages contain the word "authentication". The second commit just renames `login_failed_once` to `authentication_failed_once` in order to reflect this change. --- src/imap.rs | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/src/imap.rs b/src/imap.rs index 015fa604a..4eb432599 100644 --- a/src/imap.rs +++ b/src/imap.rs @@ -89,7 +89,7 @@ pub(crate) struct Imap { oauth2: bool, - login_failed_once: bool, + authentication_failed_once: bool, pub(crate) connectivity: ConnectivityStore, @@ -254,7 +254,7 @@ impl Imap { proxy_config, strict_tls, oauth2, - login_failed_once: false, + authentication_failed_once: false, connectivity: Default::default(), conn_last_try: UNIX_EPOCH, conn_backoff_ms: 0, @@ -402,7 +402,7 @@ impl Imap { let mut lock = context.server_id.write().await; lock.clone_from(&session.capabilities.server_id); - self.login_failed_once = false; + self.authentication_failed_once = false; context.emit_event(EventType::ImapConnected(format!( "IMAP-LOGIN as {}", lp.user @@ -416,35 +416,38 @@ impl Imap { let imap_user = lp.user.to_owned(); let message = stock_str::cannot_login(context, &imap_user).await; - let err_str = err.to_string(); warn!(context, "IMAP failed to login: {err:#}."); first_error.get_or_insert(format_err!("{message} ({err:#})")); + // If it looks like the password is wrong, send a notification: let _lock = context.wrong_pw_warning_mutex.lock().await; - if !configuring - && self.login_failed_once - && err_str.to_lowercase().contains("authentication") - && context.get_config_bool(Config::NotifyAboutWrongPw).await? - { - let mut msg = Message::new_text(message); - if let Err(e) = chat::add_device_msg_with_importance( - context, - None, - Some(&mut msg), - true, - ) - .await + if err.to_string().to_lowercase().contains("authentication") { + if self.authentication_failed_once + && !configuring + && context.get_config_bool(Config::NotifyAboutWrongPw).await? { - warn!(context, "Failed to add device message: {e:#}."); + let mut msg = Message::new_text(message); + if let Err(e) = chat::add_device_msg_with_importance( + context, + None, + Some(&mut msg), + true, + ) + .await + { + warn!(context, "Failed to add device message: {e:#}."); + } else { + context + .set_config_internal(Config::NotifyAboutWrongPw, None) + .await + .log_err(context) + .ok(); + } } else { - context - .set_config_internal(Config::NotifyAboutWrongPw, None) - .await - .log_err(context) - .ok(); + self.authentication_failed_once = true; } } else { - self.login_failed_once = true; + self.authentication_failed_once = false; } } }