Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(api): add timeout to Turnstile verification

(justin)randoneering2f226236ffe4
diff --git a/apps/api/src/utils/verify-turnstile.ts b/apps/api/src/utils/verify-turnstile.ts
index 7fc6e34d0..731901937 100644
--- a/apps/api/src/utils/verify-turnstile.ts
+++ b/apps/api/src/utils/verify-turnstile.ts
@@ -1,6 +1,8 @@
const TURNSTILE_VERIFY_URL =
"https://challenges.cloudflare.com/turnstile/v0/siteverify";
+const DEFAULT_TIMEOUT_MS = 5000;
+
export type TurnstileResult = { ok: true } | { ok: false; reason: string };
// Verifies a Cloudflare Turnstile token against the siteverify endpoint.
@@ -18,6 +20,10 @@ export async function verifyTurnstile(
return { ok: false, reason: "Captcha token missing." };
}
+ const parsed = parseInt(process.env.TURNSTILE_TIMEOUT_MS ?? "", 10);
+ const timeoutMs =
+ Number.isFinite(parsed) && parsed > 0 ? parsed : DEFAULT_TIMEOUT_MS;
+
const body = new URLSearchParams();
body.set("secret", secret);
body.set("response", token);
@@ -29,6 +35,7 @@ export async function verifyTurnstile(
const res = await fetch(TURNSTILE_VERIFY_URL, {
method: "POST",
body,
+ signal: AbortSignal.timeout(timeoutMs),
});
const data = (await res.json()) as {
success?: boolean;
@@ -43,6 +50,13 @@ export async function verifyTurnstile(
reason: `Captcha verification failed (${errorCodes}).`,
};
} catch (error) {
+ if (
+ error instanceof DOMException &&
+ (error.name === "TimeoutError" || error.name === "AbortError")
+ ) {
+ console.error("Turnstile verification timed out", error);
+ return { ok: false, reason: "Captcha verification timed out." };
+ }
console.error("Turnstile verification request failed", error);
return { ok: false, reason: "Captcha verification failed." };
}