Repository navigation
fix!: inherit read access for collection versions - #17634
Conversation
📦 esbuild Bundle Analysis for payloadThis analysis was generated by esbuild-bundle-analyzer. 🤖
Largest pathsThese visualization shows top 20 largest paths in the bundle.Meta file: packages/next/meta_index.json, Out file: esbuild/index.js
Meta file: packages/payload/meta_index.json, Out file: esbuild/index.js
Meta file: packages/payload/meta_shared.json, Out file: esbuild/exports/shared.js
Meta file: packages/richtext-lexical/meta_client.json, Out file: esbuild/exports/client_optimized/index.js
Meta file: packages/ui/meta_client.json, Out file: esbuild/exports/client_optimized/index.js
Meta file: packages/ui/meta_shared.json, Out file: esbuild/exports/shared_optimized/index.js
DetailsNext to the size is how much the size has increased or decreased compared with the base branch of this PR.
|
|
Also test coverage is needed for these scenarios:
Are globals being updated in a separate PR with the same treatment? |
paulpopus
left a comment
There was a problem hiding this comment.
Test cases to add for this change:
- A test that calls
getEntityPermissions(ordocAccessOperation) for the existinginherited-read-versionscollection, using its id-basedreadfunction, for a document thatreaddenies. Confirm thereadVersionspermission matches the direct result ofread, not the opposite. - A global collection with an id-based
readfunction, checked throughfindGlobalVersionByID. The current global test only uses aWhere-onlyreadfunction, so it cannot catch a wrongidvalue reachingread. - The new lookup inside the
readVersionsfallback indefaults.ts, and the lookup already insidefindVersionByID.ts, now both query the same version row for the samefindVersionByIDcall. Combining them would avoid a second database round trip on every call.
…ons-access-control
|
Repro (sketch): a collection with a field like
Follow-up PR with the fix + regression test: #17804 (based on this branch). |
DanRibbens
left a comment
There was a problem hiding this comment.
Let's get #17804 reviewed and merged before we merge this PR
…ons-access-control # Conflicts: # packages/payload/src/collections/operations/findVersionByID.ts
#17804) ## Summary - Follow-up to #17634. Both collection and global `findVersionByID` merge the version id lookup with the `readVersions` access result and hand the merged `where` to the DB layer without running `sanitizeWhereQuery`. `findByID`, `findVersions`, and `countVersions` all sanitize the merged `where` in the same spot, so relationship-backed virtual fields resolve to their underlying paths. - With the inherited-from-`read` defaults introduced in #17634, any collection or global whose `read` access returns a query referencing a virtual field silently breaks `findVersionByID` — the constraint (e.g. `version.relatedLabel` where `relatedLabel` is `virtual: 'related.label'`) never gets rewritten so the version lookup returns nothing and the caller sees Forbidden/NotFound. - Mirror `sanitizeWhereQuery({ fields: versionFields, payload, where })` from the sibling operations in both `findVersionByID` variants. - Adds a regression test to `test/access-control/int.spec.ts` that fails on the base PR branch and passes with this fix. ## Test plan - [x] `pnpm run build:core` - [x] `pnpm run test:int access-control` (50 passed, 6 skipped) - [x] Confirmed the regression test fails without the sanitize call and passes with it 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]> Co-authored-by: Jarrod Flesch <[email protected]>
…ons-access-control # Conflicts: # packages/payload/src/collections/config/defaults.ts # packages/payload/src/collections/config/sanitize.ts # test/access-control/int.spec.ts # test/types/types.spec.ts
paulpopus
left a comment
There was a problem hiding this comment.
Left a few comments, written with AI
| (async (args) => { | ||
| const result = await effectiveRead({ ...args, id: undefined }) | ||
|
|
||
| return hasWhereAccessResult(result) ? appendGlobalVersionToQueryKey(result) : result |
There was a problem hiding this comment.
This wrapper changes relatedLabel to version.relatedLabel. Global find, count, restore, and collection or global permission paths send it to the database without sanitizeWhereQuery. Valid virtual-field access then returns no versions or no permission. Sanitise each path and add regression tests.
|
|
||
| If the Collection has [Versions](../versions/overview) enabled, the `readVersions` Access Control function determines whether or not the currently logged in user can access the version history of a Document. | ||
|
|
||
| If `readVersions` is not configured, Payload uses the Collection's `read` Access Control. Boolean results are preserved, and query constraints are automatically adjusted to target fields within the stored version document. |
There was a problem hiding this comment.
This fallback changes omitted readVersions from authenticated-user access to inherited read access. A public read function can therefore expose historical or unpublished versions to unauthenticated users. Add a v4 migration entry with both outcomes and show how to preserve authenticated-only access.
| `SanitizedCollectionConfig` and its auth type no longer use `DeepRequired`. This reduces TypeScript checker work and makes optionality match runtime defaults. | ||
|
|
||
| Properties Payload does not default, such as `access.readVersions`, `auth.cookies.domain`, `auth.useAPIKey`, and `auth.depth`, now require undefined handling: | ||
| Properties Payload does not default, such as `auth.cookies.domain`, `auth.useAPIKey`, and `auth.depth`, now require undefined handling: |
There was a problem hiding this comment.
docs/migration-guide/v4.mdx:432-437 still says SanitizedGlobalConfig.access.readVersions can be undefined. This change makes it required at runtime and in the exported type. Update the optional-property list and remove the undefined check.
This was accounted for. We merged the referenced PR.
Payload 4 is in canary (4.0.0-canary.37) and every install into a v4 project was refused. Allow major 4 in both support-matrix targets and in every manifest (payloadMajors [3, 4], payload peer "^3.0.0 || ^4.0.0-0"), the component template, and the `new` scaffold. Widening the ranges alone was not enough: semver.intersects without includePrerelease tests each comparator on its own, so "<5.0.0-0" rejects 4.0.0-canary.37 and a pinned prerelease never intersects "^4.0.0-0". The "-0" bounds on every required range keep prereleases inside their major. detectProject now names an unsupported Payload or Next.js major instead of reporting "Unsupported project shape", which is what a v4 project got. The base Pages collection gains an explicit readVersions (signed-in only). Payload 4 inherits read access for versions (payloadcms/payload#17634), so visitors could list every previously published version. Payload 3 already requires a user here, so this is a no-op on v3. Co-Authored-By: Claude Opus 5.5 <[email protected]>
* cli(feat): accept Payload 4 projects alongside Payload 3 Payload 4 is in canary (4.0.0-canary.37) and every install into a v4 project was refused. Allow major 4 in both support-matrix targets and in every manifest (payloadMajors [3, 4], payload peer "^3.0.0 || ^4.0.0-0"), the component template, and the `new` scaffold. Widening the ranges alone was not enough: semver.intersects without includePrerelease tests each comparator on its own, so "<5.0.0-0" rejects 4.0.0-canary.37 and a pinned prerelease never intersects "^4.0.0-0". The "-0" bounds on every required range keep prereleases inside their major. detectProject now names an unsupported Payload or Next.js major instead of reporting "Unsupported project shape", which is what a v4 project got. The base Pages collection gains an explicit readVersions (signed-in only). Payload 4 inherits read access for versions (payloadcms/payload#17634), so visitors could list every previously published version. Payload 3 already requires a user here, so this is a no-op on v3. Co-Authored-By: Claude Opus 5.5 <[email protected]> * cli(fix): scaffold the pinned Payload 4 version and template in the fresh smoke create-payload-app 4 renamed --version to --payload-version. Its argument parser is permissive, so the old flag was silently dropped and a smoke pinned to 4.0.0-canary.37 scaffolded whatever the `canary` dist-tag resolved to. Pass the flag each major reads, and both for a dist-tag. create-payload-app 4 also downloads its template from the payloadcms/payload main branch instead of the release it installs. Main had already renamed createLocalReq (payloadcms/payload#18287, after canary.37 shipped), so the pinned scaffold failed tsc in the template's own seed route. Pin a v4 template to the release tag with --branch when that tag exists; v3 keeps its CLI's 3.x branch and needs no network check. Co-Authored-By: Claude Opus 5.5 <[email protected]> * ci(feat): add a non-blocking nightly Payload canary smoke The nightly fresh smoke follows Payload `latest`, so nothing exercised the next major before it ships. Run the same bare and website scenarios against `npm view payload@canary version` on Node.js 24 (Payload 4 requires 24.15+), schedule-only and with continue-on-error, outside pr-gate. Co-Authored-By: Claude Opus 5.5 <[email protected]> * cli(fix): widen related-posts to Payload 4 and hold manifests to the support matrix related-posts (#600) landed declaring Payload 3 only, so once Payload 4 is allowed it would be the one component refusing v4 projects. Give it the same supports and peer range as every other manifest; manifest-only, so the versioned-source contract needs no version bump. payload-components-support-matrix.int.spec.ts makes this class of drift fail: every manifest, the component template and the `new` scaffold must declare exactly the Payload and Next.js majors their targets allow, with peer ranges that cover each major's stable line and the newest Payload major's prereleases. A v3-only fixture keeps the guard from passing vacuously. buildManifest is exported so the scaffold's output can be checked directly. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
Inherits a collection or global's existing
readaccess for version operations whenreadVersionsis not configured. ExplicitreadVersionsaccess remains authoritative.How access resolves
readVersionsis configuredreadVersionsis omittedreadfunction, includingbaseAccess.readbaseAccess.readVersionsis applied independently in both cases, so inherited access combinesread,baseAccess.read, andbaseAccess.readVersionswith AND semantics.Boolean results pass through directly. Query-based results from inherited
readaccess are translated to the version table's document structure:{ status: { equals: 'published' } }{ 'version.status': { equals: 'published' } }{ 'version.status': { equals: 'published' } }{ id: { equals: documentID } }{ parent: { equals: documentID } }Nested
andandorconstraints are translated recursively. Queries returned from an explicitly configuredreadVersionsfunction are not changed because they already target the version table.findVersionByIDCollection version lookups involve two different IDs:
readVersionsandbaseAccess.readVersionsfunctions.readfunction, matching normal document reads.Payload resolves the parent ID internally before evaluating inherited access. If access returns a query constraint, the final lookup applies the translated constraint; a boolean result can reuse the prefetched version. The internal parent selection is removed from the response when the caller did not request it.
For
findVersionsandcountVersions, access functions receiveid: undefinedbecause those operations do not target a single record.Breaking Changes
Projects with versions enabled that omit
readVersionsnow restrict version history using their existingreadaccess. Projects that intentionally want version history to remain independently available to any authenticated user must configure that behavior explicitly:access: { read: customDocumentReadAccess, + readVersions: ({ req }) => Boolean(req.user), }Adds unit and integration coverage for boolean, query-based, mixed, and ID-aware access across collection and global
findVersions,countVersions, andfindVersionByIDoperations.PYLD-3081