From 69da87eec008c62421a11a1bba087942eafc1d91 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 21:24:49 -0600 Subject: [PATCH] fix(aggregator): validate saved view rows --- pm-stack/aggregator/src/views.ts | 27 +++++++++++++++++++--- pm-stack/aggregator/test/views.test.ts | 31 +++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/pm-stack/aggregator/src/views.ts b/pm-stack/aggregator/src/views.ts index f12731b071..b3105be933 100644 --- a/pm-stack/aggregator/src/views.ts +++ b/pm-stack/aggregator/src/views.ts @@ -85,6 +85,24 @@ export function validateViewName(n: unknown): string | null { return name !== "" && name.length <= 120 ? name : null; } +function isSavedView(value: unknown): value is SavedView { + if (typeof value !== "object" || value === null || Array.isArray(value)) return false; + const view = value as Record; + return ( + typeof view.id === "string" && + view.id.length > 0 && + validateViewName(view.name) !== null && + typeof view.owner === "string" && + view.owner.length > 0 && + validateViewFilters(view.filters).ok && + typeof view.isDefault === "boolean" && + typeof view.createdAt === "string" && + Number.isFinite(Date.parse(view.createdAt)) && + typeof view.updatedAt === "string" && + Number.isFinite(Date.parse(view.updatedAt)) + ); +} + let counter = 0; export function newViewId(now = new Date()): string { counter = (counter + 1) % 46656; @@ -195,9 +213,12 @@ export class ViewsStore { if (typeof parsed !== "object" || parsed === null || !Array.isArray((parsed as ViewsFile).views)) { throw new Error("store file does not have the expected { views: [...] } shape"); } - this.views = (parsed as ViewsFile).views.filter( - (v) => typeof v === "object" && v !== null && typeof (v as SavedView).id === "string", - ); + const rows = (parsed as { views: unknown[] }).views; + const malformedIndex = rows.findIndex((view) => !isSavedView(view)); + if (malformedIndex !== -1) { + throw new Error(`view row ${malformedIndex} does not have the expected SavedView shape`); + } + this.views = rows.filter(isSavedView); } catch (err) { this.views = []; await this.quarantine(`is unparseable (${err instanceof Error ? err.message : String(err)})`); diff --git a/pm-stack/aggregator/test/views.test.ts b/pm-stack/aggregator/test/views.test.ts index 82599ca355..952af5770b 100644 --- a/pm-stack/aggregator/test/views.test.ts +++ b/pm-stack/aggregator/test/views.test.ts @@ -5,7 +5,7 @@ // the identical set), the shareable-URL encode/parse round-trip, and // the mediated preference verbs that mutate the store. import { describe, expect, test } from "bun:test"; -import { existsSync, mkdtempSync, readdirSync, writeFileSync } from "node:fs"; +import { existsSync, mkdtempSync, readFileSync, readdirSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { validateNewAction } from "../src/actions.ts"; @@ -106,6 +106,35 @@ describe("views store (#1276)", () => { expect(existsSync(file)).toBe(true); expect(readdirSync(dir).filter((f) => f.startsWith("views.json.corrupt-"))).toHaveLength(1); }); + + test("well-formed JSON with a row missing its name is quarantined before later saves", async () => { + const { dir, file, store } = tmpStore(); + const raw = JSON.stringify({ + version: 1, + views: [{ id: "vw-1", owner: drewKey, filters: { repo: "a/b" }, isDefault: false, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() }], + }); + writeFileSync(file, raw); + + expect(await store.all()).toEqual([]); + const quarantined = readdirSync(dir).filter((f) => f.startsWith("views.json.corrupt-")); + expect(quarantined).toHaveLength(1); + expect(readFileSync(join(dir, quarantined[0]), "utf8")).toBe(raw); + await expect(store.save({ owner: drewKey, name: "Fresh", filters: { state: "open" } })).resolves.toMatchObject({ name: "Fresh" }); + }); + + test("malformed default filters are quarantined instead of becoming a match-all view", async () => { + const { dir, file, store } = tmpStore(); + const raw = JSON.stringify({ + version: 1, + views: [{ id: "vw-1", name: "Default", owner: drewKey, filters: "repo:other/repo", isDefault: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() }], + }); + writeFileSync(file, raw); + + expect(await store.defaultFor(drewKey)).toBeNull(); + const quarantined = readdirSync(dir).filter((f) => f.startsWith("views.json.corrupt-")); + expect(quarantined).toHaveLength(1); + expect(readFileSync(join(dir, quarantined[0]), "utf8")).toBe(raw); + }); }); const items: ViewItemLike[] = [