Skip to content

Commit 414f93e

Browse files
mscdexjasnell
authored andcommittedMay 23, 2017
http: fix IPv6 Host header check
PR-URL: #13122 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent 69f3db4 commit 414f93e

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed
 

‎lib/_http_client.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,14 @@ function ClientRequest(options, cb) {
173173
}
174174
if (host && !this.getHeader('host') && setHost) {
175175
var hostHeader = host;
176-
var posColon = -1;
177176

178177
// For the Host header, ensure that IPv6 addresses are enclosed
179178
// in square brackets, as defined by URI formatting
180179
// https://tools.ietf.org/html/rfc3986#section-3.2.2
181-
if (-1 !== (posColon = hostHeader.indexOf(':')) &&
182-
-1 !== (posColon = hostHeader.indexOf(':', posColon)) &&
183-
'[' !== hostHeader[0]) {
180+
var posColon = hostHeader.indexOf(':');
181+
if (posColon !== -1 &&
182+
hostHeader.indexOf(':', posColon + 1) !== -1 &&
183+
hostHeader.charCodeAt(0) !== 91/*'['*/) {
184184
hostHeader = `[${hostHeader}]`;
185185
}
186186

‎test/parallel/test-http-host-header-ipv6-fail.js

+23-20
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,31 @@
1212
const common = require('../common');
1313
const assert = require('assert');
1414
const http = require('http');
15+
const net = require('net');
1516

16-
const hostname = '::1';
17+
const requests = [
18+
{ host: 'foo:1234', headers: { expectedhost: 'foo:1234:80' } },
19+
{ host: '::1', headers: { expectedhost: '[::1]:80' } }
20+
];
1721

18-
function httpreq() {
19-
const req = http.request({
20-
host: hostname,
21-
port: server.address().port,
22-
path: '/',
23-
method: 'GET'
24-
});
25-
req.end();
22+
function createLocalConnection(options) {
23+
options.host = undefined;
24+
options.port = this.port;
25+
options.path = undefined;
26+
return net.createConnection(options);
2627
}
2728

28-
if (!common.hasIPv6) {
29-
console.error('Skipping test, no IPv6 support');
30-
return;
31-
}
32-
33-
const server = http.createServer(common.mustCall(function(req, res) {
34-
assert.ok(req.headers.host, `[${hostname}]`);
29+
http.createServer(common.mustCall(function(req, res) {
30+
this.requests = this.requests || 0;
31+
assert.strictEqual(req.headers.host, req.headers.expectedhost);
3532
res.end();
36-
server.close(true);
37-
}));
38-
39-
server.listen(0, hostname, () => httpreq());
33+
if (++this.requests === requests.length)
34+
this.close();
35+
}, requests.length)).listen(0, function() {
36+
const address = this.address();
37+
for (let i = 0; i < requests.length; ++i) {
38+
requests[i].createConnection =
39+
common.mustCall(createLocalConnection.bind(address));
40+
http.get(requests[i]);
41+
}
42+
});

0 commit comments

Comments
 (0)
Please sign in to comment.