Bug 1380263 - data: URI should inherit origin attributes. r=smaug
authorYoshi Huang <allstars.chh@mozilla.com>
Tue, 18 Jul 2017 17:53:14 +0800
changeset 418333 c869a61f3eab89c40444689a9685896b42a2b19d
parent 418332 57f7743d4330047ae3ec2737836b7bc523fb3a97
child 418334 9dc2a03c3574769c85305450afedb19672960f5e
push id7566
push usermtabara@mozilla.com
push dateWed, 02 Aug 2017 08:25:16 +0000
treeherdermozilla-beta@86913f512c3c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1380263
milestone56.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 1380263 - data: URI should inherit origin attributes. r=smaug
browser/components/originattributes/test/browser/browser_firstPartyIsolation_aboutPages.js
docshell/base/nsDocShell.cpp
--- a/browser/components/originattributes/test/browser/browser_firstPartyIsolation_aboutPages.js
+++ b/browser/components/originattributes/test/browser/browser_firstPartyIsolation_aboutPages.js
@@ -78,21 +78,18 @@ add_task(async function test_remote_wind
   });
 
   await ContentTask.spawn(browser, {}, async function() {
     info("origin: " + content.document.nodePrincipal.origin);
 
     Assert.ok(content.document.nodePrincipal.isNullPrincipal,
               "The principal of data: document should be a NullPrincipal.");
 
-    let str = content.document.nodePrincipal.originNoSuffix;
-    let expectDomain = str.substring("moz-nullprincipal:{".length, str.length - 1) + ".mozilla";
-    Assert.equal(content.document.nodePrincipal.originAttributes.firstPartyDomain,
-                 expectDomain,
-                 "data: URI should have firstPartyDomain set to " + expectDomain);
+    Assert.ok(content.document.nodePrincipal.originAttributes.firstPartyDomain != "",
+              "data: URI should have firstPartyDomain set.");
   });
 
   win.close();
 });
 
 /**
  * data: document contains an iframe, and we test that iframe should inherit
  * origin attributes from the data: document.
@@ -111,24 +108,21 @@ add_task(async function test_remote_wind
     info("origin " + content.document.nodePrincipal.origin);
 
     let iframe = content.document.getElementById("iframe1");
     info("iframe principal: " + iframe.contentDocument.nodePrincipal.origin);
 
     Assert.ok(content.document.nodePrincipal.isNullPrincipal,
               "The principal of data: document should be a NullPrincipal.");
 
-    let str = content.document.nodePrincipal.originNoSuffix;
-    let expectDomain = str.substring("moz-nullprincipal:{".length, str.length - 1) + ".mozilla";
-    Assert.equal(content.document.nodePrincipal.originAttributes.firstPartyDomain,
-                 expectDomain,
-                 "data: URI should have firstPartyDomain set to " + expectDomain);
+    Assert.ok(content.document.nodePrincipal.originAttributes.firstPartyDomain != "",
+              "data: URI should have firstPartyDomain set.");
 
     Assert.equal(iframe.contentDocument.nodePrincipal.originAttributes.firstPartyDomain,
-                 expectDomain,
+                 content.document.nodePrincipal.originAttributes.firstPartyDomain,
                  "iframe should inherit firstPartyDomain from parent document.");
     Assert.equal(iframe.contentDocument.cookie, "test2=foo", "iframe should have cookies");
   });
 
   win.close();
 });
 
 /**
--- a/docshell/base/nsDocShell.cpp
+++ b/docshell/base/nsDocShell.cpp
@@ -10954,31 +10954,35 @@ nsDocShell::DoURILoad(nsIURI* aURI,
   }
 
   // Getting the right triggeringPrincipal needs to be updated and is only
   // ready for use once bug 1182569 landed. Until then, we cannot rely on
   // the triggeringPrincipal for TYPE_DOCUMENT loads.
   MOZ_ASSERT(aTriggeringPrincipal, "Need a valid triggeringPrincipal");
 
   bool isSandBoxed = mSandboxFlags & SANDBOXED_ORIGIN;
-  // only inherit if we have a aPrincipalToInherit
-  bool inherit = false;
+
+  // We want to inherit aPrincipalToInherit when:
+  // 1. ChannelShouldInheritPrincipal returns true.
+  // 2. aURI is not data: URI, or data: URI is not configured as unique opaque
+  //    origin.
+  bool inheritAttrs = false, inheritPrincipal = false;
 
   if (aPrincipalToInherit) {
+    inheritAttrs = nsContentUtils::ChannelShouldInheritPrincipal(
+      aPrincipalToInherit,
+      aURI,
+      true, // aInheritForAboutBlank
+      isSrcdoc);
+
     bool isData;
     bool isURIUniqueOrigin = nsIOService::IsDataURIUniqueOpaqueOrigin() &&
                              NS_SUCCEEDED(aURI->SchemeIs("data", &isData)) &&
                              isData;
-    // If aURI is data: URI and is treated as a unique opaque origin, we don't
-    // want to inherit principal.
-    inherit = nsContentUtils::ChannelShouldInheritPrincipal(
-      aPrincipalToInherit,
-      aURI,
-      true, // aInheritForAboutBlank
-      isSrcdoc) && !isURIUniqueOrigin ;
+    inheritPrincipal = inheritAttrs && !isURIUniqueOrigin;
   }
 
   nsLoadFlags loadFlags = mDefaultLoadFlags;
   nsSecurityFlags securityFlags =
     nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL;
 
   if (aFirstParty) {
     // tag first party URL loads
@@ -10986,17 +10990,17 @@ nsDocShell::DoURILoad(nsIURI* aURI,
   }
 
   if (mLoadType == LOAD_ERROR_PAGE) {
     // Error pages are LOAD_BACKGROUND
     loadFlags |= nsIChannel::LOAD_BACKGROUND;
     securityFlags |= nsILoadInfo::SEC_LOAD_ERROR_PAGE;
   }
 
-  if (inherit) {
+  if (inheritPrincipal) {
     securityFlags |= nsILoadInfo::SEC_FORCE_INHERIT_PRINCIPAL;
   }
   if (isSandBoxed) {
     securityFlags |= nsILoadInfo::SEC_SANDBOXED;
   }
 
   nsCOMPtr<nsILoadInfo> loadInfo =
     (aContentPolicyType == nsIContentPolicy::TYPE_DOCUMENT) ?
@@ -11035,24 +11039,19 @@ nsDocShell::DoURILoad(nsIURI* aURI,
   // OriginAttributes of the parent document. Or in case there isn't a
   // parent document.
   bool isTopLevelDoc = mItemType == typeContent &&
                        (aContentPolicyType == nsIContentPolicy::TYPE_DOCUMENT ||
                         GetIsMozBrowser());
 
   OriginAttributes attrs;
 
-  // If inherit is true, which means loadInfo will have SEC_FORCE_INHERIT_PRINCIPAL
-  // set, so later when we create principal of the document from
-  // nsScriptSecurityManager::GetChannelResultPrincipal, we will use
-  // principalToInherit of the loadInfo as the document principal.
-  // Therefore we use the origin attributes from aPrincipalToInherit.
-  //
+  // Inherit origin attributes from aPrincipalToInherit if inheritAttrs is true.
   // Otherwise we just use the origin attributes from docshell.
-  if (inherit) {
+  if (inheritAttrs) {
     MOZ_ASSERT(aPrincipalToInherit, "We should have aPrincipalToInherit here.");
     attrs = aPrincipalToInherit->OriginAttributesRef();
     // If firstPartyIsolation is not enabled, then PrincipalToInherit should
     // have the same origin attributes with docshell.
     MOZ_ASSERT_IF(!OriginAttributes::IsFirstPartyEnabled(), attrs == GetOriginAttributes());
   } else {
     attrs = GetOriginAttributes();
     attrs.SetFirstPartyDomain(isTopLevelDoc, aURI);