Bug 1101584 - sContentUtils::GetUTFOrigin should check if the URI has the nsIURIWithPrincipal interface, r=bz, r=smaug
authorAndrea Marchesini <amarchesini@mozilla.com>
Wed, 19 Nov 2014 11:40:00 +0100
changeset 216632 1c5592132500082db9c53827cedff1df7d0e7941
parent 216631 68dd715f6f79a4fc540b7fffeec047cfe4dc3b6a
child 216633 f52a81db4ecd7a59d65354c026478f9f278a236c
push idunknown
push userunknown
push dateunknown
reviewersbz, smaug
bugs1101584
milestone36.0a1
Bug 1101584 - sContentUtils::GetUTFOrigin should check if the URI has the nsIURIWithPrincipal interface, r=bz, r=smaug
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,31 @@ nsContentUtils::GetASCIIOrigin(nsIPrinci
 }
 
 /* static */
 nsresult
 nsContentUtils::GetASCIIOrigin(nsIURI* aURI, nsCString& aOrigin)
 {
   NS_PRECONDITION(aURI, "missing uri");
 
+  // For Blob URI we have to return the origin of page using its principal.
+  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 && 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 +5819,31 @@ nsContentUtils::GetUTFOrigin(nsIPrincipa
 }
 
 /* static */
 nsresult
 nsContentUtils::GetUTFOrigin(nsIURI* aURI, nsString& aOrigin)
 {
   NS_PRECONDITION(aURI, "missing uri");
 
+  // For Blob URI we have to return the origin of page using its principal.
+  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 && 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>