-
Notifications
You must be signed in to change notification settings - Fork 39
fix(follows): cap huge public offsets #357
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
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 |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { createClient } from "@/lib/supabase/server"; | ||
|
|
||
| const MAX_FOLLOW_OFFSET = 100_000; | ||
|
|
||
| function parsePositiveInt(value: string | null, fallback: number, max: number): number { | ||
| const parsed = Number.parseInt(value ?? "", 10); | ||
| if (!Number.isFinite(parsed) || parsed <= 0) { | ||
|
|
@@ -9,12 +11,12 @@ function parsePositiveInt(value: string | null, fallback: number, max: number): | |
| return Math.min(parsed, max); | ||
| } | ||
|
|
||
| function parseNonNegativeInt(value: string | null, fallback: number): number { | ||
| function parseNonNegativeInt(value: string | null, fallback: number, max: number): number { | ||
| const parsed = Number.parseInt(value ?? "", 10); | ||
| if (!Number.isFinite(parsed) || parsed < 0) { | ||
| return fallback; | ||
| } | ||
| return parsed; | ||
| return Math.min(parsed, max); | ||
| } | ||
|
|
||
| // GET /api/users/[username]/followers - list a user's followers | ||
|
|
@@ -27,7 +29,7 @@ export async function GET( | |
| const supabase = await createClient(); | ||
| const searchParams = request.nextUrl.searchParams; | ||
| const limit = parsePositiveInt(searchParams.get("limit"), 20, 100); | ||
| const offset = parseNonNegativeInt(searchParams.get("offset"), 0); | ||
| const offset = parseNonNegativeInt(searchParams.get("offset"), 0, MAX_FOLLOW_OFFSET); | ||
|
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.
When a client sends |
||
|
|
||
| // Look up target user | ||
| const { data: targetProfile, error: profileError } = await supabase | ||
|
|
||
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.
MAX_FOLLOW_OFFSET,parsePositiveInt, andparseNonNegativeIntare defined identically in bothfollowers/route.tsandfollowing/route.ts. If the cap value or parsing logic needs to change (e.g., lowering the max, fixing an edge case), both files must be updated in sync — a future maintainer will likely only update one. Extracting these to a shared module (e.g.,src/app/api/users/[username]/pagination.ts) would eliminate the drift risk.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!