From 5132c307339651d16677d623cb02d56184720f56 Mon Sep 17 00:00:00 2001 From: Mihai Potra Date: Fri, 19 Feb 2016 10:21:49 +0200 Subject: [PATCH] http: Corrects IPv6 address in Host header IPv6 addresses in Host header (URI), must be enclosed within square brackets, in order to properly separate the host address from any port reference. test: add test for IPv6 hostname conformance in Host header http: Ensure IPv6 is enclosed within square brackets in Host header http: use net.isIPv6 in ClientRequest test: update test with const instead of var test: use common.mustCall and cleanup http: ClientRequest, drop isIPv6() in favor of indexOf() checks http: ClientRequest, replace concatenation with string literal test: use const test: clean-up test: move test to parallel test: skip if no IPv6 support --- lib/_http_client.js | 11 +++++ .../test-http-host-header-ipv6-fail.js | 41 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 test/parallel/test-http-host-header-ipv6-fail.js diff --git a/lib/_http_client.js b/lib/_http_client.js index 25beb97ba90e09..bc294263c7f881 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -87,6 +87,17 @@ function ClientRequest(options, cb) { } if (host && !this.getHeader('host') && setHost) { var hostHeader = host; + var posColon = -1; + + // For the Host header, ensure that IPv6 addresses are enclosed + // in square brackets, as defined by URI formatting + // https://tools.ietf.org/html/rfc3986#section-3.2.2 + if (-1 !== (posColon = hostHeader.indexOf(':')) && + -1 !== (posColon = hostHeader.indexOf(':', posColon)) && + '[' !== hostHeader[0]) { + hostHeader = `[${hostHeader}]`; + } + if (port && +port !== defaultPort) { hostHeader += ':' + port; } diff --git a/test/parallel/test-http-host-header-ipv6-fail.js b/test/parallel/test-http-host-header-ipv6-fail.js new file mode 100644 index 00000000000000..57573432a70e1c --- /dev/null +++ b/test/parallel/test-http-host-header-ipv6-fail.js @@ -0,0 +1,41 @@ +'use strict'; +/* + * When using the object form of http.request and using an IPv6 address + * as a hostname, and using a non-standard port, the Host header + * is improperly formatted. + * Issue: https://github.com/nodejs/node/issues/5308 + * As per https://tools.ietf.org/html/rfc7230#section-5.4 and + * https://tools.ietf.org/html/rfc3986#section-3.2.2 + * the IPv6 address should be enclosed in square brackets + */ + +const common = require('../common'); +const assert = require('assert'); +const http = require('http'); + +const hostname = '::1'; +const port = common.PORT; + +function httpreq() { + var req = http.request({ + host: hostname, + port: port, + path: '/', + method: 'GET' + }); + req.end(); +} + +if (!common.hasIPv6) { + console.error('Skipping test, no IPv6 support'); + return; +} + +const server = http.createServer(common.mustCall(function(req, res) { + assert.ok(req.headers.host, `[${hostname}]`); + res.end(); + server.close(true); +})); + +server.listen(port, hostname, () => httpreq()); +