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
+9 -5
View File
@@ -2,12 +2,16 @@ import type { BasePayload } from 'payload'
import type { I18nConfig } from '../i18n/index.js'
import { checkRateLimit, clientIp, tooManyRequests } from '../shared/rateLimit.js'
import type { ClientIpOptions } from '../shared/rateLimit.js'
import { clientIp, consumeRateLimit, tooManyRequests } from '../shared/rateLimit.js'
import { searchContent } from './searchContent.js'
type CreateSearchRouteArgs = {
/** Absolute origin to prefix result URLs with. */
baseUrl?: string
/** How to read the client IP for the rate limit — see clientIp(). */
clientIp?: ClientIpOptions
/** Pages collection. Default 'pages'. */
collection?: string
config: I18nConfig
@@ -17,7 +21,7 @@ type CreateSearchRouteArgs = {
/** Home slug map/string so the home result collapses to root. */
homeSlug?: Record<string, string> | string
/**
* Per-IP limit (in memory, per instance). Default { max: 120, windowMs: 60_000 }.
* Per-IP limit (in memory per instance, or the store from setRateLimitStore). Default { max: 120, windowMs: 60_000 }.
* `false` = off (a limiter in front, e.g. Cloudflare WAF rate limiting).
*/
rateLimit?: { max?: number; windowMs?: number } | false
@@ -50,9 +54,9 @@ export function createSearchRoute(args: CreateSearchRouteArgs): (req: Request) =
return async (req: Request) => {
if (rl) {
const ip = clientIp(req)
if (!checkRateLimit({ key: ip, scope: 'search', ...rl })) {
return tooManyRequests(ip, 'search', { count: 0, error: 'Too many requests', query: '', results: [] })
const limit = await consumeRateLimit({ key: clientIp(req, args.clientIp), scope: 'search', ...rl })
if (!limit.ok) {
return tooManyRequests(limit.retryAfter, { count: 0, error: 'Too many requests', query: '', results: [] })
}
}
const url = new URL(req.url)