Browse Source

Check the pending 2FA login for this user and device

Look up the pending 2FA login for the user and device instead of the
latest one on the device, answer a failed check with the same error and
IP/username log detail as a wrong password, and escape the device id in
the logs.
pull/7827/head
Daniel García 1 month ago
parent
commit
24d20cf816
No known key found for this signature in database GPG Key ID: FC8A7D14C3CD543A
  1. 57
      src/api/core/two_factor/email.rs

57
src/api/core/two_factor/email.rs

@ -14,7 +14,10 @@ use crate::{
crypto, crypto,
db::{ db::{
DbConn, DbConn,
models::{AuthRequest, AuthRequestId, DeviceId, EventType, TwoFactor, TwoFactorType, User, UserId}, models::{
AuthRequest, AuthRequestId, DeviceId, EventType, TwoFactor, TwoFactorIncomplete, TwoFactorType, User,
UserId,
},
}, },
error::{Error, MapResult}, error::{Error, MapResult},
mail, mail,
@ -94,42 +97,22 @@ async fn send_email_login(data: Json<SendEmailLoginData>, client_headers: Client
{ {
err!("AuthRequest doesn't exist", "Invalid device, IP or code") err!("AuthRequest doesn't exist", "Invalid device, IP or code")
} }
} else { } else if let Some(device_identifier) = &data.device_identifier {
// Fallback for clients (e.g. iOS) that call this endpoint with an email but // iOS/Android SSO logins send the email and device id but no password hash,
// without a masterPasswordHash or authRequestId. This can happen when the // so accept a device that has a pending 2FA login for this user
// client receives a 2FA-required response from the token endpoint and then if TwoFactorIncomplete::find_by_user_and_device(&user.uuid, device_identifier, &conn).await.is_none() {
// immediately calls send-email-login without re-submitting credentials. err!(
// "Username or password is incorrect. Try again",
// If the client provided a device identifier, use it to look up the most format!("IP: {}. Username: {}.", client_headers.ip.ip, email.escape_debug())
// recently active device for the account and verify it matches the submitted )
// email. This preserves a meaningful security check while remaining
// compatible with these clients.
//
// If no device identifier is present either, reject the request to prevent
// unauthenticated actors from triggering emails for arbitrary accounts.
if let Some(device_identifier) = &data.device_identifier {
match User::find_by_device_for_email2fa(device_identifier, &conn).await {
Some(device_user) if device_user.email.to_lowercase() == email.to_lowercase() => {
// Device matches the requested email – allow the token to be sent.
// Log so operators can monitor how often this fallback path is used.
debug!(
"Email 2FA send-email-login: using device-identifier fallback for user '{}' \
(device: {}). No masterPasswordHash or authRequestId was provided.",
email, device_identifier
);
}
Some(_) => {
// Device exists but belongs to a different account – reject.
err!("Username or password is incorrect. Try again.")
}
None => {
// No device record found – cannot verify the caller.
err!("No password hash has been submitted.")
}
}
} else {
err!("No password hash has been submitted.")
} }
debug!(
"Email 2FA fallback: pending login. Username: {}. Device: {}.",
user.email,
device_identifier.to_string().escape_debug()
);
} else {
err!("No password hash has been submitted.")
} }
user user
@ -141,7 +124,7 @@ async fn send_email_login(data: Json<SendEmailLoginData>, client_headers: Client
let Some(user) = User::find_by_device_for_email2fa(device_identifier, &conn).await else { let Some(user) = User::find_by_device_for_email2fa(device_identifier, &conn).await else {
err!( err!(
"Username or password is incorrect. Try again", "Username or password is incorrect. Try again",
format!("IP: {}. Device: {device_identifier}.", client_headers.ip.ip) format!("IP: {}. Device: {}.", client_headers.ip.ip, device_identifier.to_string().escape_debug())
) )
}; };

Loading…
Cancel
Save