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:
Radosław Smoliński
2026-11-25 00:39:12 +01:00
co-authored by Claude Fable 5.2
parent cff355a541
commit 8fa71238ce
11 changed files with 345 additions and 35 deletions
@@ -6,21 +6,21 @@ import { FieldDescription, FieldLabel, useField, useTranslation } from '@payload
import { useState } from 'react'
import { ipalT } from '../../../modules/admin-i18n/t.js'
import { SECRET_CLEAR, SECRET_MASK } from '../../../modules/secrets/writeOnly.js'
/**
* Masked text input for sensitive values (SMTP password, R2 secret key,
* Turnstile secret key, etc.).
* Text input for a secret (SMTP password, Turnstile secret key, …).
*
* Renders the value as a password field (••••) so secrets aren't shown in
* plaintext on screen — protecting against shoulder-surfing and accidental
* exposure when the panel is open. The value is still stored and returned
* normally (UI masking, NOT hashing or encryption), because the plugin must
* read it back to authenticate with SMTP/R2/Turnstile. A toggle reveals the
* value when an authorised editor genuinely needs to check it.
* With a write-only field (modules/secrets/writeOnly.ts — the default for the plugin's own
* secrets) the stored value never reaches the browser: the field arrives as `SECRET_MASK`.
* The input then stays empty and says that a value is set; typing replaces it, leaving it
* alone keeps it, "Remove the stored value" clears it on save.
*
* Uses a raw <input> driven by useField rather than @payloadcms/ui's TextInput,
* because TextInput doesn't expose inputProps, so there's no way to switch its
* inner input to type="password".
* Without write-only (`secrets: { writeOnly: false }`, or a plain text field of a project) the
* value itself arrives; it is drawn as dots with a Show / Hide toggle, as before.
*
* Uses a raw <input> driven by useField rather than @payloadcms/ui's TextInput, because
* TextInput doesn't expose inputProps, so its inner input cannot be type="password".
*
* Assigned via a field's admin.components.Field. Works on any `text` field.
*/
@@ -32,6 +32,9 @@ export const MaskedField: TextFieldClientComponent = ({ field, path }) => {
// A string or one string per language — FieldLabel / FieldDescription translate it.
const label = field?.label || field?.name || path
const description = field?.admin?.description
const stored = value === SECRET_MASK // a value is set on the server; this is not it
const removing = value === SECRET_CLEAR
const link = { background: 'none', border: 'none', cursor: 'pointer', font: 'inherit', opacity: 0.75, padding: '.25rem .5rem', textDecoration: 'underline', whiteSpace: 'nowrap' } as const
return (
<div className="field-type text masked-field">
@@ -45,28 +48,32 @@ export const MaskedField: TextFieldClientComponent = ({ field, path }) => {
className="field-type__input"
id={`field-${path}`}
onChange={(e) => setValue(e.target.value)}
placeholder={stored ? '••••••••' : undefined}
style={{ flex: 1 }}
type={revealed ? 'text' : 'password'}
value={value ?? ''}
value={stored || removing ? '' : (value ?? '')}
/>
<button
onClick={() => setRevealed((r) => !r)}
style={{
background: 'none',
border: 'none',
cursor: 'pointer',
font: 'inherit',
opacity: 0.75,
padding: '.25rem .5rem',
textDecoration: 'underline',
whiteSpace: 'nowrap',
}}
type="button"
>
{ipalT(t as never, revealed ? 'maskedHide' : 'maskedShow')}
</button>
{stored ? (
<button onClick={() => setValue(SECRET_CLEAR)} style={link} type="button">
{ipalT(t as never, 'maskedRemove')}
</button>
) : removing ? (
<button onClick={() => setValue(SECRET_MASK)} style={link} type="button">
{ipalT(t as never, 'maskedKeep')}
</button>
) : (
<button onClick={() => setRevealed((r) => !r)} style={link} type="button">
{ipalT(t as never, revealed ? 'maskedHide' : 'maskedShow')}
</button>
)}
</div>
{stored || removing ? (
<p className="field-description" role="status" style={{ margin: '.25rem 0 0' }}>
{ipalT(t as never, stored ? 'maskedSet' : 'maskedRemoved')}
</p>
) : null}
{description && typeof description !== 'function' ? (
<FieldDescription description={description} path={path} />
) : null}