Skip to content

fix(pagination): clamp page/offset params in directory, notifications, and prompts - #299

Closed
forgou37 wants to merge 1 commit into
profullstack:masterfrom
forgou37:fix/pagination-clamping-296-297-298
Closed

fix(pagination): clamp page/offset params in directory, notifications, and prompts#299
forgou37 wants to merge 1 commit into
profullstack:masterfrom
forgou37:fix/pagination-clamping-296-297-298

Conversation

@forgou37

Copy link
Copy Markdown
Contributor

Fixes #296, #297, #298.

Changes

  • GET /api/directory?page=-1 — page is now clamped to minimum 1; negative values no longer compute a negative offset
  • GET /api/notifications?offset=-10 — offset is now clamped to minimum 0; limit is clamped to minimum 1
  • GET /api/prompts?page=-1 — page is now clamped to minimum 1; same fix as directory

All three followed the same pattern: unclamped parseInt/Number results were passed directly to Supabase .range(), causing 500 database errors for negative inputs.

…ions, and prompts

- Directory (profullstack#296): clamp page to minimum 1 to prevent negative offsets
- Notifications (profullstack#297): clamp offset to minimum 0, limit to minimum 1
- Prompts (profullstack#298): clamp page to minimum 1 to prevent negative offsets

Negative page/offset values were silently passed to Supabase .range() causing 500 errors.
@greptile-apps

greptile-apps Bot commented May 29, 2026

Copy link
Copy Markdown

Greptile Summary

This PR fixes 500 errors in three API routes caused by negative page/offset/limit query parameters being forwarded directly to Supabase .range(). The fix applies Math.max clamping after parsing, with NaN fallbacks, so all three parameters are guaranteed to stay in valid bounds before any DB call is made.

  • directory and prompts: page is now parsed as Math.max(1, parseInt(…) || 1), ensuring page ≥ 1 and the derived offset is always ≥ 0.
  • notifications: offset is clamped with Math.max(0, …) and limit is clamped with Math.max(1, … || 50) then Math.min(…, 100), keeping both in valid range without changing the existing default/ceiling behaviour.

Confidence Score: 5/5

Safe to merge — all three changes are small, targeted, and correct, with no new logic paths or side effects introduced.

Each change is a single-line guard that correctly handles all edge cases: NaN from non-numeric strings, zero (which already triggered the default fallback), and negative values. The clamping order in notifications is correct, and the NaN fallbacks fire before the Math.max clamp, so no invalid value can slip through to Supabase.

No files require special attention.

Important Files Changed

Filename Overview
src/app/api/directory/route.ts Wraps parseInt result with Math.max(1, …
src/app/api/notifications/route.ts Adds Math.max(1, …) around the limit expression and Math.max(0, …) around offset to prevent negative values from being passed to .range().
src/app/api/prompts/route.ts Same Math.max(1, parseInt(…)

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Incoming query param\npage / offset / limit] --> B{Parse numeric value}
    B -->|parseInt / Number| C{Result is NaN?}
    C -- Yes --> D[Apply fallback default\npage→1, limit→50, offset→0]
    C -- No --> E[Use parsed value]
    D --> F{Apply Math.max clamp}
    E --> F
    F -->|page: Math.max 1, val| G[page ≥ 1]
    F -->|offset: Math.max 0, val| H[offset ≥ 0]
    F -->|limit: Math.max 1 then Math.min 100| I[1 ≤ limit ≤ 100]
    G --> J[offset = page - 1 × 20]
    H --> K[Supabase .range offset, offset+limit-1]
    I --> K
    J --> K
    K --> L[Safe DB query — no negative range values]
Loading

Reviews (1): Last reviewed commit: "fix(pagination): clamp page and offset p..." | Re-trigger Greptile

@ralyodio ralyodio closed this May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Directory listing accepts invalid page numbers

2 participants