Skip to content

fix: always allow localhost to connect to dev server #5456

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3121,6 +3121,10 @@ class Server {
}

// always allow localhost host, for convenience
if (value === "localhost" || value.endsWith(".localhost")) {
return true;
}

// allow if value is in allowedHosts
if (Array.isArray(allowedHosts) && allowedHosts.length > 0) {
for (let hostIdx = 0; hostIdx < allowedHosts.length; hostIdx++) {
Expand Down Expand Up @@ -3210,14 +3214,10 @@ class Server {
// an IPv6-address in URLs,
// these are removed from the hostname in url.parse(),
// so we have the pure IPv6-address in hostname.
// 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;

if (isValidHostname) {
Expand Down
41 changes: 41 additions & 0 deletions test/server/same-origin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"use strict";

const webpack = require("webpack");
const Server = require("../../lib/Server");
const config = require("../fixtures/simple-config/webpack.config");

describe("isSameOrigin", () => {
let server;

beforeEach(async () => {
const compiler = webpack(config);
server = new Server({}, compiler);
});

it("should return true for localhost", () => {
const headers = {
host: "localhost:8080",
origin: "http://localhost:8080",
};

expect(server.isSameOrigin(headers)).toBe(true);
});

it("should return true for localhost subdomains", () => {
const headers = {
host: "localhost:8080",
origin: "http://subdomain.localhost:8080",
};

expect(server.isSameOrigin(headers)).toBe(true);
});

it("should return false for cross-origin requests", () => {
const headers = {
origin: "http://example.com",
host: "attacker.com",
};

expect(server.isSameOrigin(headers)).toBe(false);
});
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please write test in e2e directory, no need unit tests

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how to write an e2e test that exhibits the issue. Can you guide me?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please take a look at e2e directory, we have a lot of e2e tests there