Skip to content
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

Add 'allowedHosts' option #899

Merged
merged 4 commits into from
Jun 14, 2017
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
16 changes: 16 additions & 0 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function Server(compiler, options) {
this.clientOverlay = options.overlay;
this.disableHostCheck = !!options.disableHostCheck;
this.publicHost = options.public;
this.allowedHosts = options.allowedHosts;
this.sockets = [];
this.contentBaseWatchers = [];

Expand Down Expand Up @@ -443,6 +444,21 @@ Server.prototype.checkHost = function(headers) {
// always allow localhost host, for convience
if(hostname === "127.0.0.1" || hostname === "localhost") return true;

// allow if hostname is in allowedHosts
if(this.allowedHosts && this.allowedHosts.length) {
for(let hostIdx = 0; hostIdx < this.allowedHosts.length; hostIdx++) {
const allowedHost = this.allowedHosts[hostIdx];
if(allowedHost === hostname) return true;

// support "." as a subdomain wildcard
// e.g. ".example.com" will allow "example.com", "www.example.com", "subdomain.example.com", etc
if(allowedHost[0] === ".") {
if(hostname === allowedHost.substring(1)) return true; // "example.com"
if(hostname.endsWith(allowedHost)) return true; // "*.example.com"
}
}
}

// allow hostname of listening adress
if(hostname === this.listenHostname) return true;

Expand Down
7 changes: 7 additions & 0 deletions lib/optionsSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
"description": "The host the server listens to.",
"type": "string"
},
"allowedHosts": {
"description": "Specifies which hosts are allowed to access the dev server.",
"items": {
"type": "string"
},
"type": "array"
},
"filename": {
"description": "The filename that needs to be requested in order to trigger a recompile (only in lazy mode).",
"anyOf": [
Expand Down
48 changes: 47 additions & 1 deletion test/Validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ describe("Validation", function() {
message: [
" - configuration.public should be a string."
]
}, {
name: "invalid `allowedHosts` configuration",
config: { allowedHosts: 1 },
message: [
" - configuration.allowedHosts should be an array:",
" [string]",
" Specifies which hosts are allowed to access the dev server."
]
}, {
name: "invalid `contentBase` configuration",
config: { contentBase: [0] },
Expand All @@ -40,7 +48,7 @@ describe("Validation", function() {
config: { asdf: true },
message: [
" - configuration has an unknown property 'asdf'. These properties are valid:",
" object { hot?, hotOnly?, lazy?, host?, filename?, publicPath?, port?, socket?, " +
" object { hot?, hotOnly?, lazy?, host?, allowedHosts?, filename?, publicPath?, port?, socket?, " +
"watchOptions?, headers?, clientLogLevel?, overlay?, key?, cert?, ca?, pfx?, pfxPassphrase?, " +
"inline?, disableHostCheck?, public?, https?, contentBase?, watchContentBase?, open?, features?, " +
"compress?, proxy?, historyApiFallback?, staticOptions?, setup?, stats?, reporter?, " +
Expand Down Expand Up @@ -115,5 +123,43 @@ describe("Validation", function() {
throw new Error("Validation didn't fail");
}
});

describe("allowedHosts", function() {
it("should allow hosts in allowedHosts", function() {
const testHosts = [
"test.host",
"test2.host",
"test3.host"
];
const options = { allowedHosts: testHosts };
const server = new Server(compiler, options);

testHosts.forEach(function(testHost) {
const headers = { host: testHost };
if(!server.checkHost(headers)) {
throw new Error("Validation didn't fail");
}
});
});
it("should allow hosts that pass a wildcard in allowedHosts", function() {
const options = { allowedHosts: [".example.com"] };
const server = new Server(compiler, options);
const testHosts = [
"www.example.com",
"subdomain.example.com",
"example.com",
"subsubcomain.subdomain.example.com",
"example.com:80",
"subdomain.example.com:80"
];

testHosts.forEach(function(testHost) {
const headers = { host: testHost };
if(!server.checkHost(headers)) {
throw new Error("Validation didn't fail");
}
});
});
});
})
});