Skip to content

Commit b3217ca

Browse files
sokraevilebottnawi
authored andcommitted
fix: check origin header for websocket connection (#1603)
1 parent 68dd49a commit b3217ca

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

lib/Server.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -630,14 +630,16 @@ Server.prototype.setContentHeaders = function (req, res, next) {
630630
next();
631631
};
632632

633-
Server.prototype.checkHost = function (headers) {
633+
Server.prototype.checkHost = function (headers, headerToCheck) {
634634
// allow user to opt-out this security check, at own risk
635635
if (this.disableHostCheck) {
636636
return true;
637637
}
638+
639+
if (!headerToCheck) headerToCheck = 'host';
638640
// get the Host header and extract hostname
639641
// we don't care about port not matching
640-
const hostHeader = headers.host;
642+
const hostHeader = headers[headerToCheck];
641643

642644
if (!hostHeader) {
643645
return false;
@@ -725,8 +727,8 @@ Server.prototype.listen = function (port, hostname, fn) {
725727
return;
726728
}
727729

728-
if (!this.checkHost(connection.headers)) {
729-
this.sockWrite([ connection ], 'error', 'Invalid Host header');
730+
if (!this.checkHost(connection.headers) || !this.checkHost(connection.headers, 'origin')) {
731+
this.sockWrite([ connection ], 'error', 'Invalid Host/Origin header');
730732

731733
connection.close();
732734

0 commit comments

Comments
 (0)