diff --git a/pm-stack/aggregator/src/feed.ts b/pm-stack/aggregator/src/feed.ts index 310f32a535..e80c75e452 100644 --- a/pm-stack/aggregator/src/feed.ts +++ b/pm-stack/aggregator/src/feed.ts @@ -41,6 +41,10 @@ export interface FleetFeed { /** Element drops loadFeed's quota sanitization made (#2429), set * only on feeds returned by loadFeed; absent on raw parsed feeds. */ quotaDropped?: { pools: number; windows: number; burn: number }; + /** Subscription rows loadFeed's sanitization dropped (#2446), set + * only when at least one row (or a non-array section) was dropped; + * absent on raw parsed feeds and on clean loads. */ + subscriptionsDropped?: number; /** Sandbox-server + Jules sessions, pre-classification (render classifies). */ attention: SessionFeedEntry[]; /** Jules keeper totals (slice 2); absent when the keeper state is unreadable. */ @@ -416,6 +420,38 @@ export function isFleetFeed(v: unknown): v is FleetFeed { ); } +export interface SubscriptionSanitization { + /** Sanitized rows, or undefined when the section is absent (or was + * dropped whole) — consumers already treat absence as an empty strip. */ + rows?: { name: string; plan: string; renewsAt: string | null }[]; + dropped: number; +} + +/** One subscription row safe to hand to the renderers (#2446): the + * strip maps esc(s.name)/esc(s.plan) and reads renewsAt unguarded, so + * a null row or a non-string field 500s /quota and is served verbatim + * by /api/quota.json. */ +export function isSubscriptionRow(v: unknown): v is { name: string; plan: string; renewsAt: string | null } { + if (!isRecord(v)) return false; + return ( + typeof v.name === "string" && + typeof v.plan === "string" && + (typeof v.renewsAt === "string" || v.renewsAt === null) + ); +} + +/** Drop malformed subscription rows at the loadFeed boundary (#2446, + * the #2429 pattern): the section is optional and unvalidated by + * isFleetFeed, so element-level damage is dropped here rather than + * rejecting the whole feed for one bad row. A non-array section is + * dropped whole (treated as absent, counted as one drop). */ +export function sanitizeSubscriptions(v: unknown): SubscriptionSanitization { + if (v === undefined) return { dropped: 0 }; + if (!Array.isArray(v)) return { dropped: 1 }; + const rows = v.filter(isSubscriptionRow); + return { rows, dropped: v.length - rows.length }; +} + export async function loadFeed(path: string): Promise { try { const parsed: unknown = JSON.parse(await readFile(path, "utf8")); @@ -425,10 +461,13 @@ export async function loadFeed(path: string): Promise { // sees only renderer-safe pools/windows (#2429). Drop counts are // attached to the feed so they are visible, not swallowed. const san = sanitizeQuotaView(parsed.quota); + const subs = sanitizeSubscriptions(parsed.subscriptions); return { ...parsed, quota: san.view, quotaDropped: { pools: san.droppedPools, windows: san.droppedWindows, burn: san.droppedBurn }, + subscriptions: subs.rows, + ...(subs.dropped > 0 ? { subscriptionsDropped: subs.dropped } : {}), }; } catch { return null; diff --git a/pm-stack/aggregator/test/feed.test.ts b/pm-stack/aggregator/test/feed.test.ts index e62b1ad9ca..3ada49b4a3 100644 --- a/pm-stack/aggregator/test/feed.test.ts +++ b/pm-stack/aggregator/test/feed.test.ts @@ -1,5 +1,6 @@ import { describe, expect, test } from "bun:test"; -import { isFleetFeed, isProjectPoolSection, loadFeed, poolFromFeed, projectPoolSectionProblem, sanitizeQuotaView } from "../src/feed.ts"; +import { isFleetFeed, isProjectPoolSection, loadFeed, poolFromFeed, projectPoolSectionProblem, sanitizeQuotaView, sanitizeSubscriptions } from "../src/feed.ts"; +import { renderQuota } from "../src/render.ts"; import { mkdtempSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; @@ -237,3 +238,72 @@ describe("feed quota section gate + sanitization (#2429)", () => { expect(await loadFeed(file)).toBeNull(); }); }); + +describe("feed subscriptions sanitization (#2446)", () => { + const SUB = { name: "Wispr Flow", plan: "Pro $151.91/yr", renewsAt: "2027-10-09" }; + const SUB_NULL_RENEW = { name: "Amazon RxPass", plan: "$5/mo flat pharmacy", renewsAt: null }; + const feedWithSubs = (subscriptions: unknown) => ({ + version: 1, + generatedAt: "2026-10-01T00:00:00Z", + quota: { pools: [], burn: [], choices: { hasHeadroom: [], burnBeforeReset: [], doNotStart: [] } }, + attention: [], + subscriptions, + }); + const loadWithSubs = async (subscriptions: unknown) => { + const dir = mkdtempSync(join(tmpdir(), "agg-feed-subs-")); + const file = join(dir, "feed.json"); + writeFileSync(file, JSON.stringify(feedWithSubs(subscriptions))); + return loadFeed(file); + }; + + test("subscriptions: [null] drops the row, keeps the feed, counts the drop", async () => { + const loaded = await loadWithSubs([null]); + expect(loaded).not.toBeNull(); + expect(loaded?.subscriptions).toEqual([]); + expect(loaded?.subscriptionsDropped).toBe(1); + }); + + test("a non-array subscriptions section is dropped whole, treated as absent", async () => { + expect(sanitizeSubscriptions("nope")).toEqual({ dropped: 1 }); + const loaded = await loadWithSubs("nope"); + expect(loaded?.subscriptions).toBeUndefined(); + expect(loaded?.subscriptionsDropped).toBe(1); + }); + + test("a row with a numeric name drops; valid rows survive verbatim", async () => { + const out = sanitizeSubscriptions([SUB, { name: 42, plan: "x", renewsAt: null }, SUB_NULL_RENEW]); + expect(out.rows).toEqual([SUB, SUB_NULL_RENEW]); + expect(out.dropped).toBe(1); + }); + + test("a mixed valid/invalid array through loadFeed keeps only renderer-safe rows", async () => { + const loaded = await loadWithSubs([SUB, null, "junk", { name: "Ok", plan: 7, renewsAt: null }, SUB_NULL_RENEW]); + expect(loaded?.subscriptions).toEqual([SUB, SUB_NULL_RENEW]); + expect(loaded?.subscriptionsDropped).toBe(3); + }); + + test("renderQuota over the sanitized usage view does not throw", async () => { + const loaded = await loadWithSubs([null, SUB]); + expect(loaded).not.toBeNull(); + const usage = { + services: [], + payg: [], + charges: [], + attention: [], + domains: null, + subscriptions: loaded?.subscriptions ?? [], + history: {}, + } as never; + expect(() => renderQuota(loaded!.quota, {} as never, usage)).not.toThrow(); + }); + + test("absent and well-formed subscriptions are unchanged, no drop count attached", async () => { + expect(sanitizeSubscriptions(undefined)).toEqual({ dropped: 0 }); + const absent = await loadWithSubs(undefined); + expect(absent?.subscriptions).toBeUndefined(); + expect(absent?.subscriptionsDropped).toBeUndefined(); + const clean = await loadWithSubs([SUB, SUB_NULL_RENEW]); + expect(clean?.subscriptions).toEqual([SUB, SUB_NULL_RENEW]); + expect(clean?.subscriptionsDropped).toBeUndefined(); + }); +});