Bug 996055 - Prevent url.hostname from clearing the hostname. r=mcmanus
authorValentin Gosu <valentin.gosu@gmail.com>
Wed, 23 Apr 2014 17:45:58 +0300
changeset 180244 c223611fa19ec5a591aa0f22b1b5df4f0c712d94
parent 180243 3be5a3af4e6a4dbfede7f25b588949933c59a288
child 180245 87f6ee3f86c1d39423145d65a18524477de0f534
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewersmcmanus
bugs996055
milestone31.0a1
Bug 996055 - Prevent url.hostname from clearing the hostname. r=mcmanus
dom/base/URL.cpp
dom/base/test/test_url.html
netwerk/base/src/nsStandardURL.cpp
--- a/dom/base/URL.cpp
+++ b/dom/base/URL.cpp
@@ -342,16 +342,18 @@ void
 URL::GetHostname(nsString& aHostname) const
 {
   URL_GETTER(aHostname, GetHost);
 }
 
 void
 URL::SetHostname(const nsAString& aHostname)
 {
+  // nsStandardURL returns NS_ERROR_UNEXPECTED for an empty hostname
+  // The return code is silently ignored
   mURI->SetHost(NS_ConvertUTF16toUTF8(aHostname));
 }
 
 void
 URL::GetPort(nsString& aPort) const
 {
   aPort.Truncate();
 
--- a/dom/base/test/test_url.html
+++ b/dom/base/test/test_url.html
@@ -1,22 +1,21 @@
 
 <!DOCTYPE HTML>
 <html>
-<!--
-https://bugzilla.mozilla.org/show_bug.cgi?id=887364
--->
 <head>
   <meta charset="utf-8">
-  <title>Test for Bug 887364</title>
+  <title>Test URL API</title>
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
 </head>
 <body>
 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=887364">Mozilla Bug 887364</a>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=991471">Mozilla Bug 991471</a>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=996055">Mozilla Bug 996055</a>
 <p id="display"></p>
 <div id="content" style="display: none">
   <iframe name="x" id="x"></iframe>
   <iframe name="y" id="y"></iframe>
 </div>
 <pre id="test">
 </pre>
   <script type="application/javascript">
@@ -270,10 +269,28 @@ https://bugzilla.mozilla.org/show_bug.cg
     if ('pathname' in test) is(test.pathname, url.pathname, "pathname");
     if ('search' in test) is(test.search, url.search, "search");
     if ('hash' in test) is(test.hash, url.hash, "hash");
 
     if ('href' in test) is (test.href, url + '', 'stringify works');
   }
 
   </script>
+
+  <script>
+    /** Test for Bug 991471 **/
+    var url = new URL("http://localhost/");
+    url.hostname = "";
+    url.username = "tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt";
+    url.hostname = "www.mozilla.org";
+    url.username = "";
+    url.hostname = "www.mozilla.org";
+    is(url.href, "http://www.mozilla.org/", "No parsing error with empty host");
+  </script>
+
+  <script>
+    /** Test for Bug 996055 **/
+    var url = new URL("http://localhost/");
+    url.hostname = "";
+    is(url.href, "http://localhost/", "Empty hostname is ignored");
+  </script>
 </body>
 </html>
--- a/netwerk/base/src/nsStandardURL.cpp
+++ b/netwerk/base/src/nsStandardURL.cpp
@@ -1459,16 +1459,22 @@ nsStandardURL::SetHost(const nsACString 
 
     LOG(("nsStandardURL::SetHost [host=%s]\n", host));
 
     if (mURLType == URLTYPE_NO_AUTHORITY) {
         if (flat.IsEmpty())
             return NS_OK;
         NS_WARNING("cannot set host on no-auth url");
         return NS_ERROR_UNEXPECTED;
+    } else {
+        if (flat.IsEmpty()) {
+            // Setting an empty hostname is not allowed for
+            // URLTYPE_STANDARD and URLTYPE_AUTHORITY.
+            return NS_ERROR_UNEXPECTED;
+        }
     }
 
     if (strlen(host) < flat.Length())
         return NS_ERROR_MALFORMED_URI; // found embedded null
 
     // For consistency with SetSpec/nsURLParsers, don't allow spaces
     // in the hostname.
     if (strchr(host, ' '))