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
13 changes: 10 additions & 3 deletions pm-stack/dispatch/src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
46 changes: 46 additions & 0 deletions pm-stack/dispatch/test/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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");
Expand Down