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

http: fix IPv6 Host header check #13122

Merged
merged 1 commit into from
May 23, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ 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]) {
var posColon = hostHeader.indexOf(':');
Copy link
Contributor

Choose a reason for hiding this comment

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

const?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was sticking to the "style" of the original code. I'm also still reluctant to use const in runtime code because I've been burned so many times by it in the past (even when it hasn't triggered a permanent deopt).

Copy link
Contributor

Choose a reason for hiding this comment

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

Ack. Although before it was assigned to, and now it's just initiated.

if (posColon !== -1 &&
hostHeader.indexOf(':', posColon + 1) !== -1 &&
hostHeader.charCodeAt(0) !== 91/*'['*/) {
hostHeader = `[${hostHeader}]`;
}

Expand Down
43 changes: 23 additions & 20 deletions test/parallel/test-http-host-header-ipv6-fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,31 @@
const common = require('../common');
const assert = require('assert');
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() {
const req = http.request({
host: hostname,
port: server.address().port,
path: '/',
method: 'GET'
});
req.end();
function createLocalConnection(options) {
options.host = undefined;
options.port = this.port;
options.path = undefined;
return net.createConnection(options);
}

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}]`);
http.createServer(common.mustCall(function(req, res) {
this.requests = this.requests || 0;
assert.strictEqual(req.headers.host, req.headers.expectedhost);
res.end();
server.close(true);
}));

server.listen(0, hostname, () => httpreq());
if (++this.requests === requests.length)
this.close();
}, requests.length)).listen(0, function() {
const address = this.address();
for (let i = 0; i < requests.length; ++i) {
requests[i].createConnection =
common.mustCall(createLocalConnection.bind(address));
http.get(requests[i]);
}
});