Files
ipal-kit/src/modules/turnstile/config.ts
T
radoslaw.smolinskiandClaude Opus 5.5 ab854ef37a MCP site tools, Turnstile key alert, required 2FA injection, a11y, more tests
- MCP: new get_site_info and list_pages tools (public by design, published
  and indexable content only); search results collapse the homepage to root.
  New listContent() helper shared with searchContent.
- Dashboard alert when Turnstile is half-configured (it is silently off then).
- twoFactor.plugin is now required; the dynamic-import fallback (which never
  worked under Next and caused a Turbopack warning) is gone.
- a11y: cookie category checkboxes are labelled, buttons have type, test-email
  input is labelled; tab labels keep localized collection labels.
- IPAL_IP_HEADER: one-time production warning when unset, opt-in
  ipalEnvRules({ ipHeader: true }).
- Tests: SEO/robots/sitemap/hreflang, CSP, Markdown, consent, MCP, and a real
  Payload + SQLite integration test for forms and the roles guard (74 total).
- Drop unused mongodb-memory-server; dev .env.example for SQLite.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 01:15:19 +02:00

76 lines
3.1 KiB
TypeScript

import type { BasePayload } from 'payload'
import { getSiteIntegrations } from '../payload/getSiteIntegrations.js'
type IntegrationsWithTurnstileKeys = {
turnstileSecretKey?: null | string
turnstileSiteKey?: null | string
}
/** Env fallbacks — the same names ipalEnvRules({ turnstile: true }) requires. */
export const TURNSTILE_SITE_KEY_ENV = 'NEXT_PUBLIC_TURNSTILE_SITE_KEY'
export const TURNSTILE_SECRET_ENV = 'TURNSTILE_SECRET_KEY'
export type TurnstileConfig = {
/** True when BOTH keys are set — the widget shows and submitForm requires a token. */
enabled: boolean
/** Public site key for <TurnstileProvider siteKey>; null when Turnstile is off. */
siteKey: null | string
}
let warnedPartial = false
function resolveKeys(integrations: IntegrationsWithTurnstileKeys) {
return {
secret: integrations.turnstileSecretKey?.trim() || process.env[TURNSTILE_SECRET_ENV]?.trim() || '',
siteKey: integrations.turnstileSiteKey?.trim() || process.env[TURNSTILE_SITE_KEY_ENV]?.trim() || '',
}
}
/**
* Why Turnstile is half-configured (only one of the two keys set), or null when
* it's fully on or fully off. Shown as a dashboard alert by IntegrationAlerts —
* the half-configured state silently turns Turnstile OFF, which an editor who
* just pasted one key wouldn't expect.
*/
export function turnstileKeyProblem(integrations: IntegrationsWithTurnstileKeys): null | string {
const { secret, siteKey } = resolveKeys(integrations)
if (Boolean(siteKey) === Boolean(secret)) {return null}
return `${siteKey ? 'Secret key' : 'Site key'} is missing, so Turnstile is OFF — forms are not protected. Fill in both keys (or clear both).`
}
/**
* Whether Turnstile is on, and the public key to render it with.
*
* Turnstile is on only when BOTH keys are configured (SiteIntegrations → Turnstile
* in the panel, falling back to NEXT_PUBLIC_TURNSTILE_SITE_KEY /
* TURNSTILE_SECRET_KEY). Not configured → no widget and no check: the site works
* without it. Configured → the widget shows and submitForm REQUIRES a valid
* token, so a bot can't skip the check by leaving the token out.
*
* Half-configured (only one key) counts as off — a widget without a secret
* can't be verified, a secret without a widget would reject every visitor —
* and is logged once so it doesn't go unnoticed.
*
* // layout.tsx (server)
* const { siteKey } = await getTurnstileConfig(payload)
* <TurnstileProvider siteKey={siteKey}>…</TurnstileProvider>
*
* Returns only the public key — the secret never leaves the server.
*/
export async function getTurnstileConfig(payload: BasePayload): Promise<TurnstileConfig> {
const integrations = await getSiteIntegrations<IntegrationsWithTurnstileKeys>(payload)
const { secret, siteKey } = resolveKeys(integrations)
if (siteKey && secret) {return { enabled: true, siteKey }}
if ((siteKey || secret) && !warnedPartial) {
warnedPartial = true
console.warn(
`[ipal] Turnstile is half-configured (${siteKey ? 'secret key' : 'site key'} missing) — ` +
'treating it as off. Fill in both keys in Site Integrations → Turnstile.',
)
}
return { enabled: false, siteKey: null }
}