Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 53 additions & 10 deletions pkgs/id/src/web/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,10 @@ fn parse_iso_timestamp(s: &str) -> Option<u64> {
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)),
Expand All @@ -214,13 +213,27 @@ fn parse_iso_timestamp(s: &str) -> Option<u64> {
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)
}
Expand Down Expand Up @@ -2459,6 +2472,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
Expand Down
Loading