Repository navigation
Protect newsletter signup with Turnstile - #114
Merged
Merged
Conversation
The signup form was only protected by a honeypot. Gate the existing Pages Function on a server-side siteverify result, and embed a managed widget that stays invisible unless Cloudflare asks for a challenge. Co-authored-by: Xavier Decuyper <hi@simplyexplained.com>
Deploying savjee-be with
|
| Latest commit: |
7c3b10e
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://56548ab7.savjee-be.pages.dev |
| Branch Preview URL: | https://cursor-newsletter-turnstile.savjee-be.pages.dev |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
Bugbot Autofix prepared a fix for the issue found in the latest run.
- ✅ Fixed: CSP blocks Turnstile widget
- Added https://challenges.cloudflare.com to script-src and frame-src in both the site-wide and /trivia Content-Security-Policy rules so the Turnstile script and iframe can load.
You can send follow-ups to the cloud agent here.
Reviewed by Cursor Bugbot for commit 7706fb2. Configure here.
The signup form loads challenges.cloudflare.com, and the fail-closed siteverify check rejects submissions when the browser blocks that script and iframe.
Savjee
marked this pull request as ready for review
October 18, 2026 06:00
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 this approach
The newsletter form only had a hidden honeypot, which is why it is getting hammered. I used managed Turnstile with
interaction-only. That stays invisible for a normal visitor and only shows a challenge when Cloudflare is unsure. A fully invisible widget has no fallback, so a real person who looks automated is rejected outright. Invisible mode also requires linking Cloudflare's Turnstile privacy addendum, and this site does not have a privacy policy page. Cloudflare recommends managed mode.The existing signup handler is unchanged after the check. A valid token still posts to the Google Chat webhook. Localhost still skips that webhook. The honeypot still returns a fake success.
What changed
data-action="signup",data-appearance="interaction-only") on the newsletter index, posts, and newsletter issues.functions/newsletter/signup.jscalls siteverify before any of that logic. It requiressuccess === true, actionsignup, and a hostname inTURNSTILE_HOSTNAMES. It fails closed on network errors, missing config, and Cloudflare's public testing keys.localhostor127.0.0.1./newsletter/signup/failedpage, which the function already redirected to.What you need to configure
I could not create the widget from this environment (no Cloudflare API token). Before the next production deploy, add a Managed widget for
simplyexplained.comandwww.simplyexplained.com, then set these Pages variables:TURNSTILE_SITEKEYTURNSTILE_SECRETTURNSTILE_HOSTNAMESsimplyexplained.com,www.simplyexplained.comDo not put
localhostin the production hostname list, and do not use a Turnstile testing secret in production. After the variables exist, redeploy so the sitekey is baked into the HTML. Until then the function rejects every signup, which stops the abuse and also blocks real subscribers.A preview URL changes every deploy, so preview signups stay rejected unless that exact hostname is on both the widget and the preview
TURNSTILE_HOSTNAMESvalue.Validation
npm testpassed on Node 22 and Node 18, including live siteverify calls that reject testing keys.TURNSTILE_SITEKEYomits the widget. A build with a sitekey renders one widget on the newsletter index, a post, and a newsletter issue, and none on the homepage.Note
Medium Risk
Changes the public signup path and fails closed without correct Turnstile secrets/hostnames, which can block real subscribers until Pages env is configured; misconfiguration is the main operational risk.
Reviewed by Cursor Bugbot for commit 7c3b10e. Bugbot is set up for automated code reviews on this repo. Configure here.