Security: mandatory Turnstile when configured, admin-only test email, roles on create, safer client IP
- Turnstile is on only when both keys are set (panel or env); then submitForm requires a valid token, so a bot can no longer skip the check by omitting it. Not configured -> no widget, no check. New getTurnstileConfig() for layouts. - /api/ipal/test-email now requires the admin role (403 for other users). - roles field: create access is admin-only, so open sign-up can't self-assign admin. - clientIp reads X-Forwarded-For from the right, accepts Headers, can be pinned via option or IPAL_IP_HEADER; exposed on search/MCP routes. - Pluggable shared rate-limit store (setRateLimitStore) for multi-instance setups. - Replace the template int.spec with real unit tests in test/ (44 tests). Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -21,11 +21,12 @@ const TurnstileContext = createContext<TurnstileContextValue | null>(null)
|
||||
*
|
||||
* // layout.tsx (server) → read key, pass to provider
|
||||
* import { TurnstileProvider } from '@intecion/ipal-kit/client'
|
||||
* const siteKey = await getTurnstileSiteKey() // your server helper
|
||||
* import { getTurnstileConfig } from '@intecion/ipal-kit'
|
||||
* const { siteKey } = await getTurnstileConfig(payload) // null unless both keys are set
|
||||
* <TurnstileProvider siteKey={siteKey}>{children}</TurnstileProvider>
|
||||
*
|
||||
* When siteKey is null (Turnstile not configured), widgets render nothing and
|
||||
* token stays null — forms should treat "no Turnstile" as allowed in dev.
|
||||
* token stays null — submitForm skips the check too, so the form just works.
|
||||
*/
|
||||
export function TurnstileProvider({
|
||||
siteKey,
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
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
|
||||
|
||||
/**
|
||||
* 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 siteKey = integrations.turnstileSiteKey?.trim() || process.env[TURNSTILE_SITE_KEY_ENV]?.trim() || ''
|
||||
const secret = integrations.turnstileSecretKey?.trim() || process.env[TURNSTILE_SECRET_ENV]?.trim() || ''
|
||||
|
||||
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 }
|
||||
}
|
||||
@@ -2,4 +2,6 @@
|
||||
// imported from a client component — use ./client for the widget instead.
|
||||
export { verifyTurnstile, checkTurnstile } from './verify.js'
|
||||
export type { TurnstileCheck, TurnstileOutcome } from './verify.js'
|
||||
export { getTurnstileConfig } from './config.js'
|
||||
export type { TurnstileConfig } from './config.js'
|
||||
export { TurnstileProvider, useTurnstile } from './TurnstileProvider.js'
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'server-only'
|
||||
import type { BasePayload } from 'payload'
|
||||
import { getSiteIntegrations } from '../payload/index.js'
|
||||
import { recordIntegrationStatus, type IntegrationStatus } from '../integrations/status.js'
|
||||
import { TURNSTILE_SECRET_ENV as ENV_SECRET } from './config.js'
|
||||
|
||||
type SiteverifyResponse = {
|
||||
success: boolean
|
||||
@@ -37,8 +38,6 @@ type VerifyTurnstileArgs = {
|
||||
onConfigError?: 'allow' | 'reject'
|
||||
}
|
||||
|
||||
/** Env fallback for the secret — same name ipalEnvRules({ turnstile: true }) requires. */
|
||||
const ENV_SECRET = 'TURNSTILE_SECRET_KEY'
|
||||
const STATUS_FIELD = 'turnstileStatus'
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user