Skip to content

Commit 56a92c2

Browse files
committed
Check origin header for websocket connection
1 parent 7cdfb74 commit 56a92c2

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

lib/Server.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -510,13 +510,14 @@ Server.prototype.setContentHeaders = function (req, res, next) {
510510
next();
511511
};
512512

513-
Server.prototype.checkHost = function (headers) {
513+
Server.prototype.checkHost = function (headers, headerToCheck) {
514514
// allow user to opt-out this security check, at own risk
515515
if (this.disableHostCheck) return true;
516516

517+
if (!headerToCheck) headerToCheck = 'host';
517518
// get the Host header and extract hostname
518519
// we don't care about port not matching
519-
const hostHeader = headers.host;
520+
const hostHeader = headers[headerToCheck];
520521
if (!hostHeader) return false;
521522

522523
// use the node url-parser to retrieve the hostname from the host-header.
@@ -581,8 +582,8 @@ Server.prototype.listen = function (port, hostname, fn) {
581582

582583
sockServer.on('connection', (conn) => {
583584
if (!conn) return;
584-
if (!this.checkHost(conn.headers)) {
585-
this.sockWrite([conn], 'error', 'Invalid Host header');
585+
if (!this.checkHost(conn.headers) || !this.checkHost(conn.headers, 'origin')) {
586+
this.sockWrite([conn], 'error', 'Invalid Host/Origin header');
586587
conn.close();
587588
return;
588589
}

0 commit comments

Comments
 (0)