From 98ab5efcc82397e6a37706635d02ffcb291b93c1 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 03:56:00 -0600 Subject: [PATCH 1/2] id(range-sync): reject non-minimal varints in Reader::varint (#2596) put_varint emits only minimal base-128 encodings, but Reader::varint accepted a multi-byte spelling ending in a zero payload group (0x81 0x00 for 1), giving one value multiple wire spellings in all three positions that read a varint: the frame entry count, the Fp count, and the Ids count. Refuse a multi-byte varint whose final payload group is zero. Tests cover the width boundaries (0/1/127/128/300 minimal spellings stay accepted), overlong spellings at the reader, and overlong counts in each of the three wire positions via decode_frame. --- pkgs/id/src/event_store/range.rs | 49 +++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/pkgs/id/src/event_store/range.rs b/pkgs/id/src/event_store/range.rs index b1f1f1ba3e..f6ea3dda76 100644 --- a/pkgs/id/src/event_store/range.rs +++ b/pkgs/id/src/event_store/range.rs @@ -488,10 +488,18 @@ impl<'a> Reader<'a> { fn varint(&mut self) -> Result { let mut x = 0usize; - for shift in (0..).step_by(7).take(5) { + for (i, shift) in (0..).step_by(7).take(5).enumerate() { let b = self.byte()?; x |= ((b & 0x7f) as usize) << shift; if b < 0x80 { + // Minimal form only: put_varint never emits a trailing + // zero payload group, so a multi-byte spelling that ends + // in one (0x81 0x00 for 1) is a second wire spelling of + // the same value and is refused (#2596). + ensure!( + i == 0 || b != 0, + "varint is not minimally encoded" + ); return Ok(x); } } @@ -989,6 +997,45 @@ mod tests { ); } + /// #2596: Reader::varint accepts only the minimal base-128 spelling + /// put_varint emits. Width boundaries stay accepted; an overlong + /// spelling is refused at the reader and in all three wire positions + /// that read a varint (frame entry count, Fp count, Ids count). + #[test] + fn test_varint_refuses_non_minimal_encodings() { + let read = |bytes: &[u8]| { + let mut r = Reader { buf: bytes }; + r.varint() + }; + // Minimal spellings at the width boundaries. + assert_eq!(read(&[0]).unwrap(), 0); + assert_eq!(read(&[1]).unwrap(), 1); + assert_eq!(read(&[0x7f]).unwrap(), 127); + assert_eq!(read(&[0x80, 0x01]).unwrap(), 128); + assert_eq!(read(&[0xac, 0x02]).unwrap(), 300); + // Overlong spellings of the same values. + assert!(read(&[0x80, 0x00]).is_err(), "0 spelled in 2 bytes"); + assert!(read(&[0x81, 0x00]).is_err(), "1 spelled in 2 bytes"); + assert!(read(&[0xff, 0x80, 0x00]).is_err(), "127 spelled in 3 bytes"); + + // Position 1: the frame header entry count. + let mut frame = vec![0u8, 0x81, 0x00]; + frame.extend_from_slice(&[0, 0]); // one Skip entry, +infinity bound + assert!(decode_frame(&frame).is_err(), "overlong frame count"); + + // Position 2: the Fp count inside an entry. + let mut frame = vec![0u8, 1, 0, 1]; + frame.extend_from_slice(&[7u8; 16]); // fingerprint + frame.extend_from_slice(&[0x81, 0x00]); // overlong count of 1 + assert!(decode_frame(&frame).is_err(), "overlong Fp count"); + + // Position 3: the Ids count inside an entry. + let mut frame = vec![0u8, 1, 0, 2]; + frame.extend_from_slice(&[0x81, 0x00]); // overlong count of 1 + frame.extend_from_slice(&[9u8; 32]); // the one ID + assert!(decode_frame(&frame).is_err(), "overlong Ids count"); + } + #[test] fn test_bounds_must_increase_and_end_at_infinity() { let skip = |upper: Option>| Entry { From 4ecb5513b9a53bcab0b862aaed270d57d8ec9dcc Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 05:04:48 -0600 Subject: [PATCH 2/2] test(id): cover range varint width boundaries --- pkgs/id/src/event_store/range.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkgs/id/src/event_store/range.rs b/pkgs/id/src/event_store/range.rs index f6ea3dda76..926e497383 100644 --- a/pkgs/id/src/event_store/range.rs +++ b/pkgs/id/src/event_store/range.rs @@ -1012,10 +1012,14 @@ mod tests { assert_eq!(read(&[1]).unwrap(), 1); assert_eq!(read(&[0x7f]).unwrap(), 127); assert_eq!(read(&[0x80, 0x01]).unwrap(), 128); + assert_eq!(read(&[0xff, 0x7f]).unwrap(), 0x3fff); + assert_eq!(read(&[0x80, 0x80, 0x01]).unwrap(), 0x4000); assert_eq!(read(&[0xac, 0x02]).unwrap(), 300); // Overlong spellings of the same values. assert!(read(&[0x80, 0x00]).is_err(), "0 spelled in 2 bytes"); assert!(read(&[0x81, 0x00]).is_err(), "1 spelled in 2 bytes"); + assert!(read(&[0xff, 0xff, 0x00]).is_err(), "0x3fff spelled in 3 bytes"); + assert!(read(&[0x80, 0x80, 0x81, 0x00]).is_err(), "0x4000 spelled in 4 bytes"); assert!(read(&[0xff, 0x80, 0x00]).is_err(), "127 spelled in 3 bytes"); // Position 1: the frame header entry count.