Skip to content

fix(auth): read OAuth credentials from the runtime env on Node - #4289

Merged
danielmlr merged 5 commits into
emdash-cms:mainfrom
Antonio-Bennett:fix/oauth-node-runtime-env
Nov 7, 2026
Merged

danielmlr merged 5 commits into
emdash-cms:mainfrom
Antonio-Bennett:fix/oauth-node-runtime-env

Conversation

@Antonio-Bennett

@Antonio-Bennett Antonio-Bennett commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

On non-Cloudflare adapters virtual:emdash/env exports undefined, so the OAuth start and callback routes fell back to import.meta.env. Vite replaces that with the build-time environment. Credentials injected into process.env when a container starts were ignored, sign-in failed with provider_not_configured, and any credentials present at build time were baked into the server bundle.

What does this PR do?

We hit this running EmDash in a container on AWS ECS with Microsoft Entra login. ECS injects EMDASH_OAUTH_MICROSOFT_CLIENT_ID, _CLIENT_SECRET and _TENANT_ID from Secrets Manager at task start, and the admin showed "OAuth provider microsoft is not configured".

This adds packages/core/src/auth/oauth-env.ts, used by [provider].ts and [provider]/callback.ts:

  • resolveOAuthEnv(platformEnv, runtimeEnv, buildEnv, dev) is a pure function. A Cloudflare env is returned unchanged. Otherwise production returns only the runtime env, and dev merges build-time values underneath it, with runtime values winning.
  • getOAuthEnv() calls it with virtual:emdash/env, process.env, and import.meta.env.DEV ? import.meta.env : {}. Production bundles never reference build-time values, so nothing is inlined.

Comments in both routes, generateEnvModule and virtual-modules.d.ts describe this behavior. The changeset tells Node users how to supply credentials at runtime and to rotate secrets from shared builds, using the same wording as #3622. deployment/secrets.mdx now lists OAuth credentials in the Node paragraph.

Follows on from #1736 / #763.

Type of change

  • Bug fix
  • Feature (requires an accepted feature plan or RFC)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Your connection to this change

  • I use EmDash and ran into the problem this PR addresses
  • I maintain a tool, dependency, or service that EmDash uses, and this change concerns that integration
  • A Maintainer approved this specific change before I opened the PR

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 their merged design PR: https://github.com/emdash-cms/emdash/pull/...
  • I have included screenshots below if this PR changes the UI

AI assistance disclosure

  • This PR includes AI-generated code or text — model/tool: Grok CLI (grok-4.7). I reviewed the complete diff and reproduced and tested the fix myself.

Screenshots / test output

No UI change. Reproduced on demos/simple (Node adapter): built with no OAuth vars, then ran node dist/server/entry.mjs with dummy GitHub credentials. On main it redirects with provider_not_configured; on this branch it redirects to GitHub's authorize URL.

packages/core/tests/unit/auth/oauth-env.test.ts (resolveOAuthEnv):

  • returns the Cloudflare env unchanged
  • ignores build-time values in production
  • falls back to build-time values in dev, with runtime winning

On non-Cloudflare adapters virtual:emdash/env exports undefined, so the OAuth start and callback routes fell back to import.meta.env. Vite replaces that with the build-time environment, so credentials injected into process.env when a container starts were ignored and sign-in failed with provider_not_configured. Read them through a shared helper that keeps the Cloudflare env unchanged and otherwise lets process.env override the build-time values, so astro dev still sees credentials from .env.
@changeset-bot

changeset-bot Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: bd075ef

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

@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

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

Reading OAuth credentials at request time through a shared helper, getOAuthEnv(), is the right fix for the container-deployment bug. Both routes use it, and Cloudflare bindings keep precedence. Remaining concerns are the helper's production fallback and test strength. The reviewers disagreed on severity: claude-opus-5-5 approved with suggestions, while gpt-6.1-sol commented and marked two items as needs_fixing. Because one reviewer rated them needs_fixing, the verdict is request_changes, though it is advisory.

Headline points:

  1. The helper still spreads the bare import.meta.env, which can embed build-machine secrets in the bundle (#2139) and lets a build-time prefixed value outrank a runtime unprefixed alias.
  2. The callback-route test uses vi.stubEnv, which writes to both process.env and import.meta.env, so it would pass on main and does not prove the fix.
  3. The start-route test patches Vitest's private __vitest_worker__.metaEnv, which is fragile.

Reviewed by anthropic/claude-opus-5-5 and openai/gpt-6.1-sol.

Comment thread packages/core/src/auth/oauth-env.ts Outdated
if (platformEnv != null) return platformEnv;

if (typeof process !== "undefined") {
return { ...import.meta.env, ...process.env };

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.

[needs fixing] The helper spreads the bare import.meta.env into the production credential source. As readDefaultEnv in config/secrets.ts explains (#2139) and AGENTS.md's Environment section says, a production build replaces that expression with an object holding every build-time variable. That can embed build-machine OAuth secrets in the server bundle. Overlaying process.env fixes same-key precedence but leaves those values in place, and a runtime key that is absent silently falls back to the build-time credential.

There is also a precedence problem. The merge happens before envString walks its aliases. If a build-time .env has EMDASH_OAUTH_GITHUB_CLIENT_SECRET=old and runtime sets the unprefixed GITHUB_CLIENT_SECRET=new, the prefixed build-time value wins. The client ID and secret can also come from different sources.

Options: use only process.env on Node in production and limit the import.meta.env fallback to dev (import.meta.env.DEV). Alternatively, document node --env-file or a shell export for astro dev, as Turnstile does.

Severity disagreement: claude-opus-5-5 rated this a non-blocking suggestion because the behavior predates the PR. gpt-6.1-sol rated it needs_fixing because it conflicts with the runtime-only secrets convention.

Comment on lines +56 to +60
it("reads provider credentials from the runtime env on Node", async () => {
const db = await setupTestDatabase();
try {
vi.stubEnv("EMDASH_OAUTH_GOOGLE_CLIENT_ID", "runtime-client-id");
vi.stubEnv("EMDASH_OAUTH_GOOGLE_CLIENT_SECRET", "runtime-client-secret");

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.

[needs fixing] vi.stubEnv() sets both process.env and import.meta.env, so these credentials are visible even if the callback is reverted to the old cfEnv ?? import.meta.env code. The invalid_state assertion therefore passes on main and does not prove the fix. Unlike the start-route test, it never separates the two environments.

Fix: make the build-time env lack the credentials while the process env supplies them, or test getOAuthEnv directly. If you keep the test as is, rename it to match what it checks.

Severity disagreement: claude-opus-5-5 rated this a suggestion. gpt-6.1-sol rated it needs_fixing.

Comment on lines +107 to +117
const db = await setupTestDatabase();
const worker = (globalThis as { __vitest_worker__: { metaEnv: Record<string, unknown> } })
.__vitest_worker__;
const liveEnv = worker.metaEnv;
const previousClientId = process.env.EMDASH_OAUTH_GOOGLE_CLIENT_ID;
const previousClientSecret = process.env.EMDASH_OAUTH_GOOGLE_CLIENT_SECRET;
try {
// Vitest makes import.meta.env and process.env the same object, so freeze
// the build-time env here the way a production build does.
worker.metaEnv = { ...liveEnv };
process.env.EMDASH_OAUTH_GOOGLE_CLIENT_ID = "runtime-client-id";

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.

[suggestion] Reassigning globalThis.__vitest_worker__.metaEnv relies on a private Vitest internal. An upgrade could break it, or make it a silent no-op so the test passes for the wrong reason. A sturdier design is to have the helper take its sources, for example resolveOAuthEnv(platformEnv, buildEnv, runtimeEnv). Precedence could then be unit-tested with plain objects, and the route tests would only need to check that each route goes through the helper.

Production Node servers read OAuth credentials from process.env only, so build-machine values are never inlined into the server bundle. Precedence lives in a pure resolveOAuthEnv helper with plain-object tests replacing the route tests that depended on Vitest internals.
@Antonio-Bennett

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

@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 shared runtime credential reader remains the right approach for the Node/container sign-in failure. This follow-up fixes production credential selection and replaces the fragile Vitest-internal manipulation and ineffective callback test with direct source-precedence tests. Those test concerns are addressed.

I reviewed the follow-up diff, both route integrations, the helper and tests, and the changeset against the environment conventions. One part of the earlier concern remains: the build environment is still passed unconditionally, so excluding it from the returned credentials does not prevent build-time secrets from being embedded in the server artifact.

Static review only; tests and production bundle output were not run or inspected.


Follow-up review by openai/gpt-6.1-sol, scoped to the changes since the last review.

Comment thread packages/core/src/auth/oauth-env.ts Resolved
Co-authored-by: emdashbot[bot] <273199577+emdashbot[bot]@users.noreply.github.com>

@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 shared runtime credential reader remains the right approach for the Node/container OAuth failure. This follow-up addresses the remaining finding by guarding the build-environment argument with import.meta.env.DEV, allowing production dead-code elimination to remove it while preserving the development fallback.

I checked the follow-up diff, helper, source-precedence tests, both route integrations, and environment conventions. The earlier test concerns remain addressed; no outstanding findings or new defects were found in this change.

Static review only; tests and production bundle output were not run or inspected.


Follow-up review by openai/gpt-6.1-sol, scoped to the changes since the last review.

@danielmlr danielmlr added the ci:run Approve and re-run CI on the PR's latest commit label Oct 29, 2026
@github-actions github-actions Bot removed the ci:run Approve and re-run CI on the PR's latest commit label Oct 29, 2026
@pkg-pr-new

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

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

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

@emdash-cms/auth

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

@emdash-cms/auth-atproto

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

@emdash-cms/blocks

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

@emdash-cms/cloudflare

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

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

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

emdash

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

create-emdash

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

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

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

@emdash-cms/plugin-cli

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

@emdash-cms/plugin-test

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

@emdash-cms/plugin-types

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

@emdash-cms/registry-client

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

@emdash-cms/registry-lexicons

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

@emdash-cms/registry-loader

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

@emdash-cms/registry-moderation

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

@emdash-cms/registry-verification

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

upgrade-emdash

npm i https://pkg.pr.new/upgrade-emdash@4289

@emdash-cms/sandbox-workerd

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

@emdash-cms/x402

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

@emdash-cms/plugin-ai-moderation

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

@emdash-cms/plugin-ai-search

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-search@4289

@emdash-cms/plugin-atproto

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

@emdash-cms/plugin-audit-log

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

@emdash-cms/plugin-color

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

@emdash-cms/plugin-embeds

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

@emdash-cms/plugin-field-kit

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

@emdash-cms/plugin-forms

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

@emdash-cms/plugin-webhook-notifier

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

commit: bd075ef

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

Thanks for working on this!

  1. I think the changeset should tell Node users what to do when they upgrade. Today, if the OAuth credentials are in .env when the site is built, the values get built into the server, and sign-in works. A Docker build like the one in the Node deployment docs copies .env into the build, so it works like that too. After this change, those sites can't sign in with OAuth until the values are in the server's environment. The Turnstile fix in #3622 had the same situation, and its changeset wording would fit here. It says to set the values in the host's environment, or to start the server with node --env-file=.env ./dist/server/entry.mjs. It also says to rotate any secret that was in .env at build time if the build output was shared or stored.
  2. The Node paragraph in docs/src/content/docs/deployment/secrets.mdx only names the Turnstile key. It'd be great to add the OAuth credentials there too, so the docs match the changeset.
  3. Could you update the PR description? It still describes the { ...import.meta.env, ...process.env } merge and the route tests from the first version.

@emdashbot emdashbot Bot added review/needs-review No maintainer or bot review yet and removed review/needs-review No maintainer or bot review yet labels Oct 29, 2026
@github-actions github-actions Bot added review/awaiting-author Reviewed; waiting on the author to respond and removed review/needs-review No maintainer or bot review yet labels Oct 29, 2026
@github-actions github-actions Bot added review/needs-rereview New commits since the last review area/docs cla: needed and removed review/awaiting-author Reviewed; waiting on the author to respond cla: signed labels Nov 2, 2026
@Antonio-Bennett

Copy link
Copy Markdown
Contributor Author

Thanks Daniel. Updated the changeset to call out the Node change (runtime env or node --env-file=.env, rotate secrets from shared builds), reusing the Turnstile wording from #3622. Added OAuth credentials to the Node paragraph in secrets.mdx, and rewrote the description to match the current helper and tests.

@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 runtime credential reader remains the right fix for Node/container OAuth sign-in. This follow-up appropriately documents the runtime-only production behavior and the action existing .env deployments need to take.

I reviewed both documentation changes against the helper, route integrations, source-precedence tests, Turnstile reader, and changeset guidance. The earlier code and test findings remain addressed. The expanded changeset explains the failure, migration command, development fallback, and secret-rotation precaution; the deployment guide matches the implementation. No outstanding findings or new defects found.

Static review only; tests and production bundle output were not run or inspected.


Follow-up review by openai/gpt-6.1-sol, scoped to the changes since the last review.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

This PR can't be merged until the Contributor License Agreement is signed. The CLA comment above explains how to sign. If it is still unsigned 7 days after the PR was opened, the PR will be closed.

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

Thanks for sticking with this! The new changeset, docs and description are exactly what I was hoping for, so this looks great to me.

The only thing left is the CLA check. Your last commit used an email that isn't linked to your GitHub account, so the bot can't match it to your signature. The bot's comment has the steps to fix that.

@emdashbot emdashbot Bot added review/needs-rereview New commits since the last review and removed review/needs-rereview New commits since the last review labels Nov 5, 2026
@Antonio-Bennett

Copy link
Copy Markdown
Contributor Author

recheck

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

Thank you!

@emdashbot emdashbot Bot added review/needs-rereview New commits since the last review and removed review/needs-rereview New commits since the last review labels Nov 7, 2026
@github-actions github-actions Bot added review/approved Approved; no new commits since and removed review/needs-rereview New commits since the last review labels Nov 7, 2026
@danielmlr
danielmlr merged commit 19c4f28 into emdash-cms:main Nov 7, 2026
54 of 55 checks passed
@emdashbot

emdashbot Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Thanks for contributing to EmDash! 🎉

Join the EmDash Discord, then run /link to connect your GitHub account and claim the Contributor role.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants