Bug 1507991 - Part 2: Serialize domain in ContentPrincipalInfo, r=baku
authorNika Layzell <nika@thelayzells.com>
Thu, 28 Feb 2019 16:50:28 +0000
changeset 519642 f02a51eb1d7b49a55c5fd9e9ece10e1143967690
parent 519641 58296c56129ba56cce696004f1e37511f73285d6
child 519643 e865352417c9483eaf6d8e1315a49e33d12c9391
push id10862
push userffxbld-merge
push dateMon, 11 Mar 2019 13:01:11 +0000
treeherdermozilla-beta@a2e7f5c935da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1507991
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1507991 - Part 2: Serialize domain in ContentPrincipalInfo, r=baku This is needed to maintain full feature parity with the existing nsIPrincipal serializer while switching to using the PrincipalInfo-based one. Depends on D14434 Differential Revision: https://phabricator.services.mozilla.com/D20854
caps/nsJSPrincipals.cpp
dom/cache/DBSchema.cpp
dom/serviceworkers/ServiceWorkerRegistrar.cpp
dom/serviceworkers/test/gtest/TestReadWrite.cpp
ipc/glue/BackgroundUtils.cpp
ipc/glue/PBackgroundSharedTypes.ipdlh
--- a/caps/nsJSPrincipals.cpp
+++ b/caps/nsJSPrincipals.cpp
@@ -240,18 +240,19 @@ static bool ReadPrincipalInfo(JSStructur
 #ifdef FUZZING
     if (originNoSuffix.IsEmpty()) {
       return false;
     }
 #endif
 
     MOZ_DIAGNOSTIC_ASSERT(!originNoSuffix.IsEmpty());
 
-    aInfo =
-        ContentPrincipalInfo(attrs, originNoSuffix, spec, std::move(policies));
+    // XXX: Do we care about mDomain for structured clone?
+    aInfo = ContentPrincipalInfo(attrs, originNoSuffix, spec, Nothing(),
+                                 std::move(policies));
   } else {
 #ifdef FUZZING
     return false;
 #else
     MOZ_CRASH("unexpected principal structured clone tag");
 #endif
   }
 
--- a/dom/cache/DBSchema.cpp
+++ b/dom/cache/DBSchema.cpp
@@ -2491,17 +2491,17 @@ nsresult ReadResponse(mozIStorageConnect
 
     nsCString origin;
     url->Origin(origin);
 
     // CSP is recovered from the headers, no need to initialise it here.
     nsTArray<mozilla::ipc::ContentSecurityPolicy> policies;
     aSavedResponseOut->mValue.principalInfo() =
         mozilla::ipc::ContentPrincipalInfo(attrs, origin, specNoSuffix,
-                                           std::move(policies));
+                                           Nothing(), std::move(policies));
   }
 
   bool nullPadding = false;
   rv = state->GetIsNull(6, &nullPadding);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
--- a/dom/serviceworkers/ServiceWorkerRegistrar.cpp
+++ b/dom/serviceworkers/ServiceWorkerRegistrar.cpp
@@ -109,17 +109,17 @@ nsresult CreatePrincipalInfo(nsILineInpu
   rv = GetOrigin(aEntry->scope(), origin);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
   // CSP will be applied during the script load.
   nsTArray<mozilla::ipc::ContentSecurityPolicy> policies;
   aEntry->principal() = mozilla::ipc::ContentPrincipalInfo(
-      attrs, origin, aEntry->scope(), std::move(policies));
+      attrs, origin, aEntry->scope(), Nothing(), std::move(policies));
 
   return NS_OK;
 }
 
 }  // namespace
 
 NS_IMPL_ISUPPORTS(ServiceWorkerRegistrar, nsIObserver, nsIAsyncShutdownBlocker)
 
--- a/dom/serviceworkers/test/gtest/TestReadWrite.cpp
+++ b/dom/serviceworkers/test/gtest/TestReadWrite.cpp
@@ -269,17 +269,18 @@ TEST(ServiceWorkerRegistrar, TestWriteDa
       reg.currentWorkerActivatedTime() = PR_Now();
       reg.lastUpdateTime() = PR_Now();
 
       nsAutoCString spec;
       spec.AppendPrintf("spec write %d", i);
 
       nsTArray<mozilla::ipc::ContentSecurityPolicy> policies;
       reg.principal() = mozilla::ipc::ContentPrincipalInfo(
-          mozilla::OriginAttributes(i, i % 2), spec, spec, std::move(policies));
+          mozilla::OriginAttributes(i, i % 2), spec, spec, mozilla::Nothing(),
+          std::move(policies));
 
       swr->TestRegisterServiceWorker(reg);
     }
 
     nsresult rv = swr->TestWriteData();
     ASSERT_EQ(NS_OK, rv) << "WriteData() should not fail";
   }
 
@@ -858,17 +859,18 @@ TEST(ServiceWorkerRegistrar, TestDedupeW
       reg.updateViaCache() =
           nsIServiceWorkerRegistrationInfo::UPDATE_VIA_CACHE_IMPORTS;
 
       nsAutoCString spec;
       spec.AppendPrintf("spec write dedupe/%d", i);
 
       nsTArray<mozilla::ipc::ContentSecurityPolicy> policies;
       reg.principal() = mozilla::ipc::ContentPrincipalInfo(
-          mozilla::OriginAttributes(0, false), spec, spec, std::move(policies));
+          mozilla::OriginAttributes(0, false), spec, spec, mozilla::Nothing(),
+          std::move(policies));
 
       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
@@ -99,16 +99,29 @@ already_AddRefed<nsIPrincipal> Principal
       // Origin must match what the_new_principal.getOrigin returns.
       nsAutoCString originNoSuffix;
       rv = principal->GetOriginNoSuffix(originNoSuffix);
       if (NS_WARN_IF(NS_FAILED(rv)) ||
           !info.originNoSuffix().Equals(originNoSuffix)) {
         MOZ_CRASH("Origin must be available when deserialized");
       }
 
+      if (info.domain()) {
+        nsCOMPtr<nsIURI> domain;
+        rv = NS_NewURI(getter_AddRefs(domain), *info.domain());
+        if (NS_WARN_IF(NS_FAILED(rv))) {
+          return nullptr;
+        }
+
+        rv = principal->SetDomain(domain);
+        if (NS_WARN_IF(NS_FAILED(rv))) {
+          return nullptr;
+        }
+      }
+
       if (info.securityPolicies().Length() > 0) {
         nsCOMPtr<nsIContentSecurityPolicy> csp =
             do_CreateInstance(NS_CSPCONTEXT_CONTRACTID, &rv);
         if (NS_WARN_IF(NS_FAILED(rv))) {
           return nullptr;
         }
 
         rv = csp->SetRequestContext(nullptr, principal);
@@ -271,30 +284,45 @@ nsresult PrincipalToPrincipalInfo(nsIPri
   }
 
   nsCString originNoSuffix;
   rv = aPrincipal->GetOriginNoSuffix(originNoSuffix);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
+  nsCOMPtr<nsIURI> domainUri;
+  rv = aPrincipal->GetDomain(getter_AddRefs(domainUri));
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+
+  Maybe<nsCString> domain;
+  if (domainUri) {
+    domain.emplace();
+    rv = domainUri->GetSpec(domain.ref());
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return rv;
+    }
+  }
+
   nsCOMPtr<nsIContentSecurityPolicy> csp;
   rv = aPrincipal->GetCsp(getter_AddRefs(csp));
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
   nsTArray<ContentSecurityPolicy> policies;
   if (csp) {
     PopulateContentSecurityPolicies(csp, policies);
   }
 
   *aPrincipalInfo =
       ContentPrincipalInfo(aPrincipal->OriginAttributesRef(), originNoSuffix,
-                           spec, std::move(policies));
+                           spec, domain, std::move(policies));
   return NS_OK;
 }
 
 bool IsPincipalInfoPrivate(const PrincipalInfo& aPrincipalInfo) {
   if (aPrincipalInfo.type() != ipc::PrincipalInfo::TContentPrincipalInfo) {
     return false;
   }
 
--- a/ipc/glue/PBackgroundSharedTypes.ipdlh
+++ b/ipc/glue/PBackgroundSharedTypes.ipdlh
@@ -25,16 +25,18 @@ struct ContentPrincipalInfo
   // on.
   // Another important reason why we have this attribute is that
   // ContentPrincipalInfo is used out of the main-thread. Having this value
   // here allows us to retrive the origin without creating a full nsIPrincipal.
   nsCString originNoSuffix;
 
   nsCString spec;
 
+  nsCString? domain;
+
   ContentSecurityPolicy[] securityPolicies;
 };
 
 struct SystemPrincipalInfo
 { };
 
 struct NullPrincipalInfo
 {