Bug 1340163 - Introducing originNoSuffix as attribute in ContentPrincipalInfo. r=smaug, a=jcristau
authorAndrea Marchesini <amarchesini@mozilla.com>
Mon, 20 Mar 2017 16:03:45 +0100
changeset 355618 f3f2a995a239280091abfe6f6dab8ce413cf64cf
parent 355617 0618256da57a335d1daf998f26fededd95fa13c8
child 355619 330904d6f0dc54686a5ef771ebebed10ffedfcd9
push id7017
push userryanvm@gmail.com
push dateMon, 10 Apr 2017 16:40:06 +0000
treeherdermozilla-esr52@49aadb25b1ec [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug, jcristau
bugs1340163
milestone52.0.3
Bug 1340163 - Introducing originNoSuffix as attribute in ContentPrincipalInfo. r=smaug, a=jcristau
caps/nsJSPrincipals.cpp
dom/cache/DBSchema.cpp
dom/workers/ServiceWorkerRegistrar.cpp
dom/workers/test/gtest/TestReadWrite.cpp
ipc/glue/BackgroundUtils.cpp
ipc/glue/PBackgroundSharedTypes.ipdlh
--- a/caps/nsJSPrincipals.cpp
+++ b/caps/nsJSPrincipals.cpp
@@ -130,26 +130,32 @@ ReadSuffixAndSpec(JSStructuredCloneReade
                   nsACString& aSpec)
 {
     uint32_t suffixLength, specLength;
     if (!JS_ReadUint32Pair(aReader, &suffixLength, &specLength)) {
         return false;
     }
 
     nsAutoCString suffix;
-    suffix.SetLength(suffixLength);
+    if (!suffix.SetLength(suffixLength, fallible)) {
+        return false;
+    }
+
     if (!JS_ReadBytes(aReader, suffix.BeginWriting(), suffixLength)) {
         return false;
     }
 
     if (!aAttrs.PopulateFromSuffix(suffix)) {
         return false;
     }
 
-    aSpec.SetLength(specLength);
+    if (!aSpec.SetLength(specLength, fallible)) {
+        return false;
+    }
+
     if (!JS_ReadBytes(aReader, aSpec.BeginWriting(), specLength)) {
         return false;
     }
 
     return true;
 }
 
 static bool
@@ -190,17 +196,17 @@ ReadPrincipalInfo(JSStructuredCloneReade
         aInfo = expanded;
     } else if (aTag == SCTAG_DOM_CONTENT_PRINCIPAL) {
         PrincipalOriginAttributes attrs;
         nsAutoCString spec;
         if (!ReadSuffixAndSpec(aReader, attrs, spec)) {
             return false;
         }
 
-        aInfo = ContentPrincipalInfo(attrs, spec);
+        aInfo = ContentPrincipalInfo(attrs, void_t(), spec);
     } else {
         MOZ_CRASH("unexpected principal structured clone tag");
     }
 
     return true;
 }
 
 /* static */ bool
--- a/dom/cache/DBSchema.cpp
+++ b/dom/cache/DBSchema.cpp
@@ -1966,25 +1966,25 @@ ReadResponse(mozIStorageConnection* aCon
   }
 
   nsAutoCString serializedInfo;
   rv = state->GetUTF8String(5, serializedInfo);
   if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
 
   aSavedResponseOut->mValue.principalInfo() = void_t();
   if (!serializedInfo.IsEmpty()) {
-    nsAutoCString originNoSuffix;
+    nsAutoCString specNoSuffix;
     PrincipalOriginAttributes attrs;
-    if (!attrs.PopulateFromOrigin(serializedInfo, originNoSuffix)) {
+    if (!attrs.PopulateFromOrigin(serializedInfo, specNoSuffix)) {
       NS_WARNING("Something went wrong parsing a serialized principal!");
       return NS_ERROR_FAILURE;
     }
 
     aSavedResponseOut->mValue.principalInfo() =
-      mozilla::ipc::ContentPrincipalInfo(attrs, originNoSuffix);
+      mozilla::ipc::ContentPrincipalInfo(attrs, void_t(), specNoSuffix);
   }
 
   rv = state->GetBlobAsUTF8String(6, aSavedResponseOut->mValue.channelInfo().securityInfo());
   if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
 
   rv = aConn->CreateStatement(NS_LITERAL_CSTRING(
     "SELECT "
       "name, "
--- a/dom/workers/ServiceWorkerRegistrar.cpp
+++ b/dom/workers/ServiceWorkerRegistrar.cpp
@@ -347,17 +347,17 @@ ServiceWorkerRegistrar::ReadData()
       PrincipalOriginAttributes attrs;
       if (!attrs.PopulateFromSuffix(suffix)) {
         return NS_ERROR_INVALID_ARG;
       }
 
       GET_LINE(entry->scope());
 
       entry->principal() =
-        mozilla::ipc::ContentPrincipalInfo(attrs, entry->scope());
+        mozilla::ipc::ContentPrincipalInfo(attrs, void_t(), entry->scope());
 
       GET_LINE(entry->currentWorkerURL());
 
       nsAutoCString cacheName;
       GET_LINE(cacheName);
       CopyUTF8toUTF16(cacheName, entry->cacheName());
     } else if (version.EqualsLiteral("3")) {
       overwrite = true;
@@ -372,17 +372,17 @@ ServiceWorkerRegistrar::ReadData()
       }
 
       // principal spec is no longer used; we use scope directly instead
       GET_LINE(unused);
 
       GET_LINE(entry->scope());
 
       entry->principal() =
-        mozilla::ipc::ContentPrincipalInfo(attrs, entry->scope());
+        mozilla::ipc::ContentPrincipalInfo(attrs, void_t(), entry->scope());
 
       GET_LINE(entry->currentWorkerURL());
 
       nsAutoCString cacheName;
       GET_LINE(cacheName);
       CopyUTF8toUTF16(cacheName, entry->cacheName());
     } else if (version.EqualsLiteral("2")) {
       overwrite = true;
@@ -397,17 +397,17 @@ ServiceWorkerRegistrar::ReadData()
       }
 
       // principal spec is no longer used; we use scope directly instead
       GET_LINE(unused);
 
       GET_LINE(entry->scope());
 
       entry->principal() =
-        mozilla::ipc::ContentPrincipalInfo(attrs, entry->scope());
+        mozilla::ipc::ContentPrincipalInfo(attrs, void_t(), entry->scope());
 
       // scriptSpec is no more used in latest version.
       GET_LINE(unused);
 
       GET_LINE(entry->currentWorkerURL());
 
       nsAutoCString cacheName;
       GET_LINE(cacheName);
--- a/dom/workers/test/gtest/TestReadWrite.cpp
+++ b/dom/workers/test/gtest/TestReadWrite.cpp
@@ -224,17 +224,18 @@ TEST(ServiceWorkerRegistrar, TestWriteDa
       reg.scope() = nsPrintfCString("scope write %d", i);
       reg.currentWorkerURL() = nsPrintfCString("currentWorkerURL write %d", i);
       reg.cacheName() =
         NS_ConvertUTF8toUTF16(nsPrintfCString("cacheName write %d", i));
 
       nsAutoCString spec;
       spec.AppendPrintf("spec write %d", i);
       reg.principal() =
-        mozilla::ipc::ContentPrincipalInfo(mozilla::PrincipalOriginAttributes(i, i % 2), spec);
+        mozilla::ipc::ContentPrincipalInfo(mozilla::PrincipalOriginAttributes(i, i % 2),
+                                           mozilla::void_t(), spec);
 
       swr->TestRegisterServiceWorker(reg);
     }
 
     nsresult rv = swr->TestWriteData();
     ASSERT_EQ(NS_OK, rv) << "WriteData() should not fail";
   }
 
@@ -448,17 +449,18 @@ TEST(ServiceWorkerRegistrar, TestDedupeW
       reg.scope() = NS_LITERAL_CSTRING("scope write dedupe");
       reg.currentWorkerURL() = nsPrintfCString("currentWorkerURL write %d", i);
       reg.cacheName() =
         NS_ConvertUTF8toUTF16(nsPrintfCString("cacheName write %d", i));
 
       nsAutoCString spec;
       spec.AppendPrintf("spec write dedupe/%d", i);
       reg.principal() =
-        mozilla::ipc::ContentPrincipalInfo(mozilla::PrincipalOriginAttributes(0, false), spec);
+        mozilla::ipc::ContentPrincipalInfo(mozilla::PrincipalOriginAttributes(0, false),
+                                           mozilla::void_t(), spec);
 
       swr->TestRegisterServiceWorker(reg);
     }
 
     nsresult rv = swr->TestWriteData();
     ASSERT_EQ(NS_OK, rv) << "WriteData() should not fail";
   }
 
--- a/ipc/glue/BackgroundUtils.cpp
+++ b/ipc/glue/BackgroundUtils.cpp
@@ -82,16 +82,28 @@ PrincipalInfoToPrincipal(const Principal
         attrs = info.attrs();
       }
       principal = BasePrincipal::CreateCodebasePrincipal(uri, attrs);
       rv = principal ? NS_OK : NS_ERROR_FAILURE;
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return nullptr;
       }
 
+      // When the principal is serialized, the origin is extract from it. This
+      // can fail, and in case, here we will havea Tvoid_t. If we have a string,
+      // it must match with what the_new_principal.getOrigin returns.
+      if (info.originNoSuffix().type() == ContentPrincipalInfoOriginNoSuffix::TnsCString) {
+        nsAutoCString originNoSuffix;
+        rv = principal->GetOriginNoSuffix(originNoSuffix);
+        if (NS_WARN_IF(NS_FAILED(rv)) ||
+            !info.originNoSuffix().get_nsCString().Equals(originNoSuffix)) {
+          MOZ_CRASH("If the origin was in the contentPrincipalInfo, it must be available when deserialized");
+        }
+      }
+
       return principal.forget();
     }
 
     case PrincipalInfo::TExpandedPrincipalInfo: {
       const ExpandedPrincipalInfo& info = aPrincipalInfo.get_ExpandedPrincipalInfo();
 
       nsTArray<nsCOMPtr<nsIPrincipal>> whitelist;
       nsCOMPtr<nsIPrincipal> wlPrincipal;
@@ -192,18 +204,28 @@ PrincipalToPrincipalInfo(nsIPrincipal* a
   }
 
   nsCString spec;
   rv = uri->GetSpec(spec);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
+  ContentPrincipalInfoOriginNoSuffix infoOriginNoSuffix;
+
+  nsCString originNoSuffix;
+  rv = aPrincipal->GetOriginNoSuffix(originNoSuffix);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    infoOriginNoSuffix = void_t();
+  } else {
+    infoOriginNoSuffix = originNoSuffix;
+  }
+
   *aPrincipalInfo = ContentPrincipalInfo(BasePrincipal::Cast(aPrincipal)->OriginAttributesRef(),
-                                         spec);
+                                         infoOriginNoSuffix, spec);
   return NS_OK;
 }
 
 nsresult
 LoadInfoToLoadInfoArgs(nsILoadInfo *aLoadInfo,
                        OptionalLoadInfoArgs* aOptionalLoadInfoArgs)
 {
   if (!aLoadInfo) {
--- a/ipc/glue/PBackgroundSharedTypes.ipdlh
+++ b/ipc/glue/PBackgroundSharedTypes.ipdlh
@@ -3,19 +3,31 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 using mozilla::PrincipalOriginAttributes from "mozilla/ipc/BackgroundUtils.h";
 using struct mozilla::void_t from "ipc/IPCMessageUtils.h";
 
 namespace mozilla {
 namespace ipc {
 
+union ContentPrincipalInfoOriginNoSuffix
+{
+  nsCString;
+  void_t;
+};
+
 struct ContentPrincipalInfo
 {
   PrincipalOriginAttributes attrs;
+
+  // nsIPrincipal.originNoSuffix can fail. In case this happens, this value
+  // will be set to void_t. So far, this is used only for dom/media.
+  // It will be removed in bug 1347817.
+  ContentPrincipalInfoOriginNoSuffix originNoSuffix;
+
   nsCString spec;
 };
 
 struct SystemPrincipalInfo
 { };
 
 struct NullPrincipalInfo
 {