Repository navigation
fix(auth): read OAuth credentials from the runtime env on Node - #4289
Conversation
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 detectedLatest commit: bd075ef 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 |
|
All contributors have signed the CLA ✍️ ✅ |
There was a problem hiding this comment.
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:
- 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. - The callback-route test uses
vi.stubEnv, which writes to bothprocess.envandimport.meta.env, so it would pass on main and does not prove the fix. - 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.
| if (platformEnv != null) return platformEnv; | ||
|
|
||
| if (typeof process !== "undefined") { | ||
| return { ...import.meta.env, ...process.env }; |
There was a problem hiding this comment.
[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.
| 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"); |
There was a problem hiding this comment.
[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.
| 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"; |
There was a problem hiding this comment.
[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.
|
I have read the CLA Document and I hereby sign the CLA |
There was a problem hiding this comment.
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.
Co-authored-by: emdashbot[bot] <273199577+emdashbot[bot]@users.noreply.github.com>
There was a problem hiding this comment.
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.
@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
upgrade-emdash
@emdash-cms/sandbox-workerd
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-ai-search
@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: |
danielmlr
left a comment
There was a problem hiding this comment.
Thanks for working on this!
- I think the changeset should tell Node users what to do when they upgrade. Today, if the OAuth credentials are in
.envwhen 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.envinto 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 withnode --env-file=.env ./dist/server/entry.mjs. It also says to rotate any secret that was in.envat build time if the build output was shared or stored. - The Node paragraph in
docs/src/content/docs/deployment/secrets.mdxonly names the Turnstile key. It'd be great to add the OAuth credentials there too, so the docs match the changeset. - Could you update the PR description? It still describes the
{ ...import.meta.env, ...process.env }merge and the route tests from the first version.
|
Thanks Daniel. Updated the changeset to call out the Node change (runtime env or |
There was a problem hiding this comment.
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.
|
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
left a comment
There was a problem hiding this comment.
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.
|
recheck |
|
Thanks for contributing to EmDash! 🎉 Join the EmDash Discord, then run |
On non-Cloudflare adapters
virtual:emdash/envexportsundefined, so the OAuth start and callback routes fell back toimport.meta.env. Vite replaces that with the build-time environment. Credentials injected intoprocess.envwhen a container starts were ignored, sign-in failed withprovider_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_SECRETand_TENANT_IDfrom 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].tsand[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 withvirtual:emdash/env,process.env, andimport.meta.env.DEV ? import.meta.env : {}. Production bundles never reference build-time values, so nothing is inlined.Comments in both routes,
generateEnvModuleandvirtual-modules.d.tsdescribe 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.mdxnow lists OAuth credentials in the Node paragraph.Follows on from #1736 / #763.
Type of change
Your connection to this 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.AI assistance disclosure
Screenshots / test output
No UI change. Reproduced on
demos/simple(Node adapter): built with no OAuth vars, then rannode dist/server/entry.mjswith dummy GitHub credentials. On main it redirects withprovider_not_configured; on this branch it redirects to GitHub's authorize URL.packages/core/tests/unit/auth/oauth-env.test.ts(resolveOAuthEnv):