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:
2026-10-15 23:54:25 +02:00
co-authored by Claude Opus 5.5
parent aa284736c9
commit ce0a390351
25 changed files with 786 additions and 133 deletions
+16 -7
View File
@@ -1,8 +1,9 @@
import 'server-only'
import type { BasePayload } from 'payload'
import { verifyTurnstile } from '../turnstile/index.js'
import { getTurnstileConfig } from '../turnstile/config.js'
import { verifyTurnstile } from '../turnstile/verify.js'
import { validateSubmission } from './validateSubmission.js'
import { checkRateLimit } from './rateLimit.js'
import { consumeRateLimit } from './rateLimit.js'
export type SubmitFormArgs = {
payload: BasePayload
@@ -10,7 +11,11 @@ export type SubmitFormArgs = {
formId: string
/** Submitted field data — shape matches the form's fields. */
data: Record<string, unknown>
/** Turnstile token; when present it is verified, when absent it is skipped. */
/**
* Turnstile token. Required whenever Turnstile is configured (both keys —
* see getTurnstileConfig): a missing token is then rejected. Ignored when
* Turnstile is not configured.
*/
turnstileToken?: string
/** Client IP — used for Turnstile and rate limiting. */
ip?: string
@@ -96,14 +101,18 @@ export async function submitForm({
}: SubmitFormArgs): Promise<SubmitFormResult> {
// 1. Rate limit — cheapest gate, drops a flood before any real work.
if (maxPerMinute > 0 && ip) {
if (!checkRateLimit({ key: ip, max: maxPerMinute })) {
if (!(await consumeRateLimit({ key: ip, max: maxPerMinute })).ok) {
return { success: false, reason: 'rate_limited' }
}
}
// 2. Turnstile — verify when a token is supplied; reject on failure.
if (turnstileToken !== undefined) {
const ok = await verifyTurnstile({ token: turnstileToken, payload, ip, onConfigError: turnstileOnConfigError })
// 2. Turnstile — decided by the SERVER's config, never by whether the client
// sent a token: configured → a token is required and verified (a bot can't
// opt out by omitting it); not configured → skipped, the widget isn't shown.
if ((await getTurnstileConfig(payload)).enabled) {
const ok =
Boolean(turnstileToken) &&
(await verifyTurnstile({ token: turnstileToken!, payload, ip, onConfigError: turnstileOnConfigError }))
if (!ok) {
return { success: false, reason: 'turnstile' }
}