Repository navigation
fix(meditations): version history 400s — a read beforeOperation hook also fires on findVersions - #768
Conversation
…filters
Payload maps `find`, `findByID`, `findVersions` and `findVersionByID` onto
the single `read` hook operation (`operationToHookOperation`). So a
`beforeOperation` hook guarding on `operation === 'read'` also runs on a
versions read, and the `where` it appends is applied to the *versions*
collection, where a document's fields live under `version.` and its id is
`parent`.
`filterMeditationsByLocale` appended `{ locale: { equals: req.locale } }`,
which is not a queryable path there. Query validation runs before the read
and regardless of `overrideAccess`, so `payload.findVersions({ collection:
'meditations' })` threw `QueryError: The following path cannot be queried:
locale` for every caller, and the admin version-history tab for meditations
was broken. `excludeFinishedEvents` appended `schedule.lastDate` the same
way, for API clients.
`'id' in args` did not stop either: it separates `findByID` from `find`, not
a versions read from either. The new `isVersionsRead` tells them apart by
the `draft` argument, which every `find`/`findByID` call site passes (both
the Local API and the REST handler, in both operations) and neither versions
read passes at all. It gates on the `read` hook operation, so `count` — which
has no `draft` either — is untouched, and `countVersions` maps to its own
hook operation.
Both filters are scoping, not authorization: `locale=all` and an explicit
`schedule.lastDate` clause already opt any caller out of them, and
`access.readVersions` (#719) is unchanged. So declining them on a versions
read widens no one's authority.
The specs are pinned by reintroducing the defect: with the guard deleted,
the three new cases fail with the QueryError above, and the two
"guards the guard" cases stay green.
Closes #745
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01YTcPYAyJXziiqJFJwUmPCt
Collapse the #745 rationale into one place. The mechanism now lives in `isVersionsRead`'s doc comment; the two hooks and both specs point at it in one line each instead of restating it. Also drop two new test cases that duplicated assertions already sitting a few lines above them in the same `describe` ("still filters an ordinary find" / "still filters an ordinary client list read"), and align the guard's placement in both hooks — after the operation gate, which now type-checks since the helper takes the hook's own operation union. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01YTcPYAyJXziiqJFJwUmPCt
…claims An adversarial pass found the guard under-tested in the direction that fails silently. `isVersionsRead` reads an argument Payload supplies, so if an upgrade stopped supplying it, both filters would switch off for ordinary reads rather than erroring — a wrong result, not a 400. Every meditations locale case passed `draft: true` by hand, which makes the test true whatever Payload does, so nothing there would have noticed. Adds a case that leaves `draft` to Payload, on published fixtures so the German meditation is excluded by the filter rather than by being a draft. Verified by making the helper return `true` unconditionally and watching it go red. `listIds` in the events suite already covered this by omitting `draft`; a comment now says so, since adding one would quietly remove the pin. Also corrects an over-claim: only `findVersions` ever reached the 400. Both hooks already exited on `'id' in args`, which `findVersionByID` carries. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01YTcPYAyJXziiqJFJwUmPCt
…read The mapping is the part a future hook author cannot see from the code, and this is the second hook to trip on it. Adds the rule to `src/collections/AGENTS.md` with the guard's shape, names the other `read` hooks that legitimately keep firing on a versions read, and warns that the guard fails silently in the widening direction. Also lists `versionsRead` in `src/AGENTS.md`'s `utilities/` inventory, and records the new coverage in `tests/COVERAGE.md`. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01YTcPYAyJXziiqJFJwUmPCt
|
🚅 Deployed to the SahajCloud-pr-768 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
The diff matches the ticket. Two hooks, one shared guard, no smuggled refactor.
I re-derived the discriminator from payload 3.86.0, and it holds. find and findByID have two call sites each, and all four pass draft. Neither versions read passes it. Declining is safe too: readVersions already scopes meditations versions via withVersionHistoryAccess. Both open questions are about shape, not correctness.
Findings
should-fix— the ticket's first acceptance criterion is unpinned.tests/int/meditations.int.spec.ts:356consider— a second answer to a hazard this repo already answers.src/lib/utilities/versionsRead.ts:48consider— one 3-line predicate, four prose restatements.src/lib/utilities/versionsRead.ts:5
What was checked
Ticket #745. The whole diff. Payload 3.86.0 dist: operationToHookOperation, all four callers of findOperation and findByIDOperation, both versions handlers. Read whole: both hooks, ensureWebPathDeps (benign as claimed), accessConfigs.ts, both spec blocks.
🤖 Written by the sydevs autonomous loop — see what this is
Generated by Claude Code
AC 1 of #745 asked for a versions read that resolves for a manager at a single locale. Both new cases passed `overrideAccess: true`, which skips `readVersions`, so the suite pinned query validation but never the path the bug was reported on. Adds one case with `overrideAccess: false` and a `meditations-editor` manager, plus a negative half proving the gate is consulted rather than skipped. Verified by deleting the hook's guard (red, "path cannot be queried") and by disabling the `Where` translation in `withVersionHistoryAccess` (still green — that grant is unconditional, so the comment says the translation is pinned elsewhere rather than here). Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01M9ax3qykqyrNQ4dW7oC9W9
…late The repo now answers "a document `Where` reaching a versions read" twice — `withVersionHistoryAccess` translates it, these two hooks decline it — and the AGENTS.md section gave the next hook author no rule for choosing. Adds the rule: a filter that scopes a listing is declined, a filter that decides authority is translated, because declining that one widens access. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01M9ax3qykqyrNQ4dW7oC9W9
One 3-line predicate carried four prose restatements — a 43-line doc comment, a 49-line AGENTS.md section, an `src/AGENTS.md` bullet, and both hooks' own comments. Roughly 110 of this PR's 255 added lines said the same thing. Keeps the argument table, which cannot live anywhere else, plus a pointer to the AGENTS.md section that now owns the rule. Nothing is lost: the consumers, the silent-failure warning, and what to pin are all stated there. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01M9ax3qykqyrNQ4dW7oC9W9
…ns-read-hook-guard
|
Three threads, all adopted. Pushed
One correction, in the first thread: that case does not pin Gate green: lint, both typechecks, 1683 unit. 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
The old wording — "every `find` and `findByID` call site passes `draft`" — read as a claim about our callers, which makes the discriminator look caller-dependent and so brittle. It is not. All eight read call sites live in payload's dist, under `collections/endpoints/` and `collections/operations/local/`. The four document reads name `draft` as a literal key in the object they build, so the key is present even when its value is `undefined` — a REST query with no `?draft=` still carries it. The four versions reads never name it. Comment only. No behaviour change. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LA8ocXYFkcRKFCyLLN2vJq
|
One thread, answered rather than adopted. Pushed
Your call, and the PR waits on it. The one design that removes the check is moving locale scoping into Gate green at 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
|
One thread, settled by the reviewer. No commit, nothing pushed — the head stays
The PR body no longer says this PR waits on an answer. Every other thread was already adopted and resolved in the earlier rounds. 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
Summary
payload.findVersions({ collection: 'meditations' })resolves again. It threwQueryErrorfor every caller,overrideAccess: trueincluded, so the admin Versions tab was broken.find,findByID,findVersionsandfindVersionByIDonto onereadhook operation. The two hooks that append awherenow decline a versions read.access.readVersions(fix(access)!:readVersionsis unset on every collection, so any API client can read drafts through/api/{collection}/versions#719) is untouched, so nobody's authority widens.Changes
src/lib/utilities/versionsRead.ts— newisVersionsRead. Tells a versions read from a document read by thedraftargument, which onlyfind/findByIDcarry.src/collections/Meditations/hooks/filterMeditationsByLocale.ts— stops appending{ locale: { equals } }to a versions query.src/collections/Events/hooks/excludeFinishedEvents.ts— same, forschedule.lastDate.src/collections/AGENTS.md— the mapping written down, where the next hook author reads it, plus the rule for choosing between declining a filter and translating it.tests/int/meditations.int.spec.ts— three versions cases, one of them through the access gate as ameditations-editormanager.Preview
Test results
pnpm lint,pnpm typecheck,pnpm typecheck:tests,pnpm test:unit(1683 passed), re-run atf9c16f1meditations.int+events.int+role-based-access.int— 100 passedManual verification
Notes for reviewer
The one open question is settled. @Ardnived asked whether the
draftcheck is the right approach (thread) and answered "leave it as it is" on 2026-09-12. No code changed in response. The evidence is below. For the record, the only design that removes the check is moving the meditations locale scoping intoaccess.read— which reclassifies a scoping filter as authorization and drops it foroverrideAccess: truecallers.excludeFinishedEventscannot move there at all: itsschedule.lastDateopt-out ("a client asking for past events deliberately gets them") is not expressible in access control.The discriminator reads a Payload-internal argument shape — and Payload's own code decides it, not ours. There is no public "is this a versions read" signal: the hook receives
operation: 'read'for all four operations, and'id' in argsonly separatesfindByIDfromfind.draftis the one argument that splits them. All eight read call sites live in payload 3.86.0's dist, undercollections/endpoints/andcollections/operations/local/. The four document reads namedraftas a literal key in the object they build — so the key is present even when its value isundefined, as a REST query with no?draft=gives — and the four versions reads never name it.buildBeforeOperation.jsmarks the mapping itself// TODO: v4 should not need this mapping.It fails in the widening direction, which is why the false branch is pinned too. If an upgrade stopped supplying
draft, both filters would silently switch off rather than error. Every meditations locale case passeddraft: trueby hand, which would have hidden that, so there is now one that leavesdraftto Payload.listIdsin the events suite already covered it by omittingdraft; a comment now says so.Adversarial review round 1: three findings, all adopted. One
should-fix— the ticket's first acceptance criterion was unpinned, because every versions case passedoverrideAccess: trueand so skippedreadVersions. Twoconsider— the repo answered this hazard twice with no rule for choosing, and one 3-line predicate carried four prose restatements.Two things I found and did not fix — flagging rather than widening the PR:
ensureWebPathDepsis a thirdreadhook that also fires on a versions read. It mutatesselect, notwhere, andsanitizeSelectdrops unknown keys rather than throwing, so it is benign today. It is out of this ticket's scope, but it is the same shape.readDistinct(findDistinct), so the admin's list-filter dropdown on meditations is not locale-scoped. Pre-existing, unchanged here. Say the word and I will file either.Detail
What the access-gate case does and does not pin. It needs the hook's guard: deleting that turns all three meditations versions cases red with
The following path cannot be queried. It does not pinwithVersionHistoryAccess'sWheretranslation —meditations-editor'supdategrant is unconditional, so disabling the translation leaves the case green. Verified both ways, and the case comment says so and points atrole-based-access.int.spec.ts, which pins the translation onpages. A second half with apath-editormanager asserts the same read is refused, so the positive half cannot pass with the gate silently skipped.The ticket's framing of the events half was slightly off, and the spec says so. It expected a client's
findVersionsoneventsto be getting a 400 where a 403 is honest. In factfindVersionsOperationrunsexecuteAccess(access.readVersions)beforevalidateQueryPaths, and no client role grantsevents: update, so a client was already getting the 403. The 400 was reachable only withoverrideAccess: true— an internal read forwarding a clientreq— and that is the shape the regression pin uses. The access-gate case is kept beside it, with a comment saying plainly that it passes before the fix as well.Only
findVersionsever reached the 400. Both hooks already exited on'id' in args, whichfindVersionByIDcarries. The helper still answers for both, so a hook that does not checkidis covered.Declining versus translating is now a stated rule, not a size judgement.
src/collections/AGENTS.md: a filter that scopes a listing (locale, finished events) is declined, because a versions read is not that listing and nobody's authority widens. A filter that decides authority is translated, never declined, because declining it widens who may read a version row — which is exactly whatwithVersionHistoryAccessdoes with Payload'sappendVersionToQueryKey.Rejected alternative: a wrapper or a plugin-level guard.
usagePluginis the only place that injectsbeforeOperationhooks across collections, and a blanket versions-skip there would be wrong —validateClientQueryParamsHookand the usage counter should keep firing on a versions read. The hazard is narrower than "read hooks", andgrep "args.where ="returns exactly the two hooks fixed here.Why no unit test for the helper. A unit test would assert the object shape this code already assumes, and agree with itself. The pin is a real
findVersionsthrough a booted Payload; a version bump that changes the argument shape turns it red. Both branches were verified by reintroducing the defect: deleting the guard fails the versions cases withThe following path cannot be queried, and forcing the helper toreturn truefails the false-branch case.Where the rule lives now. The helper's doc comment carries the argument table and the call-site evidence for it, and nothing else.
src/collections/AGENTS.mdowns the rule, the consumer list, the silent-failure warning, and what to pin when a third hook needs the guard.Fixture pre-mortem. The specs assume both collections enable versions — checked against
src/collections/Meditations/Meditations.ts:136andsrc/collections/Events/Events.ts:105, not assumed. The false-branch case needs published documents, because meditations fixtures default to draft; it creates its own so the German one is excluded by the locale filter rather than by being a draft.No migration. Hooks only, no schema change.
Security review skipped — no path in the diff matches
securityReview.triggerPatternin.claude/workflow.json. The access reasoning is in the Summary instead.Closes #745
🤖 Written by the sydevs autonomous loop — see what this is
🤖 Generated with Claude Code
https://claude.ai/code/session_01LA8ocXYFkcRKFCyLLN2vJq