Skip to content

fix(comments): read the Turnstile secret at runtime - #3622

Merged
ascorbic merged 2 commits into
emdash-cms:mainfrom
swissky:fix/turnstile-runtime-secret
Sep 29, 2026
Merged

ascorbic merged 2 commits into
emdash-cms:mainfrom
swissky:fix/turnstile-runtime-secret

Conversation

@swissky

@swissky swissky commented Sep 29, 2026

Copy link
Copy Markdown
Member

What does this PR do?

Comment Turnstile verification read its secret key from import.meta.env. Vite replaces import.meta.env at build time, so the key is fixed when the site is built:

  • If EMDASH_TURNSTILE_SECRET_KEY (or TURNSTILE_SECRET_KEY) was not present at build time, getTurnstileSecretKey() compiled to return "". A key added later with wrangler secret put or a container environment variable never switched verification on, so comment submissions were accepted without a Turnstile check.
  • If the key was present at build time, its value was written into the server bundle (dist/server/chunks/turnstile-*.mjs).

I confirmed both on a built emdash 1.0.1 site. getTurnstileSecretKey() now reads process.env only, the same rule config/secrets.ts follows for the other secrets.

Upgrade note for Node sites: a key kept only in a .env file used to reach the bundle at build time. Now it has to be in the server's process environment at runtime, because neither astro dev on Node nor @astrojs/node loads .env into process.env. The changeset and deployment/secrets.mdx say what to do (node --env-file=.env ./dist/server/entry.mjs, or set it in the host environment). On Cloudflare, astro dev passes .env and .dev.vars into the Worker's process.env, so nothing changes there.

The regression test builds turnstile.ts with Vite, imports the built module, and checks that the key set in process.env at runtime is returned. Vitest maps import.meta.env onto process.env, so a plain unit test passes on the old code. The Vite build test fails on it (it gets "").

Out of scope, possible follow-up: the OAuth routes fall back to import.meta.env for client secrets when there's no Cloudflare env.

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm test passes (or targeted tests for my change)
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)
  • User-visible strings in the admin UI are wrapped for translation (if applicable). Do not include messages.po changes except in translation PRs — a workflow extracts catalogs on merge to main.
  • I have added and reviewed the user-facing changeset (if this PR changes a published package)
  • New features link to an approved Discussion: https://github.com/emdash-cms/emdash/discussions/...
  • I have included screenshots below if this PR changes the UI

No admin strings or UI changes; this is a bug fix, so no Discussion.

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Cursor + Claude Opus 5.5

Screenshots / test output

Not applicable (no UI change).

pnpm --filter emdash exec vitest run tests/unit/comments/turnstile.test.ts
 ✓ getTurnstileSecretKey in a production bundle > reads a secret set only at runtime

The test covers EMDASH_TURNSTILE_SECRET_KEY, the TURNSTILE_SECRET_KEY fallback, and the unset case. With the old import.meta.env code it fails with expected '' to be 'runtime-secret'. Full core suite: 8,937 passed.

@changeset-bot

changeset-bot Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b7615e6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 12 packages
Name Type
emdash Patch
@emdash-cms/cloudflare Patch
@emdash-cms/plugin-test Patch
@emdash-cms/sandbox-workerd Patch
@emdash-cms/admin Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
create-emdash Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@3622

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@3622

@emdash-cms/auth-atproto

npm i https://pkg.pr.new/@emdash-cms/auth-atproto@3622

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@3622

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@3622

@emdash-cms/contentful-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/contentful-to-portable-text@3622

emdash

npm i https://pkg.pr.new/emdash@3622

create-emdash

npm i https://pkg.pr.new/create-emdash@3622

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@3622

@emdash-cms/plugin-cli

npm i https://pkg.pr.new/@emdash-cms/plugin-cli@3622

@emdash-cms/plugin-test

npm i https://pkg.pr.new/@emdash-cms/plugin-test@3622

@emdash-cms/plugin-types

npm i https://pkg.pr.new/@emdash-cms/plugin-types@3622

@emdash-cms/registry-client

npm i https://pkg.pr.new/@emdash-cms/registry-client@3622

@emdash-cms/registry-lexicons

npm i https://pkg.pr.new/@emdash-cms/registry-lexicons@3622

@emdash-cms/registry-loader

npm i https://pkg.pr.new/@emdash-cms/registry-loader@3622

@emdash-cms/registry-moderation

npm i https://pkg.pr.new/@emdash-cms/registry-moderation@3622

@emdash-cms/registry-verification

npm i https://pkg.pr.new/@emdash-cms/registry-verification@3622

@emdash-cms/sandbox-workerd

npm i https://pkg.pr.new/@emdash-cms/sandbox-workerd@3622

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@3622

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@3622

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@3622

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@3622

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@3622

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@3622

@emdash-cms/plugin-field-kit

npm i https://pkg.pr.new/@emdash-cms/plugin-field-kit@3622

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@3622

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@3622

commit: b7615e6

@emdashbot emdashbot Bot 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.

This is the right fix for a real security/correctness bug. Vite statically replaces import.meta.env at build time, so reading the Turnstile secret from there baked the build‑machine value into the server bundle and ignored runtime secrets set with wrangler secret put or container env vars. Switching getTurnstileSecretKey() to process.env aligns the comment Turnstile path with the existing config/secrets.ts implementation and the AGENTS.md convention that secrets read process.env only.

What I checked:

  • packages/core/src/comments/turnstile.ts now reads process.env.EMDASH_TURNSTILE_SECRET_KEY || process.env.TURNSTILE_SECRET_KEY || "", matching the prefixed/unprefixed fallback style used elsewhere.
  • public-submission.ts still calls getTurnstileSecretKey() at request time and fails closed when a secret is configured but verification fails.
  • The new regression test builds the module with Vite and asserts the runtime process.env value is returned; the PR description notes, and the code confirms, that this would have failed on the old import.meta.env path.
  • docs/src/content/docs/deployment/secrets.mdx and the changeset accurately describe the Node runtime requirement and give a concrete migration path (node --env-file=.env ./dist/server/entry.mjs).

I did not find any blocking issues. The only related systemic gap is that the OAuth routes still fall back to import.meta.env when no Cloudflare env is present; the author flags this as out of scope, and it is pre-existing rather than introduced here. It should be handled in a separate follow-up.

@emdashbot emdashbot Bot added review/approved Approved; no new commits since and removed review/needs-review No maintainer or bot review yet labels Sep 29, 2026
@swissky
swissky force-pushed the fix/turnstile-runtime-secret branch from 58a2efc to 7667250 Compare September 29, 2026 13:43
@github-actions github-actions Bot added review/needs-rereview New commits since the last review and removed review/approved Approved; no new commits since labels Sep 29, 2026
Comment thread .changeset/turnstile-runtime-secret.md Outdated

On Node, the key must now be in the server's process environment at runtime. If you only set it in a `.env` file, load it when starting the server (for example `node --env-file=.env ./dist/server/entry.mjs`) or set it in your host's environment; otherwise comments are accepted without a Turnstile check. If your server build output was shared or stored, rotate a key that was present at build time.

On Cloudflare, set the key as a Worker secret (`wrangler secret put EMDASH_TURNSTILE_SECRET_KEY`) rather than only as a Workers Builds build variable. The Worker needs `nodejs_compat` with a compatibility date of 2025-04-01 or later, or the `nodejs_compat_populate_process_env` flag, so secrets reach `process.env`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

All supported versions of Astro already set nodejs_compat, so it doesn't need this note

@swissky swissky added the bot:review Trigger an emdashbot code review on this PR label Sep 29, 2026
@swissky
swissky force-pushed the fix/turnstile-runtime-secret branch from 7667250 to 92fa659 Compare September 29, 2026 14:32

@emdashbot emdashbot Bot 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.

Re-review: this is still the right fix for the Turnstile runtime-secret bug and I don’t see any new issues.

The PR switches getTurnstileSecretKey() from import.meta.env to process.env, which matches the AGENTS.md rule that secrets must be read at runtime only. public-submission.ts already calls the function at request time, so built bundles no longer bake in the build-machine key and runtime secrets added via wrangler secret put/container env are now honored.

What I re-checked:

  • packages/core/src/comments/turnstile.ts now uses process.env with the same EMDASH_TURNSTILE_SECRET_KEY → TURNSTILE_SECRET_KEY → "" fallback order as before.
  • The regression test builds the module with Vite, imports the SSR bundle, and asserts the value comes from the runtime process.env. This correctly fails on the old import.meta.env code.
  • The changeset and docs/src/content/docs/deployment/secrets.mdx give a concrete Node migration path (node --env-file=.env ./dist/server/entry.mjs) and advise rotating a key that leaked into a shared build artifact.
  • No new admin UI strings, SQL changes, content-table queries, or logged-out route additions are introduced.

This remains a clean, scoped bug fix. The pre-existing OAuth import.meta.env fallback flagged in the prior review is still out of scope.

@emdashbot emdashbot Bot added review/approved Approved; no new commits since and removed bot:review Trigger an emdashbot code review on this PR review/needs-rereview New commits since the last review labels Sep 29, 2026
@ascorbic
ascorbic merged commit 0b9426e into emdash-cms:main Sep 29, 2026
45 checks passed
@emdashbot emdashbot Bot mentioned this pull request Sep 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants