Skip to content

fix: respect the allowedHosts option for cross-origin header check #5510

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 3 commits into from
Jun 3, 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
23 changes: 16 additions & 7 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1986,6 +1986,13 @@ class Server {
const headers =
/** @type {{ [key: string]: string | undefined }} */
(req.headers);
const headerName = headers[":authority"] ? ":authority" : "host";

if (this.isValidHost(headers, headerName, false)) {
next();
return;
}

if (
headers["sec-fetch-mode"] === "no-cors" &&
headers["sec-fetch-site"] === "cross-site"
Expand Down Expand Up @@ -3166,9 +3173,10 @@ class Server {
* @private
* @param {{ [key: string]: string | undefined }} headers
* @param {string} headerToCheck
* @param {boolean} validateHost
* @returns {boolean}
*/
isValidHost(headers, headerToCheck) {
isValidHost(headers, headerToCheck, validateHost = true) {
if (this.options.allowedHosts === "all") {
return true;
}
Expand Down Expand Up @@ -3210,12 +3218,13 @@ class Server {
// For convenience, always allow localhost (hostname === 'localhost')
// and its subdomains (hostname.endsWith(".localhost")).
// allow hostname of listening address (hostname === this.options.host)
const isValidHostname =
ipaddr.IPv4.isValid(hostname) ||
ipaddr.IPv6.isValid(hostname) ||
hostname === "localhost" ||
hostname.endsWith(".localhost") ||
hostname === this.options.host;
const isValidHostname = validateHost
? ipaddr.IPv4.isValid(hostname) ||
ipaddr.IPv6.isValid(hostname) ||
hostname === "localhost" ||
hostname.endsWith(".localhost") ||
hostname === this.options.host
: false;

return isValidHostname;
}
Expand Down
106 changes: 106 additions & 0 deletions test/e2e/cross-origin-request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe("cross-origin requests", () => {
htmlServer.listen(htmlServerPort, htmlServerHost);

const { page, browser } = await runBrowser();

try {
const pageErrors = [];

Expand Down Expand Up @@ -91,6 +92,111 @@ describe("cross-origin requests", () => {
htmlServer.listen(htmlServerPort, htmlServerHost);

const { page, browser } = await runBrowser();

try {
const pageErrors = [];

page.on("pageerror", (error) => {
pageErrors.push(error);
});

const scriptTagRequest = page.waitForResponse(
`http://localhost:${devServerPort}/main.js`,
);

await page.goto(`http://${htmlServerHost}:${htmlServerPort}`);

const response = await scriptTagRequest;

expect(response.status()).toBe(200);
} catch (error) {
throw error;
} finally {
await browser.close();
await server.stop();
htmlServer.close();
}
});

it("should return 200 for cross-origin no-cors non-module script tag requests with the 'allowedHost' option and 'all' value", async () => {
const compiler = webpack(config);
const devServerOptions = {
port: devServerPort,
allowedHosts: "all",
};
const server = new Server(devServerOptions, compiler);

await server.start();

// Start a separate server for serving the HTML file
const http = require("http");
const htmlServer = http.createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/html" });
res.end(`
<html>
<head>
<script src="http://localhost:${devServerPort}/main.js"></script>
</head>
<body></body>
</html>
`);
});
htmlServer.listen(htmlServerPort, htmlServerHost);

const { page, browser } = await runBrowser();

try {
const pageErrors = [];

page.on("pageerror", (error) => {
pageErrors.push(error);
});

const scriptTagRequest = page.waitForResponse(
`http://localhost:${devServerPort}/main.js`,
);

await page.goto(`http://${htmlServerHost}:${htmlServerPort}`);

const response = await scriptTagRequest;

expect(response.status()).toBe(200);
} catch (error) {
throw error;
} finally {
await browser.close();
await server.stop();
htmlServer.close();
}
});

it("should return 200 for cross-origin no-cors non-module script tag requests with the `allowedHost` option and the `localhost` value", async () => {
const compiler = webpack(config);
const devServerOptions = {
port: devServerPort,
allowedHosts: ["localhost"],
};
const server = new Server(devServerOptions, compiler);

await server.start();

// Start a separate server for serving the HTML file
const http = require("http");
const htmlServer = http.createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/html" });
res.end(`
<html>
<head>
<script src="http://localhost:${devServerPort}/main.js"></script>
</head>
<body></body>
</html>
`);
});
htmlServer.listen(htmlServerPort, htmlServerHost);

const { page, browser } = await runBrowser();

try {
const pageErrors = [];

Expand Down
1 change: 1 addition & 0 deletions types/lib/Server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,7 @@ declare class Server<
* @private
* @param {{ [key: string]: string | undefined }} headers
* @param {string} headerToCheck
* @param {boolean} validateHost
* @returns {boolean}
*/
private isValidHost;
Expand Down
Loading