Skip to content

Send captcha check as json and delete captcha-bypass api routes - #154

Merged
iMeriem merged 1 commit into
geeksblabla:masterfrom
makraz:fix/captcha-enforcement
Nov 28, 2026
Merged

iMeriem merged 1 commit into
geeksblabla:masterfrom
makraz:fix/captcha-enforcement

Conversation

@makraz

@makraz makraz commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

Why

The captcha on /before-start was shown but never enforced:

  • isCaptchaValid sent a JSON body with Content-Type: application/x-www-form-urlencoded. Turnstile then can't find the secret and returns missing-input-secret, so every check failed. Production runs with CAPTCHA_ENABLED unset, which skips the check entirely: an initSession call with no captcha token gets "Invalid id token" rather than "Captcha token is required".
  • /api/init-session (GET) still handed out a __session cookie with no captcha check. Nothing in the app calls it, but it is deployed, so enabling the captcha alone would not have stopped scripted submissions.

Changes

  • src/lib/captcha.ts:
    • Send Content-Type: application/json.
    • Rename the secret from PUBLIC_TURNSTILE_SECRET_KEY to TURNSTILE_SECRET_KEY, because the PUBLIC_ prefix marks a variable as safe to bundle into client code.
    • Fail closed: the captcha is required unless CAPTCHA_ENABLED=false. An unset or any other value enforces it.
    • Rename isCaptchaValid → passesCaptcha(token?), which now owns the on/off rule. initSession makes one call.
    • Log Turnstile error-codes when a token is rejected, so a missing secret shows up in the logs.
  • src/pages/before-start.astro: after an error, clear the token and call turnstile.reset(). Tokens are single-use, so a retry needs a fresh one.
  • Delete the unused src/pages/api/init-session.ts, src/pages/api/remove-session.ts and the removeSession action.
  • src/lib/captcha.test.ts: covers the JSON request, fail-closed when CAPTCHA_ENABLED is unset, the "false" skip, and network failure.
  • src/env.d.ts, README.md, docs/architecture.md, CLAUDE.md: replace the stale RECAPTCHA_* names and drop the removed files.

Deploy steps (required)

Production runs with CAPTCHA_ENABLED unset, so this merge turns the captcha on.

On Netlify, before this merge:

  1. Set TURNSTILE_SECRET_KEY to the Turnstile secret key. Without it, every captcha is rejected and no one can start the survey.
  2. Do not set CAPTCHA_ENABLED in production. If it is set, remove it.
  3. Remove the old PUBLIC_TURNSTILE_SECRET_KEY if it exists.

Env vars are baked in at build time, so any later change to them needs a redeploy.

Local dev: set CAPTCHA_ENABLED=false in .env.local.

Testing

  • Called Turnstile siteverify with Cloudflare's always-pass test secret: the old header returns success: false (missing-input-secret), the new one returns success: true.
  • pnpm test: 188 passed. astro check: 0 errors, 0 warnings. ESLint clean.
  • After deploy:
    • Start the survey from /before-start with the captcha solved and confirm it reaches /survey.
    • Confirm GET /api/init-session returns 404.
    • If the start fails, check the function logs for Captcha rejected:.
  • Deploy previews now enforce the captcha too. They work only if the preview hostname is allowed on the Turnstile site key.

@iMeriem iMeriem left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm

@iMeriem
iMeriem merged commit 60762b8 into geeksblabla:master Nov 28, 2026
1 check passed
@makraz
makraz deleted the fix/captcha-enforcement branch November 28, 2026 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants