From 132543d4eed580201b3a9fdbfe6ae84b1ddd82ba Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sat, 31 Oct 2026 08:53:00 -0500 Subject: [PATCH] fix(projector): parseLimit honours only decimal-digit spellings (#2971) --- pm-stack/projector/src/server.ts | 6 +++++- pm-stack/projector/test/limit.test.ts | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/pm-stack/projector/src/server.ts b/pm-stack/projector/src/server.ts index cb654759c5..85211caf6c 100644 --- a/pm-stack/projector/src/server.ts +++ b/pm-stack/projector/src/server.ts @@ -49,8 +49,12 @@ export const MAX_COMMENTS_LIMIT = 1000; */ export function parseLimit(raw: string | null, defaultLimit: number, maxLimit: number): number { if (raw === null) return defaultLimit; + // Only a decimal-digit spelling is honoured (#2971): Number() coercion + // re-admits every spelling it accepts ('1e2', '0x10', '5.0', '+5', ' 5'), + // which is the #1547 residual this gate exists to close. + if (!/^[0-9]+$/.test(raw)) return defaultLimit; const n = Number(raw); - if (!Number.isInteger(n) || n < 1) return defaultLimit; + if (n < 1) return defaultLimit; return Math.min(n, maxLimit); } diff --git a/pm-stack/projector/test/limit.test.ts b/pm-stack/projector/test/limit.test.ts index db9e5dc6ed..303f9b226b 100644 --- a/pm-stack/projector/test/limit.test.ts +++ b/pm-stack/projector/test/limit.test.ts @@ -105,6 +105,17 @@ describe("parseLimit", () => { } }); + test("coercible non-decimal spellings fall back to the default (#2971)", () => { + for (const raw of ["1e2", "0x10", "5.0", "+5", " 5", "5 "]) { + expect(parseLimit(raw, DEFAULT_ITEMS_LIMIT, MAX_ITEMS_LIMIT)).toBe(DEFAULT_ITEMS_LIMIT); + expect(parseLimit(raw, DEFAULT_COMMENTS_LIMIT, MAX_COMMENTS_LIMIT)).toBe(DEFAULT_COMMENTS_LIMIT); + } + expect(parseLimit("5", DEFAULT_ITEMS_LIMIT, MAX_ITEMS_LIMIT)).toBe(5); + expect(parseLimit("5", DEFAULT_COMMENTS_LIMIT, MAX_COMMENTS_LIMIT)).toBe(5); + expect(parseLimit("99999", DEFAULT_ITEMS_LIMIT, MAX_ITEMS_LIMIT)).toBe(MAX_ITEMS_LIMIT); + expect(parseLimit("99999", DEFAULT_COMMENTS_LIMIT, MAX_COMMENTS_LIMIT)).toBe(MAX_COMMENTS_LIMIT); + }); + test("over-max values clamp to the named per-endpoint maximum", () => { expect(parseLimit("999999999", DEFAULT_ITEMS_LIMIT, MAX_ITEMS_LIMIT)).toBe(MAX_ITEMS_LIMIT); expect(parseLimit("1001", DEFAULT_COMMENTS_LIMIT, MAX_COMMENTS_LIMIT)).toBe(MAX_COMMENTS_LIMIT); @@ -120,8 +131,8 @@ describe("read API limit handling", () => { }); test("GET /items with an invalid limit never 500s and stays bounded by the default", async () => { - for (const raw of ["-1", "0", "2.7", "abc"]) { - const { status, body } = await get(`/items?limit=${raw}`); + for (const raw of ["-1", "0", "2.7", "abc", "1e2", "0x10", "5.0", "+5"]) { + const { status, body } = await get(`/items?limit=${encodeURIComponent(raw)}`); expect(status).toBe(200); // 8 seeded items < default 200, so the default-bounded result is all 8; // parseLimit's unit tests pin that the bound itself is the default.