Skip to content

Protect newsletter signup with Turnstile - #114

Merged
Savjee merged 2 commits into
masterfrom
cursor/newsletter-turnstile-dce6
Oct 18, 2026
Merged

Savjee merged 2 commits into
masterfrom
cursor/newsletter-turnstile-dce6

Conversation

@Savjee

@Savjee Savjee commented Oct 17, 2026 •

Copy link
Copy Markdown
Owner

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

  • The signup box embeds Turnstile (data-action="signup", data-appearance="interaction-only") on the newsletter index, posts, and newsletter issues.
  • functions/newsletter/signup.js calls siteverify before any of that logic. It requires success === true, action signup, and a hostname in TURNSTILE_HOSTNAMES. It fails closed on network errors, missing config, and Cloudflare's public testing keys.
  • Production allowlists cannot mix in localhost or 127.0.0.1.
  • Missing /newsletter/signup/failed page, which the function already redirected to.
  • Tests cover rejection, a successful check, replay, the honeypot, and live calls to siteverify with Cloudflare's testing keys.

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.com and www.simplyexplained.com, then set these Pages variables:

Name Type Value
TURNSTILE_SITEKEY Plaintext, available at build Widget sitekey
TURNSTILE_SECRET Encrypted secret Widget secret
TURNSTILE_HOSTNAMES Plaintext simplyexplained.com,www.simplyexplained.com

Do not put localhost in 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_HOSTNAMES value.

Validation

  • npm test passed on Node 22 and Node 18, including live siteverify calls that reject testing keys.
  • A build without TURNSTILE_SITEKEY omits 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.
  • In the browser, the form shows no checkbox. Submitting with Cloudflare's invisible test widget still attaches a token, and the Pages Function rejects that testing key before the webhook.

newsletter_signup_turnstile_rejection.mp4

Open in Web Open in Cursor 

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.

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>
@cloudflare-workers-and-pages

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

Copy link
Copy Markdown

Deploying savjee-be with  Cloudflare Pages  Cloudflare Pages

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

View logs

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

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
Savjee marked this pull request as ready for review October 18, 2026 06:00
@Savjee
Savjee merged commit 369243b into master Oct 18, 2026
6 checks passed
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