Repository navigation
Send captcha check as json and delete captcha-bypass api routes - #154
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
The captcha on
/before-startwas shown but never enforced:isCaptchaValidsent a JSON body withContent-Type: application/x-www-form-urlencoded. Turnstile then can't find the secret and returnsmissing-input-secret, so every check failed. Production runs withCAPTCHA_ENABLEDunset, which skips the check entirely: aninitSessioncall with no captcha token gets "Invalid id token" rather than "Captcha token is required"./api/init-session(GET) still handed out a__sessioncookie 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:Content-Type: application/json.PUBLIC_TURNSTILE_SECRET_KEYtoTURNSTILE_SECRET_KEY, because thePUBLIC_prefix marks a variable as safe to bundle into client code.CAPTCHA_ENABLED=false. An unset or any other value enforces it.isCaptchaValid→passesCaptcha(token?), which now owns the on/off rule.initSessionmakes one call.error-codeswhen 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 callturnstile.reset(). Tokens are single-use, so a retry needs a fresh one.src/pages/api/init-session.ts,src/pages/api/remove-session.tsand theremoveSessionaction.src/lib/captcha.test.ts: covers the JSON request, fail-closed whenCAPTCHA_ENABLEDis unset, the"false"skip, and network failure.src/env.d.ts,README.md,docs/architecture.md,CLAUDE.md: replace the staleRECAPTCHA_*names and drop the removed files.Deploy steps (required)
Production runs with
CAPTCHA_ENABLEDunset, so this merge turns the captcha on.On Netlify, before this merge:
TURNSTILE_SECRET_KEYto the Turnstile secret key. Without it, every captcha is rejected and no one can start the survey.CAPTCHA_ENABLEDin production. If it is set, remove it.PUBLIC_TURNSTILE_SECRET_KEYif it exists.Env vars are baked in at build time, so any later change to them needs a redeploy.
Local dev: set
CAPTCHA_ENABLED=falsein.env.local.Testing
success: false(missing-input-secret), the new one returnssuccess: true.pnpm test: 188 passed.astro check: 0 errors, 0 warnings. ESLint clean./before-startwith the captcha solved and confirm it reaches/survey.GET /api/init-sessionreturns 404.Captcha rejected:.