Skip to content

Commit

Permalink
Handle ipv6 addresses in host-header correctly with TLS
Browse files Browse the repository at this point in the history
When a url uses an ipv6-addresses as the host part, the host-header of the request will be

[::1]:3000

(for ipv6 address ::1). To verify the IP address against a TLS certificate, we need to extract the
IP-address correctly.

Requires node with nodejs/node#14736 resolved to work.
  • Loading branch information
mattiasholmlund committed Sep 29, 2017
1 parent 7c46df1 commit 664b086
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,21 @@ Agent.prototype.addRequest = function addRequest(req, options) {
options.servername = options.host;
const hostHeader = req.getHeader('host');
if (hostHeader) {
options.servername = hostHeader.replace(/:.*$/, '');
// abc => abc
// abc:123 => abc
// [::1] => ::1
// [::1]:123 => ::1
if (hostHeader.startsWith('[')) {
const index = hostHeader.indexOf(']');
if (index === -1) {
// Leading '[', but no ']'. Need to do something...
options.serverName = hostHeader;
} else {
options.servername = hostHeader.substr(1, index - 1);
}
} else {
options.servername = hostHeader.split(':')[0];
}
}
}

Expand Down

0 comments on commit 664b086

Please sign in to comment.