M1 (1): stored secrets are write-only
smtpPassword and turnstileSecretKey in the Site integrations global are returned
as '********' to every read that goes through access control (REST, GraphQL, the
panel's form state); the Local API with access control off still gets the value.
A save that sends nothing, an empty value or the mask keeps the stored one;
'[[clear]]' removes it. MaskedField says that a value is set and offers to
remove it. Off: secrets: { writeOnly: false }. writeOnlySecret(s) and the hooks
are exported for a project's own fields.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
cff355a541
commit
8fa71238ce
@@ -10,6 +10,10 @@ export const integrationsTexts = defineTexts({
|
||||
|
||||
// Masked secret field
|
||||
maskedHide: 'Hide',
|
||||
maskedKeep: 'Keep the stored value',
|
||||
maskedRemove: 'Remove the stored value',
|
||||
maskedRemoved: 'The stored value will be removed when you save.',
|
||||
maskedSet: 'A value is set. It is not shown; type a new one to replace it.',
|
||||
maskedShow: 'Show',
|
||||
|
||||
// Status banner on an integration's tab (IntegrationStatusBanner)
|
||||
@@ -63,6 +67,10 @@ export const integrationsTexts = defineTexts({
|
||||
alertsUnknownError: 'Nieznany błąd.',
|
||||
|
||||
maskedHide: 'Ukryj',
|
||||
maskedKeep: 'Zostaw zapisaną wartość',
|
||||
maskedRemove: 'Usuń zapisaną wartość',
|
||||
maskedRemoved: 'Zapisana wartość zostanie usunięta po zapisaniu.',
|
||||
maskedSet: 'Wartość jest ustawiona. Nie jest wyświetlana; wpisz nową, aby ją zastąpić.',
|
||||
maskedShow: 'Pokaż',
|
||||
|
||||
statusCode: 'Kod: {{code}}.',
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
import type { Field, FieldHook } from 'payload'
|
||||
|
||||
/**
|
||||
* Write-only secrets: a field whose stored value never leaves the server.
|
||||
*
|
||||
* `smtpPassword` and `turnstileSecretKey` live in the Site integrations global — the panel is
|
||||
* their source. Until now `MaskedField` only drew dots on screen: the value itself went to the
|
||||
* browser, and REST and GraphQL returned it to whoever could read the global. A stolen admin
|
||||
* session, or any way past the global's read rule, was a working mailbox password.
|
||||
*
|
||||
* With `writeOnlySecret` the value is:
|
||||
* - **returned as `SECRET_MASK`** when it is set (and as it is when empty) to every read that
|
||||
* goes through access control — REST, GraphQL, the panel's form state, versions;
|
||||
* - **returned in full** only to a Local API read with access control off (the default of
|
||||
* `payload.findGlobal` / `payload.find` on the server) — how the plugin's own code reads it;
|
||||
* - **kept** when a save sends nothing, an empty value or the mask back;
|
||||
* - **replaced** when a save sends a new value, and **removed** when it sends `SECRET_CLEAR`.
|
||||
*
|
||||
* Off for the two built-in fields: `secrets: { writeOnly: false }`. For a field of your own:
|
||||
* `writeOnlySecret({ name: 'apiToken', type: 'text', admin: { components: { Field: '@intecion/ipal-kit/admin#MaskedField' } } })`.
|
||||
*/
|
||||
|
||||
/** What a read gets instead of a stored secret: "a value is set". */
|
||||
export const SECRET_MASK = '********'
|
||||
/** Send this as the value to remove a stored secret (an empty value keeps it). */
|
||||
export const SECRET_CLEAR = '[[clear]]'
|
||||
|
||||
/** True when a value read from the API means "a secret is stored" rather than being one. */
|
||||
export const isSecretMask = (value: unknown): boolean => value === SECRET_MASK
|
||||
|
||||
const dig = (doc: unknown, path: (number | string)[]): unknown => path.reduce<unknown>((at, key) => (at && typeof at === 'object' ? (at as Record<string, unknown>)[key as string] : undefined), doc)
|
||||
|
||||
/** Reads: the stored value only for server code that reads with access control off. */
|
||||
export const maskSecretAfterRead: FieldHook = ({ overrideAccess, value }) => {
|
||||
if (overrideAccess === true) {return value}
|
||||
return typeof value === 'string' && value !== '' ? SECRET_MASK : value
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes: nothing, an empty value or the mask keep what is stored; `SECRET_CLEAR` removes it;
|
||||
* anything else is the new secret. Runs in `beforeValidate`, so every later hook (the global's
|
||||
* own `beforeChange` included) already sees the real value.
|
||||
*/
|
||||
export const keepSecretBeforeValidate: FieldHook = ({ originalDoc, path, value }) => {
|
||||
if (value === SECRET_CLEAR) {return null}
|
||||
if (value === undefined || value === null || value === '' || value === SECRET_MASK) {
|
||||
const stored = dig(originalDoc, path)
|
||||
// The original document is read by Payload with access control off, so this is the real value;
|
||||
// a mask here would mean it was not — never store the mask.
|
||||
return typeof stored === 'string' && stored !== SECRET_MASK ? stored : (stored ?? null)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
/** Adds the two hooks to a field (a text field as a rule). The field's own hooks stay, and run around them. */
|
||||
export function writeOnlySecret<F extends Field>(field: F): F {
|
||||
const own = (field as { hooks?: { afterRead?: FieldHook[]; beforeValidate?: FieldHook[] } }).hooks ?? {}
|
||||
return {
|
||||
...field,
|
||||
custom: { ...(field as { custom?: Record<string, unknown> }).custom, ipalWriteOnly: true },
|
||||
hooks: {
|
||||
...own,
|
||||
afterRead: [...(own.afterRead ?? []), maskSecretAfterRead],
|
||||
beforeValidate: [keepSecretBeforeValidate, ...(own.beforeValidate ?? [])],
|
||||
},
|
||||
} as F
|
||||
}
|
||||
|
||||
/** `writeOnlySecret` for the named fields, wherever they are in the tree (tabs, rows, groups, collapsibles). */
|
||||
export function writeOnlySecrets(fields: Field[], names: string[]): Field[] {
|
||||
return fields.map((field) => {
|
||||
const f = field as { fields?: Field[]; name?: string; tabs?: { fields: Field[] }[] } & Field
|
||||
if (f.name && names.includes(f.name) && !('fields' in f && Array.isArray(f.fields))) {return writeOnlySecret(field)}
|
||||
if (Array.isArray(f.tabs)) {return { ...f, tabs: f.tabs.map((tab) => ({ ...tab, fields: writeOnlySecrets(tab.fields, names) })) } as Field}
|
||||
if (Array.isArray(f.fields)) {return { ...f, fields: writeOnlySecrets(f.fields, names) } as Field}
|
||||
return field
|
||||
})
|
||||
}
|
||||
|
||||
/** The plugin's own stored secrets (Site integrations). */
|
||||
export const INTEGRATION_SECRET_FIELDS = ['smtpPassword', 'turnstileSecretKey']
|
||||
Reference in New Issue
Block a user