Skip to content

feat: hide x-opennext header from server requests when poweredByHeader is false #567

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 5 commits into from
Oct 18, 2024
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/giant-vans-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

Hides the x-opennext header from server requests when poweredByHeader is false in next config
1 change: 1 addition & 0 deletions examples/pages-router/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const nextConfig = {
},
],
trailingSlash: true,
poweredByHeader: true,
};

module.exports = nextConfig;
6 changes: 4 additions & 2 deletions packages/open-next/src/core/routing/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import crypto from "node:crypto";
import { OutgoingHttpHeaders } from "node:http";
import { Readable } from "node:stream";

import { BuildId, HtmlPages } from "config/index.js";
import { BuildId, HtmlPages, NextConfig } from "config/index.js";
import type { IncomingMessage, StreamCreator } from "http/index.js";
import { OpenNextNodeResponse } from "http/openNextResponse.js";
import { parseHeaders } from "http/util.js";
Expand Down Expand Up @@ -322,7 +322,9 @@ export function fixSWRCacheHeader(headers: OutgoingHttpHeaders) {
* @__PURE__
*/
export function addOpenNextHeader(headers: OutgoingHttpHeaders) {
headers["X-OpenNext"] = "1";
if (NextConfig.poweredByHeader) {
headers["X-OpenNext"] = "1";
}
if (globalThis.openNextDebug) {
headers["X-OpenNext-Version"] = globalThis.openNextVersion;
headers["X-OpenNext-RequestId"] = globalThis.__als.getStore()?.requestId;
Expand Down
1 change: 1 addition & 0 deletions packages/open-next/src/types/next-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export interface NextConfig {
appDir?: boolean;
};
images: ImageConfig;
poweredByHeader?: boolean;
}

export interface RouteDefinition {
Expand Down
4 changes: 4 additions & 0 deletions packages/tests-e2e/tests/appRouter/headers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ test("Headers", async ({ page }) => {
// Request header should be available in RSC
let el = page.getByText(`request-header`);
await expect(el).toBeVisible();

// Both these headers should not be present cause poweredByHeader is false in appRouter
expect(headers["x-powered-by"]).toBeFalsy();
expect(headers["x-opennext"]).toBeFalsy();
});
14 changes: 14 additions & 0 deletions packages/tests-e2e/tests/pagesRouter/header.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { expect, test } from "@playwright/test";

test("should test if poweredByHeader adds the correct headers ", async ({
page,
}) => {
const result = await page.goto("/");
expect(result).toBeDefined();
expect(result?.status()).toBe(200);
const headers = result?.headers();

// Both these headers should be present cause poweredByHeader is true in pagesRouter
expect(headers?.["x-powered-by"]).toBe("Next.js");
expect(headers?.["x-opennext"]).toBe("1");
});
Loading