Repository navigation
fix(core): read OAuth env via virtual module, not locals.runtime.env - #1845
Conversation
🦋 Changeset detectedLatest commit: d833b18 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: |
|
Can confirm that this solves the issue |
There was a problem hiding this comment.
This is the right fix for the right problem. Astro 6 removed Astro.locals.runtime.env, and EmDash’s existing optional-chaining fallback could not catch a throwing getter. Adding a virtual:emdash/env module mirrors the already-established virtual:emdash/wait-until pattern, keeps core adapter-agnostic, and restores the import.meta.env fallback on Node builds. Both affected OAuth routes are updated consistently.
I reviewed the diff and the changed files statically; I did not run tests or builds. The changeset is present and correctly targets the emdash package, and there are no SQL, localization, RTL, authorization, or locale-filtering concerns in this server-only change.
No blocking issues. I have two minor suggestions around test hygiene and coverage symmetry, both non-blocking.
| describe("OAuth start route (#1736)", () => { | ||
| it("does not throw when locals.runtime.env is a throwing getter (Astro 6+ Cloudflare)", async () => { | ||
| const request = new Request("http://localhost:4321/_emdash/api/auth/oauth/google"); | ||
|
|
||
| const response = await startOAuth({ | ||
| params: { provider: "google" }, | ||
| request, | ||
| locals: makeThrowingRuntimeLocals(), | ||
| redirect: (url: string) => new Response(null, { status: 302, headers: { Location: url } }), | ||
| } as unknown as Parameters<typeof startOAuth>[0]); | ||
|
|
||
| // No credentials configured in this test env -- the route should | ||
| // reach the provider-configured check (not crash into the generic | ||
| // oauth_error path the old code hit when locals.runtime.env threw). | ||
| const location = response.headers.get("Location") ?? ""; | ||
| expect(location).toContain("error=provider_not_configured"); | ||
| expect(location).not.toContain("error=oauth_error"); | ||
| }); | ||
|
|
||
| it("reads provider credentials from the virtual:emdash/env module on Cloudflare", async () => { | ||
| vi.resetModules(); | ||
| vi.doMock( | ||
| "virtual:emdash/env", | ||
| () => ({ | ||
| env: { | ||
| EMDASH_OAUTH_GOOGLE_CLIENT_ID: "test-client-id", | ||
| EMDASH_OAUTH_GOOGLE_CLIENT_SECRET: "test-client-secret", | ||
| }, | ||
| }), | ||
| { virtual: true }, | ||
| ); | ||
| const { GET: startOAuthWithEnv } = | ||
| await import("../../../src/astro/routes/api/auth/oauth/[provider].js"); | ||
|
|
||
| const request = new Request("http://localhost:4321/_emdash/api/auth/oauth/google"); | ||
| const response = await startOAuthWithEnv({ | ||
| params: { provider: "google" }, | ||
| request, | ||
| locals: { emdash: { db: {} as never, config: {} } }, | ||
| redirect: (url: string) => new Response(null, { status: 302, headers: { Location: url } }), | ||
| } as unknown as Parameters<typeof startOAuth>[0]); | ||
|
|
||
| // Credentials found -- the route proceeds past the provider-configured | ||
| // check into building the real authorization URL (which then fails on | ||
| // the unmocked DB, but NOT with provider_not_configured or oauth_error | ||
| // from the env-reading step itself). | ||
| const location = response.headers.get("Location") ?? ""; | ||
| expect(location).not.toContain("error=provider_not_configured"); | ||
|
|
||
| vi.doUnmock("virtual:emdash/env"); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
[suggestion] The vi.doMock("virtual:emdash/env") and vi.resetModules() in the second test are not cleaned up after the file finishes. If more tests are added later (here or in another file sharing the same worker), they may see the mocked Cloudflare env instead of the Vitest stub.
Use an afterEach hook to reset module state and unmock the virtual module after each test:
| describe("OAuth start route (#1736)", () => { | |
| it("does not throw when locals.runtime.env is a throwing getter (Astro 6+ Cloudflare)", async () => { | |
| const request = new Request("http://localhost:4321/_emdash/api/auth/oauth/google"); | |
| const response = await startOAuth({ | |
| params: { provider: "google" }, | |
| request, | |
| locals: makeThrowingRuntimeLocals(), | |
| redirect: (url: string) => new Response(null, { status: 302, headers: { Location: url } }), | |
| } as unknown as Parameters<typeof startOAuth>[0]); | |
| // No credentials configured in this test env -- the route should | |
| // reach the provider-configured check (not crash into the generic | |
| // oauth_error path the old code hit when locals.runtime.env threw). | |
| const location = response.headers.get("Location") ?? ""; | |
| expect(location).toContain("error=provider_not_configured"); | |
| expect(location).not.toContain("error=oauth_error"); | |
| }); | |
| it("reads provider credentials from the virtual:emdash/env module on Cloudflare", async () => { | |
| vi.resetModules(); | |
| vi.doMock( | |
| "virtual:emdash/env", | |
| () => ({ | |
| env: { | |
| EMDASH_OAUTH_GOOGLE_CLIENT_ID: "test-client-id", | |
| EMDASH_OAUTH_GOOGLE_CLIENT_SECRET: "test-client-secret", | |
| }, | |
| }), | |
| { virtual: true }, | |
| ); | |
| const { GET: startOAuthWithEnv } = | |
| await import("../../../src/astro/routes/api/auth/oauth/[provider].js"); | |
| const request = new Request("http://localhost:4321/_emdash/api/auth/oauth/google"); | |
| const response = await startOAuthWithEnv({ | |
| params: { provider: "google" }, | |
| request, | |
| locals: { emdash: { db: {} as never, config: {} } }, | |
| redirect: (url: string) => new Response(null, { status: 302, headers: { Location: url } }), | |
| } as unknown as Parameters<typeof startOAuth>[0]); | |
| // Credentials found -- the route proceeds past the provider-configured | |
| // check into building the real authorization URL (which then fails on | |
| // the unmocked DB, but NOT with provider_not_configured or oauth_error | |
| // from the env-reading step itself). | |
| const location = response.headers.get("Location") ?? ""; | |
| expect(location).not.toContain("error=provider_not_configured"); | |
| vi.doUnmock("virtual:emdash/env"); | |
| }); | |
| }); | |
| describe("OAuth start route (#1736)", () => { | |
| afterEach(() => { | |
| vi.doUnmock("virtual:emdash/env"); | |
| vi.resetModules(); | |
| }); | |
| it("does not throw when locals.runtime.env is a throwing getter (Astro 6+ Cloudflare)", async () => { |
| @@ -115,11 +115,18 @@ export const GET: APIRoute = async ({ params, request, locals, session, redirect | |||
| } | |||
|
|
|||
| try { | |||
There was a problem hiding this comment.
[suggestion] The callback route has the same virtual:emdash/env change as the start route, but only the start route has a new regression test. Consider adding a mirrored test (e.g. oauth-callback-route.test.ts) that exercises the callback route with a throwing locals.runtime.env getter and verifies it reaches provider_not_configured rather than oauth_error. This closes the symmetry gap and prevents the callback path from silently regressing if someone reintroduces locals.runtime access.
Overlapping PRsThis PR modifies files that are also changed by other open PRs: This may cause merge conflicts or duplicated work. A maintainer will coordinate. |
…1736) Astro 6 removed Astro.locals.runtime.env -- accessing it now throws rather than returning undefined, so the existing `locals.runtime?.env ?? import.meta.env` fallback never reached import.meta.env: the throw propagated into the route's outer try/catch and surfaced as a generic "oauth_error", masking the real provider_not_configured diagnosis. Both OAuth routes (start + callback) hit this; passkey auth was unaffected since it doesn't read this env. Added a `virtual:emdash/env` module (mirrors the existing `virtual:emdash/wait-until` pattern exactly): re-exports Cloudflare's `env` from `cloudflare:workers` under that adapter, `undefined` otherwise. Core stays adapter-agnostic this way -- a direct `cloudflare:workers` import would fail to resolve on a Node build. Both routes now read through it instead of `locals.runtime`. Co-Authored-By: Claude Opus 4.8 ultracode <noreply@anthropic.com>
ff14694 to
b17e790
Compare
…ta.env Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
There was a problem hiding this comment.
Re-review of #1845 for #1736. The approach is still correct: virtual:emdash/env mirrors the established virtual:emdash/wait-until pattern, keeps core adapter-agnostic, and restores the import.meta.env fallback on Node builds. Both OAuth routes are migrated consistently, and I found no other locals.runtime.env accesses in packages/core. The changeset is present and well-written, and the regression tests cover the throwing-getter case.
This is an approve, with two non-blocking test suggestions carried over from the previous review:
- Coverage symmetry —
oauth-callback-route.test.tsonly has the throwing-getter regression test, whileoauth-start-route.test.tsalso verifies that credentials provided viavirtual:emdash/envare actually consumed. The callback route should get the same second test. - Test hygiene —
oauth-start-route.test.tscallsvi.doUnmock("virtual:emdash/env")inside the second test even though theafterEachhook already does it. Remove the body-level call.
I also noticed a sibling implementation that will hit the same Astro 6 issue, though it is out of scope for this PR: packages/cloudflare/src/plugins/vectorize-search.ts still reads locals?.runtime?.env and should be migrated in a follow-up.
I reviewed statically only.
Findings
-
[suggestion]
packages/core/tests/unit/auth/oauth-callback-route.test.ts:30-51The callback route test file only covers the throwing-getter regression. The start route has an additional test that mocks
virtual:emdash/envwith provider credentials and asserts the provider configured check is reached. Add a matching second test here so callback route coverage stays symmetric and the credentials-from-env path is also exercised for the callback route. -
[suggestion]
packages/core/tests/unit/auth/oauth-start-route.test.ts:86This
vi.doUnmock("virtual:emdash/env")call is redundant; theafterEachhook already un-mocks the module after every test. Remove the body-level call to keep mock teardown in one place.
|
can it be merged? This is the only PR left which will fix oAtuh providers login. |
What does this PR do?
Astro 6 removed
Astro.locals.runtime.env— accessing it now throws rather than returningundefined, so the existinglocals.runtime?.env ?? import.meta.envfallback never reachedimport.meta.env: the throw propagated into the route's outer try/catch and surfaced as a genericoauth_error, masking the realprovider_not_configureddiagnosis. Both OAuth routes (start + callback) hit this; passkey auth was unaffected since it doesn't read this env.Added a
virtual:emdash/envmodule (mirrors the existingvirtual:emdash/wait-untilpattern exactly): re-exports Cloudflare'senvfromcloudflare:workersunder that adapter,undefinedotherwise. Core stays adapter-agnostic this way — a directcloudflare:workersimport would fail to resolve on a Node build. Both routes now read through it instead oflocals.runtime.Closes #1736
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/astro/integration/virtual-modules.test.ts+tests/unit/auth/oauth-start-route.test.ts— 20/20 pass;pnpm lint:quickclean;pnpm typecheckclean.Opened as draft: fork already has several open PRs against this repo (non-collaborator open-PR limit).