Bug 1101584 - sContentUtils::GetUTFOrigin should check if the URI has the nsIURIWithPrincipal interface, r=bz
☠☠ backed out by df4b53792111 ☠ ☠
authorAndrea Marchesini <amarchesini@mozilla.com>
Wed, 19 Nov 2014 16:32:47 +0000
changeset 216501 8e604ebfc31388fefc9a8647bde4734e6f97eb87
parent 216500 f8863cbd49cbabec9baf8749e0ee2cad4c108657
child 216502 410efdab71211281d60d3600e64bb737141692a0
push idunknown
push userunknown
push dateunknown
reviewersbz
bugs1101584
milestone36.0a1
Bug 1101584 - sContentUtils::GetUTFOrigin should check if the URI has the nsIURIWithPrincipal interface, r=bz
dom/base/URL.cpp
dom/base/nsContentUtils.cpp
dom/base/test/test_url.html
--- a/dom/base/URL.cpp
+++ b/dom/base/URL.cpp
@@ -234,27 +234,16 @@ URL::SetHref(const nsAString& aHref, Err
 
   mURI = uri;
   UpdateURLSearchParams();
 }
 
 void
 URL::GetOrigin(nsString& aOrigin, ErrorResult& aRv) const
 {
-  nsCOMPtr<nsIURIWithPrincipal> uriWithPrincipal = do_QueryInterface(mURI);
-  if (uriWithPrincipal) {
-    nsCOMPtr<nsIPrincipal> principal;
-    uriWithPrincipal->GetPrincipal(getter_AddRefs(principal));
-
-    if (principal) {
-      nsContentUtils::GetUTFOrigin(principal, aOrigin);
-      return;
-    }
-  }
-
   nsContentUtils::GetUTFOrigin(mURI, aOrigin);
 }
 
 void
 URL::GetProtocol(nsString& aProtocol, ErrorResult& aRv) const
 {
   nsCString protocol;
   if (NS_SUCCEEDED(mURI->GetScheme(protocol))) {
--- a/dom/base/nsContentUtils.cpp
+++ b/dom/base/nsContentUtils.cpp
@@ -5746,16 +5746,30 @@ nsContentUtils::GetASCIIOrigin(nsIPrinci
 }
 
 /* static */
 nsresult
 nsContentUtils::GetASCIIOrigin(nsIURI* aURI, nsCString& aOrigin)
 {
   NS_PRECONDITION(aURI, "missing uri");
 
+  nsCOMPtr<nsIURIWithPrincipal> uriWithPrincipal = do_QueryInterface(aURI);
+  if (uriWithPrincipal) {
+    nsCOMPtr<nsIPrincipal> principal;
+    uriWithPrincipal->GetPrincipal(getter_AddRefs(principal));
+
+    nsCOMPtr<nsIURI> uri;
+    nsresult rv = principal->GetURI(getter_AddRefs(uri));
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    if (uri != aURI) {
+      return GetASCIIOrigin(uri, aOrigin);
+    }
+  }
+
   aOrigin.Truncate();
 
   nsCOMPtr<nsIURI> uri = NS_GetInnermostURI(aURI);
   NS_ENSURE_TRUE(uri, NS_ERROR_UNEXPECTED);
 
   nsCString host;
   nsresult rv = uri->GetAsciiHost(host);
 
@@ -5804,16 +5818,30 @@ nsContentUtils::GetUTFOrigin(nsIPrincipa
 }
 
 /* static */
 nsresult
 nsContentUtils::GetUTFOrigin(nsIURI* aURI, nsString& aOrigin)
 {
   NS_PRECONDITION(aURI, "missing uri");
 
+  nsCOMPtr<nsIURIWithPrincipal> uriWithPrincipal = do_QueryInterface(aURI);
+  if (uriWithPrincipal) {
+    nsCOMPtr<nsIPrincipal> principal;
+    uriWithPrincipal->GetPrincipal(getter_AddRefs(principal));
+
+    nsCOMPtr<nsIURI> uri;
+    nsresult rv = principal->GetURI(getter_AddRefs(uri));
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    if (uri != aURI) {
+      return GetUTFOrigin(uri, aOrigin);
+    }
+  }
+
   aOrigin.Truncate();
 
   nsCOMPtr<nsIURI> uri = NS_GetInnermostURI(aURI);
   NS_ENSURE_TRUE(uri, NS_ERROR_UNEXPECTED);
 
   nsCString host;
   nsresult rv = uri->GetHost(host);
 
--- a/dom/base/test/test_url.html
+++ b/dom/base/test/test_url.html
@@ -322,10 +322,19 @@
 
   <script>
     var blob = new Blob(['a']);
     var url = URL.createObjectURL(blob);
 
     var u = new URL(url);
     ok(u.origin, 'http://mochi.test:8888', "The URL generated from a blob URI has an origin");
   </script>
+
+  <script>
+    var blob = new Blob(['a']);
+    var url = URL.createObjectURL(blob);
+
+    var a = document.createElement('A');
+    a.href = url;
+    ok(a.origin, 'http://mochi.test:8888', "The 'a' element has the correct origin");
+  </script>
 </body>
 </html>