diff --git a/pkgs/id/src/tag_claims.rs b/pkgs/id/src/tag_claims.rs index 30d7d4e469..c3180ac19e 100644 --- a/pkgs/id/src/tag_claims.rs +++ b/pkgs/id/src/tag_claims.rs @@ -119,7 +119,7 @@ pub fn is_tag_kind(kind: &str) -> bool { /// /// Returns [`TagError::KindMismatch`] when the operation is not the one `kind` names. Returns /// [`TagError::Field`] for a name or value that breaks its limit or the NFC rule, and for a clear -/// that names more than [`MAX_CLEAR_CLAIMS`] claims. +/// that names more than [`MAX_CLEAR_CLAIMS`] claims or names the same claim twice. pub fn check_op(kind: &str, op: &TagOp) -> Result<(), TagError> { match (kind, op) { (SET_KIND, TagOp::Set { name, value, .. }) => { @@ -136,6 +136,10 @@ pub fn check_op(kind: &str, op: &TagOp) -> Result<(), TagError> { claims.len() ))); } + let mut seen = HashSet::with_capacity(claims.len()); + if !claims.iter().all(|claim| seen.insert(claim)) { + return Err(TagError::Field("a clear names a claim twice".to_owned())); + } Ok(()) } _ => Err(TagError::KindMismatch { @@ -382,6 +386,34 @@ mod tests { assert!(check("a", Some("e\u{301}".to_owned())).is_err()); } + #[test] + fn test_a_clear_naming_a_claim_twice_is_refused() { + let adjacent = TagOp::Clear { + claims: vec![hash(1), hash(1)], + }; + assert!(matches!( + check_op(CLEAR_KIND, &adjacent), + Err(TagError::Field(_)) + )); + let non_adjacent = TagOp::Clear { + claims: vec![hash(1), hash(2), hash(1)], + }; + assert!(matches!( + check_op(CLEAR_KIND, &non_adjacent), + Err(TagError::Field(_)) + )); + } + + #[test] + fn test_a_clear_naming_max_distinct_claims_is_accepted() { + let op = TagOp::Clear { + claims: (0..MAX_CLEAR_CLAIMS) + .map(|n| Hash::digest(n.to_be_bytes())) + .collect(), + }; + assert!(check_op(CLEAR_KIND, &op).is_ok()); + } + #[test] fn test_a_clear_naming_too_many_claims_is_refused() { let op = TagOp::Clear {