Skip to content

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

Description

@sydevs-bot

The Atlas half of sydevs/SahajCloud#629, raised at your request on that ticket. Not started — this carries proposal, not approved.

sydevs/SahajCloud#629 adds turnstile: true to the registrations create policy, after which POST /api/events/:id/register requires a valid x-turnstile-token header. The flip is breaking for any client that does not send one, and #629 says to land this side first.

The original plan was "mirror the submission form (#147)". That reference does not exist yet — #147 is open and unimplemented, and there is no x-turnstile-token sender anywhere in the widget today. So this is written from scratch rather than mirrored, and it is also where a broader decision lands.

The decision this ticket carries

useTurnstile reports blocked when the challenge cannot load — no site key, a host CSP omitting challenges.cloudflare.com, or frame-src blocking the challenge iframe. Today exactly one caller handles that, and it degrades: ReportIssueForm swaps its submit button for mailto:[email protected].

The decision recorded on #629 is that Turnstile is an essential requirement for the embed to function, and that the degradation goes away:

If it does not load due to the CSP the entire widget should throw an error which triggers the ErrorFallback. This error should explain the issue and reference the embedding.md document on how to fix it. We should also remove the degradation from the report issue form. We don't want to actually expose our email address.

That is a deliberate trade and worth naming plainly: it converts an out-of-date host CSP from "the report form degrades" into "this embed does not work", which is louder and gets fixed, where the quiet version has a host believing their page is fine.

Decided: load eagerly, but asynchronously

Settling the open question this ticket previously carried — neither of the two shapes it offered, but a third:

Yes, we should load Turnstile eagerly to ensure that it's working. Registration is the ultimate KPI for this widget, so if registration is not possible than the widget is non-functional and this needs to be raised early and fast so that the website manager can address it. However, it doesn't strictly have to load before render. We can assume that this will usually always work and optimize for that use case, so we can load it async and then raise an error if it fails.

So: start the load without waiting for it. The challenge is fetched as soon as the interface mounts rather than when a form needs it, nothing blocks first paint, and a blocked verdict raises the widget-level error whenever it arrives. The optimistic path costs nothing; the broken path is still loud, and is reported without a visitor having to try to register first.

⚠ "Eagerly" must mean when the interface mounts, not when the shell does. .claude/rules/components.md is explicit that anything which fetches or injects a script has to mount with FullInterface, never with AppShell — a collapsed compact card renders one button, and four separate bugs (the home-region redirect, warmCaches, and both Fathom effects) came from effects firing there. Injecting a third-party challenge script into a host's page for a card nobody has pressed would be the fifth. A framed or unexpanded embed therefore stays quiet until the interface opens; that is a narrowing of "at boot", not a contradiction of it. Say if you want it truly at boot instead — it is reachable, it just needs that rule amended rather than ignored.

Scope

  • Registration form sends the token. Turnstile widget in RegistrationForm; solved token in the x-turnstile-token header on createRegistration (src/config/api/mutate.ts), not the body.
  • captcha_failed resets the challenge and lets the reader retry, rather than surfacing as a generic failure.
  • Turnstile loads eagerly and asynchronously with FullInterface, and a blocked verdict throws an atlasError whose copy explains that the host's CSP must allow challenges.cloudflare.com and points at docs/embedding.md. Nothing blocks first paint; a collapsed compact card loads nothing.
  • Remove the mailto: degradation from ReportIssueForm (ReportIssueForm.tsx:303), and the story case and spec that assert it (ReportIssueForm.stories.tsx:79, ReportIssueForm.test.tsx:98). CONTACT_EMAIL likely loses its last consumer.
  • docs/embedding.md + CHANGELOG.md in the same PR. The CSP table currently scopes challenges.cloudflare.com to "loaded lazily when a viewer opens the report-issue form"; it becomes a hard requirement for the whole widget, and no longer lazy — the single most host-visible change here.
  • Ladle story + specs for the registration challenge and for the blocked path. Include a spec asserting a collapsed card injects nothing, alongside the existing ones in CompactEmbedView.mount.test.tsx.
  • Check the eager load against the pnpm size eager-payload budget before pushing.

Related

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

Activity

  1. Ardnived commented on Aug 29, 2026

    @Ardnived
    Contributor

    @sydevs-bot Yes, we should load Turnstile eagerly to ensure that it's working. Registration is the ultimate KPI for this widget, so if registration is not possible than the widget is non-functional and this needs to be raised early and fast so that the website manager can address it. However, it doesn't strictly have to load before render. We can assume that this will usually always work and optimize for that use case, so we can load it async and then raise an error if it fails.

  2. sydevs-bot commented on Aug 29, 2026

    @sydevs-bot
    ContributorAuthor

    Recorded — scope and the decision section now say eager-but-async, and the open question is gone.

    One narrowing I've written in, which you should overturn if it's wrong: "eagerly" now means when FullInterface mounts, not when the widget boots. .claude/rules/components.md forbids a collapsed compact card from fetching or injecting anything — that rule exists because four separate bugs (the home-region redirect, warmCaches, and both Fathom effects) all came from effects firing on the shell. Loading a third-party challenge script into a host's page for a card nobody has pressed would be the same bug again.

    The cost is that a sidebar embed nobody expands reports nothing — but it also registers nobody, so the KPI argument for loading early doesn't apply to it. If you want it truly at boot, that's reachable; it needs that rule amended rather than quietly ignored, so I'd want you to say so.

    Still proposal — I haven't started it and won't without approved. Everything above is ticket-writing, not code.

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


    Generated by Claude Code

  3. sydevs-bot commented on Aug 30, 2026

    @sydevs-bot
    ContributorAuthor

    Built and open for review as #184 — all five checks green. Every scope item is done.

    One deliberate deviation from the ticket, which you should overturn if I've read it wrong. The ticket says the error copy should "explain that the host's CSP must allow challenges.cloudflare.com and points at docs/embedding.md". I split that across two audiences instead of putting it all on screen:

    • The viewer gets "This Atlas can't run on this page: its security check was blocked." — it says the embed is broken without asking them to do something they cannot do. A visitor on a meditation page cannot edit a CSP, and a directive aimed at them is noise.
    • The site's developer gets the full directive on the browser console — both script-src and frame-src, plus the docs/embedding.md link — via reportIntegrationWarning, which is the existing channel for host-side integration mistakes. That is where somebody debugging the embed is actually looking.

    The doc link and the exact directive both ship, in other words; they just don't ship to the wrong person. If you'd rather the on-screen copy carried them too, that's a one-line copy change across ten locales.

    Scope, item by item
    Scope item Where
    Registration sends the token in x-turnstile-token mutate.ts — init.headers, merged over the SDK's baseInit so auth and locale still ride alongside
    captcha_failed resets and lets the reader retry RegistrationForm — resets on any refusal, since the token is spent either way
    Eager + async load with FullInterface; blocked throws use-turnstile-guard.ts, called from FullInterface — never AppShell
    mailto: removed from the report form ReportIssueForm.tsx — CONTACT_EMAIL gone, story case and spec rewritten
    docs/embedding.md + CHANGELOG.md Both, in the same PR. The CSP row moves from "degrades" to fatal; the load-bearing list goes 5 → 6
    Stories + specs, incl. the collapsed-card assertion CompactEmbedView.mount.test.tsx gains the Turnstile case; use-turnstile-guard.test.tsx is new
    pnpm size checked embed 300.5/308 KiB, standalone 297.9/308 — unchanged from main

    Two things worth knowing that the ticket didn't anticipate, both explained at length on the PR:

    captcha-blocked became its own ErrorKind rather than a config variant, because it needs to offer no report CTA — the report form is captcha-gated too, so pointing a viewer at it hands them a second form the same failure disabled.

    And it does offer a retry, which looks wrong beside config until you notice the probe cannot tell a CSP block from a script request that failed on the network. The second recovers; the first costs one press. Fallbacks.test.tsx's "never leaves a viewer with no way out" invariant is what caught my first attempt at retry: false.

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


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Fields

Priority

None yet

Hold Until

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions