-
Notifications
You must be signed in to change notification settings - Fork 779
avoid to get context.session to use export const prerender = true
#434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "emdash": patch | ||
| --- | ||
|
|
||
| Avoid accessing sessions on prerendered public routes. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
packages/core/tests/unit/astro/middleware-prerender.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| import { describe, it, expect, vi } from "vitest"; | ||
|
|
||
| vi.mock("astro:middleware", () => ({ | ||
| defineMiddleware: (handler: unknown) => handler, | ||
| })); | ||
|
|
||
| vi.mock( | ||
| "virtual:emdash/config", | ||
| () => ({ | ||
| default: { | ||
| database: { config: {} }, | ||
| auth: { mode: "none" }, | ||
| }, | ||
| }), | ||
| { virtual: true }, | ||
| ); | ||
|
|
||
| vi.mock( | ||
| "virtual:emdash/dialect", | ||
| () => ({ | ||
| createDialect: vi.fn(), | ||
| isSessionEnabled: vi.fn().mockReturnValue(false), | ||
| getD1Binding: vi.fn(), | ||
| getDefaultConstraint: vi.fn().mockReturnValue("first-unconstrained"), | ||
| getBookmarkCookieName: vi.fn().mockReturnValue("emdash-bookmark"), | ||
| createSessionDialect: vi.fn(), | ||
| }), | ||
| { virtual: true }, | ||
| ); | ||
|
|
||
| vi.mock("virtual:emdash/media-providers", () => ({ mediaProviders: [] }), { virtual: true }); | ||
| vi.mock("virtual:emdash/plugins", () => ({ plugins: [] }), { virtual: true }); | ||
| vi.mock( | ||
| "virtual:emdash/sandbox-runner", | ||
| () => ({ | ||
| createSandboxRunner: null, | ||
| sandboxEnabled: false, | ||
| }), | ||
| { virtual: true }, | ||
| ); | ||
| vi.mock("virtual:emdash/sandboxed-plugins", () => ({ sandboxedPlugins: [] }), { virtual: true }); | ||
| vi.mock("virtual:emdash/storage", () => ({ createStorage: null }), { virtual: true }); | ||
|
|
||
| vi.mock("../../../src/loader.js", () => ({ | ||
| getDb: vi.fn(async () => ({ | ||
| selectFrom: () => ({ | ||
| selectAll: () => ({ | ||
| limit: () => ({ | ||
| execute: async () => [], | ||
| }), | ||
| }), | ||
| }), | ||
| })), | ||
| })); | ||
|
|
||
| import { createSessionDialect, getD1Binding, isSessionEnabled } from "virtual:emdash/dialect"; | ||
|
|
||
| import onRequest from "../../../src/astro/middleware.js"; | ||
|
|
||
| describe("astro middleware prerendered routes", () => { | ||
| it("does not access session on prerendered public runtime routes", async () => { | ||
| const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => undefined); | ||
| try { | ||
| vi.mocked(isSessionEnabled).mockReturnValue(true); | ||
| vi.mocked(getD1Binding).mockReturnValue({ | ||
| withSession: () => { | ||
| throw new Error("withSession reached"); | ||
| }, | ||
| }); | ||
| vi.mocked(createSessionDialect).mockReturnValue(undefined as never); | ||
|
|
||
| const cookies = { | ||
| get: vi.fn(() => undefined), | ||
| }; | ||
|
|
||
| const context: Record<string, unknown> = { | ||
| request: new Request("https://example.com/robots.txt"), | ||
| url: new URL("https://example.com/robots.txt"), | ||
| cookies, | ||
| locals: {}, | ||
| redirect: vi.fn(), | ||
| isPrerendered: true, | ||
| }; | ||
|
|
||
| Object.defineProperty(context, "session", { | ||
| get() { | ||
| throw new Error("session should not be accessed during prerender"); | ||
| }, | ||
| }); | ||
|
|
||
| await expect( | ||
| onRequest(context as Parameters<typeof onRequest>[0], async () => new Response("ok")), | ||
| ).rejects.toThrow("withSession reached"); | ||
| } finally { | ||
| consoleErrorSpy.mockRestore(); | ||
| } | ||
| }); | ||
|
|
||
| it("does not access session when prerendering public pages", async () => { | ||
| const cookies = { | ||
| get: vi.fn(() => undefined), | ||
| }; | ||
| const redirect = vi.fn( | ||
| (location: string) => new Response(null, { status: 302, headers: { Location: location } }), | ||
| ); | ||
|
|
||
| const context: Record<string, unknown> = { | ||
| request: new Request("https://example.com/"), | ||
| url: new URL("https://example.com/"), | ||
| cookies, | ||
| locals: {}, | ||
| redirect, | ||
| isPrerendered: true, | ||
| }; | ||
|
|
||
| Object.defineProperty(context, "session", { | ||
| get() { | ||
| throw new Error("session should not be accessed during prerender"); | ||
| }, | ||
| }); | ||
|
|
||
| const response = await onRequest( | ||
| context as Parameters<typeof onRequest>[0], | ||
| async () => new Response("ok"), | ||
| ); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(redirect).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.