Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion pm-stack/aggregator/src/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,36 @@ export const DEFAULT_CLASSES: Record<string, boolean> = NOTIFICATION_CLASS_DEFAU
/** 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<string, boolean> {
const out = { ...DEFAULT_CLASSES };
let clientKeys = 0;
const seenClient: Record<string, true> = {};
if (input && typeof input === "object") {
for (const [k, v] of Object.entries(input as Record<string, unknown>)) {
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;
Expand Down
34 changes: 34 additions & 0 deletions pm-stack/aggregator/test/alerts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ import {
buildTestPayload,
classEnabled,
createAlertsStoreGetter,
DEFAULT_CLASSES,
duplicateWouldRepeatDelivery,
incidentObjectRef,
incidentUrl,
inQuietHours,
MAX_CLASS_KEY_LENGTH,
MAX_CLASS_KEYS,
mergeClasses,
mergeQuiet,
runAlertTick,
Expand Down Expand Up @@ -105,6 +108,37 @@ describe("class + quiet merges (design §1/§2)", () => {
expect(mergeClasses({ incident: true, "quota-wall": false })["quota-wall"]).toBe(false);
expect(mergeClasses({ incident: "yes" }).incident).toBe(true); // non-boolean ignored
});
// #4981: bound class-key growth until the class registry lands (#3317).
test("class keys are capped in count beyond the default class set", () => {
const many: Record<string, boolean> = {};
for (let i = 0; i < MAX_CLASS_KEYS + 5; i++) many[`class-${i}`] = true;
const out = mergeClasses(many);
expect(Object.keys(out).filter((k) => !Object.hasOwn(DEFAULT_CLASSES, k))).toHaveLength(MAX_CLASS_KEYS);
expect(out["class-0"]).toBe(true);
expect(out[`class-${MAX_CLASS_KEYS}`]).toBeUndefined(); // over-limit dropped
// The defaults still merge 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({ ...DEFAULT_CLASSES, [longest]: false });
expect(mergeClasses({ [tooLong]: true })).toEqual({ ...DEFAULT_CLASSES });
});
test("accepted class keys keep their explicit true/false", () => {
expect(mergeClasses({ incident: false, "quota-wall": true })).toEqual({
...DEFAULT_CLASSES,
incident: false,
"quota-wall": true,
});
});
test("prototype-named keys count against the cap", () => {
const input: Record<string, unknown> = { toString: true, constructor: false };
expect(mergeClasses(input)).toEqual({ ...DEFAULT_CLASSES, toString: true, constructor: false });
});
test("non-boolean class values are ignored", () => {
expect(mergeClasses({ "quota-wall": "yes", a: 1, b: null })).toEqual({ ...DEFAULT_CLASSES });
});
test("classEnabled preserves old rows while new rows get defaults", () => {
expect(classEnabled({ classes: {} }, "incident")).toBe(true);
expect(classEnabled({ classes: { incident: false } }, "incident")).toBe(false);
Expand Down
Loading