Skip to content

Commit 5d1741b

Browse files
committed
Fix FirefoxDriver's parsing of IPv6 hostports when setting proxy configuration.
1 parent 6435986 commit 5d1741b

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

javascript/firefox-driver/js/proxy.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,35 @@ fxdriver.proxy.ProxyConfig;
4545
fxdriver.proxy.LOG_ = fxdriver.logging.getLogger('fxdriver.proxy');
4646

4747

48+
/**
49+
* Splits a hostport, e.g., 'www.example.com:80' into host and port.
50+
* @param {string} hostport A hostport.
51+
* @return {{host: string, port: ?number}} A host and port. If no port is
52+
* present in the argument hostport, port is null.
53+
* @private
54+
*/
55+
fxdriver.proxy.splitHostPort_ = function(hostport) {
56+
var colonIndex = hostport.lastIndexOf(':');
57+
if (colonIndex < 0) {
58+
return {host: hostport, port: null};
59+
}
60+
var secondColonIndex = hostport.indexOf(':');
61+
if (secondColonIndex != colonIndex && !hostport.includes('[')) {
62+
// The hostport contains multiple colons, but no part of it is bracketed,
63+
// e.g., '2001:4860:4860::8888'. Treat it as an IPv6 literal with no port.
64+
return {host: hostport, port: null};
65+
}
66+
67+
var host = hostport.slice(0, colonIndex);
68+
// Strip brackets from bracketed IPv6 hosts, e.g., '[::1]'.
69+
if (host.startsWith('[') && host.endsWith(']')) {
70+
host = host.slice(1, -1);
71+
}
72+
var portStr = hostport.slice(colonIndex + 1);
73+
return {host: host, port: parseInt(portStr, 10)};
74+
};
75+
76+
4877
/**
4978
* Set a specific proxy preference.
5079
*
@@ -57,10 +86,12 @@ fxdriver.proxy.setProxyPreference_ = function(prefs, type, setting) {
5786
if (!setting) {
5887
return;
5988
}
60-
var hostPort = setting.split(':');
61-
prefs.setCharPref('network.proxy.' + type, hostPort[0]);
62-
if (hostPort.length > 1) {
63-
prefs.setIntPref('network.proxy.' + type + '_port', parseInt(hostPort[1]));
89+
var hostAndPort = fxdriver.proxy.splitHostPort_(setting);
90+
var host = hostAndPort.host;
91+
var port = hostAndPort.port;
92+
prefs.setCharPref('network.proxy.' + type, host);
93+
if (port != null) {
94+
prefs.setIntPref('network.proxy.' + type + '_port', port);
6495
}
6596
};
6697

0 commit comments

Comments
 (0)