Skip to content

Commit b18e7d7

Browse files
committed
fix(crypto): Fix error when reading VerifiedStateOrBool with old PreviouslyVerifiedButNoLonger value
1 parent 612ba6f commit b18e7d7

File tree

1 file changed

+58
-19
lines changed
  • crates/matrix-sdk-crypto/src/identities

1 file changed

+58
-19
lines changed

crates/matrix-sdk-crypto/src/identities/user.rs

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,7 @@ enum OwnUserIdentityVerifiedState {
911911
NeverVerified,
912912

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

916917
/// We have verified the current identity.
@@ -1530,26 +1531,10 @@ pub(crate) mod tests {
15301531
/// that we can deserialize boolean values.
15311532
#[test]
15321533
fn test_deserialize_own_user_identity_bool_verified() {
1533-
let mut json = json!({
1534-
"user_id": "@example:localhost",
1535-
"master_key": {
1536-
"user_id":"@example:localhost",
1537-
"usage":["master"],
1538-
"keys":{"ed25519:rJ2TAGkEOP6dX41Ksll6cl8K3J48l8s/59zaXyvl2p0":"rJ2TAGkEOP6dX41Ksll6cl8K3J48l8s/59zaXyvl2p0"},
1539-
},
1540-
"self_signing_key": {
1541-
"user_id":"@example:localhost",
1542-
"usage":["self_signing"],
1543-
"keys":{"ed25519:0C8lCBxrvrv/O7BQfsKnkYogHZX3zAgw3RfJuyiq210":"0C8lCBxrvrv/O7BQfsKnkYogHZX3zAgw3RfJuyiq210"}
1544-
},
1545-
"user_signing_key": {
1546-
"user_id":"@example:localhost",
1547-
"usage":["user_signing"],
1548-
"keys":{"ed25519:DU9z4gBFKFKCk7a13sW9wjT0Iyg7Hqv5f0BPM7DEhPo":"DU9z4gBFKFKCk7a13sW9wjT0Iyg7Hqv5f0BPM7DEhPo"}
1549-
},
1550-
"verified": false
1551-
});
1534+
let mut json = own_user_identity_data();
15521535

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

@@ -1559,6 +1544,38 @@ pub(crate) mod tests {
15591544
assert_eq!(*id.verified.read().unwrap(), OwnUserIdentityVerifiedState::Verified);
15601545
}
15611546

1547+
#[test]
1548+
fn test_own_user_identity_verified_state_verification_violation_deserializes() {
1549+
// Given data containing verified: VerificationViolation
1550+
let mut json = own_user_identity_data();
1551+
*json.get_mut("verified").unwrap() = "VerificationViolation".into();
1552+
1553+
// When we deserialize
1554+
let id: OwnUserIdentityData = serde_json::from_value(json.clone()).unwrap();
1555+
1556+
// Then the value is correctly populated
1557+
assert_eq!(
1558+
*id.verified.read().unwrap(),
1559+
OwnUserIdentityVerifiedState::VerificationViolation
1560+
);
1561+
}
1562+
1563+
#[test]
1564+
fn test_own_user_identity_verified_state_previously_verified_deserializes() {
1565+
// Given data containing verified: PreviouslyVerifiedButNoLonger
1566+
let mut json = own_user_identity_data();
1567+
*json.get_mut("verified").unwrap() = "PreviouslyVerifiedButNoLonger".into();
1568+
1569+
// When we deserialize
1570+
let id: OwnUserIdentityData = serde_json::from_value(json.clone()).unwrap();
1571+
1572+
// Then the old value is re-interpreted as VerificationViolation
1573+
assert_eq!(
1574+
*id.verified.read().unwrap(),
1575+
OwnUserIdentityVerifiedState::VerificationViolation
1576+
);
1577+
}
1578+
15621579
#[test]
15631580
fn own_identity_check_signatures() {
15641581
let response = own_key_query();
@@ -1895,4 +1912,26 @@ pub(crate) mod tests {
18951912
assert!(!own_identity.was_previously_verified());
18961913
assert!(!own_identity.has_verification_violation());
18971914
}
1915+
1916+
fn own_user_identity_data() -> Value {
1917+
json!({
1918+
"user_id": "@example:localhost",
1919+
"master_key": {
1920+
"user_id":"@example:localhost",
1921+
"usage":["master"],
1922+
"keys":{"ed25519:rJ2TAGkEOP6dX41Ksll6cl8K3J48l8s/59zaXyvl2p0":"rJ2TAGkEOP6dX41Ksll6cl8K3J48l8s/59zaXyvl2p0"},
1923+
},
1924+
"self_signing_key": {
1925+
"user_id":"@example:localhost",
1926+
"usage":["self_signing"],
1927+
"keys":{"ed25519:0C8lCBxrvrv/O7BQfsKnkYogHZX3zAgw3RfJuyiq210":"0C8lCBxrvrv/O7BQfsKnkYogHZX3zAgw3RfJuyiq210"}
1928+
},
1929+
"user_signing_key": {
1930+
"user_id":"@example:localhost",
1931+
"usage":["user_signing"],
1932+
"keys":{"ed25519:DU9z4gBFKFKCk7a13sW9wjT0Iyg7Hqv5f0BPM7DEhPo":"DU9z4gBFKFKCk7a13sW9wjT0Iyg7Hqv5f0BPM7DEhPo"}
1933+
},
1934+
"verified": false
1935+
})
1936+
}
18981937
}

0 commit comments

Comments
 (0)