Skip to content

fix(api): unbounded client limit and page reach SQL and return 500 - #890

Merged
sydevs-bot merged 9 commits into
mainfrom
claude/fix-887-bound-client-limit-page
Oct 10, 2026
Merged

sydevs-bot merged 9 commits into
mainfrom
claude/fix-887-bound-client-limit-page

Conversation

@sydevs-bot

@sydevs-bot sydevs-bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Change outline

Contract

What a client sees. Phase 1 adds one refusal and changes no successful response.

 GET /api/meditations?select[label]=true&limit=10          → 200
-GET /api/meditations?select[label]=true&limit=99999999999 → 500  DrizzleQueryError
+GET /api/meditations?select[label]=true&limit=99999999999 → 400  "limit" … between 0 and 2000
-GET /api/meditations?select[label]=true&page=99999999999  → 500  limit $1 offset $2
+GET /api/meditations?select[label]=true&page=99999999999  → 400  "page" … between 0 and 10000
+GET /api/meditations?select[label]=true&page=-1           → 400  (was 500: OFFSET -20)
 GET /api/regions?select[name]=true&pagination=false       → 200  whole table, unchanged
 GET /api/meditations?select[label]=true&limit=0           → 200  whole table, unchanged
 GET /api/docs — the limit and page parameters      src/plugins/openapi/clientReadParametersDocs.ts
-  limit  maximum 100          # prose only; nothing enforced it
+  limit  maximum MAX_CLIENT_LIMIT
-  limit  minimum 1            # refused limit=0, which the server answers 200
+  limit  minimum 0
-  page   no maximum
+  page   maximum MAX_CLIENT_PAGE
   page   minimum 1            # unchanged: page is 1-based, so 0 is not a page

Behaviour

The gate gains one check, and its position is the deliberate part.

 validateClientQueryParamsHook(args, operation, req)        src/plugins/usage/hooks.ts
   not a client read → return
+  currentDepth is numeric → return                         # payload's own population reads
+  assertWithinBound limit, page
+    not a number → return                                  # a non-numeric limit stays a string
+    whole, 0 … max → return                                # 0 is payload's "no limit" sentinel
+    else → warn, APIError 400 naming the bound
   asTrustedReq opt-out → return
   live-preview request → return
-  currentDepth is numeric → return                         # moved above the bounds
   no select → APIError 400
   depth > 1 and no populate → APIError 400

The select gate's two bypasses relax a shape policy. A range bound is a security gate — the file's own rule is never to honour isTrustedReq in one — so it runs ahead of both. No trusted endpoint forwards a client-supplied limit today, so this closes a latent hole, not a live one. Payload's own population reads stay exempt, and that one early return now sits above everything rather than being tested twice: their numbers are payload's, not the caller's.

Preview

Test results

  • Lean gate: ✓ pnpm lint && pnpm typecheck && pnpm typecheck:tests && pnpm test:unit — 2278 unit tests
  • Targeted specs: 246 passed across 10 int files — client-query-validation, globals-client-reads, client-origin-enforcement, and the six endpoints that forward asTrustedReq
  • Build: N/A — Railway builds the preview

Manual verification

  1. Against a published client key, confirm 400 for …?select[label]=true&limit=99999999999999999999 and for the same URL with page=, and 200 for limit=10.
  2. Confirm 200 for …/api/regions?select[name]=true&depth=0&pagination=false, and that the Atlas widget still loads the whole atlas against the preview — its three feeds read that way.

Notes for reviewer

  • The cap is 2000, not the 1000 the ticket proposed. The owner offered either. 2000 was taken because option (b) is now the decision for phase 2, so this ceiling becomes the page size every client paginates with, and a higher one halves the round trips. Evidence either way: our own largest paginated read asks for 1000, the largest any consumer sends is 100.
  • page is bounded independently of limit, not as a page × limit product. The ticket said either was fine. Two bounds give two messages that each name what the caller sent; at both ceilings the offset stays under 2 × 10⁷.
  • A negative limit is refused although it does not 500. Reproduced: the adapter swallows it and returns rows. A negative page does 500. One rule covers both rather than two rules that differ by a sign.
  • The published limit minimum was wrong, and nothing tied it to the gate. The spec said minimum: 1 while the gate accepts limit=0 — the whole-table spelling docs/rules/api-clients.md names and the Atlas widget sends on three feeds. A spec-validating client generator would have refused, ahead of the server, a request the server answers 200. page keeps minimum: 1: it is 1-based, so 0 is not a page, and the gate admits it only because one rule covers both numbers.
  • Phase 2 is feat(api): refuse an unbounded client read, once the Atlas widget paginates #889, not a phase on this PR. The blocker is refactor(api): paginate the three pagination: false SahajCloud reads SahajAtlasWeb#253 merging and deploying — another repo's work — so an unticked phase box here would have re-fired an implement session that could only stop again.
  • Security review skipped: securityReview.triggerPattern matches none of the three source paths. Reviewed by /simplify (four lenses) and the branch comment lint instead; pr-review-toolkit is not installed in a routine.
Detail

Reproduced, not inferred. With the check removed, the int spec reports the real failures:

limit=99999999999999999999 → Failed query: … limit $1   params: 100000000000000000000
page=99999999999999999999  → Failed query: … limit $1 offset $2   params: 10,1e+21
page=-1                    → Failed query: … limit $1 offset $2   params: 10,-20
limit=-1                   → resolves. No 500.

Every new assertion has failed once, under six separate mutations: the checks removed (the four rejections go red), both ceilings set to 1 (allows a limit at the cap, allows a page at the bound, allows limit=0 go red), the old ordering restored behind skipClientQueryValidation (rejects … on a trusted internal read goes red), Number.isInteger dropped (rejects a fractional limit goes red), the published maximum returned to the pre-PR hardcoded 100 (publishes the bounds the gate enforces goes red), and minimum: 1 restored on limit (publishes limit=0 and keeps page 1-based goes red).

Two cases cannot fail against any phase-1 mutation, by design. allows an absent limit and page and allows pagination=false pin the boundary this PR declines to cross; they go red the day phase 2 lands, which is the point of them.

parseRestQuery had to be widened first. It coerced only depth, so a REST case asserting limit=2001 would have compared the string '2001' against 2000 and passed while proving nothing. It now runs payload's own isNumber over depth, limit and page, rather than a local copy of that rule.

No native cap exists to turn on. maxLimit is absent from [email protected] (grep -rln maxLimit node_modules/payload/dist/ is empty); pagination.defaultLimit is a default an explicit limit overrides. The gate is the only seam, and it is the narrowest one that covers both GET /api/events and the geojson endpoint, which forwards query.limit itself.

The two bounds are not on the @/plugins/usage barrel. clientReadParametersDocs.ts deep-imports ./constants because that barrel also exports the pg-pool seam, so a barrel export would be a door straight back to the cost the deep import avoids. Nothing imported them that way.

Rejected: bounding the page × limit product (one number, but a message that cannot name what the caller sent); a zod schema per endpoint (would have been written twice and missed the CRUD surface); refusing a non-numeric limit (limit=abc stays a string through parseParams and never reproduced a failure — out of scope, as the ticket says).

Per-endpoint limits are unaffected. for-audience, related-lectures and related-meditations each declare tighter zod bounds (20, 100). Those are business limits; 2000 is the driver-safety floor underneath them.

WeMeditateApp was checked at the owner's request: its largest SahajCloud limit is 100, it sends pagination nowhere, and it has no limit=0. No follow-up ticket filed there.

Closes #887

🤖 Generated with Claude Code

https://claude.ai/code/session_01NKMvNBzNieXpPYvM21TU4n


Generated by Claude Code

claude added 3 commits October 6, 2026 20:36
…ange

An API client had to declare `select` and `populate`, but nothing bounded
`limit` or `page`. Both reached Postgres unvalidated, and either one alone
returned 500 from the driver: a 20-digit `limit` overflows directly, and a
20-digit `page` overflows the `(page - 1) * limit` offset. A negative `page`
reaches the same 500 by a shorter route, as `OFFSET -20`.

The gate is the only seam — `maxLimit` does not exist in [email protected], so
there is no native cap to turn on. Both ceilings are headroom: our own largest
paginated read asks for 1000 and the largest any consumer sends is 100.

`limit=0` and `pagination=false` stay allowed. They are payload's two spellings
of an unbounded read, and the Atlas widget sends the second on three live
feeds, so refusing them is a policy decision with a cross-repo migration behind
it rather than part of closing the 500.

Refs #887

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NKMvNBzNieXpPYvM21TU4n
Review pass over the branch. Four changes, each from a finding:

The `asTrustedReq` and live-preview returns relax a shape policy — the file's
own rule says never to honour them in a security gate, and a driver-range bound
is the latter. No trusted endpoint forwards a client-supplied `limit` today, so
this closes a latent hole rather than a live one. Payload's internal population
reads stay exempt: their numbers are payload's, not the caller's.

The two ceilings move to `constants.ts`, where the plugin's other policy numbers
live, and reach the barrel. `clientReadParametersDocs.ts` now reads them, so the
published spec stops advertising a `limit` cap of 100 and an unbounded `page`.

The gate's arg shape gets one named type instead of two spellings, and the
`assertWithinBound` doc keeps only what the code cannot show.

The bounds spec collapses ten near-identical cases into two tables, the REST
case moves beside its siblings in the REST describe, and the manager fixture is
hoisted. Two cases are new: a fractional limit, and a trusted internal read.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NKMvNBzNieXpPYvM21TU4n
`endpoints.md` said a forwarded read bypasses the client query gate. Half of
that is still true, and the half that is not is the one a reader would act on.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NKMvNBzNieXpPYvM21TU4n
@railway-app

railway-app Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

🚅 Deployed to the SahajCloud-pr-890 environment in sahajcloud

Service Status Web Updated
SahajCloud ✅ Success (View Logs) Web Oct 10, 2026 at 2:00 am UTC
1 service not affected by this PR
  • mailpit

@sydevs-bot

sydevs-bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor Author

🤖 no session running

recent dispatches
  • 21:08Z address-review · attempt 1 · session · done
  • 21:08Z address-review · attempt 1 · session
  • 20:57Z adversarial-review · attempt 1 · session · done
  • 20:57Z adversarial-review · attempt 1 · session

Written by the sydevs dispatcher. Sessions never edit this comment.

@sydevs-bot sydevs-bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧐 Adversarial review

Nothing here blocks. Four source files and about 90 lines bound two numbers, at the only seam payload offers. The PR closes a reproduced 500 and leaves the owner's option (b) to #889 behind a named cross-repo blocker, rather than capping three live Atlas feeds now. No consumer sends a limit over 100 or any page, so neither ceiling breaks anything today. Three small questions follow, none of them blocking.

Findings

  • consider — hooks.ts:231 repeats the currentDepth test that line 249 already makes.
  • consider — index.ts:22 re-exports both constants through a barrel the one outside consumer deliberately bypasses.
  • consider — clientReadParametersDocs.ts:132 publishes minimum: 1 while the gate accepts limit=0.

What was checked

  • #887's comments. The 2000 cap and option (b) are the owner's own words, so the ticket's "1000" is superseded.
  • [email protected]. pagination carries only defaultLimit and limits (collections/config/types.d.ts:432), and maxLimit is absent from payload/dist. The gate really is the only seam.
  • find.js:73 reads page || 1, so the 0 the gate admits becomes page 1. The JSDoc's claim holds.
  • Bounding ahead of asTrustedReq follows docs/rules/api-clients.md:257, against the ticket's own criterion. Every trusted endpoint passes a constant or a zod bound under 100. Only geojson.ts:98 forwards a client limit, and it runs unwrapped.
  • Consumer sides. SahajAtlasWeb sends pagination: false and no limit on its three reads. WeMeditateWeb's largest is 100.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

Comment thread src/plugins/usage/hooks.ts Resolved
Comment thread src/plugins/usage/index.ts Resolved
@sydevs-bot sydevs-bot added bot:working Lock: a cloud session is running on this item. Removed when it ends. and removed bot:working Lock: a cloud session is running on this item. Removed when it ends. labels Oct 6, 2026
claude added 4 commits October 6, 2026 21:12
The bounds were wrapped in `currentDepth !== 'number'` while the gate
below repeated the same test as an early return. Hoisting that return
above the bounds is equivalent — the two checks it now precedes,
`SKIP_VALIDATION` and `isLivePreviewRequest`, are pure context reads
with no side effect — and it drops both the duplicate and the nesting.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LKbsvvnPNiQk9DHDL9kSn6
Nothing imports them through it. The one outside consumer,
`clientReadParametersDocs.ts`, deep-imports `./constants` on purpose,
because this barrel also exports the pg-pool seam — so a barrel export
is a door back to the cost that deep import exists to avoid.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LKbsvvnPNiQk9DHDL9kSn6
The spec said `minimum: 1` while the gate accepts `limit=0` and
`docs/rules/api-clients.md` names it a supported whole-table read the
Atlas widget makes on three feeds. A generated client would have
refused, ahead of the server, a request the server answers 200.

`page` keeps `minimum: 1`: it is 1-based, so 0 is not a page. The gate
admits it only because one rule covers both numbers, and `find.js`
reads `page || 1`.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LKbsvvnPNiQk9DHDL9kSn6
Nothing tied the OpenAPI parameter shape to the gate it documents, which
is how `minimum: 1` outlived a gate accepting `limit=0`. Both assertions
were watched red first: the maximum against the pre-PR hardcoded 100,
the minimum against the `1` just removed.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LKbsvvnPNiQk9DHDL9kSn6
@railway-app
railway-app Bot temporarily deployed to sahajcloud / SahajCloud-pr-890 October 6, 2026 21:19 Destroyed
@sydevs-bot

Copy link
Copy Markdown
Contributor Author

Addressed the adversarial review — three threads, four commits, pushed as 7f9a6d4.

Adopted

Left open for you

  • page keeps minimum: 1. It is 1-based, so 0 is not a page, and the gate admits it only because one rule covers both numbers. Same thread.

Also

  • 7f9a6d4 pins both published bounds against the constants. Nothing tied the spec to the gate, which is how minimum: 1 outlived it. Both assertions were watched red first.

Gate green: lint, both typechecks, 2278 unit tests, client-query-validation 40/40. PR body refreshed — the gate's order changed.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@sydevs-bot sydevs-bot removed the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Oct 6, 2026
@sydevs-bot
sydevs-bot marked this pull request as ready for review October 6, 2026 21:27
@sydevs-bot
sydevs-bot requested a review from Ardnived October 6, 2026 21:27
@sydevs-bot
sydevs-bot enabled auto-merge October 6, 2026 21:27
@railway-app
railway-app Bot temporarily deployed to sahajcloud / SahajCloud-pr-890 October 10, 2026 01:58 Destroyed
@sydevs-bot
sydevs-bot added this pull request to the merge queue Oct 10, 2026
Merged via the queue into main with commit d1b0322 Oct 10, 2026
15 checks passed
@sydevs-bot
sydevs-bot deleted the claude/fix-887-bound-client-limit-page branch October 10, 2026 17:25
@sydevs-bot sydevs-bot moved this from Revising to Done in SYDevelopers Technical Roadmap Oct 10, 2026
sydevs-bot pushed a commit that referenced this pull request Oct 10, 2026
One conflict, in `tests/unit/openapi-custom-endpoints.spec.ts`: both sides
added an import on the same lines. Kept both — `getProjectSlugs` for this
branch's `meditation-transcripts` project-tier assertions, and main's
`limitParameter`/`pageParameter` for the client-limit bounds spec (#890).

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01L39RLXia3g6pcNupvCQMpz
Ardnived pushed a commit that referenced this pull request Nov 5, 2026
One conflict, in `tests/unit/openapi-custom-endpoints.spec.ts`: both sides
added an import on the same lines. Kept both — `getProjectSlugs` for this
branch's `meditation-transcripts` project-tier assertions, and main's
`limitParameter`/`pageParameter` for the client-limit bounds spec (#890).

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01L39RLXia3g6pcNupvCQMpz

This branch was successfully deployed

No deployments
sahajcloud / SahajCloud-pr-890 — 69ab4b63 Deployed Oct 10, 2026 by railway-app[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

fix(api): unbounded client limit and page reach SQL and return 500

3 participants