From c49f3bae67b535a46bd2b08c236d10b900eac882 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 16:41:41 -0600 Subject: [PATCH] fix(id): tag-key decoders reject non-3 field counts (#1832) decode_alpha_key/decode_omega_key guarded with fields.len() < 3 while erroring "expected 3": a 4+-field key decoded as a real tag in prefix listings but del_tag deletes the exact 3-field encoding, leaving a phantom undeletable tag. Both guards now require exactly 3 fields. Regression tests build 4-field alpha/omega keys with the tuple encoder and assert the expected-3 rejection. Also fixes #4044: the pkgs/id lib test target did not compile on main - the idset.rs test initializer for chain::ChainExt was missing the note field added by #3814. One line (note: None) in that test initializer; without it no pkgs/id test can run. --- pkgs/id/src/idset.rs | 1 + pkgs/id/src/tags.rs | 45 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/pkgs/id/src/idset.rs b/pkgs/id/src/idset.rs index 3ee02f0ad4..3fab81bb78 100644 --- a/pkgs/id/src/idset.rs +++ b/pkgs/id/src/idset.rs @@ -759,6 +759,7 @@ mod tests { tag: None, document: None, removes: None, + note: None, }, ) .unwrap(); diff --git a/pkgs/id/src/tags.rs b/pkgs/id/src/tags.rs index 2f48127a2d..54f121f70d 100644 --- a/pkgs/id/src/tags.rs +++ b/pkgs/id/src/tags.rs @@ -584,7 +584,7 @@ fn encode_omega_null_key_prefix(key: &[u8]) -> Vec { /// Handles both `TupleValue::String` and `TupleValue::Bytes` for binary support. fn decode_alpha_key(raw: &[u8]) -> Result<(TagValue, TagValue, Option)> { let fields = decode(raw)?; - if fields.len() < 3 { + if fields.len() != 3 { bail!("α key has {} fields, expected 3", fields.len()); } let subject = tuple_value_to_tag_value(&fields[0]) @@ -608,7 +608,7 @@ fn decode_alpha_key(raw: &[u8]) -> Result<(TagValue, TagValue, Option) /// back to `(subject, key, value?)`. Handles both String and Bytes variants. fn decode_omega_key(raw: &[u8]) -> Result<(TagValue, TagValue, Option)> { let fields = decode(raw)?; - if fields.len() < 3 { + if fields.len() != 3 { bail!("Ω key has {} fields, expected 3", fields.len()); } let value = if fields[0].is_null() { @@ -1942,6 +1942,47 @@ mod tests { assert_eq!(value, Some(TagValue::from("rust"))); } + /// Build a 4-field α-shaped key: a valid tag key plus a trailing field. + /// No writer emits this; it models the phantom key from issue #1832. + fn encode_alpha_key_4_fields() -> Vec { + let mut enc = TupleEncoder::new(); + encode_smart(&mut enc, b"file.txt"); + encode_smart(&mut enc, b"tag"); + encode_smart(&mut enc, b"v1"); + encode_smart(&mut enc, b"extra"); + enc.build() + } + + /// Build a 4-field Ω-shaped key: (value, key, subject) plus a trailing field. + fn encode_omega_key_4_fields() -> Vec { + let mut enc = TupleEncoder::new(); + encode_smart(&mut enc, b"v1"); + encode_smart(&mut enc, b"tag"); + encode_smart(&mut enc, b"file.txt"); + encode_smart(&mut enc, b"extra"); + enc.build() + } + + #[test] + fn test_decode_alpha_key_rejects_four_fields() { + let raw = encode_alpha_key_4_fields(); + let err = decode_alpha_key(&raw).unwrap_err(); + assert!( + err.to_string().contains("expected 3"), + "unexpected error: {err}" + ); + } + + #[test] + fn test_decode_omega_key_rejects_four_fields() { + let raw = encode_omega_key_4_fields(); + let err = decode_omega_key(&raw).unwrap_err(); + assert!( + err.to_string().contains("expected 3"), + "unexpected error: {err}" + ); + } + #[test] fn test_decode_omega_key_null() { let raw = encode_omega_key(b"file.txt", b"pinned", None);