Bug 1338154 - not normalize ipv4 for resource and chrome uri. r=valentin a=gchang
authorJunior Hsu <juhsu@mozilla.com>
Mon, 13 Feb 2017 03:10:00 +0800
changeset 376204 6db1cdc5eedd295f9c984b21c73859c4de992f72
parent 376203 7f305a9951d3fa7af72c49a705826deddfb68a30
child 376205 6898429f8828083e114ca5c5a07567627298366c
push id6996
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 20:48:21 +0000
treeherdermozilla-beta@d89512dab048 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersvalentin, gchang
bugs1338154
milestone53.0a2
Bug 1338154 - not normalize ipv4 for resource and chrome uri. r=valentin a=gchang
netwerk/base/nsStandardURL.cpp
netwerk/test/mochitests/test_uri_scheme.html
netwerk/test/unit/test_standardurl.js
--- a/netwerk/base/nsStandardURL.cpp
+++ b/netwerk/base/nsStandardURL.cpp
@@ -765,20 +765,22 @@ nsStandardURL::BuildNormalizedSpec(const
         if (tempHost.Contains('\0'))
             return NS_ERROR_MALFORMED_URI;  // null embedded in hostname
         if (tempHost.Contains(' '))
             return NS_ERROR_MALFORMED_URI;  // don't allow spaces in the hostname
         nsresult rv = NormalizeIDN(tempHost, encHost);
         if (NS_FAILED(rv)) {
             return rv;
         }
-        nsAutoCString ipString;
-        rv = NormalizeIPv4(encHost, ipString);
-        if (NS_SUCCEEDED(rv)) {
-          encHost = ipString;
+        if (!SegmentIs(spec, mScheme, "resource") &&
+            !SegmentIs(spec, mScheme, "chrome")) {
+            nsAutoCString ipString;
+            if (NS_SUCCEEDED(NormalizeIPv4(encHost, ipString))) {
+                encHost = ipString;
+            }
         }
 
         // NormalizeIDN always copies, if the call was successful.
         useEncHost = true;
         approxLen += encHost.Length();
 
         if (!ValidIPv6orHostname(encHost.BeginReading(), encHost.Length())) {
             return NS_ERROR_MALFORMED_URI;
@@ -2000,20 +2002,21 @@ nsStandardURL::SetHost(const nsACString 
 
     uint32_t len;
     nsAutoCString hostBuf;
     nsresult rv = NormalizeIDN(flat, hostBuf);
     if (NS_FAILED(rv)) {
         return rv;
     }
 
-    nsAutoCString ipString;
-    rv = NormalizeIPv4(hostBuf, ipString);
-    if (NS_SUCCEEDED(rv)) {
-      hostBuf = ipString;
+    if (!SegmentIs(mScheme, "resource") && !SegmentIs(mScheme, "chrome")) {
+        nsAutoCString ipString;
+        if (NS_SUCCEEDED(NormalizeIPv4(hostBuf, ipString))) {
+          hostBuf = ipString;
+        }
     }
 
     // NormalizeIDN always copies if the call was successful
     host = hostBuf.get();
     len = hostBuf.Length();
 
     if (!ValidIPv6orHostname(host, len)) {
         return NS_ERROR_MALFORMED_URI;
--- a/netwerk/test/mochitests/test_uri_scheme.html
+++ b/netwerk/test/mochitests/test_uri_scheme.html
@@ -26,16 +26,22 @@ function dotest2()
   try {
       o.href ="aaaaaaaaaaa:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
   } catch(e) { }
   o.hash = "#";
   o.pathname = "/";
   o.protocol = "http:";
   try { o.href = "http://localhost/"; } catch(e) { }
   ok(o.protocol, "http:");
+  dotest3();
+}
+
+function dotest3()
+{
+  is(new URL("resource://123/").href, "resource://123/");
   SimpleTest.finish();
 }
 </script>
 </head>
 <body onload="dotest1();">
 <p id="display"></p>
 <div id="content" style="display: none"></div>
 <pre id="test">
--- a/netwerk/test/unit/test_standardurl.js
+++ b/netwerk/test/unit/test_standardurl.js
@@ -396,16 +396,17 @@ add_test(function test_ipv4Normalize()
      "http://017700000001",
      "http://0x7f.0x00.0x00.0x01",
      "http://0x7f.0x01",
      "http://0x7f000001",
      "http://0x007f.0x0000.0x0000.0x0001",
      "http://000177.0.00000.0x0001",
      "http://127.0.0.1.",
     ].map(stringToURL);
+
   var url;
   for (url of localIPv4s) {
     do_check_eq(url.spec, "http://127.0.0.1/");
   }
 
   // These should treated as a domain instead of an IPv4.
   var nonIPv4s =
     ["http://0xfffffffff/",
@@ -418,23 +419,29 @@ add_test(function test_ipv4Normalize()
      "http://-1.0.0.0/",
      "http://1.2.3.4.5/",
      "http://010000000000000000/",
      "http://2+3/",
      "http://0.0.0.-1/",
      "http://1.2.3.4../",
      "http://1..2/",
      "http://.1.2.3.4/",
+     "resource://123/",
+     "resource://4294967296/",
     ];
   var spec;
   for (spec of nonIPv4s) {
     url = stringToURL(spec);
     do_check_eq(url.spec, spec);
   }
 
+  var url = stringToURL("resource://path/to/resource/");
+  url.host = "123";
+  do_check_eq(url.host, "123");
+
   run_next_test();
 });
 
 add_test(function test_invalidHostChars() {
   var url = stringToURL("http://example.org/");
   for (let i = 0; i <= 0x20; i++) {
     Assert.throws(() => { url.host = "a" + String.fromCharCode(i) + "b"; }, "Trying to set hostname containing char code: " + i);
   }