Skip to content

Add Cloudflare Turnstile verification to the contact form - #198

Merged
ianmuchyri merged 2 commits into
mainfrom
feat/contact-turnstile
Oct 19, 2026
Merged

ianmuchyri merged 2 commits into
mainfrom
feat/contact-turnstile

Conversation

@ianmuchyri

Copy link
Copy Markdown
Contributor

What

Adds a Cloudflare Turnstile check to the contact form so bot submissions are rejected before any email is sent.

  • contact.astro: renders the Turnstile widget (action contact) above the submit button, blocks submission until the check is complete, and resets the widget after each attempt since tokens are single-use. The sitekey is public and defaults to the production widget; PUBLIC_TURNSTILE_SITEKEY overrides it at build time.
  • api/contact.js: after field validation, verifies the token with Siteverify and only sends mail when it succeeds, the action is contact, and the hostname is in TURNSTILE_HOSTNAMES. Verification failures return 403; a missing secret or hostname list returns 503, so the form fails closed.
  • wrangler.jsonc: TURNSTILE_HOSTNAMES=absmach.eu,www.absmach.eu.
  • .dev.vars.example: TURNSTILE_SECRET and TURNSTILE_HOSTNAMES=localhost,127.0.0.1 for local dev.
  • eslint.config.mjs: allow the AbortSignal global in API routes (used for the 10s Siteverify timeout).

Deployment

  • TURNSTILE_SECRET must be set as a secret on the absmach-website Worker (already done).
  • Preview / *.workers.dev URLs are not in TURNSTILE_HOSTNAMES, so the form returns 403 there.

Testing

  • pnpm check and pnpm build pass.
  • Locally with wrangler dev: missing token and a token without the contact action are rejected with 403; the honeypot path is unchanged; the client blocks submission until the widget is completed; a real submission passes verification.

Signed-off-by: ianmuchyri <ianmuchiri8@gmail.com>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
absmach-website eed1bbc Commit Preview URL

Branch Preview URL
Oct 19 2026, 08:24 AM

Signed-off-by: ianmuchyri <ianmuchiri8@gmail.com>
@ianmuchyri
ianmuchyri merged commit 2952ea2 into main Oct 19, 2026
2 checks passed
@ianmuchyri
ianmuchyri deleted the feat/contact-turnstile branch October 19, 2026 08:27
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.

1 participant