opencode/packages/console/app/src/lib/hcaptcha.ts
Victor Navarro a62028dd2a
Some checks failed
check / typecheck (push) Waiting to run
deploy-files / deploy (push) Waiting to run
deploy-www / deploy (push) Waiting to run
deploy / deploy (push) Waiting to run
nix-eval / nix-eval (push) Waiting to run
nix-hashes / compute-hash (blacksmith-4vcpu-ubuntu-2404, x86_64-linux) (push) Waiting to run
nix-hashes / compute-hash (blacksmith-4vcpu-ubuntu-2404-arm, aarch64-linux) (push) Waiting to run
nix-hashes / compute-hash (macos-latest, aarch64-darwin) (push) Waiting to run
nix-hashes / update-hashes (push) Blocked by required conditions
publish / version (push) Waiting to run
publish / build-cli (push) Blocked by required conditions
publish / sign-cli-macos (push) Blocked by required conditions
publish / build-node-app-archive (push) Blocked by required conditions
publish / build-node-cli (map[bun_install_flags:--cpu=* host:blacksmith-4vcpu-windows-2025 target:windows-arm64]) (push) Blocked by required conditions
publish / build-node-cli (map[host:blacksmith-4vcpu-ubuntu-2404 target:linux-x64]) (push) Blocked by required conditions
publish / build-node-cli (map[host:blacksmith-4vcpu-ubuntu-2404-arm target:linux-arm64]) (push) Blocked by required conditions
publish / build-node-cli (map[host:blacksmith-4vcpu-windows-2025 target:windows-x64]) (push) Blocked by required conditions
publish / build-node-cli (map[host:macos-26 target:darwin-arm64]) (push) Blocked by required conditions
publish / sign-cli-windows (push) Blocked by required conditions
publish / build-electron (map[bun_install_flags:--cpu=* host:windows-2025 platform_flag:--win --arm64 target:aarch64-pc-windows-msvc]) (push) Blocked by required conditions
publish / build-electron (map[bun_install_flags:--os=darwin --cpu=arm64 host:macos-26 platform_flag:--mac --arm64 target:aarch64-apple-darwin]) (push) Blocked by required conditions
publish / build-electron (map[bun_install_flags:--os=darwin --cpu=x64 host:macos-26-intel platform_flag:--mac --x64 target:x86_64-apple-darwin]) (push) Blocked by required conditions
publish / build-electron (map[host:blacksmith-4vcpu-ubuntu-2404 platform_flag:--linux target:x86_64-unknown-linux-gnu]) (push) Blocked by required conditions
publish / build-electron (map[host:blacksmith-4vcpu-ubuntu-2404-arm platform_flag:--linux --arm64 target:aarch64-unknown-linux-gnu]) (push) Blocked by required conditions
publish / build-electron (map[host:blacksmith-4vcpu-windows-2025 platform_flag:--win target:x86_64-pc-windows-msvc]) (push) Blocked by required conditions
publish / publish (push) Blocked by required conditions
publish / notify-discord-v2 (push) Blocked by required conditions
storybook / storybook build (push) Waiting to run
test / affected packages (push) Waiting to run
test / unit (linux) (push) Waiting to run
test / unit (windows) (push) Waiting to run
test / build (linux) (push) Waiting to run
test / build (windows) (push) Waiting to run
test / e2e (linux, app-1) (push) Blocked by required conditions
test / e2e (linux, app-2) (push) Blocked by required conditions
test / e2e (linux, app-components) (push) Blocked by required conditions
test / e2e (linux, session-ui-components) (push) Blocked by required conditions
test / e2e (windows, app-1) (push) Blocked by required conditions
test / e2e (windows, app-2) (push) Blocked by required conditions
test / e2e (windows, app-components) (push) Blocked by required conditions
test / e2e (windows, session-ui-components) (push) Blocked by required conditions
test / e2e (linux) (push) Blocked by required conditions
test / e2e (windows) (push) Blocked by required conditions
containers / build (push) Has been cancelled
feat(console): add hCaptcha to enterprise form (#57439)
2026-10-20 17:50:03 +02:00

44 lines
1.7 KiB
TypeScript

import { Resource } from "@opencode/console-resource"
const TIMEOUT_MS = 3000
const CONFIG_ERRORS = new Set(["missing-input-secret", "invalid-input-secret", "sitekey-secret-mismatch"])
// Returns undefined when hCaptcha is unreachable so the caller can fail open.
export async function verifyHcaptcha(
token: string,
options: {
fetch?: (request: Request) => Promise<Response>
secret?: string
remoteip?: string
timeoutMs?: number
} = {},
): Promise<boolean | undefined> {
try {
const body = new URLSearchParams({ secret: options.secret ?? Resource.HCAPTCHA_SECRET_KEY.value, response: token })
if (options.remoteip) body.set("remoteip", options.remoteip)
const response = await (options.fetch ?? fetch)(
new Request("https://api.hcaptcha.com/siteverify", {
method: "POST",
body,
signal: AbortSignal.timeout(options.timeoutMs ?? TIMEOUT_MS),
}),
)
if (!response.ok) {
console.error("hCaptcha verification failed:", response.status)
return undefined
}
const data = (await response.json()) as { success?: unknown; "error-codes"?: unknown }
if (data.success === true) return true
const errors = Array.isArray(data["error-codes"]) ? data["error-codes"] : []
// A broken secret or site key is our misconfiguration, not a bot, so it fails open too.
if (errors.some((code) => CONFIG_ERRORS.has(code))) {
console.error("hCaptcha is misconfigured:", errors)
return undefined
}
console.warn("hCaptcha rejected token", { errors })
return false
} catch (error) {
console.error("hCaptcha verification unavailable:", error instanceof Error ? error.name : "unknown")
return undefined
}
}