diff --git a/pm-stack/aggregator/src/events.ts b/pm-stack/aggregator/src/events.ts index 80dfbb9e85..5e6e91a0b6 100644 --- a/pm-stack/aggregator/src/events.ts +++ b/pm-stack/aggregator/src/events.ts @@ -21,6 +21,7 @@ // never blocks or fails the underlying action, publish, or merge. import { mkdir, readFile, rename, writeFile } from "node:fs/promises"; import { dirname } from "node:path"; +import { isIsoDate } from "./validate.ts"; export interface StreamEvent { /** ISO timestamp of the event. */ @@ -51,7 +52,12 @@ const FIELD = /^[\w ./#:@-]+$/; export function validateStreamEvent(e: unknown): { ok: true; value: StreamEvent } | { ok: false; error: string } { if (typeof e !== "object" || e === null) return { ok: false, error: "event must be an object" }; const v = e as Record; - if (typeof v.at !== "string" || !Number.isFinite(Date.parse(v.at))) { + // Real calendar dates only (#5154): Date.parse rolls day-overflow + // dates over (2026-02-30 parses as March 2), so the shared isIsoDate + // primitive from validate.ts is the gate, like the DNS/charges + // boundaries. Genuine ISO timestamps (leap dates, offsets, fractional + // seconds) still pass. + if (typeof v.at !== "string" || !isIsoDate(v.at)) { return { ok: false, error: "event.at must be an ISO timestamp string" }; } // Identity fields align with their upstream contracts (#4489 Codex diff --git a/pm-stack/aggregator/test/events.test.ts b/pm-stack/aggregator/test/events.test.ts index 2646585cc3..522fb7383c 100644 --- a/pm-stack/aggregator/test/events.test.ts +++ b/pm-stack/aggregator/test/events.test.ts @@ -160,6 +160,22 @@ describe("events store (#1739)", () => { expect(recorded.actor).toBe("orchestrator (retry 2)"); }); + test("validateStreamEvent refuses impossible calendar dates (#5154)", () => { + // Date.parse rolls these over to a neighbouring month instead of + // returning NaN; the ingest boundary must refuse them outright. + expect(validateStreamEvent({ ...base, at: "2026-02-30" }).ok).toBe(false); + expect(validateStreamEvent({ ...base, at: "2026-02-30T10:00:00Z" }).ok).toBe(false); + expect(validateStreamEvent({ ...base, at: "2026-04-31T10:00:00Z" }).ok).toBe(false); + expect(validateStreamEvent({ ...base, at: "2026-13-01T00:00:00Z" }).ok).toBe(false); + expect(validateStreamEvent({ ...base, at: "2026-02-29T10:00:00Z" }).ok).toBe(false); + // Controls: real timestamps still validate — leap date, offset, + // fractional seconds, date-only. + expect(validateStreamEvent({ ...base, at: "2024-02-29T10:00:00Z" }).ok).toBe(true); + expect(validateStreamEvent({ ...base, at: "2026-11-01T10:00:00+02:00" }).ok).toBe(true); + expect(validateStreamEvent({ ...base, at: "2026-11-01T10:00:00.500Z" }).ok).toBe(true); + expect(validateStreamEvent({ ...base, at: "2026-02-28" }).ok).toBe(true); + }); + test("validateStreamEvent rejects malformed records", () => { expect(validateStreamEvent({ ...base, at: "2026-11-01T10:00:00.000Z" }).ok).toBe(true); expect(validateStreamEvent({ ...base, at: "not a date" }).ok).toBe(false);