Repository navigation
fix(comments): read the Turnstile secret at runtime - #3622
Conversation
🦋 Changeset detectedLatest commit: b7615e6 The changes in this PR will be included in the next version bump. This PR includes changesets to release 12 packages
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 |
@emdash-cms/admin
@emdash-cms/auth
@emdash-cms/auth-atproto
@emdash-cms/blocks
@emdash-cms/cloudflare
@emdash-cms/contentful-to-portable-text
emdash
create-emdash
@emdash-cms/gutenberg-to-portable-text
@emdash-cms/plugin-cli
@emdash-cms/plugin-test
@emdash-cms/plugin-types
@emdash-cms/registry-client
@emdash-cms/registry-lexicons
@emdash-cms/registry-loader
@emdash-cms/registry-moderation
@emdash-cms/registry-verification
@emdash-cms/sandbox-workerd
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-atproto
@emdash-cms/plugin-audit-log
@emdash-cms/plugin-color
@emdash-cms/plugin-embeds
@emdash-cms/plugin-field-kit
@emdash-cms/plugin-forms
@emdash-cms/plugin-webhook-notifier
commit: |
There was a problem hiding this comment.
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.tsnow readsprocess.env.EMDASH_TURNSTILE_SECRET_KEY || process.env.TURNSTILE_SECRET_KEY || "", matching the prefixed/unprefixed fallback style used elsewhere.public-submission.tsstill callsgetTurnstileSecretKey()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.envvalue is returned; the PR description notes, and the code confirms, that this would have failed on the oldimport.meta.envpath. docs/src/content/docs/deployment/secrets.mdxand 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.
58a2efc to
7667250
Compare
|
|
||
| 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`. |
There was a problem hiding this comment.
All supported versions of Astro already set nodejs_compat, so it doesn't need this note
7667250 to
92fa659
Compare
There was a problem hiding this comment.
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.tsnow usesprocess.envwith the sameEMDASH_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 oldimport.meta.envcode. - The changeset and
docs/src/content/docs/deployment/secrets.mdxgive 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.
What does this PR do?
Comment Turnstile verification read its secret key from
import.meta.env. Vite replacesimport.meta.envat build time, so the key is fixed when the site is built:EMDASH_TURNSTILE_SECRET_KEY(orTURNSTILE_SECRET_KEY) was not present at build time,getTurnstileSecretKey()compiled toreturn "". A key added later withwrangler secret putor a container environment variable never switched verification on, so comment submissions were accepted without a Turnstile check.dist/server/chunks/turnstile-*.mjs).I confirmed both on a built emdash 1.0.1 site.
getTurnstileSecretKey()now readsprocess.envonly, the same ruleconfig/secrets.tsfollows for the other secrets.Upgrade note for Node sites: a key kept only in a
.envfile used to reach the bundle at build time. Now it has to be in the server's process environment at runtime, because neitherastro devon Node nor@astrojs/nodeloads.envintoprocess.env. The changeset anddeployment/secrets.mdxsay what to do (node --env-file=.env ./dist/server/entry.mjs, or set it in the host environment). On Cloudflare,astro devpasses.envand.dev.varsinto the Worker'sprocess.env, so nothing changes there.The regression test builds
turnstile.tswith Vite, imports the built module, and checks that the key set inprocess.envat runtime is returned. Vitest mapsimport.meta.envontoprocess.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.envfor client secrets when there's no Cloudflare env.Type of change
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change)pnpm formathas been runmessages.pochanges except in translation PRs — a workflow extracts catalogs on merge tomain.No admin strings or UI changes; this is a bug fix, so no Discussion.
AI-generated code disclosure
Screenshots / test output
Not applicable (no UI change).
The test covers
EMDASH_TURNSTILE_SECRET_KEY, theTURNSTILE_SECRET_KEYfallback, and the unset case. With the oldimport.meta.envcode it fails withexpected '' to be 'runtime-secret'. Full core suite: 8,937 passed.