From 39dbb66f372d1a06405a0413adb83169537ff418 Mon Sep 17 00:00:00 2001 From: Muse Sweep Date: Sat, 24 Oct 2026 07:20:00 -0500 Subject: [PATCH] fix(id-web): reject out-of-range dates in parse_iso_timestamp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit parse_iso_timestamp rolled invalid components over into a different, plausible timestamp instead of returning None: month 13 became next January, day 0 equalled day 1, Feb 30 became Mar 2, hour 25/min 60/sec 60 rolled over, and a pre-1970 date collapsed to Some(0) — the epoch. Validate year (1970..=9999, which also bounds the per-year loop), month, day against the leap-aware month length, and hour/min/sec. Fixes #756 --- pkgs/id/src/web/routes.rs | 63 ++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/pkgs/id/src/web/routes.rs b/pkgs/id/src/web/routes.rs index fbaeb4c25..5f0a88ed2 100644 --- a/pkgs/id/src/web/routes.rs +++ b/pkgs/id/src/web/routes.rs @@ -170,11 +170,10 @@ fn parse_iso_timestamp(s: &str) -> Option { let (year, month, day) = (date_parts[0], date_parts[1], date_parts[2]); let (hour, min, sec) = (time_parts[0], time_parts[1], time_parts[2]); - // Simple days-since-epoch calculation (not accounting for leap seconds) - let mut days: u64 = 0; - for y in 1970..year { - days += if is_leap_year(y) { 366 } else { 365 }; - } + // Reject anything the arithmetic below cannot represent faithfully + // instead of rolling it over into a different date (#756). Generated + // names carry a 4-digit year; the u64 result cannot represent dates + // before the epoch, and a leap second (second 60) is not modelled. let month_days = [ 31, 28 + u64::from(is_leap_year(year)), @@ -189,13 +188,27 @@ fn parse_iso_timestamp(s: &str) -> Option { 30, 31, ]; - #[allow(clippy::cast_possible_truncation)] // month is max 12, fits in usize + if !(1970..=9999).contains(&year) + || !(1..=12).contains(&month) + || day == 0 + || day > month_days[(month - 1) as usize] + || hour > 23 + || min > 59 + || sec > 59 + { + return None; + } + + // Simple days-since-epoch calculation (not accounting for leap seconds) + let mut days: u64 = 0; + for y in 1970..year { + days += if is_leap_year(y) { 366 } else { 365 }; + } + #[allow(clippy::cast_possible_truncation)] // month is 1..=12, fits in usize for m in 0..(month.saturating_sub(1) as usize) { - if m < month_days.len() { - days += month_days[m]; - } + days += month_days[m]; } - days += day.saturating_sub(1); + days += day - 1; Some(days * 86400 + hour * 3600 + min * 60 + sec) } @@ -2249,6 +2262,36 @@ mod tests { assert!(parse_iso_timestamp("noprefix").is_none()); } + #[test] + fn test_parse_iso_timestamp_rejects_out_of_range_components() { + // Each of these used to roll over into a different, plausible + // timestamp instead of returning None (#756). + assert!(parse_iso_timestamp("2026-13-01T00:00:00Z").is_none()); + assert!(parse_iso_timestamp("2026-00-01T00:00:00Z").is_none()); + assert!(parse_iso_timestamp("2026-03-00T00:00:00Z").is_none()); + assert!(parse_iso_timestamp("2026-03-32T00:00:00Z").is_none()); + assert!(parse_iso_timestamp("2026-02-30T00:00:00Z").is_none()); + assert!(parse_iso_timestamp("2026-03-12T25:00:00Z").is_none()); + assert!(parse_iso_timestamp("2026-03-12T00:60:00Z").is_none()); + assert!(parse_iso_timestamp("2026-03-12T00:00:60Z").is_none()); + // Before the epoch the u64 result cannot represent the date; + // it used to collapse to Some(0), the same value as the epoch. + assert!(parse_iso_timestamp("1969-01-01T00:00:00Z").is_none()); + // Absurd years must not be accepted (or iterated year by year). + assert!(parse_iso_timestamp("999999999-01-01T00:00:00Z").is_none()); + } + + #[test] + fn test_parse_iso_timestamp_leap_day() { + // 2024 is a leap year: Feb 29 is real and must keep parsing. + assert_eq!( + parse_iso_timestamp("2024-02-29T00:00:00Z"), + Some(1_709_164_800) + ); + // 2026 is not: Feb 29 does not exist. + assert!(parse_iso_timestamp("2026-02-29T00:00:00Z").is_none()); + } + #[test] fn test_parse_iso_timestamp_epoch() { // 1970-01-01T00:00:00.000Z should be 0