Skip to content

fix(api): allow the Turnstile token header through CORS preflight - #1085

Merged
Ardnived merged 1 commit into
mainfrom
fix/cors-turnstile-header
Nov 5, 2026
Merged

Ardnived merged 1 commit into
mainfrom
fix/cors-turnstile-header

Conversation

@Ardnived

@Ardnived Ardnived commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Every Atlas registration and issue report fails in production, unsent. The widget posts them from the visitor's browser with an x-turnstile-token header, and cors.headers (src/payload.config.ts) appended only the live-preview secret to Payload's allow-list. The preflight answers without the token header, so the browser drops the POST before it leaves the page.
  • The visitor sees the captcha succeed, then "Something went wrong — the security check needs doing again" after Register (reported 2026-11-05 on sahajatlas.com/uk/london/1326/register).
  • Nothing reaches this server to log it. Production HTTP logs for the last 7 days show only OPTIONS /api/user-submissions 200, and no POST.
  • The fix adds the header to cors.headers. The name becomes one constant, TURNSTILE_TOKEN_HEADER (src/lib/turnstile/header.ts), read by both the CORS config and the write guard, so the two cannot drift apart.
  • WeMeditateWeb is unaffected: it forwards submissions server-side, where CORS does not apply.

How long: cors.headers has been unchanged since July (#579), and the widget has sent this header since sydevs/SahajAtlasWeb#184 (2026-08-31).

Refs sydevs/SahajAtlasWeb#259 (criterion 4: a test sign-up and report from the live site). The widget's misleading error copy and colour are fixed separately in SahajAtlasWeb.

Test results

  • tests/int/cors-config.int.spec.ts asserts the allow-list carries x-turnstile-token. I watched it fail against the old config (expected 'origin, x-requested-with, content-typ…' to contain 'x-turnstile-token'), and it passes with the fix.
  • tests/e2e/cors-preflight.e2e.spec.ts adds a case that replays the widget's own preflight (POST, authorization, content-type, x-turnstile-token) on /api/user-submissions against the preview.
  • tests/int/atlas-submission-routing.int.spec.ts and tests/int/client-origin-enforcement.int.spec.ts (the write paths that read the header) pass: 26 tests.
  • Lean gate .claude/skills/pr-prep/check.sh passes: lint, format, both typechecks, unit lane 239 files / 3,084 tests.

Manual verification

  1. On the PR preview: curl -si -X OPTIONS <preview>/api/user-submissions -H 'Origin: https://sahajatlas.com' -H 'Access-Control-Request-Method: POST' -H 'Access-Control-Request-Headers: authorization,content-type,x-turnstile-token'. Confirm that access-control-allow-headers includes x-turnstile-token.
  2. After deploy: register for a class on sahajatlas.com and send an issue report. Both should succeed, and a POST /api/user-submissions should appear in the Railway HTTP logs.

🤖 Generated with Claude Code

The Atlas widget sends every registration and issue report from the
visitor's browser with an `x-turnstile-token` header. `cors.headers`
appended only the live-preview secret to Payload's allow-list, so the
preflight answered without the token header and the browser dropped the
POST unsent. Every atlas registration and report has failed this way
since the widget started sending the header, with nothing in this
server's logs: production HTTP logs show only OPTIONS on
/api/user-submissions, no POST.

The header name moves to one constant, `TURNSTILE_TOKEN_HEADER`, read by
both the CORS config and the write guard, so the two cannot drift apart.
The int spec asserts the allow-list carries it (watched failing against
the old config), and the e2e preflight spec replays the widget's own
preflight against the preview.

Refs sydevs/SahajAtlasWeb#259

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@railway-app

railway-app Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

🚅 Deployed to the SahajCloud-pr-1085 environment in sahajcloud

Service Status Web Updated
SahajCloud ✅ Success (View Logs) Web Nov 5, 2026 at 11:13 pm UTC
1 service not affected by this PR
  • mailpit

@railway-app
railway-app Bot temporarily deployed to sahajcloud / SahajCloud-pr-1085 November 5, 2026 23:08 Destroyed
@sydevs-bot sydevs-bot added the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Nov 5, 2026
@sydevs-bot

sydevs-bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

🤖 no session running

recent dispatches
  • 23:18Z review-pr · attempt 1 · session · done
  • 23:18Z review-pr · attempt 1 · session

Written by the sydevs dispatcher. Sessions never edit this comment.

@sydevs-bot sydevs-bot moved this from Awaiting to Working in SYDevelopers Technical Roadmap Nov 5, 2026
@Ardnived
Ardnived merged commit 1062819 into main Nov 5, 2026
9 checks passed
@Ardnived
Ardnived deleted the fix/cors-turnstile-header branch November 5, 2026 23:18
@sydevs-bot sydevs-bot moved this from Working to Done in SYDevelopers Technical Roadmap Nov 5, 2026
@sydevs-bot sydevs-bot removed the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Nov 5, 2026
Ardnived added a commit to sydevs/SahajAtlasWeb that referenced this pull request Nov 5, 2026
…g answered (#365)

* fix(registration): blame the connection, not the captcha, when nothing answered

Every atlas registration failed in production because SahajCloud's CORS
preflight did not allow `x-turnstile-token` (fixed in sydevs/SahajCloud#1085).
The browser dropped the POST unsent, and the form told the visitor "the
security check needs doing again" — advice that could not help, since the
captcha had passed — in the brand blue a refusal uses rather than the
error colour.

The generic failure now renders as `danger`. Its sentence follows whether
anything answered: a request that got no HTTP response (offline, SahajCloud
unreachable, a rejected preflight) never reached the captcha check, so it
says `common.errors.offline` or `common.errors.server` by `classifyError`;
an uncoded response may have spent the token, so it keeps the "wait for the
check, then register again" sentence. Named refusals keep their blue alert.
The report form already split this way and needs no change.

A smoke case replays the widget's own preflight against the SahajCloud
origin the bundle names, so the cross-repo contract fails a lane rather
than every visitor. It is red against production until #1085 deploys.

Refs #259

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* docs(testing): the smoke lane is 18 cases with the preflight check, not 19

CI counted 18, the new preflight case included: the lane was 17 before it,
so the '18' in production-smoke.yml was already one ahead.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
sydevs-bot pushed a commit that referenced this pull request Nov 5, 2026
The smoke spec `cors-preflight.e2e.spec.ts` failed on
`expect(allowed).toContain('x-turnstile-token')`, reading back only
`…, x-sahajcloud-preview-secret` from the preview.

Nothing in this branch caused it, and nothing in this branch could fix it.
#1085 landed on main one commit ago, adding both the assertion and the
`TURNSTILE_TOKEN_HEADER` entry in `payload.config.ts` that satisfies it. The
two reach CI by different routes: Actions checks out the pull request's merge
ref, so the suite already ran main's new assertion, while Railway builds the
preview from the branch head, which predates the config change. The spec
therefore asked a server built without the header for that header.

Merging the base closes the gap — the next preview builds with the entry.

Refs #1084

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Mg9rjWtNAKQrTJVjeBpXuQ
Ardnived added a commit that referenced this pull request Nov 6, 2026
* fix(api): accept an issue report from an API client

Every atlas issue report was refused with "The following field is invalid:
System > Form". The form-builder plugin proves a submission's form exists
with `findByID` on the caller's req and no `select`; for an API client that
read trips the select-required client gate, the plugin catches the 400, and
reports the form as missing. Production logs show the gate's warning
immediately before each refusal. It was hidden until now behind the CORS
preflight that dropped every atlas POST (#1085).

`perTypeForm` now runs its own existence check: the same `findByID` on the
caller's req, so a client still cannot cite a form it may not read, with a
one-column `select` that satisfies the gate.

No spec could see this because the test harness registered `usagePlugin`
before `formsPlugin`, the reverse of payload.config.ts, so `forms` and
`user-submissions` never received the client read gates under test. The
harness now mirrors production, and a new case submits a report as the
atlas client through the write guard: it failed with the production error
before the fix and passes after.

Refs sydevs/SahajAtlasWeb#259

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(api): hand the plugin's form check a trusted req

The form-builder plugin already owns the form-exists check. Rebuilding it
here duplicated the plugin's refusal string, the `extractID` cast and a
bare `catch` that reported a dropped connection as a missing form.

`asTrustedReq` is this repo's opt-out for an internal read forwarding the
caller's `req` with no `select`, which five modules already use. The
plugin stays the single owner of its check, and origin enforcement still
applies to the forwarded read. Trade: the plugin's read passes no
`select` and runs at the default depth, so it is heavier than a
one-column one.

Restores the `src/collections/AGENTS.md` pointer the earlier docblock
rewrite dropped: supplying `validate` replaces Payload's default, which
is why `relationshipValidation` is composed at all.

Addresses #1089 (comment)
Addresses #1089 (comment)

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Mg9rjV2RHc4z2qB8cRpKq7

* fix(tests): assert the form refusal's own message

`toThrow(/Form/)` matched the wrapper, `The following field is invalid:
System > Form`, so it would pass on any refusal naming that field. The
case now reads `data.errors` and pins the field's own sentence, as
`user-submissions-create` and `forms-action-type` do.

This does not make the case discriminate the defect, and nothing can:
the plugin catches both a genuinely missing form and the select gate's
400, and returns the same sentence for each. The accept case beside it is
the regression test — reproduced against the old validator, it fails with
the production `System > Form`.

Addresses #1089 (comment)

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Mg9rjV2RHc4z2qB8cRpKq7

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>

This branch was successfully deployed

No deployments
sahajcloud / SahajCloud-pr-1085 — 6b5fad9e Deployed Nov 5, 2026 by railway-app[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

2 participants