Skip to content

Commit 75a31dd

Browse files
sommeeeervicb
andauthored
fix: Validate statusCode is number and not NaN in OpenNextNodeResponse constructor (#945)
* fix: Validate statusCode is number and not NaN in OpenNextNodeResponse constructor * changeset * add e2e * Update packages/open-next/src/http/openNextResponse.ts Co-authored-by: Victor Berchet <[email protected]> * review --------- Co-authored-by: Victor Berchet <[email protected]>
1 parent 2b7f7cb commit 75a31dd

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

.changeset/brown-apes-kick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
fix: Validate statusCode is number and not NaN in OpenNextNodeResponse constructor

examples/app-router/middleware.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ export function middleware(request: NextRequest) {
2828
const u = new URL("https://opennext.js.org/share.png");
2929
return NextResponse.rewrite(u);
3030
}
31+
if (path === "/rewrite-status-code") {
32+
const u = new URL("/rewrite-destination", `${protocol}://${host}`);
33+
return NextResponse.rewrite(u, {
34+
status: 403,
35+
});
36+
}
3137
if (path === "/cookies") {
3238
const res = NextResponse.next();
3339
res.cookies.set("foo", "bar");

packages/open-next/src/http/openNextResponse.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,14 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
7474
statusCode?: number,
7575
) {
7676
super();
77-
if (statusCode !== undefined) {
77+
// We only set the status code if it is not a NaN and it is a number
78+
// Only allow status codes between 100 and 599 https://httpwg.org/specs/rfc9110.html#status.codes
79+
if (
80+
statusCode &&
81+
Number.isInteger(statusCode) &&
82+
statusCode >= 100 &&
83+
statusCode <= 599
84+
) {
7885
this.statusCode = statusCode;
7986
}
8087
}

packages/tests-e2e/tests/appRouter/middleware.rewrite.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,12 @@ test("Middleware Rewrite External Image", async ({ page }) => {
3232
expect(validateMd5(bodyBuffer, OPENNEXT_PNG_MD5)).toBe(true);
3333
});
3434
});
35+
36+
test("Middleware Rewrite Status Code", async ({ page }) => {
37+
await page.goto("/rewrite-status-code");
38+
const el = page.getByText("Rewritten Destination", { exact: true });
39+
await expect(el).toBeVisible();
40+
page.on("response", async (response) => {
41+
expect(response.status()).toBe(403);
42+
});
43+
});

0 commit comments

Comments
 (0)