Repository navigation
fix(events): require a region on every event, drafts included - #1313
Conversation
A region manager's write access to an event is its region, and Payload skips `required` on a draft save, so a first draft saved without a region locked out the manager who created it. `requireRegion` refuses a create with no region, and any update that names an empty one, for everyone. Two related holes in the same region-subtree scoping: - Duplicate ran create access with empty data, then copied the source's region or parent, so a region manager could copy any readable class or region into an area they do not own. Events and Regions now set `disableDuplicate`. - A posted `parent: null` was not treated as a move, so a region manager could detach a sub-region, or their own top region, from the tree. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…on id Access reads the raw body, and Payload turns `''`, `'none'` and `'null'` into an empty parent only after it, so `parent: 'none'` still detached a region. The test is now "not an owned id". `requireRegion` takes the access layer's `relationId`, so a region posted as a numeric string is no longer refused. Adds the allowed root-region save, the legacy-row carve-out, and a config guard tying `disableDuplicate` to the scoped collections. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🚅 Deployed to the SahajCloud-pr-1313 environment in sahajcloud
1 service not affected by this PR
|
sydevs-bot
left a comment
There was a problem hiding this comment.
🧐 Adversarial review
The diff is the size of its problem. Three gates for one invariant are forced rather than chosen: access cannot bind admins, and required is skipped on a draft. What is chosen is where two of them live — disableDuplicate is hand-set per collection although accessPlugin already rewrites every collection this scoping covers, and the new events guard is a near-copy of the regions one that agrees with it on nothing. No contract moves: no contractSync source changed, and the one consumer path that creates an event already supplies a region.
Findings
should-fix—disableDuplicatebelongs inaccessPlugin, keyed onisRegionSubtreeCollection—Events.ts:134should-fix—requireRegionandrequireOwnedParentOnCreateare one invariant in two shapes —requireRegion.ts:18should-fix— Streamline: the newEvents/AGENTS.mdsection restatesrequireRegion's JSDoc —Events/AGENTS.md:36should-fix— the newaccess.mdbullet gives two facts a second home —access.md:127consider— the=== nullemptiness test reads as the bug its own sibling ⚠ forbids —requireRegion.ts:20
What was checked
Diff whole (9 files), plus regionSubtreeAccess.ts, requireOwnedParentOnCreate.ts, relationId, eventTitle.ts and accessPlugin.ts on the branch. Every row of the change outline traced to code; all hold. Confirmed relationId('') === 0, and that Payload's ''/'none'/'null' → null coercion runs ahead of field hooks, which is what makes both gates right. Preview: 3 links ON_HOST, ids 1025001/1025008/1002002 all in src/fixture/, each showing its claim. The one deleted test is rightly deleted: resolveTitlePlace reads a region. Streamliner: 1 piece listed, 1 kept, 1 finding posted; its second reverses a stated owner decision, so it was dropped.
🤖 Written by the sydevs autonomous loop — see what this is
Generated by Claude Code
Being region-scoped is what forces the flag, so the plugin that already rewrites every such collection's hooks, access and admin sets it too. A slug added to REGION_SUBTREE_COLLECTIONS now carries the guard with no second edit, and the two per-collection lines and their duplicated rationale go. `?? isRegionSubtreeCollection(slug)` leaves a collection free to set the flag for its own reasons, as Albums, AppCards, Files, Images and imports do. role-based-access.int.spec.ts already reads the flag off the booted config, so it covers the new wiring unchanged. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjYUbi9DHrWsxwPCzz2
relationId('') is 0, so `relationId(value) !== null` reads as the bug the
⚠ in regionSubtreeAccess.ts forbids. It is correct only because Payload
normalises every empty relationship spelling to null in the field pass
before field hooks run. Without the clause the next edit copies the shape
to a layer that runs earlier, where it is the hole.
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Mg9rjYUbi9DHrWsxwPCzz2
The section retold the hook's own JSDoc and its condition list, and code outranks prose. Its two whys with no other home move into that JSDoc, a clause each: a regionless first draft locked out its own creator, and an omitted `region` passes so a job touching an older regionless row is not refused. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjYUbi9DHrWsxwPCzz2
The bullet gave three facts a second home: the duplicate rationale, which lives in accessPlugin's ⚠; the draft/`required` rationale, which lives in requireRegion's JSDoc; and an inventory of what requireOwnedParentOnCreate refuses, which the symbol already says. The invariant above them has no other home, so it stays. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjYUbi9DHrWsxwPCzz2
`sanitizeConfig` runs after every plugin and gives an auth collection `disableDuplicate ?? true`, so the `??` form wrote an explicit `false` for every other collection and defeated that default: `managers` and `clients` gained a registered `POST /api/:collection/:id/duplicate`, a local `duplicate()` that no longer threw, and a Duplicate action in the admin. No role grants `create` on either, so only an admin could reach it, but the route should not exist. A conditional spread writes the key only where being region-scoped forces it, and leaves every other collection exactly as its own config had it. The int case now pins both auth collections' flag; it fails against the `??` form. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjYUbi9DHrWsxwPCzz2
|
Four of five critic threads adopted, one rebutted. Adopted — into Rebutted — one invariant, two shapes. A root region legitimately has no parent, so a hook bound on update would refuse the root-region save this branch covers. The security sweep caught a regression in the first fix (a78e4b4). The Lean gate ✓; 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
There was a problem hiding this comment.
Approved for Ardnived, who wrote it: GitHub refuses an author's own approval. CI is green and the automated review has run. Enable auto-merge when you want it merged: the queue tests it against its base. 🤖 Written by the sydevs autonomous loop — see what this is
Two writers showed access no region: `restoreVersion` runs update access
with `{ _status }` alone and then writes the old version's region, and a
proposal accept creates the event with `overrideAccess: true` at the
region the reviewer left on the submission. So a region manager could
restore an event out of their area, or accept a new class into any city.
`keepRegionInSubtree` refuses either, after the field pass, for any
non-admin manager on `req`. A region equal to the stored one is no move,
so an event assignee still saves their own event.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…region-always-set # Conflicts: # docs/rules/access.md
Summary
Change outline
Contract — what an event or region write is refused for
Three writes that went through are now refused. All three let a region manager make a row they could not reach afterwards, or move one out of another manager's area.
Flow — writes that show access no region
Restoring a version and accepting a proposal both write a region that access never saw. A collection hook now judges the region the write will store.
POST /api/events/versions/:id (restoreVersion) update access with { _status } only # the old version's region is not in it + Events.hooks.beforeValidate: keepRegionInSubtree src/collections/Events/hooks/keepRegionInSubtree.ts + non-admin manager on req, region ≠ stored, not in owned subtree → 403 PATCH /api/user-submissions/:id { status: 'accepted' } src/collections/UserSubmissions/lifecycle/review.ts payload.create(events, overrideAccess: true, req = reviewer) + keepRegionInSubtree: a create always counts as a move → region must be in the reviewer's subtree update proposals: patch carries no region → equal to stored → passesBehaviour — why a draft could orphan an event
How the lockout worked: create access lets a missing
regionthrough, because the admin asks the same question with no data to decide whether to show the Create button. Update access then reads the storedregion.scopeRegionSubtreeWrite(events) src/plugins/access/regionSubtreeAccess.ts create: region posted → must be in the owned subtree no region → allowed if the manager owns any region - (draft save skips `required` → row stored with region = null) + (requireRegion then refuses the save, so no row is stored) update: stored region in subtree, or manager == user → allowed - stored region null, manager unset → 403 for the manager who created itPreview
Sign in as Rafael Quintero, who manages Fixture Harbourton, the city above Fixture Lantern Hall.
Test results
role-based-access,user-submissions-review-lifecycle,atlas-collectionsandevents-geojson(192 tests), plus every int spec that creates events or regions or touches submissions (47 files, 1077 tests) before the merge of the loop's commitsManual verification
Notes for reviewer
regionorparentid does not bypass subtree access. The access layer'srelationIdparses numeric strings, and a probe refused all four create and update cases.parent: 'none'and'null'still detached a region, because access reads the raw body and Payload empties those spellings afterwards. The test is now "not an owned id".requireRegionalso refused a region posted as a numeric string, so it now uses the access layer'srelationId.disableDuplicatemoved intoaccessPlugin, keyed onisRegionSubtreeCollection. The first form wrote?? isRegionSubtreeCollection(slug), an explicitfalseeverywhere else, which defeated thedisableDuplicate ?? truethatsanitizeConfiggives an auth collection after every plugin — somanagersandclientsgained a registered duplicate route and an admin Duplicate action. No role grantscreateon either, so only an admin could reach it. The key is now written only astrue, and the int case pins both auth collections' flag.requireRegionwithrequireOwnedParentOnCreatebehind one generic field hook. An event has no legitimate regionless state; a root region legitimately has no parent, which is why that hook is create-only, and a hook bound on update would refuse the root-region save this branch added a case for.parentbypassing access: false, because the access layer'srelationIdparses numeric strings, and a probe confirmed it.regionout still pass, and there is a test for that.keepRegionInSubtree, and each has a case that fails without the hook. Dismissed in that review round: a claim that a partial manager update is refused, which is false because Payload refills an omittedregionbefore collection hooks run, as the existing in-subtree and assignee update cases show. Also a claim thatregion: ''slips pastrequireRegion, which is false because Payload empties it before the hook, now pinned by a case.Detail
Why a field hook, not
requiredor a collection hook. Payload skips every field validator on a draft save (skipValidation: isSavingDraft && !hasDraftValidationEnabled(...)incollections/operations/create.jsandutilities/update.js), andversions.drafts.validatewould validate every field of every draft. A collectionbeforeValidatehook cannot tell an omittedregionfrom an empty one: Payload refills an omitted field from the stored row right after that field's own hooks. So the hook reads the omission offsiblingData, nevervalue, which already carries the refill.Why Duplicate was a hole.
duplicateOperationcallscreateOperationwithdata: {}. Create access runs on that empty data, beforegetDuplicateDocumentData's copy reaches the field hooks. Reads are project-wide, so a region manager could open any event or region and duplicate it into an area they do not own. A probe confirmed it for both collections, with and without a data override.Why
parent: nullwas a hole.relationId(null)isnull, which the update branch read as "no parent posted" and allowed. On a sub-region that detached the region from the tree. On the manager's own top region it took the region out of the country manager's area. Admins still clear a parent freely: the admin bypass answers before this scoping runs.🤖 Generated with Claude Code