Bug 1536411 - StoragePrincipal - part 7 - test for DOMCache and ServiceWorkers, r=Ehsan,asuth
☠☠ backed out by a047b64590a4 ☠ ☠
authorAndrea Marchesini <amarchesini@mozilla.com>
Thu, 11 Apr 2019 16:29:13 +0000
changeset 469039 c68dc3152faae7e8628c298caea5f28cf64842cc
parent 469038 f67d157e8baf47547f62845d809b0729744b1de3
child 469040 f94dfbf2476c82b25e2b81cebedf7d42eab64851
push id35856
push usercsabou@mozilla.com
push dateFri, 12 Apr 2019 03:19:48 +0000
treeherdermozilla-central@940684cd1065 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersEhsan, asuth
bugs1536411
milestone68.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 1536411 - StoragePrincipal - part 7 - test for DOMCache and ServiceWorkers, r=Ehsan,asuth Differential Revision: https://phabricator.services.mozilla.com/D25778
dom/serviceworkers/ServiceWorkerPrivate.cpp
dom/workers/ScriptLoader.cpp
toolkit/components/antitracking/test/browser/browser.ini
toolkit/components/antitracking/test/browser/browser_partitionedDOMCache.js
toolkit/components/antitracking/test/browser/browser_partitionedServiceWorkers.js
toolkit/components/antitracking/test/browser/matchAll.js
--- a/dom/serviceworkers/ServiceWorkerPrivate.cpp
+++ b/dom/serviceworkers/ServiceWorkerPrivate.cpp
@@ -1724,16 +1724,20 @@ nsresult ServiceWorkerPrivate::SpawnWork
   // bug 965637 is ever fixed this can be removed.
   info.mPrincipal =
       BasePrincipal::CreateCodebasePrincipal(uri, mInfo->GetOriginAttributes());
   if (NS_WARN_IF(!info.mPrincipal)) {
     return NS_ERROR_FAILURE;
   }
   info.mLoadingPrincipal = info.mPrincipal;
 
+  // StoragePrincipal for ServiceWorkers is equal to mPrincipal because, at the
+  // moment, ServiceWorkers are not exposed in partitioned contexts.
+  info.mStoragePrincipal = info.mPrincipal;
+
   info.mStorageAccess =
       nsContentUtils::StorageAllowedForServiceWorker(info.mPrincipal);
 
   info.mCookieSettings = mozilla::net::CookieSettings::Create();
   MOZ_ASSERT(info.mCookieSettings);
 
   info.mOriginAttributes = mInfo->GetOriginAttributes();
 
--- a/dom/workers/ScriptLoader.cpp
+++ b/dom/workers/ScriptLoader.cpp
@@ -1291,17 +1291,19 @@ class ScriptLoaderRunnable final : publi
       nsILoadGroup* loadGroup = mWorkerPrivate->GetLoadGroup();
       MOZ_DIAGNOSTIC_ASSERT(loadGroup);
 
       // Override the principal on the WorkerPrivate.  This is only necessary
       // in order to get a principal with exactly the correct URL.  The fetch
       // referrer logic depends on the WorkerPrivate principal having a URL
       // that matches the worker script URL.  If bug 1340694 is ever fixed
       // this can be removed.
-      // TODO: storagePrincipal here?
+      // XXX: force the storagePrincipal to be equal to the response one. This
+      // is OK for now because we don't want to expose storagePrincipal
+      // functionality in ServiceWorkers yet.
       rv = mWorkerPrivate->SetPrincipalsOnMainThread(
           responsePrincipal, responsePrincipal, loadGroup);
       MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
 
       rv = mWorkerPrivate->SetCSPFromHeaderValues(aCSPHeaderValue,
                                                   aCSPReportOnlyHeaderValue);
       MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
 
--- a/toolkit/components/antitracking/test/browser/browser.ini
+++ b/toolkit/components/antitracking/test/browser/browser.ini
@@ -90,8 +90,11 @@ support-files = localStorage.html
 support-files = localStorageEvents.html
 [browser_workerPropagation.js]
 support-files = workerIframe.html
 [browser_cookieBetweenTabs.js]
 [browser_partitionedMessaging.js]
 [browser_partitionedIndexedDB.js]
 [browser_partitionedCookies.js]
 support-files = cookies.sjs
+[browser_partitionedDOMCache.js]
+[browser_partitionedServiceWorkers.js]
+support-files = matchAll.js
new file mode 100644
--- /dev/null
+++ b/toolkit/components/antitracking/test/browser/browser_partitionedDOMCache.js
@@ -0,0 +1,21 @@
+/* import-globals-from storageprincipal_head.js */
+
+StoragePrincipalHelper.runTest("DOMCache",
+  async (win3rdParty, win1stParty, allowed) => {
+    // DOM Cache is not supported. Always blocked.
+    await win3rdParty.caches.open("wow").then(
+      _ => { ok(allowed, "DOM Cache cannot be used!"); },
+      _ => { ok(!allowed, "DOM Cache cannot be used!"); }
+    );
+
+    await win1stParty.caches.open("wow").then(
+      _ => { ok(true, "DOM Cache shoulw be available"); },
+      _ => { ok(false, "DOM Cache shoulw be available"); },
+    );
+  },
+
+  async _ => {
+    await new Promise(resolve => {
+      Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, value => resolve());
+    });
+  });
new file mode 100644
--- /dev/null
+++ b/toolkit/components/antitracking/test/browser/browser_partitionedServiceWorkers.js
@@ -0,0 +1,65 @@
+/* import-globals-from storageprincipal_head.js */
+
+StoragePrincipalHelper.runTest("ServiceWorkers",
+  async (win3rdParty, win1stParty, allowed) => {
+    // ServiceWorkers are not supported. Always blocked.
+    await win3rdParty.navigator.serviceWorker.register("empty.js").then(
+      _ => { ok(allowed, "Success: ServiceWorker cannot be used!"); },
+      _ => { ok(!allowed, "Failed: ServiceWorker cannot be used!"); });
+
+    await win1stParty.navigator.serviceWorker.register("empty.js").then(
+      _ => { ok(true, "Success: ServiceWorker should be available!"); },
+      _ => { ok(false, "Failed: ServiceWorker should be available!"); });
+  },
+
+  async _ => {
+    await new Promise(resolve => {
+      Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, value => resolve());
+    });
+  },
+
+  [["dom.serviceWorkers.exemptFromPerDomainMax", true],
+   ["dom.ipc.processCount", 1],
+   ["dom.serviceWorkers.enabled", true],
+   ["dom.serviceWorkers.testing.enabled", true]]);
+
+StoragePrincipalHelper.runTest("ServiceWorkers - MatchAll",
+  async (win3rdParty, win1stParty, allowed) => {
+    if (!win1stParty.sw) {
+      let reg = await win1stParty.navigator.serviceWorker.register("matchAll.js");
+      if (reg.installing.state !== "activated") {
+        await new Promise(resolve => {
+            let w = reg.installing;
+            w.addEventListener("statechange", function onStateChange() {
+              if (w.state === "activated") {
+                w.removeEventListener("statechange", onStateChange);
+                win1stParty.sw = reg.active;
+                resolve();
+              }
+            });
+          });
+      }
+    }
+
+    let msgPromise = new Promise(resolve => {
+      win1stParty.navigator.serviceWorker.addEventListener("message", msg => {
+        resolve(msg.data);
+      });
+    });
+
+    win1stParty.sw.postMessage(win3rdParty.location.href);
+    let msg = await msgPromise;
+
+    is(allowed, msg, "We want to have the 3rd party window controlled.");
+  },
+
+  async _ => {
+    await new Promise(resolve => {
+      Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, value => resolve());
+    });
+  },
+
+  [["dom.serviceWorkers.exemptFromPerDomainMax", true],
+   ["dom.ipc.processCount", 1],
+   ["dom.serviceWorkers.enabled", true],
+   ["dom.serviceWorkers.testing.enabled", true]]);
new file mode 100644
--- /dev/null
+++ b/toolkit/components/antitracking/test/browser/matchAll.js
@@ -0,0 +1,13 @@
+self.addEventListener("message", async e => {
+  let clients = await self.clients.matchAll({type: "window", includeUncontrolled: true});
+
+  let hasWindow = false;
+  for (let client of clients) {
+    if (e.data == client.url) {
+      hasWindow = true;
+      break;
+    }
+  }
+
+  e.source.postMessage(hasWindow);
+});