Skip to content

fix(secrets): read site secrets from process.env only, never import.meta.env - #2140

Merged
ascorbic merged 1 commit into
emdash-cms:mainfrom
dchaudhari7177:fix/secrets-env-inlining-2139
Jul 22, 2026
Merged

ascorbic merged 1 commit into
emdash-cms:mainfrom
dchaudhari7177:fix/secrets-env-inlining-2139

Conversation

@dchaudhari7177

Copy link
Copy Markdown
Contributor

What does this PR do?

In an Astro production build, Vite statically replaces the bare import.meta.env expression in readDefaultEnv() (packages/core/src/config/secrets.ts) with an object literal built from the env loaded at build time. If EMDASH_ENCRYPTION_KEY (or any of the other five secrets the module reads) is present in .env when the operator builds:

  1. the secret is embedded in plaintext in dist/server/chunks/secrets-*.mjs and ships inside the Worker bundle, and
  2. because the chain was meta.X ?? proc.X, the inlined build-time value silently shadowed the real runtime secret set via wrangler 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 source resolveS3Config already uses, available on Node/container deployments and on Cloudflare Workers with Node compatibility). With no import.meta.env reference 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:

  • a regression test that resolveSecrets({ db }) picks up process.env when no explicit env map is passed
  • a source-hygiene test asserting the module never references import.meta.env in code (comments stripped), so the inlining vector can't be silently reintroduced
  • updated docstrings (readDefaultEnv, ResolveSecretsOptions.env) documenting why secrets are excluded from the import.meta.env convention
  • a changeset (patch, emdash) with operator guidance to rotate any key that shipped inside a bundle

Closes #2139

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 (not applicable)
  • I have added a changeset (if this PR changes a published package)
  • New features link to an approved Discussion (not applicable)

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Claude Code

Screenshots / test output

Test Files  1 passed (1)
     Tests  33 passed (33)

(packages/core/tests/unit/config/secrets.test.ts — 31 existing + 2 new; tsc --noEmit and oxlint clean on the touched files.)

Copilot AI review requested due to automatic review settings July 20, 2026 05:53
@changeset-bot

changeset-bot Bot commented Jul 20, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: c03aa48

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

This PR includes changesets to release 16 packages
Name Type
emdash Patch
@emdash-cms/cloudflare Patch
@emdash-cms/sandbox-workerd Patch
@emdash-cms/fixture-perf-site Patch
@emdash-cms/perf-demo-site Patch
@emdash-cms/cache-demo-site Patch
@emdash-cms/do-demo-site Patch
@emdash-cms/do-solo-demo-site Patch
@emdash-cms/admin Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
create-emdash 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

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions github-actions Bot added review/awaiting-author Reviewed; waiting on the author to respond area/core size/M labels Jul 20, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jul 20, 2026

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

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

@emdash-cms/auth

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

@emdash-cms/auth-atproto

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

@emdash-cms/blocks

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

@emdash-cms/cloudflare

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

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

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

emdash

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

create-emdash

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

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

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

@emdash-cms/plugin-cli

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

@emdash-cms/plugin-types

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

@emdash-cms/registry-client

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

@emdash-cms/registry-lexicons

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

@emdash-cms/registry-verification

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

@emdash-cms/sandbox-workerd

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

@emdash-cms/x402

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

@emdash-cms/plugin-ai-moderation

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

@emdash-cms/plugin-atproto

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

@emdash-cms/plugin-audit-log

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

@emdash-cms/plugin-color

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

@emdash-cms/plugin-embeds

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

@emdash-cms/plugin-field-kit

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

@emdash-cms/plugin-forms

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

@emdash-cms/plugin-webhook-notifier

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

commit: c03aa48

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

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:330

    AGENTS.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.ts no longer reads import.meta.env; readDefaultEnv now reads only process.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`.
    

@ascorbic ascorbic left a comment

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.

Good call. Thanks

@ascorbic
ascorbic merged commit c4d790e into emdash-cms:main Jul 22, 2026
48 checks passed
@emdashbot emdashbot Bot mentioned this pull request Jul 22, 2026
ascorbic added a commit that referenced this pull request Aug 19, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core review/awaiting-author Reviewed; waiting on the author to respond size/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Secrets read via bare import.meta.env are inlined into production server bundles and shadow runtime Worker secrets (EMDASH_ENCRYPTION_KEY et al.)

3 participants