Skip to content

Commit 0d19c20

Browse files
tls: handle ipv6 addresses in host-header
When an http-client requests a url with the hostname specified as an IPv6 address, the Host: header will have the following format: Host: [::1]:3000 The servername in this case should be '::1'. Fixes: nodejs#14736
1 parent 85a5a2c commit 0d19c20

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

lib/_http_agent.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@ Agent.prototype.addRequest = function addRequest(req, options, port/*legacy*/,
157157
options.servername = options.host;
158158
const hostHeader = req.getHeader('host');
159159
if (hostHeader) {
160-
options.servername = hostHeader.replace(/:.*$/, '');
160+
// abc => abc
161+
// abc:123 => abc
162+
// [::1] => ::1
163+
// [::1]:123 => ::1
164+
options.servername = hostHeader.replace(/:[^\]]+$/, '')
165+
.replace(/^\[(.*)\]$/, '$1');
161166
}
162167
}
163168

0 commit comments

Comments
 (0)