From fd1321d441039d2822adc0f99240704b195ed488 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 22:54:56 -0600 Subject: [PATCH] fix(aggregator/dns): isDnsSection owns finite-number + status rules (#2412) totalAnnualListUsd must be a finite number and entry priceUsd must be finite when present (null/absent stays valid); entry status must be active/earmarked/unused. A poisoned feed section is now rejected at the shared gate instead of crashing /dns and the usage view on .toFixed. Regression tests in slice3.test.ts cover missing/string/null/NaN/Infinity totals, string/NaN priceUsd, and unknown/missing status. --- pm-stack/aggregator/src/dns.ts | 18 +++++++++++++- pm-stack/aggregator/test/slice3.test.ts | 32 ++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/pm-stack/aggregator/src/dns.ts b/pm-stack/aggregator/src/dns.ts index 9ede55d8a1..b597a9d30d 100644 --- a/pm-stack/aggregator/src/dns.ts +++ b/pm-stack/aggregator/src/dns.ts @@ -77,6 +77,12 @@ export function isDnsSection(v: unknown): v is DnsSection { return ( typeof s.generatedAt === "string" && Number.isFinite(Date.parse(s.generatedAt)) && + // totalAnnualListUsd is dereferenced with .toFixed on the /dns and + // usage render paths, so the gate must guarantee a finite number + // (#2412) — a missing/string/null/NaN total certifies a section the + // renderers cannot render. + typeof s.totalAnnualListUsd === "number" && + Number.isFinite(s.totalAnnualListUsd) && Array.isArray(s.entries) && s.entries.length > 0 && s.entries.every( @@ -84,7 +90,17 @@ export function isDnsSection(v: unknown): v is DnsSection { typeof e === "object" && e !== null && typeof (e as DnsEntry).domain === "string" && - typeof (e as DnsEntry).renewal === "string", + typeof (e as DnsEntry).renewal === "string" && + // priceUsd is number|null in the model: null/absent stays valid, + // but a present value must be a finite number (#2412). + ((e as DnsEntry).priceUsd === null || + (e as DnsEntry).priceUsd === undefined || + (typeof (e as DnsEntry).priceUsd === "number" && Number.isFinite((e as DnsEntry).priceUsd))) && + // status feeds summarizeDns buckets; an unknown/missing status + // under-counts silently, so the gate owns the enum (#2412). + ((e as DnsEntry).status === "active" || + (e as DnsEntry).status === "earmarked" || + (e as DnsEntry).status === "unused"), ) ); } diff --git a/pm-stack/aggregator/test/slice3.test.ts b/pm-stack/aggregator/test/slice3.test.ts index c511305e09..394199e494 100644 --- a/pm-stack/aggregator/test/slice3.test.ts +++ b/pm-stack/aggregator/test/slice3.test.ts @@ -4,7 +4,7 @@ import { mkdtempSync, renameSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { CommentsStore, validateNewComment } from "../src/comments.ts"; -import { dnsSectionFromRegister, parseDnsRegister, summarizeDns } from "../src/dns.ts"; +import { dnsSectionFromRegister, isDnsSection, parseDnsRegister, summarizeDns } from "../src/dns.ts"; import type { SessionFeedEntry } from "../src/model.ts"; import { PAGES } from "../src/pages.ts"; import { agentIdentityOf, mergeRecords, parseLaunchRecords, recordFromSessionEntry } from "../src/registry.ts"; @@ -117,6 +117,36 @@ describe("dns register parser", () => { expect(parseDnsRegister("# nothing here\n")).toEqual([]); expect(dnsSectionFromRegister("# nothing\n", "2026-10-25T00:00:00Z", "test")).toBeNull(); }); + + test("isDnsSection owns the finite-number + status rules (#2412)", () => { + const good = dnsSectionFromRegister(REGISTER_MD, "2026-10-25T00:00:00Z", "test"); + expect(good).not.toBeNull(); + expect(isDnsSection(good)).toBe(true); + const noTotal = { ...good! } as Record; + delete noTotal.totalAnnualListUsd; + expect(isDnsSection(noTotal)).toBe(false); + expect(isDnsSection({ ...good, totalAnnualListUsd: "46.29" })).toBe(false); + expect(isDnsSection({ ...good, totalAnnualListUsd: null })).toBe(false); + expect(isDnsSection({ ...good, totalAnnualListUsd: Number.NaN })).toBe(false); + expect(isDnsSection({ ...good, totalAnnualListUsd: Number.POSITIVE_INFINITY })).toBe(false); + const withEntry = (priceUsd: unknown) => ({ + ...good, + entries: good!.entries.map((e, i) => (i === 0 ? { ...e, priceUsd } : e)), + }); + expect(isDnsSection(withEntry("4.07"))).toBe(false); + expect(isDnsSection(withEntry(Number.NaN))).toBe(false); + expect(isDnsSection(withEntry(null))).toBe(true); + const withStatus = (status: unknown) => ({ + ...good, + entries: good!.entries.map((e, i) => (i === 0 ? { ...e, status } : e)), + }); + expect(isDnsSection(withStatus("bogus"))).toBe(false); + expect(isDnsSection(withStatus(undefined))).toBe(false); + expect(isDnsSection(withStatus("active"))).toBe(true); + expect(isDnsSection(withStatus("earmarked"))).toBe(true); + expect(isDnsSection(withStatus("unused"))).toBe(true); + }); + }); // --- Identity registry (#906 §3) ---------------------------------------------