Skip to content

Commit d1baf90

Browse files
orenfrombergCarlos Requena López
authored and
Carlos Requena López
committed
refactor: checkHost (webpack#1619)
* Refactoring checkHost and adding Origin check * Removing unnecessary header check * Adding semicolons
1 parent 542cd6b commit d1baf90

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

lib/Server.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,15 @@ Server.prototype.setContentHeaders = function (req, res, next) {
646646
next();
647647
};
648648

649-
Server.prototype.checkHost = function (headers, headerToCheck) {
649+
Server.prototype.checkHost = function (headers) {
650+
return this.checkHeaders(headers, 'host');
651+
};
652+
653+
Server.prototype.checkOrigin = function (headers) {
654+
return this.checkHeaders(headers, 'origin');
655+
};
656+
657+
Server.prototype.checkHeaders = function (headers, headerToCheck) {
650658
// allow user to opt-out this security check, at own risk
651659
if (this.disableHostCheck) {
652660
return true;
@@ -748,7 +756,7 @@ Server.prototype.listen = function (port, hostname, fn) {
748756
return;
749757
}
750758

751-
if (!this.checkHost(connection.headers) || !this.checkHost(connection.headers, 'origin')) {
759+
if (!this.checkHost(connection.headers) || !this.checkOrigin(connection.headers)) {
752760
this.sockWrite([ connection ], 'error', 'Invalid Host/Origin header');
753761

754762
connection.close();

test/Validation.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ describe('Validation', () => {
179179
origin: 'https://test.host'
180180
};
181181
const server = new Server(compiler, options);
182-
if (!server.checkHost(headers, 'origin')) {
182+
if (!server.checkOrigin(headers)) {
183183
throw new Error("Validation didn't fail");
184184
}
185185
});

0 commit comments

Comments
 (0)