Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
@@ -1,17 +1,18 @@
|
||||
import type { CaptchaLogger } from "./types.js";
|
||||
import type { CaptchaLogger, CaptchaProvider } from "./types.js";
|
||||
|
||||
const DEFAULT_TIMEOUT_MS = 3_000;
|
||||
const SITEVERIFY_PATH = "/api/v1/pow/siteverify";
|
||||
const MCAPTCHA_SITEVERIFY_PATH = "/api/v1/pow/siteverify";
|
||||
|
||||
export type VerifyCaptchaArgs = {
|
||||
/** PoW solution token produced by the mCaptcha widget. */
|
||||
/** Challenge token produced by the captcha widget. */
|
||||
token: string | null | undefined;
|
||||
/** Public sitekey for the surface being protected. */
|
||||
sitekey: string | undefined;
|
||||
/** mCaptcha account secret (server-side only). */
|
||||
/** Server-side site/account secret. */
|
||||
secret: string | undefined;
|
||||
/** Base URL of the mCaptcha instance. */
|
||||
/** Base URL of the captcha instance. */
|
||||
instanceUrl: string | undefined;
|
||||
provider?: CaptchaProvider;
|
||||
/** Outage/misconfig policy: true (default) lets the request through. */
|
||||
failOpen?: boolean;
|
||||
logger?: CaptchaLogger;
|
||||
@@ -20,10 +21,15 @@ export type VerifyCaptchaArgs = {
|
||||
};
|
||||
|
||||
/**
|
||||
* Verifies an mCaptcha proof-of-work token against a self-hosted instance.
|
||||
* Verifies a captcha token against a self-hosted provider.
|
||||
*
|
||||
* POST {instanceUrl}/api/v1/pow/siteverify body: { token, key, secret }
|
||||
* response: { valid: boolean }
|
||||
* mCaptcha:
|
||||
* POST {instanceUrl}/api/v1/pow/siteverify body: { token, key, secret }
|
||||
* response: { valid: boolean }
|
||||
*
|
||||
* Cap:
|
||||
* POST {instanceUrl}/{sitekey}/siteverify body: { response, secret }
|
||||
* response: { success: boolean }
|
||||
*
|
||||
* Semantics:
|
||||
* - missing token -> false (real client failure, never fail-open)
|
||||
@@ -35,6 +41,7 @@ export async function verifyCaptchaToken({
|
||||
sitekey,
|
||||
secret,
|
||||
instanceUrl,
|
||||
provider = "mcaptcha",
|
||||
failOpen = true,
|
||||
logger,
|
||||
fetchImpl = fetch,
|
||||
@@ -49,43 +56,73 @@ export async function verifyCaptchaToken({
|
||||
hasSitekey: Boolean(sitekey),
|
||||
failOpen,
|
||||
},
|
||||
"mCaptcha not configured — applying fail policy",
|
||||
"captcha not configured — applying fail policy",
|
||||
);
|
||||
return failOpen;
|
||||
}
|
||||
|
||||
if (!token) return false;
|
||||
|
||||
const url = instanceUrl.replace(/\/+$/, "") + SITEVERIFY_PATH;
|
||||
const url = buildSiteverifyUrl({ provider, instanceUrl, sitekey });
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
||||
try {
|
||||
const res = await fetchImpl(url, {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ token, key: sitekey, secret }),
|
||||
body: JSON.stringify(buildSiteverifyBody({ provider, token, sitekey, secret })),
|
||||
signal: controller.signal,
|
||||
});
|
||||
if (!res.ok) {
|
||||
logger?.warn(
|
||||
{ op: "captcha.http_error", status: res.status, failOpen },
|
||||
"mCaptcha siteverify returned non-OK — applying fail policy",
|
||||
{ op: "captcha.http_error", provider, status: res.status, failOpen },
|
||||
"captcha siteverify returned non-OK — applying fail policy",
|
||||
);
|
||||
return failOpen;
|
||||
}
|
||||
const data = (await res.json()) as { valid?: boolean };
|
||||
return data.valid === true;
|
||||
const data = (await res.json()) as { valid?: boolean; success?: boolean };
|
||||
return provider === "cap" ? data.success === true : data.valid === true;
|
||||
} catch (err) {
|
||||
logger?.warn(
|
||||
{
|
||||
op: "captcha.error",
|
||||
provider,
|
||||
err: err instanceof Error ? err.message : String(err),
|
||||
failOpen,
|
||||
},
|
||||
"mCaptcha verification failed — applying fail policy",
|
||||
"captcha verification failed — applying fail policy",
|
||||
);
|
||||
return failOpen;
|
||||
} finally {
|
||||
clearTimeout(timeout);
|
||||
}
|
||||
}
|
||||
|
||||
function buildSiteverifyUrl({
|
||||
provider,
|
||||
instanceUrl,
|
||||
sitekey,
|
||||
}: {
|
||||
provider: CaptchaProvider;
|
||||
instanceUrl: string;
|
||||
sitekey: string;
|
||||
}): string {
|
||||
const base = instanceUrl.replace(/\/+$/, "");
|
||||
if (provider === "cap") return `${base}/${encodeURIComponent(sitekey)}/siteverify`;
|
||||
return base + MCAPTCHA_SITEVERIFY_PATH;
|
||||
}
|
||||
|
||||
function buildSiteverifyBody({
|
||||
provider,
|
||||
token,
|
||||
sitekey,
|
||||
secret,
|
||||
}: {
|
||||
provider: CaptchaProvider;
|
||||
token: string;
|
||||
sitekey: string;
|
||||
secret: string;
|
||||
}): Record<string, string> {
|
||||
if (provider === "cap") return { response: token, secret };
|
||||
return { token, key: sitekey, secret };
|
||||
}
|
||||
Reference in new issue
Block a user