Write-only secrets cannot be queried (review B-R1)

A where or sort on a write-only field answered by the stored value: as an admin,
where[version.smtpPassword][like]=Zq on Site integrations' versions gave a count, so the
secret could be read back one character at a time. writeOnlySecret now gives the field a
read rule, secretReadAccess (exported), that denies when Payload checks a query's fields
(no document) and allows a document read (masked as before). The panel's edit and create
views pass the document, so the field stays visible and editable there.

Covered: the global, its versions, a collection with and without versions, REST and
GraphQL, like / equals / exists / or / sort / count; reads still masked; panel
permissions; a save still stores the new value.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-26 21:10:45 +01:00
co-authored by Claude Opus 5.5
parent ecf03c8dc0
commit eb2ad27787
6 changed files with 234 additions and 3 deletions
+1
View File
@@ -421,6 +421,7 @@ export {
maskWriteOnlySecrets,
SECRET_CLEAR,
SECRET_MASK,
secretReadAccess,
SMTP_PASSWORD_BOUND_TO,
writeOnlySecret,
writeOnlySecrets,
+23 -2
View File
@@ -1,4 +1,4 @@
import type { CollectionAfterReadHook, Field, FieldHook, GlobalAfterReadHook } from 'payload'
import type { CollectionAfterReadHook, Field, FieldAccess, FieldHook, GlobalAfterReadHook } from 'payload'
import { ValidationError } from 'payload'
@@ -130,11 +130,32 @@ export function buildKeepSecret({ appliesWhen, boundTo = [] }: WriteOnlySecretOp
*/
export const keepSecretBeforeValidate: FieldHook = buildKeepSecret()
/** Adds the two hooks to a field (a text field as a rule). The field's own hooks stay, and run around them. */
/**
* The field `read` rule of a write-only secret: a document may be read (the value comes back masked), a query may
* not name the field. A `where` / `sort` on the field answers by the stored value — `like: "Zq"` gives a count —
* so it would read the secret back one character at a time (review B-R1). Payload checks a query's fields with
* permissions built WITHOUT a document and asks rules that depend on one to deny then; the panel's edit and create
* views always pass the document (a global's stored data, a collection's `{ _status: 'draft', … }`), so the field
* stays visible and editable there. The field's own `read` rule, if any, still decides for a document.
*/
export function secretReadAccess(own?: FieldAccess): FieldAccess {
return (args) => {
const { data, doc, siblingData } = args as { data?: unknown; doc?: unknown; siblingData?: unknown }
if (doc === undefined && data === undefined && siblingData === undefined) {return false}
return own ? own(args) : true
}
}
/**
* Adds the two hooks to a field (a text field as a rule), and a `read` rule that keeps it out of queries
* (`secretReadAccess`). The field's own hooks stay, and run around them; its own `access` is kept.
*/
export function writeOnlySecret<F extends Field>(field: F, options: WriteOnlySecretOptions = {}): F {
const own = (field as { hooks?: { afterRead?: FieldHook[]; beforeValidate?: FieldHook[] } }).hooks ?? {}
const access = (field as { access?: { create?: FieldAccess; read?: FieldAccess; update?: FieldAccess } }).access
return {
...field,
access: { ...access, read: secretReadAccess(access?.read) },
custom: { ...(field as { custom?: Record<string, unknown> }).custom, ipalWriteOnly: true },
hooks: {
...own,