From 4d9fb38a2c773289b6a44cb56bc52ec95993b47c Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Tue, 3 Nov 2026 15:43:41 -0600 Subject: [PATCH] fix(aggregator): sanitize stored quiet prefs on read --- pm-stack/aggregator/src/alerts.ts | 6 ++- pm-stack/aggregator/test/alerts.test.ts | 51 +++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/pm-stack/aggregator/src/alerts.ts b/pm-stack/aggregator/src/alerts.ts index 00c741b81c..21799e52fe 100644 --- a/pm-stack/aggregator/src/alerts.ts +++ b/pm-stack/aggregator/src/alerts.ts @@ -374,7 +374,11 @@ export class AlertsStore { } async listSubscriptions(): Promise { - return this.db.query(`select * from alerts.push_subscriptions order by created_at`); + const rows = await this.db.query(`select * from alerts.push_subscriptions order by created_at`); + // #3702 decision 3: old stored prefs are not rewritten, but must not + // be reinterpreted on read. Apply the same per-field fallbacks used at + // intake so delivery and the prefs page see safe values. + return rows.map((row) => ({ ...row, quiet: mergeQuiet(row.quiet) })); } /** Design §4: a 404/410 marks the subscription gone; two consecutive diff --git a/pm-stack/aggregator/test/alerts.test.ts b/pm-stack/aggregator/test/alerts.test.ts index b84c4f15e8..474775fc0f 100644 --- a/pm-stack/aggregator/test/alerts.test.ts +++ b/pm-stack/aggregator/test/alerts.test.ts @@ -136,6 +136,57 @@ describe("class + quiet merges (design §1/§2)", () => { }); }); +describe("stored subscription preference reads (#4978)", () => { + test("invalid stored quiet fields fall back per field without rewriting rows", async () => { + const invalidQuiet = { start: "99:99", end: "24:60", tz: "Not/AZone", enabled: true }; + const validQuiet = { start: "22:30", end: "06:15", tz: "UTC", enabled: true }; + const rawRows: PushSubscriptionRow[] = [ + { + id: "sub-invalid", + endpoint: "https://push.example/invalid", + p256dh: "p256dh", + auth: "auth", + classes: { incident: true }, + quiet: invalidQuiet, + user_agent: null, + created_at: "2026-11-01T00:00:00Z", + last_seen_at: "2026-11-01T00:00:00Z", + gone_count: 0, + }, + { + id: "sub-valid", + endpoint: "https://push.example/valid", + p256dh: "p256dh", + auth: "auth", + classes: { incident: true }, + quiet: validQuiet, + user_agent: null, + created_at: "2026-11-02T00:00:00Z", + last_seen_at: "2026-11-02T00:00:00Z", + gone_count: 0, + }, + ]; + const queries: string[] = []; + const db: SupabaseClient = { + ref: "test", + async query(sql: string): Promise { + queries.push(sql); + return rawRows as unknown as T[]; + }, + }; + + const loaded = await new AlertsStore(db).listSubscriptions(); + + expect(loaded.map((row) => row.quiet)).toEqual([ + { start: "23:00", end: "07:00", tz: "America/Chicago", enabled: true }, + validQuiet, + ]); + expect(inQuietHours(loaded[0].quiet, new Date("2026-01-15T12:00:00Z"))).toBe(true); + expect(rawRows.map((row) => row.quiet)).toEqual([invalidQuiet, validQuiet]); + expect(queries).toEqual(["select * from alerts.push_subscriptions order by created_at"]); + }); +}); + const incident: Incident = { id: "11111111-2222-3333-4444-555555555555", source: "globalping-watch",