Repository navigation
fix(secrets): read site secrets from process.env only, never import.meta.env - #2140
Conversation
🦋 Changeset detectedLatest commit: c03aa48 The changes in this PR will be included in the next version bump. This PR includes changesets to release 16 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-types
@emdash-cms/registry-client
@emdash-cms/registry-lexicons
@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.
The approach is correct and addresses the real vulnerability: Vite/Astro production builds replace the bare import.meta.env expression at build time, so any secret present in .env on the build machine was being baked into dist/server and shadowing runtime secrets. Switching readDefaultEnv to read only process.env removes the static-replacement target and aligns the secrets module with resolveS3Config, which already uses process.env.
I checked the changed module, its tests, the call-sites (emdash-runtime.ts, request-context middleware, preview/comment routes), resolveS3Config, the CLI surface, and the changeset. The implementation and the two new regression tests look sound.
One real gap: AGENTS.md still documents the old secrets convention (import.meta.env.EMDASH_X || import.meta.env.X || ""), while the PR removes it and the updated secrets.ts docstring now claims that convention is for public build-time config only. That contradiction is a convention violation and makes it likely the inlining bug gets reintroduced by a future contributor or agent. AGENTS.md needs to be updated as part of the same convention change.
Also worth noting outside this PR's diff: packages/core/src/comments/turnstile.ts still resolves EMDASH_TURNSTILE_SECRET_KEY via import.meta.env, so it has the same build-time-leak/shadowing problem. That's a follow-up security item, not a regression from this PR.
With AGENTS.md updated I'd approve; as-is the convention drift is a blocking-grade finding.
Findings
-
[needs fixing]
AGENTS.md:330AGENTS.md still documents the old secrets pattern that this PR removes:
- Secrets pattern: `import.meta.env.EMDASH_X || import.meta.env.X || ""`.
packages/core/src/config/secrets.tsno longer readsimport.meta.env;readDefaultEnvnow reads onlyprocess.env, and the updated docstring in that file claims the AGENTS.md convention is for public build-time config only. Leaving the convention file unchanged contradicts the code change and gives future contributors/agents the wrong signal, making it easy to reintroduce the exact build-time inlining bug this PR fixes.Update the Environment section to document the split convention:
- Use `import.meta.env.DEV` / `import.meta.env.PROD` (Vite/Astro standard). Never `process.env.NODE_ENV`. - Dev-only endpoints must check `import.meta.env.DEV` and return 403 otherwise -- it's a compile-time constant, unspoofable at runtime. - Public, build-time config pattern: `import.meta.env.EMDASH_X || import.meta.env.X || ""`. - Secrets pattern (must never use `import.meta.env`, which Vite inlines at build time): `process.env.EMDASH_X`.
The Environment section still taught the import.meta.env fallback chain as the 'secrets pattern'. Since #2140, secrets are read from process.env only — import.meta.env is statically inlined at build time, which bakes build-machine values into the bundle and shadows runtime secrets. The fallback chain remains correct for public build-time config, so the line is split rather than removed.
What does this PR do?
In an Astro production build, Vite statically replaces the bare
import.meta.envexpression inreadDefaultEnv()(packages/core/src/config/secrets.ts) with an object literal built from the env loaded at build time. IfEMDASH_ENCRYPTION_KEY(or any of the other five secrets the module reads) is present in.envwhen the operator builds:dist/server/chunks/secrets-*.mjsand ships inside the Worker bundle, andmeta.X ?? proc.X, the inlined build-time value silently shadowed the real runtime secret set viawrangler secret put— the deployed Worker encrypted plugin secrets under the developer's local dev key.This PR makes the secrets module read only
process.env(the same runtime sourceresolveS3Configalready uses, available on Node/container deployments and on Cloudflare Workers with Node compatibility). With noimport.meta.envreference left in the module, Vite has nothing to substitute, so no secret can be embedded in build output, and runtime-configured secrets are always the values actually used.Also included:
resolveSecrets({ db })picks upprocess.envwhen no explicit env map is passedimport.meta.envin code (comments stripped), so the inlining vector can't be silently reintroducedreadDefaultEnv,ResolveSecretsOptions.env) documenting why secrets are excluded from theimport.meta.envconventionemdash) with operator guidance to rotate any key that shipped inside a bundleCloses #2139
Type of change
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change)pnpm formathas been runAI-generated code disclosure
Screenshots / test output
(
packages/core/tests/unit/config/secrets.test.ts— 31 existing + 2 new;tsc --noEmitandoxlintclean on the touched files.)