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

Handle IPv6-addresses correctly in checkHost() #1026

Merged
merged 1 commit into from
Aug 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const ip = require("ip");
const serveIndex = require("serve-index");
const historyApiFallback = require("connect-history-api-fallback");
const path = require("path");
const url = require("url");
const selfsigned = require("selfsigned");
const sockjs = require("sockjs");
const spdy = require("spdy");
Expand Down Expand Up @@ -439,11 +440,15 @@ Server.prototype.checkHost = function(headers) {
// we don't care about port not matching
const hostHeader = headers.host;
if(!hostHeader) return false;
const idx = hostHeader.indexOf(":");
const hostname = idx >= 0 ? hostHeader.substr(0, idx) : hostHeader;

// always allow requests with explicit IP-address
if(ip.isV4Format(hostname)) return true;
// use the node url-parser to retrieve the hostname from the host-header.
const hostname = url.parse("//" + hostHeader, 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;

// always allow localhost host, for convience
if(hostname === "localhost") return true;
Expand Down
21 changes: 15 additions & 6 deletions test/Validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,22 @@ describe("Validation", function() {

it("should allow access for every requests using an IP", function() {
const options = {};
const headers = {
host: "192.168.1.123"
};
const testHosts = [
"192.168.1.123",
"192.168.1.2:8080",
"[::1]",
"[::1]:8080",
"[ad42::1de2:54c2:c2fa:1234]",
"[ad42::1de2:54c2:c2fa:1234]:8080"
];

const server = new Server(compiler, options);
if(!server.checkHost(headers)) {
throw new Error("Validation didn't fail");
}
testHosts.forEach(function(testHost) {
const headers = { host: testHost };
if(!server.checkHost(headers)) {
throw new Error("Validation didn't pass");
}
});
});

it("should not allow hostnames that don't match options.public", function() {
Expand Down