Skip to content

Commit

Permalink
fix(crypto): Fix error when reading VerifiedStateOrBool with old Prev…
Browse files Browse the repository at this point in the history
…iouslyVerifiedButNoLonger value
  • Loading branch information
andybalaam committed Dec 18, 2024
1 parent 901003b commit 67ed617
Showing 1 changed file with 58 additions and 19 deletions.
77 changes: 58 additions & 19 deletions crates/matrix-sdk-crypto/src/identities/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,7 @@ enum OwnUserIdentityVerifiedState {
NeverVerified,

/// We previously verified this identity, but it has changed.
#[serde(alias = "PreviouslyVerifiedButNoLonger")]
VerificationViolation,

/// We have verified the current identity.
Expand Down Expand Up @@ -1539,26 +1540,10 @@ pub(crate) mod tests {
/// that we can deserialize boolean values.
#[test]
fn test_deserialize_own_user_identity_bool_verified() {
let mut json = json!({
"user_id": "@example:localhost",
"master_key": {
"user_id":"@example:localhost",
"usage":["master"],
"keys":{"ed25519:rJ2TAGkEOP6dX41Ksll6cl8K3J48l8s/59zaXyvl2p0":"rJ2TAGkEOP6dX41Ksll6cl8K3J48l8s/59zaXyvl2p0"},
},
"self_signing_key": {
"user_id":"@example:localhost",
"usage":["self_signing"],
"keys":{"ed25519:0C8lCBxrvrv/O7BQfsKnkYogHZX3zAgw3RfJuyiq210":"0C8lCBxrvrv/O7BQfsKnkYogHZX3zAgw3RfJuyiq210"}
},
"user_signing_key": {
"user_id":"@example:localhost",
"usage":["user_signing"],
"keys":{"ed25519:DU9z4gBFKFKCk7a13sW9wjT0Iyg7Hqv5f0BPM7DEhPo":"DU9z4gBFKFKCk7a13sW9wjT0Iyg7Hqv5f0BPM7DEhPo"}
},
"verified": false
});
let mut json = own_user_identity_data();

// Set `"verified": false`
*json.get_mut("verified").unwrap() = false.into();
let id: OwnUserIdentityData = serde_json::from_value(json.clone()).unwrap();
assert_eq!(*id.verified.read().unwrap(), OwnUserIdentityVerifiedState::NeverVerified);

Expand All @@ -1568,6 +1553,38 @@ pub(crate) mod tests {
assert_eq!(*id.verified.read().unwrap(), OwnUserIdentityVerifiedState::Verified);
}

#[test]
fn test_own_user_identity_verified_state_verification_violation_deserializes() {
// Given data containing verified: VerificationViolation
let mut json = own_user_identity_data();
*json.get_mut("verified").unwrap() = "VerificationViolation".into();

// When we deserialize
let id: OwnUserIdentityData = serde_json::from_value(json.clone()).unwrap();

// Then the value is correctly populated
assert_eq!(
*id.verified.read().unwrap(),
OwnUserIdentityVerifiedState::VerificationViolation
);
}

#[test]
fn test_own_user_identity_verified_state_previously_verified_deserializes() {
// Given data containing verified: PreviouslyVerifiedButNoLonger
let mut json = own_user_identity_data();
*json.get_mut("verified").unwrap() = "PreviouslyVerifiedButNoLonger".into();

// When we deserialize
let id: OwnUserIdentityData = serde_json::from_value(json.clone()).unwrap();

// Then the old value is re-interpreted as VerificationViolation
assert_eq!(
*id.verified.read().unwrap(),
OwnUserIdentityVerifiedState::VerificationViolation
);
}

#[test]
fn own_identity_check_signatures() {
let response = own_key_query();
Expand Down Expand Up @@ -1943,4 +1960,26 @@ pub(crate) mod tests {
assert!(!own_identity.was_previously_verified());
assert!(!own_identity.has_verification_violation());
}

fn own_user_identity_data() -> Value {
json!({
"user_id": "@example:localhost",
"master_key": {
"user_id":"@example:localhost",
"usage":["master"],
"keys":{"ed25519:rJ2TAGkEOP6dX41Ksll6cl8K3J48l8s/59zaXyvl2p0":"rJ2TAGkEOP6dX41Ksll6cl8K3J48l8s/59zaXyvl2p0"},
},
"self_signing_key": {
"user_id":"@example:localhost",
"usage":["self_signing"],
"keys":{"ed25519:0C8lCBxrvrv/O7BQfsKnkYogHZX3zAgw3RfJuyiq210":"0C8lCBxrvrv/O7BQfsKnkYogHZX3zAgw3RfJuyiq210"}
},
"user_signing_key": {
"user_id":"@example:localhost",
"usage":["user_signing"],
"keys":{"ed25519:DU9z4gBFKFKCk7a13sW9wjT0Iyg7Hqv5f0BPM7DEhPo":"DU9z4gBFKFKCk7a13sW9wjT0Iyg7Hqv5f0BPM7DEhPo"}
},
"verified": false
})
}
}

0 comments on commit 67ed617

Please sign in to comment.