Skip to content

feat(turnstile): treat Turnstile as essential — gate registration, and stop degrading to mailto - #184

Merged
sydevs-bot merged 3 commits into
mainfrom
claude/feat-turnstile-essential
Aug 31, 2026
Merged

sydevs-bot merged 3 commits into
mainfrom
claude/feat-turnstile-essential

Conversation

@sydevs-bot

@sydevs-bot sydevs-bot commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

Closes #182. Unblocks sydevs/SahajCloud#629, which is Blocked by: this.

This is a breaking change for hosts, deliberately. A page whose CSP blocks challenges.cloudflare.com used to get a working atlas with a degraded report form; it now gets an error screen and no atlas. The argument for that is the whole ticket: every write the widget makes is captcha-gated, registration included — so on such a page a visitor could browse classes and then silently fail to sign up for one, while the site's owner had no reason to think anything was wrong. Failing loudly is worse for five minutes and better thereafter.

The three pieces

1. The eager check. useTurnstileGuard starts the script load when FullInterface mounts and throws captcha-blocked if it cannot complete. Eager but non-blocking — your answer on the ticket — so nothing waits on a third-party script and only a negative verdict does anything.

2. The token moves to the header. createRegistration sends x-turnstile-token, which is where SahajCloud's write-guard reads it. The submit is gated on a solved challenge, and any refusal resets it — a token is single-use and the server redeems it before doing the work it gates, so re-sending it would fail for the rest of the form's life.

3. The mailto: is gone. It published [email protected] on every host page whose CSP blocked the challenge — the address in front of every scraper reading those pages, to solve a problem the host had caused.

Two judgement calls I'd like checked

captcha-blocked is a new ErrorKind, not a config message override. It needed a policy config cannot express: no report CTA, because the report form is captcha-gated too, so offering it hands the viewer a second form the same failure has already disabled. Exactly the call offline makes about the network, one layer along, and REPORTED_KINDS mirrors it.

It does retry, which looks wrong beside config. I set retry: false first; Fallbacks.test.tsx's "never leaves a viewer with no way out" invariant went red and was right to. probeTurnstile returns the same verdict for a CSP block and for a script request that simply failed on the network, and loadTurnstile clears its cached promise on failure — so a second attempt is a real one, and the transient half genuinely recovers. A CSP block re-fails identically, costing one press.

What the eager probe can and cannot see, and why the form keeps its blocked state

The probe is strictly weaker than a real render, and that gap is deliberate rather than overlooked.

Visible from a script load: no site key · a script-src refusing challenges.cloudflare.com · a script that loads without defining the API (a content blocker or enterprise proxy answering 200 with a stub).

Not visible: a frame-src blocking the challenge iframe, and a sitekey the embedding domain isn't registered for. Both only surface when turnstile.render actually runs.

So useTurnstile's own blocked status stays as the backstop, and ReportIssueForm keeps its "the security check couldn't load" banner over a disabled submit — that state is now rare rather than gone, and its job there is to explain the dead button rather than route around it. Neither check replaces the other.

The captcha_failed code is not in the synced type union, and cannot be yet

EventRegistrationErrorCode (synced from SahajCloud) carries the four state refusals the endpoint raises itself. captcha_failed is thrown a layer above it by the write-guard plugin, which gates every client-originated write.

It is also not in that union yet, and the ordering is why: #629 is blocked on this landing — the widget must send the header before the server may start requiring it — so no SahajCloud build emits the code today and a pnpm types:cms would not produce it. mutate.ts therefore declares RegistrationErrorCode = EventRegistrationErrorCode | 'captcha_failed', which is what lets the client be ready first. When a later re-sync folds it into the synced union, that extra member becomes redundant and can go; nothing breaks either way.

Deliberately kept out of REFUSAL_MESSAGE_KEYS, which is a total Record over the state codes — its totality is what makes a re-sync fail the build there, and folding in a code from a different layer would break that.

Specs, and each defect reintroduced to watch it fail

Per .claude/rules/tests.md, every new assertion was made to fail at the site that would really cause it, then restored.

src/hooks/use-turnstile-guard.test.tsx (jsdom — the verdict arrives from a promise after first paint, so SSR would assert the healthy frame and pass regardless):

Defect reintroduced Result
Guard never throws (the degradation this removes) 1 red
Console advice deleted 1 red
Guard blocks on the probe 4 red

CompactEmbedView.mount.test.tsx gains the fifth candidate for the four-bug pattern that file exists to catch — and the first whose cost is outside our own page. Reintroducing it as <>{children}<Expandable>{children}</Expandable></> (the interface mounting alongside the card, which is the shape it would really take): 2 red, the new case included.

mutate.test.ts gains the header assertion and the unsynced-code refusal.

Gate: lint · typecheck · typecheck:tests · 1460 unit (was 1455) · build · assert:css · assert:maps · ladle:build. pnpm size: embed 300.5/308 KiB, standalone 297.9/308 — identical to main, since the guard lands in the lazy interface chunk rather than the eager graph.

Host-facing docs

docs/embedding.md: challenges.cloudflare.com moves from "degrades" to fatal across the script-src, connect-src and frame-src rows; the load-bearing list goes from five entries to six; the troubleshooting row that described the mailto: degradation now describes the error screen. CHANGELOG.md leads with the breaking change and what to do about it.

Both reference [#184], which had to be written before this PR had a number — it landed on 184, so the guess held and no follow-up commit is needed. (#181 hit the same wrinkle less luckily: [#164] was already taken in that file by a PR carrying the number of the issue being closed.)

…op the mailto

Registration is what the atlas exists for, and a registration cannot be sent
without a Turnstile token. So a challenge that cannot load is a broken embed
rather than a form that degrades, and the widget now says so.

Three changes, in the order they matter:

- `useTurnstileGuard` loads the challenge as soon as `FullInterface` mounts and
  throws `captcha-blocked` if it cannot. Eager but non-blocking: nothing waits on
  the probe, so a healthy embed pays nothing and a broken one is loud without a
  visitor having to try to register first. Mounted with the INTERFACE, never the
  shell — a collapsed compact card must not inject a third-party script into a
  host's page for a button nobody pressed.
- `createRegistration` sends the solved token in the `x-turnstile-token` header,
  which is where SahajCloud's write-guard reads it (sydevs/SahajCloud#629). The
  form gates its submit on a solved challenge and resets it after any refusal —
  the token is single-use and the server redeems it before doing the work.
- The report form's `mailto:` degradation is gone. It published
  [email protected] on every host page whose CSP blocked the challenge.

`captcha-blocked` is a new `ErrorKind` rather than a `config` message override:
it withholds the report CTA, because the report form is captcha-gated too and
pointing a viewer at it would offer a second form the same failure disabled.
It DOES retry — a blocked script and a failed script request are one verdict
here, and the second recovers.

Closes #182.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Aug 30, 2026 •

Copy link
Copy Markdown

The Native story now renders a Turnstile challenge and a Register button that
stays disabled until it is solved. Say so, rather than leaving a reader to
wonder whether the button is broken — and note that `.env`'s always-passes test
key is what makes it solve itself in Ladle.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Aug 30, 2026 •

Copy link
Copy Markdown

Deploying sahajatlas with  Cloudflare Pages  Cloudflare Pages

Latest commit: 81393dc
Status: ✅  Deploy successful!
Preview URL: https://8125fb57.sahajatlas.pages.dev
Branch Preview URL: https://claude-feat-turnstile-essent.sahajatlas.pages.dev

View logs

@Ardnived Ardnived assigned sydevs-bot and unassigned Ardnived Aug 30, 2026
…stem narration

Review feedback on #184.

- `REFUSAL_MESSAGE_KEYS` now spans every code a `RegistrationRefusedError` can
  carry, so `captcha_failed` reads as one code among the rest rather than a
  parallel concept. The synced-union totality check survives as
  `STATE_MESSAGE_KEYS`, which the merged record spreads.
- `containerRef` -> `challengeRef` in `useTurnstile` and both call sites.
- Story descriptions drop the header/env/issue-number detail.
- Source comments narrating the removed `mailto` fallback are gone.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@sydevs-bot sydevs-bot assigned Ardnived and unassigned sydevs-bot Aug 31, 2026
@sydevs-bot

Copy link
Copy Markdown
Contributor Author

All six threads addressed and resolved; green on 81393dc. Back to you.

Five were documentation — story descriptions trimmed to what a designer sees, and the comments narrating the removed mailto fallback deleted. The sixth was your question about CAPTCHA_REFUSED, and the answer is that it was never a separate error type: it is one code on RegistrationRefusedError, and the instanceof catch is untouched. The code read otherwise, so I merged the two message maps into one lookup and left CAPTCHA_REFUSED named only where it genuinely differs — a refused captcha must not invalidate the cached event, because it says nothing about the event.

Nothing here changes behaviour. Same copy per refusal code, same generic fallback, same challenge reset, same disabled conditions.

The one thing I checked rather than assumed

Merging the maps risked losing the types:cms re-sync guard, which is the whole reason the state map is typed as a total Record over the synced union. So the merge keeps that half intact and spreads it:

const STATE_MESSAGE_KEYS: Record<EventRegistrationErrorCode, string> = { … }
const REFUSAL_MESSAGE_KEYS: Record<RegistrationErrorCode, string> = {
  ...STATE_MESSAGE_KEYS,
  captcha_failed: 'registration.captcha_retry',
}

Both halves verified by breaking them:

defect reintroduced tsc
captcha_failed dropped from the merged record TS2741 … 'Record<RegistrationErrorCode, string>'
event_full dropped from the state record TS2741 … 'Record<EventRegistrationErrorCode, string>'

The second naming EventRegistrationErrorCode is what says the re-sync guard survived.

Also in this push: containerRef → challengeRef across useTurnstile and both call sites, and one more stale sentence I found on my own — the networkMode: 'always' docblock still claimed the failure copy "carries the email address, which is a route that still works when ours doesn't".

Locally: lint, typecheck, typecheck:tests, 1460 unit specs. On CI all five checks green on 81393dc.

The judgement call from the original description still stands and still wants your eye: this PR makes a host page whose CSP blocks challenges.cloudflare.com fail loudly instead of degrading. That is the change rather than a side effect of it.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@Ardnived Ardnived 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.

Looks good, ready to merge

@Ardnived Ardnived assigned sydevs-bot and unassigned Ardnived Aug 31, 2026
@sydevs-bot
sydevs-bot merged commit 2472679 into main Aug 31, 2026
5 checks passed
@sydevs-bot
sydevs-bot deleted the claude/feat-turnstile-essential branch August 31, 2026 23:30
sydevs-bot pushed a commit that referenced this pull request Aug 31, 2026
Resolves the CHANGELOG.md link-definition conflict left by #184's squash
merge: both [#181] and [#184] definitions belong, in numeric order.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018oiV3fKDoPhKcjhX39LvWp
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.

feat(turnstile): treat Turnstile as essential — gate registration, and stop degrading to mailto

3 participants