import 'server-only'; import { getSiteIntegrations } from '../payload/index.js'; import { recordIntegrationStatus } from '../integrations/status.js'; /** Env fallback for the secret — same name ipalEnvRules({ turnstile: true }) requires. */ const ENV_SECRET = 'TURNSTILE_SECRET_KEY'; const STATUS_FIELD = 'turnstileStatus'; /** Cloudflare error codes caused by our configuration / Cloudflare itself — not by the visitor. */ const CONFIG_ERRORS = { 'missing-input-secret': 'The secret key is missing in the request.', 'invalid-input-secret': 'Cloudflare rejected the secret key — check Site Integrations → Turnstile (secret and site key must come from the same widget).', 'internal-error': 'Cloudflare reported an internal error while verifying.' }; const PLUGIN_ERRORS = { 'missing-secret': `No secret key — fill in Site Integrations → Turnstile (or the ${ENV_SECRET} environment variable).`, 'network-error': 'The server could not reach Cloudflare (challenges.cloudflare.com).', 'bad-response': 'Cloudflare returned an unexpected response.' }; /** * Verifies a Turnstile token with Cloudflare and classifies the result (server-only). * * Secret: SiteIntegrations → Turnstile (panel) first, then TURNSTILE_SECRET_KEY. * Every result is logged when not `pass`, and the integration status on * SiteIntegrations is updated (throttled) so the panel shows failures. */ export async function checkTurnstile({ token, payload, ip, sendRemoteIp = false }) { if (!token) return { outcome: 'reject', code: 'missing-input-response' }; const integrations = await getSiteIntegrations(payload); const previous = integrations[STATUS_FIELD]; const secret = integrations.turnstileSecretKey?.trim() || process.env[ENV_SECRET]?.trim(); const degraded = async (code)=>{ const message = PLUGIN_ERRORS[code] ?? CONFIG_ERRORS[code] ?? `Cloudflare error: ${code}`; console.warn(`[ipal] Turnstile degraded (${code}): ${message}`); await recordIntegrationStatus(payload, STATUS_FIELD, previous, { state: 'degraded', code, message }); return { outcome: 'degraded', code }; }; if (!secret) return degraded('missing-secret'); const body = new URLSearchParams({ secret, response: token }); if (sendRemoteIp && ip) body.append('remoteip', ip); let data; try { const res = await fetch('https://challenges.cloudflare.com/turnstile/v0/siteverify', { method: 'POST', body, signal: AbortSignal.timeout(8000) }); if (res.status >= 500) return degraded('internal-error'); data = await res.json(); } catch (err) { console.warn('[ipal] Turnstile: siteverify request failed:', err instanceof Error ? err.message : err); return degraded('network-error'); } if (typeof data?.success !== 'boolean') return degraded('bad-response'); if (data.success) { await recordIntegrationStatus(payload, STATUS_FIELD, previous, { state: 'ok' }); return { outcome: 'pass' }; } const codes = data['error-codes'] ?? []; const configCode = codes.find((c)=>c in CONFIG_ERRORS); if (configCode) return degraded(configCode); // Visitor-side: invalid-input-response, timeout-or-duplicate, missing-input-response, bad-request. console.warn(`[ipal] Turnstile rejected: ${codes.join(', ') || 'unknown'}${data.hostname ? ` (hostname ${data.hostname})` : ''}`); return { outcome: 'reject', code: codes[0] ?? 'unknown' }; } /** * Boolean wrapper used by submitForm. `onConfigError` decides what a `degraded` * result means: 'allow' (default) keeps the site's forms working while the * panel shows the problem; 'reject' fails closed. */ export async function verifyTurnstile({ onConfigError = 'allow', ...args }) { const { outcome } = await checkTurnstile(args); return outcome === 'pass' || outcome === 'degraded' && onConfigError === 'allow'; } //# sourceMappingURL=verify.js.map