Fix turnstile
This commit is contained in:
@@ -1,21 +1,17 @@
|
||||
import 'server-only'
|
||||
|
||||
import type { BasePayload } from 'payload'
|
||||
|
||||
import { verifyTurnstile } from '../turnstile/index.js'
|
||||
import { checkRateLimit } from './rateLimit.js'
|
||||
import { validateSubmission } from './validateSubmission.js'
|
||||
import { checkRateLimit } from './rateLimit.js'
|
||||
|
||||
export type SubmitFormArgs = {
|
||||
/**
|
||||
* Name of the GDPR consent checkbox. A field with this name must be checked
|
||||
* for the submission to succeed (enforced server-side). Defaults to 'consent'.
|
||||
*/
|
||||
consentFieldName?: string
|
||||
/** Submitted field data — shape matches the form's fields. */
|
||||
data: Record<string, unknown>
|
||||
payload: BasePayload
|
||||
/** Form-builder form ID this submission belongs to. */
|
||||
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. */
|
||||
turnstileToken?: string
|
||||
/** Client IP — used for Turnstile and rate limiting. */
|
||||
ip?: string
|
||||
/**
|
||||
@@ -23,15 +19,17 @@ export type SubmitFormArgs = {
|
||||
* Set to 0 to disable (e.g. when a real limiter sits in front).
|
||||
*/
|
||||
maxPerMinute?: number
|
||||
payload: BasePayload
|
||||
/**
|
||||
* Name of the GDPR consent checkbox. A field with this name must be checked
|
||||
* for the submission to succeed (enforced server-side). Defaults to 'consent'.
|
||||
*/
|
||||
consentFieldName?: string
|
||||
/**
|
||||
* Turnstile can't be verified because of OUR side (missing/wrong secret,
|
||||
* Cloudflare unreachable): 'allow' (default) accepts the submission and shows
|
||||
* the problem in the panel; 'reject' refuses it.
|
||||
*/
|
||||
turnstileOnConfigError?: 'allow' | 'reject'
|
||||
/** Turnstile token; when present it is verified, when absent it is skipped. */
|
||||
turnstileToken?: string
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,26 +50,26 @@ export type SubmitFormArgs = {
|
||||
* - `error` — persistence failed unexpectedly
|
||||
*/
|
||||
export type SubmitFailure =
|
||||
| { success: false; reason: 'rate_limited' }
|
||||
| { success: false; reason: 'turnstile' }
|
||||
| {
|
||||
success: false
|
||||
reason: 'validation'
|
||||
/** The offending field's name, when one field is at fault. */
|
||||
field?: string
|
||||
/** What was wrong with it. */
|
||||
kind?: 'required' | 'too_long' | 'unknown_fields'
|
||||
reason: 'validation'
|
||||
success: false
|
||||
}
|
||||
| { success: false; reason: 'not_found' }
|
||||
| {
|
||||
field?: string
|
||||
success: false
|
||||
/** A GDPR consent field existed on the form but wasn't checked. */
|
||||
reason: 'consent'
|
||||
success: false
|
||||
field?: string
|
||||
}
|
||||
| { reason: 'error'; success: false }
|
||||
| { reason: 'not_found'; success: false }
|
||||
| { reason: 'rate_limited'; success: false }
|
||||
| { reason: 'turnstile'; success: false }
|
||||
| { success: false; reason: 'error' }
|
||||
|
||||
export type SubmitFormResult = { submissionId: number | string; success: true } | SubmitFailure
|
||||
export type SubmitFormResult = SubmitFailure | { success: true; submissionId: string | number }
|
||||
|
||||
/**
|
||||
* Handles a form submission end to end: rate limit, verify Turnstile, validate
|
||||
@@ -87,27 +85,27 @@ export type SubmitFormResult = { submissionId: number | string; success: true }
|
||||
* server-only: touches the Turnstile secret.
|
||||
*/
|
||||
export async function submitForm({
|
||||
consentFieldName,
|
||||
data,
|
||||
payload,
|
||||
formId,
|
||||
data,
|
||||
turnstileToken,
|
||||
ip,
|
||||
maxPerMinute = 5,
|
||||
payload,
|
||||
consentFieldName,
|
||||
turnstileOnConfigError = 'allow',
|
||||
turnstileToken,
|
||||
}: 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 })) {
|
||||
return { reason: 'rate_limited', success: false }
|
||||
return { success: false, reason: 'rate_limited' }
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Turnstile — verify when a token is supplied; reject on failure.
|
||||
if (turnstileToken !== undefined) {
|
||||
const ok = await verifyTurnstile({ ip, onConfigError: turnstileOnConfigError, payload, token: turnstileToken })
|
||||
const ok = await verifyTurnstile({ token: turnstileToken, payload, ip, onConfigError: turnstileOnConfigError })
|
||||
if (!ok) {
|
||||
return { reason: 'turnstile', success: false }
|
||||
return { success: false, reason: 'turnstile' }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,14 +114,14 @@ export async function submitForm({
|
||||
const validation = await validateSubmission(payload, formId, data, consentFieldName)
|
||||
if (!validation.ok) {
|
||||
if (validation.reason === 'not_found') {
|
||||
return { reason: 'not_found', success: false }
|
||||
return { success: false, reason: 'not_found' }
|
||||
}
|
||||
if (validation.reason === 'consent') {
|
||||
return { field: validation.field, reason: 'consent', success: false }
|
||||
return { success: false, reason: 'consent', field: validation.field }
|
||||
}
|
||||
return {
|
||||
reason: 'validation',
|
||||
success: false,
|
||||
reason: 'validation',
|
||||
...(validation.field ? { field: validation.field } : {}),
|
||||
...(validation.kind ? { kind: validation.kind } : {}),
|
||||
}
|
||||
@@ -142,9 +140,9 @@ export async function submitForm({
|
||||
})),
|
||||
} as never,
|
||||
})
|
||||
return { submissionId: submission.id, success: true }
|
||||
return { success: true, submissionId: submission.id }
|
||||
} catch (err) {
|
||||
payload.logger.error(`[ipal] Form submission failed: ${(err as Error).message}`)
|
||||
return { reason: 'error', success: false }
|
||||
return { success: false, reason: 'error' }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user