M1 (2, 3): the Turnstile env secret is deprecated; doctor rules access-missing, turnstile-degraded, turnstile-env-key

(2) Stage one of the deprecation: TURNSTILE_SECRET_KEY from the environment still
works when the panel field is empty, logs once that it is deprecated, and the
doctor's deep status reports it (rule turnstile-env-key, warning). Nothing is
removed.
(3) access-missing (static, warning): ipalKit without `access` means every panel
account is an admin. turnstile-degraded (deep): Turnstile is not checking forms —
warning, error after a day. The deep status carries the recorded state and where
the secret comes from, never a key.

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 8fa71238ce
commit 42fdad8c2d
8 changed files with 214 additions and 16 deletions
+12 -12
View File
@@ -297,6 +297,16 @@ export { buildTableOfContentsField, extractHeadings, headingText } from './modul
export type { ExtractHeadingsOptions, HeadingItem, HeadingLevel } from './modules/richtext/headings.js'
// Lexical → Markdown (pure, no React) — for AI-readable output.
export { lexicalToMarkdown } from './modules/richtext/lexicalToMarkdown.js'
export {
INTEGRATION_SECRET_FIELDS,
isSecretMask,
keepSecretBeforeValidate,
maskSecretAfterRead,
SECRET_CLEAR,
SECRET_MASK,
writeOnlySecret,
writeOnlySecrets,
} from './modules/secrets/writeOnly.js'
export {
buildAdminCsp,
buildAdminCspHeaders,
@@ -316,6 +326,7 @@ export {
UPLOAD_SECURITY_HEADERS,
withSecurityConfig,
} from './modules/security/index.js'
export type {
BuildAdminCspArgs,
BuildCspArgs,
@@ -327,21 +338,10 @@ export type {
TotpUser,
WithSecurityConfigOptions,
} from './modules/security/index.js'
export type { BuildSecurityHeadersArgs, SecurityHeader } from './modules/security/index.js'
export { findImportMapFile, missingImportMapEntries } from './modules/security/totp/importMapCheck.js'
export { changedViewKeys, copyViews, SAFE_TOTP_VIEW, safeAdminBack, totpViewImportKey, wrapTotpViews } from './modules/security/totp/safeBack.js'
export type { SafeBackContext, SafeBackRule, TotpViewComponent } from './modules/security/totp/safeBack.js'
export {
INTEGRATION_SECRET_FIELDS,
isSecretMask,
keepSecretBeforeValidate,
maskSecretAfterRead,
SECRET_CLEAR,
SECRET_MASK,
writeOnlySecret,
writeOnlySecrets,
} from './modules/secrets/writeOnly.js'
export { requireSecondFactor, secondFactorPassed, twoFactorSetupDeadline, twoFactorSetupState } from './modules/security/totp/secondFactor.js'
export type { TwoFactorSetupState } from './modules/security/totp/secondFactor.js'
export { SETUP_FROM_FIELD } from './modules/security/totp/secondFactor.js'
@@ -425,7 +425,7 @@ export type { Theme, ThemeConfig, ThemeSetting } from './modules/theme/index.js'
// which imports 'server-only'.
export { defineTokens, IPAL_BREAKPOINTS, IPAL_TOKENS, ipalVar } from './modules/tokens/index.js'
export type { IpalTokenName, TokensInput } from './modules/tokens/index.js'
export { getTurnstileConfig } from './modules/turnstile/config.js'
export { getTurnstileConfig, TURNSTILE_SECRET_ENV, turnstileSecretFromEnv } from './modules/turnstile/config.js'
export type { TurnstileConfig } from './modules/turnstile/config.js'
export { ipalKit } from './plugin.js'
export type { IpalOptions, ThemeOptions } from './types.js'
+45
View File
@@ -141,6 +141,51 @@ export const securityRules: DoctorRule[] = [
group: 'security',
source: 'static',
},
{
// Without `access` the plugin adds no roles: every account that can log in to the panel is an admin.
id: 'access-missing',
check: (ctx) => {
const code = cfg(ctx)
if (!/ipalKit\s*\(/.test(code)) {return []}
// `access: { authCollection | defaultRole | … }`, an empty `access: {}`, or a variable whose name says so
const set = /\baccess\s*:\s*\{\}/.test(code.replace(/\s+/g, '')) || /\baccess\s*:\s*\{[^{}]*\b(?:authCollection|defaultRole|authAccess|firstUserAdmin|staffCollections|panelRole|selfRegistration|password)\b/.test(code) || /\baccess\s*:\s*\w*[aA]ccess\w*\s*[,}]/.test(code)
return set
? []
: [{ file: configFile(ctx) ?? 'payload.config.ts', fix: "ipalKit({ access: { authCollection: 'users' } }) — roles admin > editor > user; or record the reason in ipal-doctor.json", message: 'ipalKit has no `access` option: there are no roles, so every account that can log in to the panel is an admin', severity: 'warning' }]
},
docs: 'docs/access.md',
group: 'security',
source: 'static',
},
{
// Turnstile lets submissions through while it is misconfigured or Cloudflare fails ("degraded") — visible here.
id: 'turnstile-degraded',
check: (ctx) => {
const status = ctx.deep?.turnstile
if (!status || status.state !== 'degraded') {return []}
const hours = status.since ? Math.floor((Date.parse(`${ctx.today}T23:59:59Z`) - Date.parse(status.since)) / 3_600_000) : null
const long = hours !== null && hours >= 24
return [{
fix: 'Site integrations → Spam protection: both keys from the same Cloudflare widget; then send a test form',
message: `Turnstile is not checking forms (${status.code ?? 'unknown'})${status.since ? `, since ${status.since.slice(0, 16).replace('T', ' ')}` : ''}: text forms are accepted without the check and forms with files are refused (unless forms.turnstileOnConfigError says otherwise)`,
severity: long ? 'error' : 'warning',
}]
},
docs: 'docs/turnstile.md',
group: 'security',
source: 'deep',
},
{
// The secret key from the environment is deprecated: the panel is its source.
id: 'turnstile-env-key',
check: (ctx) =>
ctx.deep?.turnstileSecretFromEnv
? [{ fix: 'copy the key into Site integrations → Spam protection → Secret key, check a form, then remove TURNSTILE_SECRET_KEY from the environment (Coolify)', message: 'the Turnstile secret key comes from the TURNSTILE_SECRET_KEY environment variable (deprecated; it will be removed in a later release)', severity: 'warning' }]
: [],
docs: 'docs/turnstile.md',
group: 'security',
source: 'deep',
},
{
id: 'cookie-scanner',
check: (ctx) => {
+18 -1
View File
@@ -10,6 +10,7 @@ import { COOKIE_REVIEW_SLUG } from '../consent/scanner/collection.js'
import { secondFactorPassed, SETUP_FROM_FIELD } from '../security/totp/secondFactor.js'
import { countAccountsWithoutTotp } from '../security/totp/setupRequired.js'
import { clientIp, consumeRateLimit, tooManyRequests } from '../shared/rateLimit.js'
import { turnstileSecretFromEnv } from '../turnstile/config.js'
/** The plugin's own version (package.json next to dist/ and src/). */
export function pluginVersion(): string {
@@ -44,7 +45,23 @@ export async function collectDoctorStatus(payload: BasePayload): Promise<DoctorS
}
}
const pendingCookies = collections.some((c) => c.slug === COOKIE_REVIEW_SLUG) ? await countPendingCookies(payload) : null
return { node: process.version, pendingCookies, plugin: pluginVersion(), usersSetupClosed, usersWithout2fa }
// Turnstile: the recorded state and where the secret comes from — never the keys themselves.
let turnstile: DoctorStatus['turnstile'] = null
let secretFromEnv: boolean | null = null
if ((payload.config.globals ?? []).some((g) => g.slug === 'site-integrations')) {
try {
const integrations = (await payload.findGlobal({ slug: 'site-integrations' as never, depth: 0, overrideAccess: true })) as unknown as {
turnstileSecretKey?: null | string
turnstileStatus?: { code?: null | string; since?: null | string; state?: null | string } | null
}
const status = integrations.turnstileStatus
turnstile = status?.state ? { code: status.code ?? null, since: status.since ?? null, state: status.state } : null
secretFromEnv = turnstileSecretFromEnv(integrations)
} catch {
/* the global cannot be read: unknown */
}
}
return { node: process.version, pendingCookies, plugin: pluginVersion(), turnstile, turnstileSecretFromEnv: secretFromEnv, usersSetupClosed, usersWithout2fa }
}
export type DoctorEndpointOptions = {
+4
View File
@@ -63,6 +63,10 @@ export type DoctorStatus = {
node?: string
pendingCookies?: null | number
plugin?: string
/** The Turnstile integration's recorded state (Site integrations): never a key, only the state. null = not known. */
turnstile?: { code?: null | string; since?: null | string; state?: null | string } | null
/** The Turnstile secret key in use comes from the environment (the panel field is empty). Deprecated. */
turnstileSecretFromEnv?: boolean | null
/** Of `usersWithout2fa`: accounts whose setup window has passed (blocked until an admin reopens setup). */
usersSetupClosed?: null | number
usersWithout2fa?: null | number
+25 -1
View File
@@ -19,6 +19,27 @@ export type TurnstileConfig = {
}
let warnedPartial = false
let warnedEnvSecret = false
/**
* True when the secret key in use comes from the environment (`TURNSTILE_SECRET_KEY`) because
* the panel field is empty. **Deprecated:** the panel is the source of the secret; the
* environment value keeps working for now and will be removed in a later, announced release.
*/
export function turnstileSecretFromEnv(integrations: IntegrationsWithTurnstileKeys): boolean {
return !integrations.turnstileSecretKey?.trim() && Boolean(process.env[TURNSTILE_SECRET_ENV]?.trim())
}
/** Says once per process that the secret came from the environment (see turnstileSecretFromEnv). */
export function warnTurnstileEnvSecret(integrations: IntegrationsWithTurnstileKeys): void {
if (warnedEnvSecret || !turnstileSecretFromEnv(integrations)) {return}
warnedEnvSecret = true
console.warn(
`[ipal] Deprecated: the Turnstile secret key comes from the ${TURNSTILE_SECRET_ENV} environment variable. ` +
'Put it into Site integrations → Spam protection → Secret key (the panel is the source of this secret). ' +
'The environment value keeps working for now and will be removed in a later release, named in the changelog.',
)
}
function resolveKeys(integrations: IntegrationsWithTurnstileKeys) {
return {
@@ -69,7 +90,10 @@ export async function getTurnstileConfig(payload: BasePayload): Promise<Turnstil
const integrations = await getSiteIntegrations<IntegrationsWithTurnstileKeys>(payload)
const { secret, siteKey } = resolveKeys(integrations)
if (siteKey && secret) {return { enabled: true, siteKey }}
if (siteKey && secret) {
warnTurnstileEnvSecret(integrations)
return { enabled: true, siteKey }
}
if ((siteKey || secret) && !warnedPartial) {
warnedPartial = true
+1 -1
View File
@@ -83,7 +83,7 @@ const LOG_DETAIL: Record<string, string> = {
/**
* Verifies a Turnstile token with Cloudflare and classifies the result (server-only).
*
* Secret: SiteIntegrations → Turnstile (panel) first, then TURNSTILE_SECRET_KEY.
* Secret: SiteIntegrations → Turnstile (panel) first, then TURNSTILE_SECRET_KEY (deprecated: turnstile/config.ts).
* Every result is logged when not `pass`, and the integration status on
* SiteIntegrations is updated (throttled) so the panel shows failures.
*/