/** * Finding: S-03 * * Cloudflare Turnstile is invisible for most humans and free. The widget gives the browser a * token; the server must verify it, otherwise the check is decorative. */ type SiteverifyResponse = { success: boolean; "error-codes"?: string[]; action?: string; hostname?: string }; export async function verifyTurnstile(token: string, ip: string, expectedAction: string): Promise { const secret = process.env.TURNSTILE_SECRET_KEY; if (!secret) throw new Error("TURNSTILE_SECRET_KEY is not configured"); const res = await fetch("https://challenges.cloudflare.com/turnstile/v0/siteverify", { method: "POST", body: new URLSearchParams({ secret, response: token, remoteip: ip }), signal: AbortSignal.timeout(4_000), }); if (!res.ok) return false; const data = (await res.json()) as SiteverifyResponse; return ( data.success && data.action === expectedAction && // token minted for *this* form, not replayed from another (data.hostname === "www.cynbit.in" || process.env.NODE_ENV !== "production") ); } /** * Zero-cost first layer. Bots fill every input and submit instantly; humans do neither. * Render the `website` input off-screen with tabIndex={-1}, autoComplete="off", aria-hidden. */ export function looksAutomated(input: { website?: string; formStartedAt: number }, minFillMs = 4_000): boolean { const now = Date.now(); const filledHoneypot = Boolean(input.website); const tooFast = now - input.formStartedAt < minFillMs; const clockNonsense = input.formStartedAt > now + 60_000; return filledHoneypot || tooFast || clockNonsense; }