Repository navigation
fix(access): a published client key can rewrite its own clients row - #844
Conversation
The self-access bypass returned `allow` for `read` and `update` alike
whenever `user.collection === collection && user.id === docId`. For a
published API key that is `PATCH /api/clients/<own id>`, answered before
any role check — so a credential that ships in the browser could rewrite
the operator configuration on its own row.
Only `apiKey` and `mailingList` carried field access. Everything else was
writable: `roles` (implicit project read for each one it names, plus
`user-submissions: ['create']`), `allowedDomains` (empty allows every
origin, and it is also the host check on `POST /api/clients/report`),
`region`, `canonical.verification` and the `usage` counters (zeroing
`highUsageDays` suppresses the abuse report operators are sent). With
`region` plus `canonical.enabled`, `canonical.embed` and a forged
`verification` in one PATCH, a key could take canonical ownership of any
region no client already claims, and `loadDirectOwners` builds public
`webUrl`s from that. `admin.readOnly` on several of them is a UI flag,
not access.
Withdrawing the grant whole, rather than locking five more fields: a
`clients` row is operator configuration end to end, so there is no half
of it a service may edit — and the field list grew by two on a second
reading of the collection, which is what per-field leaves open. A manager
row is the opposite case, a person's profile, so self-update stays there
with admin-only locks on `type` and `roles`.
The close is complete rather than partial. No client role names `clients`,
so `hasPermission` falls through to `createAccessConfig`'s manager-only
branches and returns false; the PATCH now answers 403 rather than
stripping fields. Nothing depended on the grant: every
`payload.update({ collection: 'clients' })` runs at `overrideAccess: true`
or in a job with no user, `POST /api/clients/report` writes
`embedMetadata` through the pg pool, and `disableLocalStrategy: true`
means no login or refresh path writes the row.
Self-access **read** is untouched. `GET /api/clients/me` is the widget's
boot call and the read half is the only thing that answers it.
Closes #827
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Tm8SHqvEMuYZaVWqcMGPoY
Nine cases in `clients-access.int.spec.ts`, driven through `handleEndpoints` with a real API key, against a client of their own so no earlier case depends on a value a later one overwrites. One refusal per field in the ticket's table — `roles`, `allowedDomains`, `region`, `canonical`, the `usage` counters — plus `name`, which carries no lock and holds nothing sensitive: it is the case per-field locks would have left open, so it says the close is the collection-level grant. Every write case asserts by reading the row back at `overrideAccess: true`. A status-code assertion cannot fail here — field access strips a denied field and still answers 200, which is how the hole survived #826. The one `expect(status).toBe(403)` is beside its read-back, recording that the refusal is now collection-level. Three cases keep the refusals honest, and each was watched failing at the site that would cause it: - the canonical claim is made twice, identically. The client's is refused and `getRegionOwners` reports no owner for the region; the admin manager's lands and the same map names the client. Without that pair, `toBeUndefined()` would pass against a claim path that never worked. - `POST /api/clients/report` still records a mount, which is the one write a client legitimately causes on its own row. - a non-admin manager still renames their own `managers` row while `type` is stripped. Stubbing the bypass's new condition to `false` turns it 403, so it holds the other edge of the narrowing. The seeded `usage` counters are non-zero and `allowedDomains` is non-empty on purpose: a zeroing or emptying PATCH needs a distinguishable value to fail against. Watched failing against the reintroduced defect: the `name`, `roles`, `allowedDomains` and `usage` cases all go red. The `region` and `canonical` cases hang rather than assert with the bypass restored — a client PATCH writing `region` does not return — so their non-vacuity rests on the manager counterpart above instead. Refs #827 Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Tm8SHqvEMuYZaVWqcMGPoY
The rule said "a user can always read and update their own document", which is now true of `managers` and not of `clients`. The permission-flow step said the same thing one section up. The section now carries why the split falls where it does — a manager row is a person's profile, a client row is operator configuration with no editable half — and the three facts that made withdrawing the grant safe: no `clients` write depends on it, `disableLocalStrategy` keeps auth off the row, and `GET /api/clients/me` is answered by the read half alone. Refs #827 Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Tm8SHqvEMuYZaVWqcMGPoY
Four review lenses, three of them independently on the same line.
**Spell the self-access rule as an allowlist.** `user.collection !==
'clients'` is a one-entry denylist, so a third auth collection would
inherit self-update by default. `=== 'managers'` fails closed instead, and
matches how the repo already spells this exact distinction —
`managersOnlyFieldAccess` and its docblock ("why it is spelled
positively"). Same behaviour today, opposite default tomorrow.
`docs/rules/access.md` gains that polarity note and loses the paragraph
tracing `createAccessConfig`'s branch structure, which is a moving target;
the substance it carried — no client role names `clients` — stays.
The comments shrink. The bypass no longer enumerates the writable fields
it exists not to enumerate, and names `disableLocalStrategy` as the
structural form of the manager/client split. The `Clients.apiKey` note
states what the lock is for rather than which PR changed it.
Tests:
- `testData.createRegionNode` instead of a hand-rolled `payload.create` —
`mapboxId` is a unique index and the factory's `prefix` is what keeps it
suite-unique.
- `FORGED_VERIFICATION` is typed `CanonicalVerification` over
`EMPTY_VERIFICATION` rather than double-cast. The cast was hiding a real
omission: the literal had no `routing`, so it was not the shape the
VerifyEmbeds job writes, which is what the fixture claims to be.
- The manager counterpart gets its own client and its own unclaimed
region. Sharing one row made every refusal above depend on running
first.
- `bareReq` is `managerReq` minus the user, rather than a third copy.
- The `name` case asserts `updatedAt` is unchanged, which covers the
fields nobody enumerated — the property the commit claims and the
per-field cases cannot check.
Dismissed: collapsing the five field cases into one PATCH, and folding
`region` into the canonical case. Four round-trips buy four distinct
failure messages, and #827's verification checklist asks for one case per
field. Also dismissed: moving the REST driver into
`tests/utils/restRequest.ts` as a client-key variant — right, and a
three-file refactor this PR should not carry.
Refs #827
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Tm8SHqvEMuYZaVWqcMGPoY
From the review pass. All three are prose that still described the old grant. `access.md`'s permission-flow step listed the bypass in the reverse of the order the code checks it — read literally, it said an inactive manager and a draft client still reach their own row. They are refused two steps earlier; self-access is last. The `args.id`-drop hazard was argued from a client reaching `_clients_v` through self-access, which is now unreachable. The hazard is not: a manager keeps self-update, so the same forwarded id hands them a version row by its own primary key. Same rule, the case that is still live. `Clients.apiKey`'s `create` and `update` locks are now unreachable — no client role names `clients` and no client holds self-update — so the comment says that, and says why they stay: the grant above them has been widened once already. The spec case that used to exercise the write lock now proves the property rather than the layer, and says which layer answers today. Refs #827 Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Tm8SHqvEMuYZaVWqcMGPoY
|
🚅 Deployed to the SahajCloud-pr-844 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 is the size of its ticket: five lines of behaviour, the rest tests and docs. I re-derived the close rather than taking it — no client role names clients (config/roles.ts:88-127), so createAccessConfig falls to return false (accessConfigs.ts:166-180), the 403 the first case pins. Nothing depended on the withdrawn grant. The one fix owed is a claim, not code.
Findings
- should-fix —
permissions.ts:40anddocs/rules/access.md:117still read "update ... unless a client". The code grants update tomanagersalone. The two rules differ for a third auth collection. - consider —
bypassPermissions.ts:50-54: five comment lines on a one-line rule the docs now carry.
What was checked
- #827 against the diff, file by file. Every acceptance criterion is met.
- Both
payload.update({ collection: 'clients' })sites:usage/tasks.ts:199(job req, no user),verifyEmbed.ts:96(overrideAccess: true, manager-gated). usersis non-auth, so allowlist and denylist are one rule today.
🤖 Written by the sydevs autonomous loop — see what this is
Generated by Claude Code
The flow summary and the permission-flow step both read "update unless a client", which promises a third auth collection inherits the grant. The code allowlists `managers`, so it fails closed instead. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_014iohF2pSrTpep5yWr67yHT
The five lines restated what the Self-access section in access.md now carries, and the `disableLocalStrategy` clause was an analogy about login rather than a constraint on who may PATCH. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_014iohF2pSrTpep5yWr67yHT
|
Both adversarial-review findings adopted. Comments and docs only — no behaviour change.
Lean gate green: lint, typecheck, typecheck:tests, unit (2151 passed). 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
Summary
PATCHits ownclientsrow: roles, origin allowlist, region, canonical ownership, usage counters.readalone.GET /api/clients/meis untouched; the PATCH answers 403.Changes
src/plugins/access/bypassPermissions.ts— self-accessupdateismanagersonlydocs/rules/access.md— the Self-access section, the permission-flow order, theargs.idhazardsrc/collections/Clients/Clients.ts,src/plugins/access/permissions.ts— comments the narrowing made falsetests/int/clients-access.int.spec.ts— thethe self-update lockblockPreview
Test results
pnpm lint,pnpm typecheck,pnpm typecheck:tests,pnpm test:unit(2151 passed)clients-access,managers-access,role-based-access,client-canonical. Also green:client-hooks,client-origin-enforcement,client-query-validation,user-submissions-accessManual verification
GET /api/clients/me, answered by the read half.Notes for reviewer
apiKeywrite lock is now unreachable, and stays.create/updateon that field could only ever have denied a client, and a client no longer reachesupdateat all. Kept as the layer beneath a grant that has been widened once already; its comment says so. The fix(access): a published API key reads every other client's plaintext API key #822 case that used to exercise it now asserts the property rather than the layer.regionandcanonicalhang rather than assert with the bypass restored — a client PATCH writingregiondoes not return. Their non-vacuity rests on the manager counterpart instead, which makes the identical canonical claim on an equally unclaimed region and resolves an owner for it. The other seven were each watched red at the site that would cause them.Detail
Why withdraw the grant rather than lock five more fields. Per-field
update: managersOnlyFieldAccessonroles,allowedDomains,region,canonicalandusagematches howmailingListis spelled, but leaves the next field added toClientsopen by default — and that list grew by two on a second reading of the collection.Managersis the precedent that does not transfer: a manager row is a person's profile and the person must edit their own name, email and password, so per-field is the only option there. A client row has no such half. Recorded under Rejected in #827 so it is not re-opened.The close is complete. No client role names
clients, so once the bypass stops firing the role table has nothing to fall back to andcreateAccessConfigreturnsfalse. The refusal is a 403, not a 200 with fields stripped.Nothing depended on the grant. Every
payload.update({ collection: 'clients' })onmainruns atoverrideAccess: trueor in a job with no user.POST /api/clients/reportand the usage counters both write through the pg pool, around Payload entirely.disableLocalStrategy: truemeans no login, logout or refresh writes the row. Checked the three consumer repos: SahajAtlasWeb callsGET /clients/meandPOST /clients/report, the WordPress plugin callsGET /clients/me, WeMeditateWeb touches neither. No contract impact.readVersionsandunlockare unaffected. Both derive fromupdate, butwithDerivedGrantsdropsargs.idbefore delegating, souser.id === docIdnever fired for them either way.Allowlist, not denylist.
user.collection === 'managers'rather than!== 'clients', matchingmanagersOnlyFieldAccessand its docblock. Same behaviour today; a third auth collection arrives with no self-update instead of inheriting it.Fixture pre-mortem.
FORGED_VERIFICATIONassumes it is the shape the VerifyEmbeds job writes, whichloadDirectOwnersreads a publicwebUrlout of. Verified againstcanonicalVerificationSchemainsrc/collections/Clients/Clients.tsand theCanonicalVerificationtype insrc/lib/clients/verification.ts— it is typed, not cast, sotscholds the claim. The first draft was double-cast and had norouting, so it was not that shape.Dismissed review findings. Collapsing the five field cases into one PATCH, and folding
regioninto the canonical case: four round-trips buy four distinct failure messages, and #827's checklist asks for one case per field. Moving the API-key REST driver intotests/utils/restRequest.tsas a shared variant: correct, and a three-file refactor this PR should not carry.Closes #827
🤖 Written by the sydevs autonomous loop — see what this is
🤖 Generated with Claude Code
https://claude.ai/code/session_01Tm8SHqvEMuYZaVWqcMGPoY