diff --git a/services/identity/src/device_list.rs b/services/identity/src/device_list.rs --- a/services/identity/src/device_list.rs +++ b/services/identity/src/device_list.rs @@ -326,6 +326,12 @@ == 1 } + /// Verifies if the device list contains duplicated device IDs + fn has_duplicates(device_list: &[&str]) -> bool { + let devices_set: HashSet<&str> = device_list.iter().copied().collect(); + devices_set.len() != device_list.len() + } + // This is going to be used when doing primary devicd keys rotation #[allow(unused)] pub fn primary_device_rotation_validator( @@ -347,6 +353,10 @@ return false; } + if has_duplicates(new_device_list) { + return false; + } + // allow replacing a keyserver if is_device_replaced(previous_device_list, new_device_list) { return true; @@ -407,6 +417,17 @@ assert!(is_device_replaced(&list3, &list3).not(), "Unchanged"); assert!(is_device_replaced(&list3, &list4).not(), "Reorder"); } + + #[test] + fn test_duplicated_devices() { + use std::ops::Not; + + let list1 = vec!["device1", "device2", "device3"]; + let list2 = vec!["device1", "device2", "device2"]; + + assert!(has_duplicates(&list1).not(), "No duplicates"); + assert!(has_duplicates(&list2), "With duplicates"); + } } }