Skip to content

fix(access): a published client key can rewrite its own clients row - #844

Merged
sydevs-bot merged 7 commits into
mainfrom
claude/fix-827-client-self-update
Sep 22, 2026
Merged

sydevs-bot merged 7 commits into
mainfrom
claude/fix-827-client-self-update

Conversation

@sydevs-bot

@sydevs-bot sydevs-bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • A published API key could PATCH its own clients row: roles, origin allowlist, region, canonical ownership, usage counters.
  • Self-access now grants a client read alone. GET /api/clients/me is untouched; the PATCH answers 403.
  • Nine integration cases pin it field by field, with a manager counterpart that keeps the refusals from being vacuous.

Changes

  • src/plugins/access/bypassPermissions.ts — self-access update is managers only
  • docs/rules/access.md — the Self-access section, the permission-flow order, the args.id hazard
  • src/collections/Clients/Clients.ts, src/plugins/access/permissions.ts — comments the narrowing made false
  • tests/int/clients-access.int.spec.ts — the the self-update lock block

Preview

Test results

  • Lean gate: ✓ pnpm lint, pnpm typecheck, pnpm typecheck:tests, pnpm test:unit (2151 passed)
  • Targeted specs: 141 passed — clients-access, managers-access, role-based-access, client-canonical. Also green: client-hooks, client-origin-enforcement, client-query-validation, user-submissions-access
  • Build: N/A — Railway builds the preview

Manual verification

  1. Open the services list above and edit one: change Allowed Domains and the region, save, reload. A manager's write still lands.
  2. On the Atlas preview, confirm the widget still boots — that is GET /api/clients/me, answered by the read half.

Notes for reviewer

  • The apiKey write lock is now unreachable, and stays. create/update on that field could only ever have denied a client, and a client no longer reaches update at 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.
  • Two of the nine cases could not be watched failing by assertion. region and canonical hang rather than assert with the bypass restored — a client PATCH writing region does 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.
  • Security review ran (trigger paths matched): no findings.
Detail

Why withdraw the grant rather than lock five more fields. Per-field update: managersOnlyFieldAccess on roles, allowedDomains, region, canonical and usage matches how mailingList is spelled, but leaves the next field added to Clients open by default — and that list grew by two on a second reading of the collection. Managers is 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 and createAccessConfig returns false. The refusal is a 403, not a 200 with fields stripped.

Nothing depended on the grant. Every payload.update({ collection: 'clients' }) on main runs at overrideAccess: true or in a job with no user. POST /api/clients/report and the usage counters both write through the pg pool, around Payload entirely. disableLocalStrategy: true means no login, logout or refresh writes the row. Checked the three consumer repos: SahajAtlasWeb calls GET /clients/me and POST /clients/report, the WordPress plugin calls GET /clients/me, WeMeditateWeb touches neither. No contract impact.

readVersions and unlock are unaffected. Both derive from update, but withDerivedGrants drops args.id before delegating, so user.id === docId never fired for them either way.

Allowlist, not denylist. user.collection === 'managers' rather than !== 'clients', matching managersOnlyFieldAccess and its docblock. Same behaviour today; a third auth collection arrives with no self-update instead of inheriting it.

Fixture pre-mortem. FORGED_VERIFICATION assumes it is the shape the VerifyEmbeds job writes, which loadDirectOwners reads a public webUrl out of. Verified against canonicalVerificationSchema in src/collections/Clients/Clients.ts and the CanonicalVerification type in src/lib/clients/verification.ts — it is typed, not cast, so tsc holds the claim. The first draft was double-cast and had no routing, so it was not that shape.

Dismissed review findings. 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 checklist asks for one case per field. Moving the API-key REST driver into tests/utils/restRequest.ts as 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

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
@railway-app

railway-app Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

🚅 Deployed to the SahajCloud-pr-844 environment in sahajcloud

Service Status Web Updated
SahajCloud ✅ Success (View Logs) Web Sep 22, 2026 at 4:46 pm UTC
1 service not affected by this PR
  • mailpit

@railway-app
railway-app Bot temporarily deployed to sahajcloud / SahajCloud-pr-844 September 22, 2026 16:16 Destroyed
@sydevs-bot sydevs-bot added the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Sep 22, 2026
@sydevs-bot

sydevs-bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor Author

🤖 no session running

recent dispatches
  • 16:35Z address-review · attempt 1 · session · done
  • 16:35Z address-review · attempt 1 · session
  • 16:28Z adversarial-review · attempt 1 · session · done
  • 16:28Z adversarial-review · attempt 1 · session

Written by the sydevs dispatcher. Sessions never edit this comment.

@sydevs-bot sydevs-bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧐 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:40 and docs/rules/access.md:117 still read "update ... unless a client". The code grants update to managers alone. 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).
  • users is non-auth, so allowlist and denylist are one rule today.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

Comment thread src/plugins/access/permissions.ts Resolved
Comment thread src/plugins/access/bypassPermissions.ts Resolved
@sydevs-bot sydevs-bot added bot:working Lock: a cloud session is running on this item. Removed when it ends. and removed bot:working Lock: a cloud session is running on this item. Removed when it ends. labels Sep 22, 2026
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
@railway-app
railway-app Bot temporarily deployed to sahajcloud / SahajCloud-pr-844 September 22, 2026 16:39 Destroyed
@sydevs-bot

Copy link
Copy Markdown
Contributor Author

Both adversarial-review findings adopted. Comments and docs only — no behaviour change.

  • should-fix — permissions.ts:40 and access.md:117 said "update unless a client". Both now state the allowlist. 2336a4b
  • consider — cut the bypassPermissions.ts comment from five lines to three, as suggested. 26238c4

Lean gate green: lint, typecheck, typecheck:tests, unit (2151 passed).

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@sydevs-bot sydevs-bot removed the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Sep 22, 2026
@sydevs-bot
sydevs-bot marked this pull request as ready for review September 22, 2026 16:49
@sydevs-bot
sydevs-bot requested a review from Ardnived September 22, 2026 16:49
@sydevs-bot
sydevs-bot added this pull request to the merge queue Sep 22, 2026
Merged via the queue into main with commit 0540111 Sep 22, 2026
26 of 29 checks passed
@sydevs-bot
sydevs-bot deleted the claude/fix-827-client-self-update branch September 22, 2026 17:08

This branch was successfully deployed

No deployments
sahajcloud / SahajCloud-pr-844 — 26238c4f Deployed Sep 22, 2026 by railway-app[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

fix(access): a published client key can rewrite its own clients row

3 participants