Skip to content

Commit 99b273c

Browse files
authored
Merge pull request #888 from phairoh/fix-incorrect-variable-usage
Use correct index when parsing publicHost
2 parents 7d08d1e + f26f985 commit 99b273c

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

lib/Server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ Server.prototype.checkHost = function(headers) {
417417
// also allow public hostname if provided
418418
if(typeof this.publicHost === "string") {
419419
const idxPublic = this.publicHost.indexOf(":");
420-
const publicHostname = idxPublic >= 0 ? this.publicHost.substr(0, idx) : this.publicHost;
420+
const publicHostname = idxPublic >= 0 ? this.publicHost.substr(0, idxPublic) : this.publicHost;
421421
if(hostname === publicHostname) return true;
422422
}
423423

test/Validation.test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,59 @@ describe("Validation", function() {
6161
throw new Error("Validation didn't fail");
6262
})
6363
});
64+
65+
describe("checkHost", function() {
66+
it("should always allow any host if options.disableHostCheck is set", function() {
67+
const options = {
68+
public: "test.host:80",
69+
disableHostCheck: true
70+
};
71+
const headers = {
72+
host: "bad.host"
73+
};
74+
const server = new Server(compiler, options);
75+
if(!server.checkHost(headers)) {
76+
throw new Error("Validation didn't fail");
77+
}
78+
});
79+
80+
it("should allow any valid options.public when host is localhost", function() {
81+
const options = {
82+
public: "test.host:80"
83+
};
84+
const headers = {
85+
host: "localhost"
86+
};
87+
const server = new Server(compiler, options);
88+
if(!server.checkHost(headers)) {
89+
throw new Error("Validation didn't fail");
90+
}
91+
});
92+
93+
it("should allow any valid options.public when host is 127.0.0.1", function() {
94+
const options = {
95+
public: "test.host:80"
96+
};
97+
const headers = {
98+
host: "127.0.0.1"
99+
};
100+
const server = new Server(compiler, options);
101+
if(!server.checkHost(headers)) {
102+
throw new Error("Validation didn't fail");
103+
}
104+
});
105+
106+
it("should not allow hostnames that don't match options.public", function() {
107+
const options = {
108+
public: "test.host:80",
109+
};
110+
const headers = {
111+
host: "test.hostname:80"
112+
};
113+
const server = new Server(compiler, options);
114+
if(server.checkHost(headers)) {
115+
throw new Error("Validation didn't fail");
116+
}
117+
});
118+
})
64119
});

0 commit comments

Comments
 (0)