Browse Source

Gate v2 logins on upstream's v2 encryption check

pull/7766/head
Daniel García 2 weeks ago
parent
commit
3228b1f076
  1. 14
      src/api/identity.rs
  2. 30
      src/db/models/user.rs

14
src/api/identity.rs

@ -520,16 +520,18 @@ async fn password_login(
authenticated_response(&user, &mut device, auth_tokens, twofactor_token, conn, ip).await authenticated_response(&user, &mut device, auth_tokens, twofactor_token, conn, ip).await
} }
/// Like upstream's `ClientVersionValidator`, every login needs the `Bitwarden-Client-Version` header, and a V2 account a /// Like upstream's `ClientVersionValidator`, every login needs the `Bitwarden-Client-Version` header, and a V2 account
/// client of at least 2026.10.1 (2026.10.2 for the desktop app). Called right after the credentials are checked, like /// ([`User::has_v2_encryption`]) a client of at least 2026.10.1 (2026.10.2 for the desktop app). Called right after the
/// upstream, so that it doesn't tell anyone else which accounts are V2. /// credentials are checked, like upstream, so that it doesn't tell anyone else which accounts are V2.
/// ///
/// Ref: <https://github.com/bitwarden/server/blob/15c94ac58bf361a162381749c146d254a21b2098/src/Identity/IdentityServer/RequestValidators/ClientVersionValidator.cs#L33-L79> /// Ref: <https://github.com/bitwarden/server/blob/15c94ac58bf361a162381749c146d254a21b2098/src/Identity/IdentityServer/RequestValidators/ClientVersionValidator.cs#L33-L79>
/// Ref: <https://github.com/bitwarden/server/blob/15c94ac58bf361a162381749c146d254a21b2098/src/Core/KeyManagement/Constants.cs#L5-L6> /// Ref: <https://github.com/bitwarden/server/blob/15c94ac58bf361a162381749c146d254a21b2098/src/Core/KeyManagement/Constants.cs#L5-L6>
fn check_client_version(user: &User, client_header: &ClientHeaders) -> EmptyResult { fn check_client_version(user: &User, client_header: &ClientHeaders) -> EmptyResult {
let Some((error, description)) = let Some((error, description)) = client_version_error(
client_version_error(user.is_v2(), client_header.client_version.as_ref(), client_header.device_type) user.has_v2_encryption(),
else { client_header.client_version.as_ref(),
client_header.device_type,
) else {
return Ok(()); return Ok(());
}; };
Err(crate::error::Error::from(( Err(crate::error::Error::from((

30
src/db/models/user.rs

@ -194,6 +194,19 @@ impl User {
self.signed_public_key.is_some() && self.security_state.is_some() && self.security_version.is_some() self.signed_public_key.is_some() && self.security_state.is_some() && self.security_version.is_some()
} }
/// Upstream's `HasV2Encryption`: a private key wrapped as `CoseEncrypt0B64` (type 7) and security version 2.
/// Upstream only uses it to decide which accounts need a v2-capable client.
///
/// Ref: <https://github.com/bitwarden/server/blob/8da186ad73f80d37157fe6fd42323e4284e83ec1/src/Core/Entities/User.cs#L245-L278>
pub fn has_v2_encryption(&self) -> bool {
self.security_version == Some(2)
&& self
.private_key
.as_deref()
.and_then(|private_key| private_key.split_once('.'))
.is_some_and(|(enc_type, _)| enc_type.parse::<u8>() == Ok(7))
}
pub fn check_valid_password(&self, password: &str) -> bool { pub fn check_valid_password(&self, password: &str) -> bool {
crypto::verify_password_hash( crypto::verify_password_hash(
password.as_bytes(), password.as_bytes(),
@ -751,4 +764,21 @@ mod tests {
// An absent key id stays optional // An absent key id stays optional
assert_eq!(serde_json::from_str::<Option<KeyId>>("null").unwrap(), None); assert_eq!(serde_json::from_str::<Option<KeyId>>("null").unwrap(), None);
} }
#[test]
fn test_has_v2_encryption() {
let user = |private_key: Option<&str>, security_version: Option<i32>| {
let mut user = User::new("user@example.com", None);
user.private_key = private_key.map(String::from);
user.security_version = security_version;
user
};
assert!(user(Some("7.cose"), Some(2)).has_v2_encryption());
assert!(!user(Some("7.cose"), Some(1)).has_v2_encryption());
assert!(!user(Some("7.cose"), None).has_v2_encryption());
assert!(!user(Some("2.iv|data|mac"), Some(2)).has_v2_encryption());
assert!(!user(Some("7"), Some(2)).has_v2_encryption());
assert!(!user(None, Some(2)).has_v2_encryption());
}
} }

Loading…
Cancel
Save