Repository navigation
Description
Description
readDefaultEnv() in the secrets module (src/config/secrets.ts, shipped as dist/secrets-*.mjs) reads EMDASH_ENCRYPTION_KEY — and five other secrets (EMDASH_PREVIEW_SECRET, PREVIEW_SECRET, EMDASH_IP_SALT, EMDASH_AUTH_SECRET, AUTH_SECRET) — through the bare import.meta.env object, with a process.env fallback:
function readDefaultEnv() {
const meta = import.meta.env ?? {};
const proc = typeof process !== "undefined" && process.env ? process.env : {};
return {
EMDASH_ENCRYPTION_KEY: meta.EMDASH_ENCRYPTION_KEY ?? proc.EMDASH_ENCRYPTION_KEY,
// ... same pattern for the other five
};
}In an Astro production build (DEPLOY_TARGET=cloudflare with @astrojs/cloudflare), Vite statically replaces the bare import.meta.env expression with an object literal built from the env loaded at build time. If EMDASH_ENCRYPTION_KEY is present in .env when the operator builds — which is exactly the state a machine that runs emdash dev is in — two things happen:
- The key is embedded in plaintext in
dist/server/chunks/secrets-*.mjs(and@astrojs/cloudflareadditionally writes it todist/server/.dev.vars). It ships to Cloudflare inside the Worker bundle and lingers in any build artifact. - The inlined value takes precedence over the real runtime secret. Because the chain is
meta.X ?? proc.X, a key set properly viawrangler secret put EMDASH_ENCRYPTION_KEYis silently ignored — the deployed Worker encrypts plugin secrets under the developer's local dev key while the operator believes the production secret is in use.
The failure is completely silent: no warning, nothing in logs. The operator only finds out when they rotate "the" key, inspect key fingerprints, or grep their own bundle. It also quietly undermines the multi-key rotation story being reviewed in #1688 — the key actually in use isn't any of the ones the operator manages.
The docstring on readDefaultEnv says import.meta.env deliberately wins "so build-time substitutions are honored when present." That's the right call for public config, but for secrets it's inverted: a build-time substitution of a secret is precisely the case that should never win (and arguably should never exist).
Expected: runtime secrets configured on the deployment platform are the values actually used, and no secret value appears in build output.
Steps to reproduce
- Scaffold an EmDash project configured for Cloudflare (
@astrojs/cloudflare,DEPLOY_TARGET=cloudflare). - Put a key in
.env(throwaway generated for this report, never used anywhere):EMDASH_ENCRYPTION_KEY=emdash_enc_v1_3U363i_E4JWm5WsRrBPGHomuSYn6JZCEPIiwSHqKSKc DEPLOY_TARGET=cloudflare pnpm buildgrep -rn "emdash_enc_v1_" dist/→ the full key value is present indist/server/chunks/secrets-*.mjsanddist/server/.dev.vars.- Deploy that bundle with a different key set as a Worker secret → the Worker uses the inlined
.envkey, not the Worker secret (observable via the key fingerprint, or by the fact that values encrypted before/after deploy don't round-trip under the Worker secret).
Environment
- emdash version: 0.28.1
- astro: 7.1.1, @astrojs/cloudflare: 14.1.3
- Node.js version: 22.22.3 (pnpm 10.6.5)
- Runtime: Cloudflare Workers (build host: macOS)
Logs / error output
Transformed readDefaultEnv in the built server chunk (dist/server/chunks/secrets-kp760LlD_*.mjs) — note the inlined literal and that it wins over process.env:
}, { EMDASH_ENCRYPTION_KEY: "emdash_enc_v1_3U363i_E4JWm5WsRrBPGHomuSYn6JZCEPIiwSHqKSKc" }) ?? {};
const proc = typeof process !== "undefined" && process.env ? process.env : {};
return {
EMDASH_ENCRYPTION_KEY: meta.EMDASH_ENCRYPTION_KEY ?? proc.EMDASH_ENCRYPTION_KEY,
...$ grep -rl "emdash_enc_v1_3U363i" dist/
dist/server/.dev.vars
dist/server/chunks/secrets-kp760LlD_WVSrxe7u.mjs
Suggested fixes
In rough order of preference:
- Read secrets exclusively from
process.envat runtime. Never touchimport.meta.envfor the six secret fields — then nothing can be inlined. This also covers the CLI surface the docstring mentions: outside the bundleimport.meta.envis empty anyway, soprocess.envis already the effective source there. On Workers,process.envcarries Worker secrets undernodejs_compat. - Or use
astro:envserver secrets (envField.string({ context: "server", access: "secret" })), which Astro guarantees are runtime-only and never statically replaced. - At minimum, invert the precedence for secrets (
proc.X ?? meta.X) so a real runtime secret always beats a baked-in build-time value. This fixes the silent-wrong-key problem but still leaves the plaintext copy in the bundle, so (1) or (2) is preferable.
A related nicety: since keys are prefixed (emdash_enc_v1_), a post-build check that greps the output for the prefix pattern would make this class of leak fail loudly — we run exactly that as a local guard and it's cheap.
Happy to PR whichever direction you prefer.
Activity