Bug 690307 - Make trimURL not generate URLs that parse back into search queries. r=mak, a=lmandel
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Sat, 01 Nov 2014 22:57:56 +0000
changeset 226012 ffb4891a237d
parent 226011 46829698a2b9
child 226013 9ebc7ee50a9c
push id4105
push userryanvm@gmail.com
push date2014-11-10 18:36 +0000
treeherdermozilla-beta@fa1706ebf845 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak, lmandel
bugs690307
milestone34.0
Bug 690307 - Make trimURL not generate URLs that parse back into search queries. r=mak, a=lmandel
browser/base/content/test/general/browser_urlbarTrimURLs.js
browser/base/content/utilityOverlay.js
--- a/browser/base/content/test/general/browser_urlbarTrimURLs.js
+++ b/browser/base/content/test/general/browser_urlbarTrimURLs.js
@@ -50,16 +50,19 @@ function test() {
   testVal("http://user:pass@[fe80::222:19ff:fe11:8c76]:666/file.ext");
 
   testVal("mailto:admin@mozilla.org");
   testVal("gopher://mozilla.org/");
   testVal("about:config");
   testVal("jar:http://mozilla.org/example.jar!/");
   testVal("view-source:http://mozilla.org/");
 
+  testVal("http://localhost");
+  testVal("http://someotherhostwithnodots");
+
   Services.prefs.setBoolPref(prefname, false);
 
   testVal("http://mozilla.org/");
 
   Services.prefs.setBoolPref(prefname, true);
 
   waitForExplicitFinish();
 
--- a/browser/base/content/utilityOverlay.js
+++ b/browser/base/content/utilityOverlay.js
@@ -707,13 +707,22 @@ function openPrefsHelp() {
 
   var helpTopic = document.getElementsByTagName("prefwindow")[0].currentPane.helpTopic;
   openHelpLink(helpTopic, !instantApply);
 }
 
 function trimURL(aURL) {
   // This function must not modify the given URL such that calling
   // nsIURIFixup::createFixupURI with the result will produce a different URI.
-  return aURL /* remove single trailing slash for http/https/ftp URLs */
-             .replace(/^((?:http|https|ftp):\/\/[^/]+)\/$/, "$1")
-              /* remove http:// unless the host starts with "ftp\d*\." or contains "@" */
-             .replace(/^http:\/\/((?!ftp\d*\.)[^\/@]+(?:\/|$))/, "$1");
+
+  // remove single trailing slash for http/https/ftp URLs
+  let rv = aURL.replace(/^((?:http|https|ftp):\/\/[^/]+)\/$/, "$1");
+
+  // Strip the leading http:// only if the host has at least one '.' or
+  // looks like an ipv6 ip:
+  let hostMatch = rv.match(/^http:\/\/([^\/]*)/);
+  let ipv6Regex = /\[[\da-f:]*\]/;
+  if (hostMatch && (hostMatch[1].contains(".") || ipv6Regex.test(hostMatch[1]))) {
+    /* remove http:// unless the host starts with "ftp\d*\." or contains "@" */
+    rv = rv.replace(/^http:\/\/((?!ftp\d*\.)[^\/@]+(?:\/|$))/, "$1");
+  }
+  return rv;
 }