Skip to content

Commit f18e5ad

Browse files
committed
check origin header for websocket connection
1 parent e1bd264 commit f18e5ad

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

lib/Server.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,13 +513,15 @@ Server.prototype.setContentHeaders = function (req, res, next) {
513513
next();
514514
};
515515

516-
Server.prototype.checkHost = function (headers) {
516+
Server.prototype.checkHost = function (headers, headerToCheck) {
517517
// allow user to opt-out this security check, at own risk
518518
if (this.disableHostCheck) return true;
519519

520+
if (!headerToCheck) headerToCheck = "host";
521+
520522
// get the Host header and extract hostname
521523
// we don't care about port not matching
522-
const hostHeader = headers.host;
524+
const hostHeader = headers[headerToCheck];
523525
if (!hostHeader) return false;
524526

525527
// use the node url-parser to retrieve the hostname from the host-header.
@@ -589,6 +591,11 @@ Server.prototype.listen = function (port, hostname, fn) {
589591
conn.close();
590592
return;
591593
}
594+
if (!this.checkHost(conn.headers, "origin")) {
595+
this.sockWrite([conn], 'error', 'Invalid Origin header');
596+
conn.close();
597+
return;
598+
}
592599
this.sockets.push(conn);
593600

594601
conn.on('close', () => {

0 commit comments

Comments
 (0)