Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check origin header for websocket connection #1603

Merged
merged 3 commits into from
Dec 21, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,14 +630,16 @@ Server.prototype.setContentHeaders = function (req, res, next) {
next();
};

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

if (!headerToCheck) headerToCheck = 'host';
// get the Host header and extract hostname
// we don't care about port not matching
const hostHeader = headers.host;
const hostHeader = headers[headerToCheck];

if (!hostHeader) {
return false;
Expand Down Expand Up @@ -725,8 +727,8 @@ Server.prototype.listen = function (port, hostname, fn) {
return;
}

if (!this.checkHost(connection.headers)) {
this.sockWrite([ connection ], 'error', 'Invalid Host header');
if (!this.checkHost(connection.headers) || !this.checkHost(connection.headers, 'origin')) {
this.sockWrite([ connection ], 'error', 'Invalid Host/Origin header');

connection.close();

Expand Down