Repository navigation
fix(api): unbounded client limit and page reach SQL and return 500 - #890
Conversation
…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
|
🚅 Deployed to the SahajCloud-pr-890 environment in sahajcloud
1 service not affected by this PR
|
|
🤖 no session running recent dispatchesWritten by the sydevs dispatcher. Sessions never edit this comment. |
sydevs-bot
left a comment
There was a problem hiding this comment.
🧐 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:231repeats thecurrentDepthtest that line 249 already makes.consider—index.ts:22re-exports both constants through a barrel the one outside consumer deliberately bypasses.consider—clientReadParametersDocs.ts:132publishesminimum: 1while the gate acceptslimit=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].paginationcarries onlydefaultLimitandlimits(collections/config/types.d.ts:432), andmaxLimitis absent frompayload/dist. The gate really is the only seam.find.js:73readspage || 1, so the0the gate admits becomes page 1. The JSDoc's claim holds.- Bounding ahead of
asTrustedReqfollowsdocs/rules/api-clients.md:257, against the ticket's own criterion. Every trusted endpoint passes a constant or a zod bound under 100. Onlygeojson.ts:98forwards a clientlimit, and it runs unwrapped. - Consumer sides. SahajAtlasWeb sends
pagination: falseand nolimiton its three reads. WeMeditateWeb's largest is 100.
🤖 Written by the sydevs autonomous loop — see what this is
Generated by Claude Code
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
|
Addressed the adversarial review — three threads, four commits, pushed as 7f9a6d4. Adopted
Left open for you
Also
Gate green: lint, both typechecks, 2278 unit tests, 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
…client-limit-page
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
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
Summary
limitorpagenow has a ceiling, so neither reaches Postgres out of range — each returned 500 on its own.limit=0,pagination=false) is untouched. Refusing it is feat(api): refuse an unbounded client read, once the Atlas widget paginates #889, blocked on the Atlas widget.Change outline
Contract
What a client sees. Phase 1 adds one refusal and changes no successful response.
Behaviour
The gate gains one check, and its position is the deliberate part.
The
selectgate's two bypasses relax a shape policy. A range bound is a security gate — the file's own rule is never to honourisTrustedReqin one — so it runs ahead of both. No trusted endpoint forwards a client-suppliedlimittoday, 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
limitandpage— the published ceilings, now read from the constantsmaximum: 2000/maximum: 10000on the two parametersTest results
pnpm lint && pnpm typecheck && pnpm typecheck:tests && pnpm test:unit— 2278 unit testsclient-query-validation,globals-client-reads,client-origin-enforcement, and the six endpoints that forwardasTrustedReqManual verification
400for…?select[label]=true&limit=99999999999999999999and for the same URL withpage=, and200forlimit=10.200for…/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
pageis bounded independently oflimit, not as apage × limitproduct. 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⁷.limitis refused although it does not 500. Reproduced: the adapter swallows it and returns rows. A negativepagedoes 500. One rule covers both rather than two rules that differ by a sign.limitminimum was wrong, and nothing tied it to the gate. The spec saidminimum: 1while the gate acceptslimit=0— the whole-table spellingdocs/rules/api-clients.mdnames 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.pagekeepsminimum: 1: it is 1-based, so 0 is not a page, and the gate admits it only because one rule covers both numbers.pagination: falseSahajCloud 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.securityReview.triggerPatternmatches none of the three source paths. Reviewed by/simplify(four lenses) and the branch comment lint instead;pr-review-toolkitis not installed in a routine.Detail
Reproduced, not inferred. With the check removed, the int spec reports the real failures:
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=0go red), the old ordering restored behindskipClientQueryValidation(rejects … on a trusted internal readgoes red),Number.isIntegerdropped (rejects a fractional limitgoes red), the publishedmaximumreturned to the pre-PR hardcoded 100 (publishes the bounds the gate enforcesgoes red), andminimum: 1restored onlimit(publishes limit=0 and keeps page 1-basedgoes red).Two cases cannot fail against any phase-1 mutation, by design.
allows an absent limit and pageandallows pagination=falsepin the boundary this PR declines to cross; they go red the day phase 2 lands, which is the point of them.parseRestQueryhad to be widened first. It coerced onlydepth, so a REST case assertinglimit=2001would have compared the string'2001'against 2000 and passed while proving nothing. It now runs payload's ownisNumberoverdepth,limitandpage, rather than a local copy of that rule.No native cap exists to turn on.
maxLimitis absent from[email protected](grep -rln maxLimit node_modules/payload/dist/is empty);pagination.defaultLimitis a default an explicitlimitoverrides. The gate is the only seam, and it is the narrowest one that covers bothGET /api/eventsand the geojson endpoint, which forwardsquery.limititself.The two bounds are not on the
@/plugins/usagebarrel.clientReadParametersDocs.tsdeep-imports./constantsbecause 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 × limitproduct (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-numericlimit(limit=abcstays a string throughparseParamsand never reproduced a failure — out of scope, as the ticket says).Per-endpoint limits are unaffected.
for-audience,related-lecturesandrelated-meditationseach 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
limitis 100, it sendspaginationnowhere, and it has nolimit=0. No follow-up ticket filed there.Closes #887
🤖 Generated with Claude Code
https://claude.ai/code/session_01NKMvNBzNieXpPYvM21TU4n
Generated by Claude Code