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:
@@ -5,7 +5,9 @@ import type { I18nConfig } from '../i18n/index.js'
|
||||
|
||||
import { createMarkdownRenderer, searchContent } from '../content/index.js'
|
||||
import { getDefaultLocale, isValidLocale } 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'
|
||||
|
||||
/**
|
||||
* A read-only MCP (Model Context Protocol) server for the client site, as a Next
|
||||
@@ -41,6 +43,8 @@ const DEFAULT_PROTOCOL = '2025-06-18'
|
||||
type CreateMcpRouteArgs = {
|
||||
/** Absolute origin for building result URLs. */
|
||||
baseUrl?: string
|
||||
/** How to read the client IP for the rate limit — see clientIp(). */
|
||||
clientIp?: ClientIpOptions
|
||||
/** Pages collection. Default 'pages'. */
|
||||
collection?: string
|
||||
config: I18nConfig
|
||||
@@ -50,7 +54,7 @@ type CreateMcpRouteArgs = {
|
||||
/** Max messages in one JSON-RPC batch. Default 10. */
|
||||
maxBatch?: number
|
||||
/**
|
||||
* Per-IP limit on JSON-RPC messages (a batch counts every message) (in memory, per instance). Default { max: 60, windowMs: 60_000 }.
|
||||
* Per-IP limit on JSON-RPC messages (a batch counts every message) (in memory per instance, or the store from setRateLimitStore). Default { max: 60, windowMs: 60_000 }.
|
||||
* `false` = off (a limiter in front, e.g. Cloudflare WAF rate limiting).
|
||||
*/
|
||||
rateLimit?: { max?: number; windowMs?: number } | false
|
||||
@@ -224,9 +228,9 @@ export function createMcpRoute(args: CreateMcpRouteArgs) {
|
||||
return Response.json(rpcError(null, -32600, `Batch too large (max ${maxBatch})`), { status: 400 })
|
||||
}
|
||||
if (rl) {
|
||||
const ip = clientIp(req)
|
||||
if (!checkRateLimit({ cost: messages, key: ip, scope: 'mcp', ...rl })) {
|
||||
return tooManyRequests(ip, 'mcp', rpcError(null, -32000, 'Too many requests'))
|
||||
const limit = await consumeRateLimit({ cost: messages, key: clientIp(req, args.clientIp), scope: 'mcp', ...rl })
|
||||
if (!limit.ok) {
|
||||
return tooManyRequests(limit.retryAfter, rpcError(null, -32000, 'Too many requests'))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user