From df2ea248b766b780258dbbf1506b6875bc9e07d4 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sat, 31 Oct 2026 09:26:54 -0500 Subject: [PATCH] fix(aggregator): validate dns/aa section stamps in health feedSections (#2242) feedSections blindly computed ageMs from Date.parse: a garbage dns generatedAt rendered NaN/null and a future aa generatedAt rendered age 0 (maximally fresh) for as long as the skew lasted. The from() helper now applies the #1564 convention already used for the top-level stamp: unparseable -> present:false with an invalid generatedAt (...) note; beyond GENERATED_AT_FUTURE_SKEW_MS (60s) in the future -> present:false with a descriptive note. isDnsSection/isAaSection additionally require Number.isFinite(Date.parse(generatedAt)). Regression tests cover garbage and future stamps for both sections plus the inside-skew live case; full aggregator suite 692 pass / 7 fail, identical fail set to clean main (690/7). --- pm-stack/aggregator/src/aa.ts | 4 ++- pm-stack/aggregator/src/dns.ts | 1 + pm-stack/aggregator/src/health.ts | 19 ++++++++++- pm-stack/aggregator/test/health.test.ts | 44 +++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 2 deletions(-) diff --git a/pm-stack/aggregator/src/aa.ts b/pm-stack/aggregator/src/aa.ts index b923b410d1..191ea8fc7d 100644 --- a/pm-stack/aggregator/src/aa.ts +++ b/pm-stack/aggregator/src/aa.ts @@ -1092,10 +1092,12 @@ export function aaSectionFromSupabase( export function isAaSection(v: unknown): v is AaFeedSection { if (typeof v !== "object" || v === null) return false; const s = v as Partial & Partial; - if (s.status === "absent") return typeof s.reason === "string" && typeof s.generatedAt === "string"; + if (s.status === "absent") + return typeof s.reason === "string" && typeof s.generatedAt === "string" && Number.isFinite(Date.parse(s.generatedAt)); if (s.status !== "ok") return false; return ( typeof s.generatedAt === "string" && + Number.isFinite(Date.parse(s.generatedAt)) && typeof s.batch === "string" && Array.isArray(s.rankings) && s.rankings.length > 0 && diff --git a/pm-stack/aggregator/src/dns.ts b/pm-stack/aggregator/src/dns.ts index 52eb3e8061..9ede55d8a1 100644 --- a/pm-stack/aggregator/src/dns.ts +++ b/pm-stack/aggregator/src/dns.ts @@ -76,6 +76,7 @@ export function isDnsSection(v: unknown): v is DnsSection { const s = v as Partial; return ( typeof s.generatedAt === "string" && + Number.isFinite(Date.parse(s.generatedAt)) && Array.isArray(s.entries) && s.entries.length > 0 && s.entries.every( diff --git a/pm-stack/aggregator/src/health.ts b/pm-stack/aggregator/src/health.ts index b2d45fa1d6..6fb339efbb 100644 --- a/pm-stack/aggregator/src/health.ts +++ b/pm-stack/aggregator/src/health.ts @@ -222,7 +222,24 @@ function feedSections(feed: FleetFeed | null, now: number): FeedSectionHealth[] if (!feed) return []; const from = (generatedAt: string | undefined, present: boolean, note?: string): Partial => { const ts = generatedAt ?? feed.generatedAt; - return { present, generatedAt: ts, ageMs: Math.max(0, now - Date.parse(ts)), ...(note ? { note } : {}) }; + const parsed = Date.parse(ts); + // Section stamps get the same validation as the top-level one + // (#2242, mirroring #1564): an unparseable stamp must not render + // as ageMs NaN/null, and a stamp beyond the future-skew bound + // must not render as ageMs 0 (maximally fresh) for as long as + // the skew lasts. Either way the section reports absent with a + // legible note instead of a fabricated age. + if (!Number.isFinite(parsed)) { + return { present: false, generatedAt: ts, note: `invalid generatedAt (${ts})` }; + } + if (parsed - now > GENERATED_AT_FUTURE_SKEW_MS) { + return { + present: false, + generatedAt: ts, + note: `generatedAt (${ts}) is ${ageMsText(parsed - now)} in the future — never fresh`, + }; + } + return { present, generatedAt: ts, ageMs: Math.max(0, now - parsed), ...(note ? { note } : {}) }; }; const aa = feed.aa; const aaNote = diff --git a/pm-stack/aggregator/test/health.test.ts b/pm-stack/aggregator/test/health.test.ts index c7ad29ca35..cf27f3822b 100644 --- a/pm-stack/aggregator/test/health.test.ts +++ b/pm-stack/aggregator/test/health.test.ts @@ -189,6 +189,50 @@ describe("buildHealth probes", () => { expect(view.degraded).toBe(true); }); + test("garbage dns/aa section stamps render absent with an invalid-generatedAt note, never NaN (#2242)", async () => { + const { fetcher } = fakeFetcher(allUp); + const feed = feedAt(5 * 60_000); + (feed.dns as { generatedAt: string }).generatedAt = "not-a-date"; + (feed as unknown as { aa: unknown }).aa = { status: "ok", generatedAt: "garbage!!", batch: "b", rankings: [], configs: [], testRuns: [] }; + const view = await buildHealth(inputs({ feed }), { fetcher, cache: createProbeCache(), now: NOW }); + const dns = view.feed.sections.find((x) => x.key === "dns"); + expect(dns?.present).toBe(false); + expect(dns?.note).toContain("invalid generatedAt (not-a-date)"); + expect(dns?.ageMs).toBeUndefined(); + const aa = view.feed.sections.find((x) => x.key === "aa"); + expect(aa?.present).toBe(false); + expect(aa?.note).toContain("invalid generatedAt (garbage!!)"); + expect(aa?.ageMs).toBeUndefined(); + // Untouched sections render exactly as before. + const orch = view.feed.sections.find((x) => x.key === "orchestration"); + expect(orch?.present).toBe(true); + expect(orch?.ageMs).toBe(5 * 60_000); + }); + + test("future dns/aa section stamps render absent with a future note, never age 0 (#2242)", async () => { + const { fetcher } = fakeFetcher(allUp); + const feed = feedAt(5 * 60_000); + const future = new Date(NOW + 3_600_000).toISOString(); + (feed.dns as { generatedAt: string }).generatedAt = future; + (feed as unknown as { aa: unknown }).aa = { status: "ok", generatedAt: future, batch: "b", rankings: [], configs: [], testRuns: [] }; + const view = await buildHealth(inputs({ feed }), { fetcher, cache: createProbeCache(), now: NOW }); + const dns = view.feed.sections.find((x) => x.key === "dns"); + expect(dns?.present).toBe(false); + expect(dns?.note).toContain("in the future"); + expect(dns?.ageMs).toBeUndefined(); + const aa = view.feed.sections.find((x) => x.key === "aa"); + expect(aa?.present).toBe(false); + expect(aa?.note).toContain("in the future"); + expect(aa?.ageMs).toBeUndefined(); + // A stamp inside the 60s skew bound still renders as a live section. + const feed2 = feedAt(5 * 60_000); + (feed2.dns as { generatedAt: string }).generatedAt = new Date(NOW + 30_000).toISOString(); + const view2 = await buildHealth(inputs({ feed: feed2 }), { fetcher, cache: createProbeCache(), now: NOW }); + const dns2 = view2.feed.sections.find((x) => x.key === "dns"); + expect(dns2?.present).toBe(true); + expect(dns2?.ageMs).toBe(0); + }); + test("fixtures mode never probes and reports fixtures for every service", async () => { const { fetcher, calls } = fakeFetcher(allUp); const view = await buildHealth(