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

fixes Origin validation for IP address. gives meaning to public and allowedHosts #1622

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
17 changes: 6 additions & 11 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
const fs = require('fs');
const path = require('path');

const ip = require('ip');
const tls = require('tls');
const url = require('url');
const http = require('http');
Expand Down Expand Up @@ -647,6 +646,10 @@ Server.prototype.setContentHeaders = function (req, res, next) {
};

Server.prototype.checkHost = function (headers, headerToCheck) {
/* This routine is also used to check the Origin header, whenever
* headerToCheck says so
*/

// allow user to opt-out this security check, at own risk
if (this.disableHostCheck) {
return true;
Expand All @@ -668,17 +671,9 @@ Server.prototype.checkHost = function (headers, headerToCheck) {
false,
true
).hostname;
// always allow requests with explicit IPv4 or IPv6-address.
// A note on IPv6 addresses:
// hostHeader will always contain the brackets denoting
// an IPv6-address in URLs,
// these are removed from the hostname in url.parse(),
// so we have the pure IPv6-address in hostname.
if (ip.isV4Format(hostname) || ip.isV6Format(hostname)) {
return true;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we remove this check, it can be breaking change for some developers?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check basically means no security (for both static server and sockjs server) (#1618). It is not breaking if people are following the docs 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@carlosgeos developers are not following the docs 😄 Better avoid this removing, we can do this in next major release

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hahahah ok, then I'll close this PR


// always allow localhost host, for convience
if (hostname === 'localhost') {
if (hostname === 'localhost' || hostname === '127.0.0.1') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need better check here, because some developers can use 192.168.0.1 or other local IPs

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they use a local IP, developers should specify public: '192.168.0.x'. It will be covered by the public option further down in the code:

if (hostname === publicHostname) {
return true;
}

allowedHosts can also be used.

return true;
}
// allow if hostname is in allowedHosts
Expand Down
40 changes: 20 additions & 20 deletions test/Validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,17 @@ describe('Validation', () => {
}
});

it('should allow access for every requests using an IP', () => {
const options = {};
it('should not allow access for IPs or hostnames that are not in options.public or allowedHosts and viceversa', () => {
const options = {
public: 'pointerpointer.com',
allowedHosts: ['realdevdomain.dev']
};

const tests = [
'realdevdomain.dev',
'test.hostname:80',
'google.com',
'pointerpointer.com',
'192.168.1.123',
'192.168.1.2:8080',
'[::1]',
Expand All @@ -149,28 +156,21 @@ describe('Validation', () => {
tests.forEach((test) => {
const headers = { host: test };

if (!server.checkHost(headers)) {
throw new Error("Validation didn't pass");
const isPublicHostname = test === options.public;
const isInAllowedHosts = options.allowedHosts.includes(test);
if (server.checkHost(headers)) {
if (!isPublicHostname && !isInAllowedHosts) {
throw new Error("Validation didn't fail. It should");
}
} else {
// eslint-disable-next-line no-lonely-if
if (isPublicHostname || isInAllowedHosts) {
throw new Error("Validation failed and it shouldn't");
}
}
});
});

it("should not allow hostnames that don't match options.public", () => {
const options = {
public: 'test.host:80'
};

const headers = {
host: 'test.hostname:80'
};

const server = new Server(compiler, options);

if (server.checkHost(headers)) {
throw new Error("Validation didn't fail");
}
});

it('should allow urls with scheme for checking origin', () => {
const options = {
public: 'test.host:80'
Expand Down