diff --git a/pm-stack/dispatch/src/validate.ts b/pm-stack/dispatch/src/validate.ts index 2d68212ccc..fb99e22b69 100644 --- a/pm-stack/dispatch/src/validate.ts +++ b/pm-stack/dispatch/src/validate.ts @@ -27,13 +27,20 @@ const DEFAULT_PRIORITY = 40; export function resolvePriority(input: unknown): number { if (input === undefined || input === null) return DEFAULT_PRIORITY; if (typeof input === "string") { - const v = PRIORITY_NAMES[input.trim().toLowerCase()]; - if (v === undefined) { + // Own-key guard (#2757, Phase 3 of #2792): PRIORITY_NAMES is a plain + // object, so an unguarded lookup returns inherited members for + // prototype spellings - 'constructor' yielded the Object constructor + // function and '__proto__' Object.prototype, both non-numbers that + // then flowed toward Packet.priority / p_priority. Object.hasOwn + // restricts the lookup to the four real names; every other string + // takes the same documented 400 as any unknown name. + const key = input.trim().toLowerCase(); + if (!Object.hasOwn(PRIORITY_NAMES, key)) { throw new ValidationError( `priority: unknown name ${JSON.stringify(input)} (use critical|high|normal|low or a number 10-90)`, ); } - return v; + return PRIORITY_NAMES[key]; } if (typeof input === "number" && Number.isInteger(input)) { if ((PRIORITY_SCALE as readonly number[]).includes(input)) return input; diff --git a/pm-stack/dispatch/test/validate.test.ts b/pm-stack/dispatch/test/validate.test.ts index 2cff2738f4..6d75fb671b 100644 --- a/pm-stack/dispatch/test/validate.test.ts +++ b/pm-stack/dispatch/test/validate.test.ts @@ -43,6 +43,35 @@ describe("priority mapping", () => { expect(() => resolvePriority("urgent")).toThrow(ValidationError); expect(() => resolvePriority(true)).toThrow(ValidationError); }); + + test("prototype spellings are rejected, not resolved through the prototype chain (#2757)", () => { + // PRIORITY_NAMES is a plain object: without the own-key guard these + // returned inherited members (the Object constructor function, + // Object.prototype, ...) - non-numbers that flowed toward + // Packet.priority / p_priority. Every spelling that trims/lowercases + // to an inherited member must take the ordinary unknown-name 400. + for (const name of [ + "constructor", + "Constructor", + " CONSTRUCTOR ", + "__proto__", + "__PROTO__", + "toString", + "valueOf", + "hasOwnProperty", + "isPrototypeOf", + "propertyIsEnumerable", + "toLocaleString", + "__defineGetter__", + "__lookupGetter__", + ]) { + expect(() => resolvePriority(name)).toThrow(ValidationError); + expect(() => resolvePriority(name)).toThrow(/unknown name/); + } + // The four real names still resolve. + expect(resolvePriority("critical")).toBe(10); + expect(resolvePriority(" low ")).toBe(70); + }); }); describe("model parsing", () => { @@ -87,6 +116,23 @@ describe("request validation", () => { expect(() => parseSessionRequest("nope")).toThrow(ValidationError); }); + test("a prototype-spelling priority 400s at parse time, so no non-number reaches the packet (#2757)", () => { + expect(() => + parseSessionRequest({ repo: "developing-today/code", prompt: "x", priority: "constructor" }), + ).toThrow(ValidationError); + expect(() => + parseSessionRequest({ repo: "developing-today/code", prompt: "x", priority: "__proto__" }), + ).toThrow(ValidationError); + expect(() => + parseTaskRequest({ repo: "developing-today/code", title: "t", prompt: "p", priority: "constructor" }), + ).toThrow(ValidationError); + expect(() => + parseTaskRequest({ repo: "developing-today/code", title: "t", prompt: "p", priority: "__proto__" }), + ).toThrow(ValidationError); + // A valid named priority still parses to a number. + expect(parseSessionRequest({ repo: "developing-today/code", prompt: "x", priority: "high" }).priority).toBe(20); + }); + test("tasks accept prompt or body, default kind triage and issue off", () => { const a = parseTaskRequest({ repo: "developing-today/code", title: "t", prompt: "p" }); expect(a.kind).toBe("triage");