diff options
author | Mattias Holmlund <mattias.holmlund@netinsight.net> | 2017-08-10 15:11:19 +0200 |
---|---|---|
committer | Anatoli Papirovski <apapirovski@mac.com> | 2017-11-02 21:05:20 -0400 |
commit | b6df87e1d482f18202586273200fd7a8e9bc2fcd (patch) | |
tree | 8603a7c67ac8b61dddce193bb7e7e1a452975b00 /lib/_http_agent.js | |
parent | 14181a3368b38978e3178d66a549349cc720f707 (diff) | |
download | android-node-v8-b6df87e1d482f18202586273200fd7a8e9bc2fcd.tar.gz android-node-v8-b6df87e1d482f18202586273200fd7a8e9bc2fcd.tar.bz2 android-node-v8-b6df87e1d482f18202586273200fd7a8e9bc2fcd.zip |
http, tls: better support for IPv6 addresses
- Properly handle IPv6 in Host header when setting servername.
- When comparing IP addresses against addresses in the subjectAltName
field of a certificate, format the address correctly before
doing the string comparison.
PR-URL: https://github.com/nodejs/node/pull/14772
Fixes: https://github.com/nodejs/node/issues/14736
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib/_http_agent.js')
-rw-r--r-- | lib/_http_agent.js | 41 |
1 files changed, 27 insertions, 14 deletions
diff --git a/lib/_http_agent.js b/lib/_http_agent.js index e3f9a58437..564eab9254 100644 --- a/lib/_http_agent.js +++ b/lib/_http_agent.js @@ -153,13 +153,8 @@ Agent.prototype.addRequest = function addRequest(req, options, port/*legacy*/, if (options.socketPath) options.path = options.socketPath; - if (!options.servername) { - options.servername = options.host; - const hostHeader = req.getHeader('host'); - if (hostHeader) { - options.servername = hostHeader.replace(/:.*$/, ''); - } - } + if (!options.servername) + options.servername = calculateServerName(options, req); var name = this.getName(options); if (!this.sockets[name]) { @@ -207,13 +202,8 @@ Agent.prototype.createSocket = function createSocket(req, options, cb) { if (options.socketPath) options.path = options.socketPath; - if (!options.servername) { - options.servername = options.host; - const hostHeader = req.getHeader('host'); - if (hostHeader) { - options.servername = hostHeader.replace(/:.*$/, ''); - } - } + if (!options.servername) + options.servername = calculateServerName(options, req); var name = self.getName(options); options._agentKey = name; @@ -241,6 +231,29 @@ Agent.prototype.createSocket = function createSocket(req, options, cb) { } }; +function calculateServerName(options, req) { + let servername = options.host; + const hostHeader = req.getHeader('host'); + if (hostHeader) { + // 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... + servername = hostHeader; + } else { + servername = hostHeader.substr(1, index - 1); + } + } else { + servername = hostHeader.split(':', 1)[0]; + } + } + return servername; +} + function installListeners(agent, s, options) { function onFree() { debug('CLIENT socket onFree'); |