Repository navigation
fix(web): validate page and limit query params on caps and space dashboard (#3567) #3574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { parseLimitParam, parsePageParam } from "../../lib/pagination"; | ||
|
|
||
| describe("pagination params validation (#3567)", () => { | ||
| describe("parsePageParam", () => { | ||
| it("falls back to default page on missing, non-positive, or fractional input", () => { | ||
| expect(parsePageParam(undefined)).toBe(1); | ||
| expect(parsePageParam("")).toBe(1); | ||
| expect(parsePageParam("0")).toBe(1); | ||
| expect(parsePageParam("-1")).toBe(1); | ||
| expect(parsePageParam("-10")).toBe(1); | ||
| expect(parsePageParam("1.5")).toBe(1); | ||
| expect(parsePageParam("abc")).toBe(1); | ||
| expect(parsePageParam(NaN as unknown as string)).toBe(1); | ||
| }); | ||
|
|
||
| it("parses valid positive integer pages", () => { | ||
| expect(parsePageParam("1")).toBe(1); | ||
| expect(parsePageParam("5")).toBe(5); | ||
| expect(parsePageParam(["3", "4"])).toBe(3); | ||
| }); | ||
|
|
||
| it("supports custom default page", () => { | ||
| expect(parsePageParam("-5", 2)).toBe(2); | ||
| }); | ||
| }); | ||
|
|
||
| describe("parseLimitParam", () => { | ||
| it("falls back to default limit on missing, non-positive, or fractional input", () => { | ||
| expect(parseLimitParam(undefined, 15)).toBe(15); | ||
| expect(parseLimitParam("", 15)).toBe(15); | ||
| expect(parseLimitParam("0", 15)).toBe(15); | ||
| expect(parseLimitParam("-5", 15)).toBe(15); | ||
| expect(parseLimitParam("2.5", 15)).toBe(15); | ||
| expect(parseLimitParam("foo", 15)).toBe(15); | ||
| }); | ||
|
|
||
| it("parses valid positive integer limits", () => { | ||
| expect(parseLimitParam("10", 15)).toBe(10); | ||
| expect(parseLimitParam("25", 15)).toBe(25); | ||
| expect(parseLimitParam(["20", "30"], 15)).toBe(20); | ||
| }); | ||
|
|
||
| it("caps oversized limits to maxLimit", () => { | ||
| expect(parseLimitParam("500", 15, 100)).toBe(100); | ||
| expect(parseLimitParam("100", 15, 100)).toBe(100); | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| export function parsePageParam( | ||
| page: string | string[] | undefined, | ||
| defaultValue = 1, | ||
| ): number { | ||
| const value = Array.isArray(page) ? page[0] : page; | ||
| const parsed = Number(value); | ||
|
|
||
| if (!Number.isInteger(parsed) || parsed < 1) return defaultValue; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Prompt To Fix With AIThis is a comment left during a code review.
Path: apps/web/lib/pagination.ts
Line: 8
Comment:
**Large pages still fail**
`parsePageParam` accepts enormous pages such as `1e308`. With the default limit of 15, `(page - 1) * limit` becomes `Infinity`, which both dashboards pass to `.offset()`. That cannot be a valid MySQL offset, so a crafted URL can still make the dashboard query fail. Bound the page so the calculated offset stays within a safe integer range, and add a test for oversized pages.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly. |
||
|
|
||
| return parsed; | ||
| } | ||
|
|
||
| export function parseLimitParam( | ||
| limit: string | string[] | undefined, | ||
| defaultValue = 15, | ||
| maxLimit = 100, | ||
| ): number { | ||
| const value = Array.isArray(limit) ? limit[0] : limit; | ||
| const parsed = Number(value); | ||
|
|
||
| if (!Number.isInteger(parsed) || parsed < 1) return defaultValue; | ||
|
|
||
| return Math.min(parsed, maxLimit); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The server now falls back to page 1 for fractional values, but
CapsandSharedCapsstill read the raw page withNumber(...) || 1. Opening?page=1.5shows page 1's videos while marking page 1.5 as current. The next link points to page 2.5, which again loads page 1. Use the same parser in both clients, or pass the parsed page from the server.Prompt To Fix With AI