http: fix IPv6 Host header check

PR-URL: https://github.com/nodejs/node/pull/13122
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
This commit is contained in:
Brian White 2017-05-19 17:14:53 -04:00
parent ed365653f6
commit 3774c99f66
No known key found for this signature in database
GPG Key ID: 606D7358F94DA209
2 changed files with 27 additions and 24 deletions

View File

@ -173,14 +173,14 @@ function ClientRequest(options, cb) {
} }
if (host && !this.getHeader('host') && setHost) { if (host && !this.getHeader('host') && setHost) {
var hostHeader = host; var hostHeader = host;
var posColon = -1;
// For the Host header, ensure that IPv6 addresses are enclosed // For the Host header, ensure that IPv6 addresses are enclosed
// in square brackets, as defined by URI formatting // in square brackets, as defined by URI formatting
// https://tools.ietf.org/html/rfc3986#section-3.2.2 // https://tools.ietf.org/html/rfc3986#section-3.2.2
if (-1 !== (posColon = hostHeader.indexOf(':')) && var posColon = hostHeader.indexOf(':');
-1 !== (posColon = hostHeader.indexOf(':', posColon)) && if (posColon !== -1 &&
'[' !== hostHeader[0]) { hostHeader.indexOf(':', posColon + 1) !== -1 &&
hostHeader.charCodeAt(0) !== 91/*'['*/) {
hostHeader = `[${hostHeader}]`; hostHeader = `[${hostHeader}]`;
} }

View File

@ -12,28 +12,31 @@
const common = require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const http = require('http'); const http = require('http');
const net = require('net');
const hostname = '::1'; const requests = [
{ host: 'foo:1234', headers: { expectedhost: 'foo:1234:80' } },
{ host: '::1', headers: { expectedhost: '[::1]:80' } }
];
function httpreq() { function createLocalConnection(options) {
const req = http.request({ options.host = undefined;
host: hostname, options.port = this.port;
port: server.address().port, options.path = undefined;
path: '/', return net.createConnection(options);
method: 'GET'
});
req.end();
} }
if (!common.hasIPv6) { http.createServer(common.mustCall(function(req, res) {
console.error('Skipping test, no IPv6 support'); this.requests = this.requests || 0;
return; assert.strictEqual(req.headers.host, req.headers.expectedhost);
}
const server = http.createServer(common.mustCall(function(req, res) {
assert.ok(req.headers.host, `[${hostname}]`);
res.end(); res.end();
server.close(true); if (++this.requests === requests.length)
})); this.close();
}, requests.length)).listen(0, function() {
server.listen(0, hostname, () => httpreq()); const address = this.address();
for (let i = 0; i < requests.length; ++i) {
requests[i].createConnection =
common.mustCall(createLocalConnection.bind(address));
http.get(requests[i]);
}
});