diff --git a/pkgs/id/src/didit/body.rs b/pkgs/id/src/didit/body.rs index ffd7198eba..6560edd562 100644 --- a/pkgs/id/src/didit/body.rs +++ b/pkgs/id/src/didit/body.rs @@ -56,6 +56,12 @@ pub const MAX_SUBJECTS: usize = 512; /// The most per-subject detail entries one record may carry. pub const MAX_DETAILS: usize = 512; +/// The most aliases one entity may carry. +pub const MAX_ALIASES: usize = 512; + +/// The most hours spans one hours window may list. +pub const MAX_HOURS_SPANS: usize = 512; + /// Nesting limit for a body's CBOR. Bodies are shallow by construction. const RECURSION_LIMIT: u16 = 8; @@ -92,10 +98,17 @@ impl Entity { /// # Errors /// /// Returns [`DiditError::Invalid`] for an empty or over-long text field, - /// text that is not NFC, or a repeated alias. + /// text that is not NFC, a repeated alias, or more aliases than + /// [`MAX_ALIASES`]. pub fn check(&self) -> Result<(), DiditError> { check_text(&self.kind, "kind", true)?; check_text(&self.name, "name", true)?; + if self.aliases.len() > MAX_ALIASES { + return Err(DiditError::Invalid(format!( + "entity aliases has {} entries, the most is {MAX_ALIASES}", + self.aliases.len() + ))); + } let mut seen = HashSet::new(); for alias in &self.aliases { check_text(alias, "alias", false)?; @@ -476,6 +489,12 @@ impl ActiveWindow { "an hours window lists no spans".to_owned(), )); } + if hours.len() > MAX_HOURS_SPANS { + return Err(DiditError::Invalid(format!( + "hours spans has {} entries, the most is {MAX_HOURS_SPANS}", + hours.len() + ))); + } for span in hours { span.check()?; } diff --git a/pkgs/id/src/didit/tests.rs b/pkgs/id/src/didit/tests.rs index abfb1d5c50..3251a486bb 100644 --- a/pkgs/id/src/didit/tests.rs +++ b/pkgs/id/src/didit/tests.rs @@ -1050,3 +1050,70 @@ fn interval_gap_crossing_sleep_window_seam_is_judged_across_it() { assert!(seam[0].detail.contains("6300"), "{}", seam[0].detail); assert_eq!(seam[0].window_end, day + 86_400 + 3_600); } + +#[test] +fn entity_alias_count_is_capped() { + use crate::didit::body::MAX_ALIASES; + // Oracle: the named cap MAX_ALIASES (mirroring MAX_SUBJECTS/MAX_DETAILS), + // sized well under the CBOR decoder's 8192-element LENGTH_LIMIT — a body + // at the cap round-trips, one alias over is refused by encode with an + // error naming the count and the limit. + let mut entity = Entity { + entity: dog_a(), + household: household(), + kind: "dog".to_owned(), + name: "Ada".to_owned(), + aliases: (0..MAX_ALIASES).map(|i| format!("alias-{i}")).collect(), + retired_at: None, + }; + let bytes = encode_entity_body(&entity).expect("at the cap encodes"); + assert_eq!(decode_entity_body(&bytes).expect("decode"), entity); + + entity.aliases.push("one-too-many".to_owned()); + assert!(matches!( + encode_entity_body(&entity).expect_err("over the cap is refused"), + DiditError::Invalid(message) + if message.contains("513") && message.contains("512") + )); +} + +#[test] +fn hours_span_count_is_capped() { + use crate::didit::body::MAX_HOURS_SPANS; + // Oracle: the named cap MAX_HOURS_SPANS, same shape as the alias cap — + // an expectation at the cap round-trips, one span over is refused by + // encode with an error naming the count and the limit. + let span = HoursSpan { + from_minute: 420, + to_minute: 1320, + days: None, + }; + let mut fed = expectation( + 0x61, + vec![dog_a()], + "fed", + Window::CountPerPeriod { + count: 1, + period: Period::Custom(PeriodCustom { + anchor: midnight(2026, 6, 15), + length_seconds: 86_400, + }), + }, + ); + fed.active = ActiveWindow::Hours { + hours: vec![span.clone(); MAX_HOURS_SPANS], + tz: CHICAGO.to_owned(), + }; + let bytes = encode_expectation_body(&fed).expect("at the cap encodes"); + assert_eq!(decode_expectation_body(&bytes).expect("decode"), fed); + + let ActiveWindow::Hours { hours, .. } = &mut fed.active else { + panic!("the expectation carries an hours window"); + }; + hours.push(span); + assert!(matches!( + encode_expectation_body(&fed).expect_err("over the cap is refused"), + DiditError::Invalid(message) + if message.contains("513") && message.contains("512") + )); +}