From 3a6fbbffaefdb6c7066250fad6f2d6480ec24e67 Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Tue, 3 Nov 2026 18:35:39 -0600 Subject: [PATCH] fix(aggregator): validate audit ingest dates --- pm-stack/aggregator/src/events.ts | 2 +- pm-stack/aggregator/test/events.test.ts | 38 ++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/pm-stack/aggregator/src/events.ts b/pm-stack/aggregator/src/events.ts index b4278ff3d9..ba2a79f050 100644 --- a/pm-stack/aggregator/src/events.ts +++ b/pm-stack/aggregator/src/events.ts @@ -305,7 +305,7 @@ function isAuditEvent(value: unknown): value is AuditEvent { return ( v.version === 2 && typeof v.id === "string" && v.id !== "" && - typeof v.ingestedAt === "string" && Number.isFinite(Date.parse(v.ingestedAt)) && + typeof v.ingestedAt === "string" && isIsoDate(v.ingestedAt) && typeof v.prevHash === "string" && typeof v.hash === "string" ); } diff --git a/pm-stack/aggregator/test/events.test.ts b/pm-stack/aggregator/test/events.test.ts index 54d805ad24..ddbe815101 100644 --- a/pm-stack/aggregator/test/events.test.ts +++ b/pm-stack/aggregator/test/events.test.ts @@ -7,14 +7,16 @@ import { describe, expect, test } from "bun:test"; import { mkdtempSync } from "node:fs"; import { tmpdir } from "node:os"; -import { join } from "node:path"; +import { dirname, join } from "node:path"; import { EVENT_RETENTION_MAX, + eventHash, EventsStore, emitEvent, filterEvents, pruneEvents, validateStreamEvent, + type AuditEvent, type StreamEvent, } from "../src/events.ts"; @@ -212,6 +214,40 @@ import { readFileSync, writeFileSync, mkdirSync, rmSync, existsSync, readdirSync import { verifyEventChain } from "../src/events.ts"; describe("events audit v2 (#3322 slice A)", () => { + async function writeV2WithIngestedAt(ingestedAt: string) { + const { file, store } = tmpStore(); + await store.record({ ...base, at: new Date(Date.now() - 60_000).toISOString() }); + const stored = JSON.parse(readFileSync(file, "utf8")) as { version: 2; events: AuditEvent[] }; + const event = { ...stored.events[0]!, ingestedAt }; + event.hash = eventHash(event.prevHash, event); + const raw = JSON.stringify({ ...stored, events: [event] }, null, 2); + writeFileSync(file, raw); + return { file, dir: dirname(file), raw }; + } + + test.each([ + ["impossible November day", "2026-11-31T00:00:00.000Z"], + ["impossible February day", "2026-02-30T00:00:00.000Z"], + ["non-ISO date spelling", "November 2, 2026"], + ])("a v2 record with %s ingestedAt is quarantined", async (_label, ingestedAt) => { + const { file, dir, raw } = await writeV2WithIngestedAt(ingestedAt); + expect(await new EventsStore(file).list()).toEqual([]); + const quarantined = readdirSync(dir).filter((name) => name.startsWith("events.json.corrupt-")); + expect(quarantined).toHaveLength(1); + expect(readFileSync(join(dir, quarantined[0]!), "utf8")).toBe(raw); + }); + + test.each([ + ["fractional seconds", "2026-11-01T10:00:00.500Z"], + ["offset", "2026-11-01T10:00:00+02:00"], + ])("a v2 record with valid ISO ingestedAt (%s) loads", async (_label, ingestedAt) => { + const { file, dir } = await writeV2WithIngestedAt(ingestedAt); + const store = new EventsStore(file); + expect((await store.list()).map((event) => event.ingestedAt)).toEqual([ingestedAt]); + expect((await store.health()).integrity.ok).toBe(true); + expect(readdirSync(dir).some((name) => name.startsWith("events.json.corrupt-"))).toBe(false); + }); + test("record stamps v2 audit fields: stable id, ingest time, attribution, source, hash link", async () => { const { store } = tmpStore(); const at = new Date(Date.now() - 60_000).toISOString();