Skip to content

Commit

Permalink
add support for subdomain wildcard in 'allowedHosts' option
Browse files Browse the repository at this point in the history
  • Loading branch information
orteth01 committed Jun 14, 2017
1 parent 36cb650 commit 8eb8585
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,19 @@ Server.prototype.checkHost = function(headers) {
if(hostname === "127.0.0.1" || hostname === "localhost") return true;

// allow if hostname is in allowedHosts
if(this.allowedHosts && this.allowedHosts.indexOf(hostname) >= 0) return true;
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

0 comments on commit 8eb8585

Please sign in to comment.