Repository navigation
feat(clients): make the mailing-list API key write-only - #1127
Conversation
A service's mailing-list provider key sat in the `clients` edit form's state and in plain text in Postgres. Payload's live preview posts that whole state to the preview iframe, so the planned Clients preview on sahajatlas.com would have received the secret. `secretField()` owns the storage half, so the next stored credential is closed by default: `read: () => false`, `admin.disabled`, never required, and encrypted at rest through Payload's own `encrypt`/`decrypt`. Empty values pass through untouched, because `LIST_ELIGIBLE` filters the column with `not_equals: ''`, and a failed decrypt reports to Sentry and answers `null` rather than taking down every read of the row. A key is now set through `POST /api/clients/:id/mailing-list-key`, which proves it against the provider and list it will be used beside and then writes through `payload.update`, so every lock and hook a form save runs still runs. The Atlas tab shows whether a key is stored and its last four characters, and nothing more. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjVKFZaNEE4JgBvahah
…locks Taking the key out of form state made `validateMailingList` short-circuit at its "is this configured" guard on every ordinary save, so a changed provider or list went in without a provider round trip. The stored key now stands in for the absent one, which is what those credentials will be used beside anyway. `lockTabs` ANDs the Atlas tab's own rule onto every field it holds, and `payload.update` strips a field it refuses rather than erroring — so the key endpoint, gating on `managersOnlyFieldAccess` alone, answered 200 with a key's last four characters for a write that stored nothing. Both locks are evaluated now, from one definition in `Clients/access.ts`. The drawer read the response body before its status and had no catch, so a 500 or a dropped connection showed nothing at all. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjVKFZaNEE4JgBvahah
|
🚅 Deployed to the SahajCloud-pr-1127 environment in sahajcloud
1 service not affected by this PR
|
|
🤖 no session running recent dispatchesWritten by the sydevs dispatcher. Sessions never edit this comment. |
The four conflicts were one fact in four places: the unit lane's file and test counts, which `tests/unit/perf-figures-pinned.spec.ts` pins to the tree. Both sides had re-measured the same lane, so neither number survives a merge that carries both sides' specs. Measured the merged tree instead: 265 files / 3554 tests, recorded as PERF.md's fiftieth row. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjYuo2i4Tcn7Jn749TB
|
Merged Neither side's number survives a merge carrying both sides' specs, so I measured the merged tree rather than taking a side — 265 files / 3554 tests, recorded as PERF.md's fiftieth row. That supersedes this PR body's No code overlap: 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
Three documentation conflicts, all the unit lane's pinned file and test counts. Both sides re-measured the lane on the same day and recorded different figures, and `tests/PERF.md` carried a "fiftieth" history row on each side. Resolved by re-measuring the merged tree, which is the only figure either side's number could have been: 266 files and 3559 tests. Both history rows are kept, with this branch's renumbered to fifty-first, and this merge's own run recorded as fifty-second. `AGENTS.md` carried a stale 265 that no conflict marked, and `perf-figures-pinned.spec.ts` pins all four consumers to PERF.md, so it moved too. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjXAWznsCS8FxqnbDJu
|
Merged Neither number fits the merged tree, so I re-measured it: 266 files, 3559 tests. Both sides' Lean gate green: lint, format, typecheck, typecheck:tests, 266 files / 3559 tests. 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
sydevs-bot
left a comment
There was a problem hiding this comment.
🧐 Adversarial review
Two open PRs build #1109. #1125 is the other — also draft, also locked, 26 files to this one's 20, independent history. Which ships is a decision ahead of reviewing either, and #1125 names a gap this branch leaves open: switching a list on re-proves nothing, since enabled is not in CREDENTIAL_KEYS.
On its own terms the diff is no bigger than the ticket. Every piece is one #1109 named, secretField()'s single consumer included, and Clients/access.ts breaks a cycle rather than adding a layer. The write goes through payload.update, so the tab lock, the field locks and the hook all still run, and payload-types.ts is untouched. What I would hold is the admin control: it hand-rolls a fetch the repo has a written rule for.
Findings
- blocker — the fetch sends no
?locale=, so the endpoint reads default-locale roles (MailingListKeyField.tsx:49). - should-fix — the POST response is read by nobody, and its comment describes a dropped write-back (
mailingListKey.ts:147). - should-fix — the endpoint forces
enabled: true, committing unsaved form values (mailingListKey.ts:130). - consider — the stand-in may be a no-op, and no spec pins the value sent (
validateMailingList.ts:55). - consider — the description renders nowhere now (
Clients.ts:417).
What was checked
Read on the branch: secretField.ts, mailingListKey.ts, Clients/access.ts, MailingListKeyField.tsx, validateMailingList.ts, the migration, the int spec. Ladder: payload.encrypt/decrypt over a dependency, plus capture, requireActiveManager, parseBody, errorResponse — all reused; requireManagerPermission rightly not, this gate is a row not a grant. Also docs/rules/access.md, endpoints.md, accessConfigs.ts. Both ## Preview links answered a Cloudflare Access page this run cannot pass.
🤖 Written by the sydevs autonomous loop — see what this is
Generated by Claude Code
- secretFieldEndpoints asks the row's own update too, as Save does, and reads the control's name and the stored value through the shared helpers. - The status answers last4 alone; a staged value keeps nothing but its token client-side, so remounting the tab cannot desync it. - The control refetches on save, not on the document-lock heartbeat. - dependsOn derives from CREDENTIAL_KEYS; the spec resets its provider mocks per case. Co-Authored-By: Claude Opus 5.5 <[email protected]>
No field required reaches a key the form never holds, so removing one saved a list that read as configured and delivered nothing, and LIST_ELIGIBLE handed its region to the fallback client's list. A removal now clears the provider in the same save. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A refused or failed status read showed 'No API Key is set' over a stored key, and a network error went unhandled. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Four doc conflicts, all the same one: #1136 stopped pinning the unit lane's file and test counts anywhere but tests/PERF.md, and this branch had bumped those counts in all four docs. Took main's side in each — the counts it removed are the ones no doc may quote now. tests/PERF.md keeps main's `Files / tests (second session)` row: that row is the second session's record now, not the current tree's. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjWVXimsbUmfehpBf19
#1136 landed a check that every collection endpoint an API client can call is in the hand-written OpenAPI document. This branch's two secret routes are not callable by a client: both run requireActiveManager and refuse an API key, so they belong in the same exemption list as the Registrations CSV download. Recorded the merged tree's lane measurement in tests/PERF.md. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjWVXimsbUmfehpBf19
|
Resolved against Four doc conflicts, one cause: #1136 stopped pinning the unit lane's file and test counts outside One semantic conflict the text merge did not show: #1136's new Lean gate green: 270 files / 3641 tests. 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
#1127 made the mailing-list API key write-only and dropped `clients.mailing_list_enabled`. Two files conflicted, and both took both sides. `src/migrations/index.ts`: main's `20261112_211802_drop_mailing_list_enabled` registers between this branch's two migrations, in timestamp order. `tests/PERF.md`: the session table is append-only, so the two sides numbered the same rows differently. Both sets of rows are kept, each side's relative order preserved, and the ordinals renumbered from the fiftieth. `20261112_234150_merge_main_snapshot_9` carries the snapshot the merged tree needs and no DDL. This branch's `20261112_213652_files_spoken_language` snapshot is the newest by timestamp and still holds `clients.mailing_list_enabled`, so `migrate:create` re-emitted the drop that main's migration already ships. A probe run afterwards writes nothing. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjZZbcvj2E56uPJHKA1
`main` landed #1127, which moved `isAtlasClient` into `src/collections/Clients/access.ts`, made `mailingList.apiKey` a `secretField()` and registered `secretFieldEndpoints('clients')`. This branch adds `POST /api/clients/:id/email-preview` and the per-tab preview targets, so every hunk took both sides: - `Clients.ts` — the union of both import sets, and an `endpoints` array carrying `clientEmailPreview` beside the secret-field routes. - `openapi-endpoint-coverage.spec.ts` — `MANAGER_ONLY` gains all three entries. Each authenticates a manager and takes no client API key. - `tests/PERF.md` — both sides' rows, renumbered in one sequence. The log is append-only, so neither side's measurements are droppable. - `payload-types.ts` and `importMap.js` are generated, so they were regenerated rather than hand-merged. The merge order this PR's body calls load-bearing is now satisfied: #1127 is in `main`, so the form state Payload posts to the preview frame no longer carries a provider secret. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjXL6U5yjrAFXfgZgc3
`main` (#1127) and this branch each appended measurement rows to `tests/PERF.md` under their own ordinals, and the four #1131 rows reached both sides at different indices. Keeps `main`'s numbering, which already carries those four, and appends this branch's three #1121 rows after it. No row is dropped or renumbered, and the session's own measurement is recorded beside them. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjWtuecC3drMgcCJUso
Summary
secretField(), ready for the next stored credential.Change outline
Flow — how a key gets stored
The set modal stages the key on the server; only a token enters the form, and the form's own Save applies it.
Contract —
clients.mailingListTwo routes on any collection holding a
secretField, admin-panel only:Files — the generic field
Preview
Data: not seeded. This repo sets no
previewUrl.seed, and a Railway PR environment boots with only its admin, so create a service with the Sahaj Atlas role to see the section. Accepting a key needs a real provider key; a wrong one shows the provider's refusal.Migration
20261111_182500_encrypt_mailing_list_key.ts,20261112_211802_drop_mailing_list_enabled.tspayload.encryptciphertext. Then any list whose checkbox was off loses its provider (its other settings stay), andclients.mailing_list_enabledis dropped.downdecrypts. The drop'sdownrestores the column as on wherever a provider remains, but cannot restore a providerupcleared.Test results
test:unit3598 / 3599 passed:ics-builder.spec.ts(from feat(events): serve a token-addressed calendar feed for a class #1134, untouched here) fails in a UTC−7 shell and passes withTZ=UTC, as CI runs itmailing-list-key.int17 passed,atlas-submission-routing.int15 passed,secret-field+mailing-listunit 50 passedManual verification
•••• <last4>.clients.mailing_list_api_keystraight from Postgres and confirm it is ciphertext.Notes for reviewer
Scope grew past the ticket, by request in review: the key field became a generic
secretField, and the mailing-list section lost its checkbox. The title is unchanged.This PR merges first in its set. sydevs/SahajAtlasWeb#389 is next, then #1110, whose Atlas-tab preview is what would have posted this secret to sahajatlas.com. #1125 is the other open build of #1109.
The loop's last review was of the old key route, which this branch replaces. Its four adopted fixes are carried onto
SecretField: the status row is aTextInput, and the modal's reset is scoped to its own class. The DELETE route, and the specs pinning its gate, went with the route. A removal is now an instruction applied on Save, pinned by two cases:''stored, and a locked-out manager's removal changes nothing.Dismissed from this run's
/code-review high:null, and a later save back-fills it over the ciphertext. That needsPAYLOAD_SECRETto change, which already breaks every client API key Payload encrypts the same way. The failed decrypt is reported to Sentry.us14), so the last 4 characters don't tell keys apart. The mask confirms that a key is stored, not which one. Deciding what to show per provider is the generic field's call, and can wait for a second consumer.afterChange, so a rollback after it forces re-entering the key. That needs a later hook to fail after the row write. The cost is typing the key again.payload_migrationsruns it once, and no code can save a key before migrations run on boot.Deferred from
/simplify:custom.secret, so a consumer cannot forget to spreadsecretFieldEndpoints.verifyfrom the factory on every save instead of from the collection hook.payload.kvwithout agetper staged entry.Each is small, but none is needed with one consumer.
Detail
Why a staged token, not a direct write. The first version's key route wrote through
payload.updatewith the form's unsaved provider and list. A key set and then abandoned left delivery live against settings nobody saved. Staging writes nothing to the row:verifychecks the key against the form's unsaved values, which the modal posts assiblings, filtered todependsOn. Save then proves it again against what is stored. The provider is called twice, on purpose.Why the control comes first. Payload runs a field's
beforeValidatehooks and then its access check, in field order. The control's own write lock has to strip a refused instruction before the stored field's hook reads it, and the stored field's lock refuses the value again.Why a chosen provider with no key is refused here. No field
requiredreaches a key the form never holds. A key-less list read as configured, delivered nothing, and madeLIST_ELIGIBLEhand its region to the fallback client's list.Security review:
/security-reviewfound nothing this run. It checked the route gates, the:pathresolution, the token's binding and randomness, the hook order, provider-host control and the migration SQL.Closes #1109
🤖 Generated with Claude Code