From 9bd17b64b264ef00e02894c5ddf87b606c84da16 Mon Sep 17 00:00:00 2001 From: repo-worker-code Date: Tue, 3 Nov 2026 10:55:22 -0600 Subject: [PATCH] fix(aggregator): bound mergeClasses class keys by count and length (#4981) A client could grow the subscription `classes` object without bound: every boolean key it sent was copied into the stored JSON. Cap the client-supplied key count at MAX_CLASS_KEYS (16) beyond the `incident` default and the key length at MAX_CLASS_KEY_LENGTH (64); over-limit input is dropped rather than extending the stored object, and defaults still merge. Pass-through for accepted keys is unchanged (explicit true/false survive, non-boolean values are ignored), so the existing quota-wall case still holds. Prototype-named keys count against the cap like any other client key. Registry validation stays deferred to #3317, as the packet scopes it. Tests: key-count cap, key-length cap, accepted keys unchanged, non-boolean ignored, prototype-named keys. --- pm-stack/aggregator/src/alerts.ts | 27 ++++++++++++++++++++- pm-stack/aggregator/test/alerts.test.ts | 32 +++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/pm-stack/aggregator/src/alerts.ts b/pm-stack/aggregator/src/alerts.ts index 26af109d79..6d3c12bdd0 100644 --- a/pm-stack/aggregator/src/alerts.ts +++ b/pm-stack/aggregator/src/alerts.ts @@ -116,11 +116,36 @@ export const DEFAULT_CLASSES: Record = { incident: true }; /** Design §2: default quiet window 23:00–07:00 America/Chicago. */ export const DEFAULT_QUIET: QuietPrefs = { start: "23:00", end: "07:00", tz: "America/Chicago", enabled: true }; +/** #4981: how many client-supplied class keys one subscription may carry + * beyond {@link DEFAULT_CLASSES}. The class registry that would validate + * names and give the set its real shape arrives with #3317; until then a + * fixed cap keeps a client from growing the stored `classes` object + * without bound. Keys past the cap are dropped, not stored. */ +export const MAX_CLASS_KEYS = 16; +/** #4981: longest accepted class key. Real class names are short slugs + * ("quota-wall"); anything longer is not a class this build knows. */ +export const MAX_CLASS_KEY_LENGTH = 64; + export function mergeClasses(input: unknown): Record { const out = { ...DEFAULT_CLASSES }; + let clientKeys = 0; + const seenClient: Record = {}; if (input && typeof input === "object") { for (const [k, v] of Object.entries(input as Record)) { - if (typeof v === "boolean") out[k] = v; + // #4981: bound growth — a key must be a plausible class name, and a + // subscription may carry only MAX_CLASS_KEYS client-supplied keys. + // Over-limit input is dropped rather than extending the stored + // object. Object.hasOwn (not `in`) so prototype names like + // "toString" count against the cap like any other client key. + if (k.length === 0 || k.length > MAX_CLASS_KEY_LENGTH) continue; + if (typeof v !== "boolean") continue; + const isClientKey = !Object.hasOwn(DEFAULT_CLASSES, k) && !Object.hasOwn(seenClient, k); + if (isClientKey && clientKeys >= MAX_CLASS_KEYS) continue; + if (isClientKey) { + clientKeys += 1; + seenClient[k] = true; + } + out[k] = v; } } return out; diff --git a/pm-stack/aggregator/test/alerts.test.ts b/pm-stack/aggregator/test/alerts.test.ts index d3ae9b95d7..89bb07b5fb 100644 --- a/pm-stack/aggregator/test/alerts.test.ts +++ b/pm-stack/aggregator/test/alerts.test.ts @@ -13,6 +13,8 @@ import { incidentObjectRef, incidentUrl, inQuietHours, + MAX_CLASS_KEY_LENGTH, + MAX_CLASS_KEYS, mergeClasses, mergeQuiet, runAlertTick, @@ -53,6 +55,36 @@ describe("class + quiet merges (design §1/§2)", () => { expect(mergeClasses({ incident: true, "quota-wall": false })).toEqual({ incident: true, "quota-wall": false }); expect(mergeClasses({ incident: "yes" })).toEqual({ incident: true }); // non-boolean ignored }); + // #4981: bound class-key growth until the class registry lands (#3317). + test("class keys are capped in count beyond the incident default", () => { + const many: Record = {}; + for (let i = 0; i < MAX_CLASS_KEYS + 5; i++) many[`class-${i}`] = true; + const out = mergeClasses(many); + expect(Object.keys(out).filter((k) => k !== "incident")).toHaveLength(MAX_CLASS_KEYS); + expect(out["class-0"]).toBe(true); + expect(out[`class-${MAX_CLASS_KEYS}`]).toBeUndefined(); // over-limit dropped + // The default still merges even when the cap is reached. + expect(out.incident).toBe(true); + }); + test("class keys longer than the maximum are dropped", () => { + const longest = "k".repeat(MAX_CLASS_KEY_LENGTH); + const tooLong = "k".repeat(MAX_CLASS_KEY_LENGTH + 1); + expect(mergeClasses({ [longest]: false })).toEqual({ incident: true, [longest]: false }); + expect(mergeClasses({ [tooLong]: true })).toEqual({ incident: true }); + }); + test("accepted class keys keep their explicit true/false", () => { + expect(mergeClasses({ incident: false, "quota-wall": true })).toEqual({ + incident: false, + "quota-wall": true, + }); + }); + test("prototype-named keys count against the cap", () => { + const input: Record = { toString: true, constructor: false }; + expect(mergeClasses(input)).toEqual({ incident: true, toString: true, constructor: false }); + }); + test("non-boolean class values are ignored", () => { + expect(mergeClasses({ "quota-wall": "yes", a: 1, b: null })).toEqual({ incident: true }); + }); test("classEnabled: only an explicit false disables", () => { expect(classEnabled({ classes: {} }, "incident")).toBe(true); expect(classEnabled({ classes: { incident: false } }, "incident")).toBe(false);