Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion pm-stack/aggregator/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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<string, unknown>;
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
Expand Down
16 changes: 16 additions & 0 deletions pm-stack/aggregator/test/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down