Until a visitor passes a Cloudflare Turnstile check, app pages ("/" is
now Worker-first too) answer with a self-contained verification page and
/img covers with 403, so a browser loads nothing of the site before the
check. Passing sets an HttpOnly, host-only cookie (HMAC of its issue
time) good for BOT_GATE_TTL_HOURS (48). The page then reloads into the
link that was asked for, and the legal gate follows as before.
- /assets, /data (desktop/Android apps, CC BY reuse), robots, sitemap,
og image, /api/* and /admin* stay ungated.
- Verified bots pass with the token a zone Transform Rule adds
(x-fig-known-bot); clients that only claim to be a search crawler get
a 503, never the noindex page.
- Fails open: it needs BOT_GATE="on", the Turnstile keys, GATE_SECRET
and GATE_BOT_TOKEN; deleting GATE_SECRET switches it off at once.
- POST /api/verify: same-origin, RL_GATE (IPv4 / IPv6 /64), siteverify
with action "gate". GET reports the pass for the page and the SPA.
- turnstile.ts: siteverify shared with Suggest, which now also checks
the hostname and action (a token from one page can't be spent on the
other) and answers 503 when Turnstile is unreachable.
- SPA: a tab whose pass ran out re-checks when shown again or when a
cover fails, and reloads into the verification page.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
63 lines
2.3 KiB
TypeScript
63 lines
2.3 KiB
TypeScript
/**
|
|
* Cloudflare Turnstile's server-side check (siteverify), shared by the Suggest
|
|
* form and the verification gate. A token is single-use and valid for 300 s.
|
|
* The widget's `action` must match, so a token minted on one page cannot be
|
|
* spent on the other, and the hostname must be ours.
|
|
*/
|
|
|
|
const SITEVERIFY = 'https://challenges.cloudflare.com/turnstile/v0/siteverify'
|
|
|
|
type TurnstileAction = 'gate' | 'suggest'
|
|
|
|
/**
|
|
* - ok: a person passed the widget on our site, for this action.
|
|
* - rejected: the token is invalid, spent, or was issued elsewhere.
|
|
* - unavailable: siteverify could not answer (timeout, 5xx, bad secret).
|
|
*/
|
|
export type SiteverifyOutcome = 'ok' | 'rejected' | 'unavailable'
|
|
|
|
export interface SiteverifyInput {
|
|
secret: string
|
|
token: string
|
|
/** The visitor's IP (cf-connecting-ip), '' when unknown. */
|
|
ip: string
|
|
action: TurnstileAction
|
|
/** Hostname the widget must have run on (SITE_ORIGIN's). */
|
|
hostname: string
|
|
}
|
|
|
|
/** Turnstile's documented test secrets (1x… always passes, 2x… fails, 3x… "already spent"). */
|
|
export function isTestSecret(secret: string): boolean {
|
|
return /^[123]x0+AA$/.test(secret)
|
|
}
|
|
|
|
export async function siteverify(
|
|
input: SiteverifyInput,
|
|
fetcher: typeof fetch,
|
|
timeoutMs = 5000,
|
|
): Promise<SiteverifyOutcome> {
|
|
const form = new FormData()
|
|
form.set('secret', input.secret)
|
|
form.set('response', input.token)
|
|
if (input.ip) form.set('remoteip', input.ip)
|
|
let outcome: { success?: unknown; hostname?: unknown; action?: unknown; 'error-codes'?: unknown }
|
|
try {
|
|
const res = await fetcher(SITEVERIFY, { method: 'POST', body: form, signal: AbortSignal.timeout(timeoutMs) })
|
|
if (!res.ok) return 'unavailable'
|
|
outcome = await res.json()
|
|
} catch {
|
|
return 'unavailable'
|
|
}
|
|
if (outcome?.success !== true) {
|
|
const codes = Array.isArray(outcome?.['error-codes']) ? (outcome['error-codes'] as unknown[]) : []
|
|
if (codes.some((c) => c === 'missing-input-secret' || c === 'invalid-input-secret')) {
|
|
console.error('turnstile: the secret key was refused')
|
|
return 'unavailable'
|
|
}
|
|
return codes.includes('internal-error') ? 'unavailable' : 'rejected'
|
|
}
|
|
// The test secrets answer with placeholder hostname and action values.
|
|
if (isTestSecret(input.secret)) return 'ok'
|
|
return outcome.hostname === input.hostname && outcome.action === input.action ? 'ok' : 'rejected'
|
|
}
|