Skip to content

Fix middleware bypass #810

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 2 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/bright-balloons-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

Fix a security vulnerability similar to the recent CVE-2025-29927
10 changes: 9 additions & 1 deletion packages/open-next/src/core/routing/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
FunctionsConfigManifest,
MiddlewareManifest,
NextConfig,
PrerenderManifest,
} from "config/index.js";
import type { InternalEvent, InternalResult } from "types/open-next.js";
import { emptyReadableStream } from "utils/stream.js";
Expand Down Expand Up @@ -57,7 +58,14 @@ export async function handleMiddleware(
const headers = internalEvent.headers;

// We bypass the middleware if the request is internal
if (headers["x-isr"]) return internalEvent;
// We should only do that if the request has the correct `x-prerender-revalidate` header
// The `x-prerender-revalidate` header is set at build time and should be safe to trust
if (
headers["x-isr"] &&
headers["x-prerender-revalidate"] ===
PrerenderManifest.preview.previewModeId
)
return internalEvent;

// We only need the normalizedPath to check if the middleware should run
const normalizedPath = localizePath(internalEvent);
Expand Down
3 changes: 3 additions & 0 deletions packages/open-next/src/types/next-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ export interface PrerenderManifest {
dataRouteRegex: string;
};
};
preview: {
previewModeId: string;
};
}

export type Options = {
Expand Down
45 changes: 45 additions & 0 deletions packages/tests-unit/tests/core/routing/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ vi.mock("@opennextjs/aws/adapters/config/index.js", () => ({
version: 2,
},
FunctionsConfigManifest: undefined,
PrerenderManifest: {
preview: {
previewModeId: "preview",
},
},
}));

vi.mock("@opennextjs/aws/core/routing/i18n/index.js", () => ({
Expand Down Expand Up @@ -76,6 +81,7 @@ describe("handleMiddleware", () => {
const event = createEvent({
headers: {
"x-isr": "1",
"x-prerender-revalidate": "preview",
},
});
const result = await handleMiddleware(event, middlewareLoader);
Expand All @@ -84,6 +90,45 @@ describe("handleMiddleware", () => {
expect(result).toEqual(event);
});

it("should not bypass middleware for request with an incorrect x-prerender-revalidate", async () => {
const event = createEvent({
headers: {
"x-isr": "1",
"x-prerender-revalidate": "incorrect",
},
});
middleware.mockResolvedValue({
status: 302,
headers: new Headers({
location: "/redirect",
}),
});
const result = await handleMiddleware(event, middlewareLoader);

expect(middlewareLoader).toHaveBeenCalled();
expect(result.statusCode).toEqual(302);
expect(result.headers.location).toEqual("/redirect");
});

it("should not bypass middleware if there is no x-prerender-revalidate", async () => {
const event = createEvent({
headers: {
"x-isr": "1",
},
});
middleware.mockResolvedValue({
status: 302,
headers: new Headers({
location: "/redirect",
}),
});
const result = await handleMiddleware(event, middlewareLoader);

expect(middlewareLoader).toHaveBeenCalled();
expect(result.statusCode).toEqual(302);
expect(result.headers.location).toEqual("/redirect");
});

it("should invoke middleware with redirect", async () => {
const event = createEvent({});
middleware.mockResolvedValue({
Expand Down
Loading