diff --git a/lib/internal/http.js b/lib/internal/http.js index 4f250a2e70a20f..aa3ec354dabf4a 100644 --- a/lib/internal/http.js +++ b/lib/internal/http.js @@ -112,7 +112,9 @@ class ProxyConfig { const { host, hostname, port, protocol, username, password } = new URL(proxyUrl); this.href = proxyUrl; // Full URL of the proxy server. this.host = host; // Full host including port, e.g. 'localhost:8080'. - this.hostname = hostname.replace(/^\[|\]$/g, ''); // Trim off the brackets from IPv6 addresses. + // Trim off the brackets from IPv6 addresses. As it's parsed from a valid URL, an opening + // "[" Must already have a matching "]" at the end. + this.hostname = hostname[0] === '[' ? hostname.slice(1, -1) : hostname; this.port = port ? NumberParseInt(port, 10) : (protocol === 'https:' ? 443 : 80); this.protocol = protocol; // Protocol of the proxy server, e.g. 'http:' or 'https:'.