Bug 1233315 - Correct WorkerPushSubscription keys, r?kitcambridge draft
authorMartin Thomson <martin.thomson@gmail.com>
Thu, 17 Dec 2015 16:57:57 +1100
changeset 315904 8386571fd80e5b20e0e7f61626bd7d0ec53e37c3
parent 315469 2b324879163fe9927d4c3a1f81fa2c04613f01fc
child 512104 f6023c8bb92d9d48c03af64a7847f764a539680a
push id8485
push usermartin.thomson@gmail.com
push dateThu, 17 Dec 2015 06:30:30 +0000
reviewerskitcambridge
bugs1233315
milestone45.0a1
Bug 1233315 - Correct WorkerPushSubscription keys, r?kitcambridge
dom/push/PushClient.js
dom/push/PushCrypto.jsm
dom/push/PushManager.cpp
dom/push/test/test_data.html
dom/push/test/worker.js
--- a/dom/push/PushClient.js
+++ b/dom/push/PushClient.js
@@ -106,18 +106,18 @@ PushClient.prototype = {
     }
 
     let key;
     if (registration.p256dhKey) {
       key = new Uint8Array(registration.p256dhKey);
     }
 
     let authSecret;
-    if (registration.authSecret) {
-      authSecret = new Uint8Array(registration.authSecret);
+    if (registration.authenticationSecret) {
+      authSecret = new Uint8Array(registration.authenticationSecret);
     }
 
     request.onPushEndpoint(Cr.NS_OK,
                            registration.pushEndpoint,
                            key ? key.length : 0,
                            key,
                            authSecret ? authSecret.length : 0,
                            authSecret);
--- a/dom/push/PushCrypto.jsm
+++ b/dom/push/PushCrypto.jsm
@@ -178,17 +178,17 @@ function generateNonce(base, index) {
     nonce[nonce.byteLength - 1 - i] ^= (index / Math.pow(256, i)) & 0xff;
   }
   return nonce;
 }
 
 this.PushCrypto = {
 
   generateAuthenticationSecret() {
-    return crypto.getRandomValues(new Uint8Array(12));
+    return crypto.getRandomValues(new Uint8Array(12)).buffer;
   },
 
   generateKeys() {
     return crypto.subtle.generateKey(ECDH_KEY, true, ['deriveBits'])
       .then(cryptoKey =>
          Promise.all([
            crypto.subtle.exportKey('raw', cryptoKey.publicKey),
            crypto.subtle.exportKey('jwk', cryptoKey.privateKey)
--- a/dom/push/PushManager.cpp
+++ b/dom/push/PushManager.cpp
@@ -606,21 +606,20 @@ public:
     if (proxy->CleanedUp()) {
       return NS_OK;
     }
 
     AutoJSAPI jsapi;
     jsapi.Init();
 
     nsTArray<uint8_t> rawP256dhKey(aKeyLen);
-    rawP256dhKey.ReplaceElementsAt(0, aKeyLen, aKey, aKeyLen);
+    rawP256dhKey.InsertElementsAt(0, aKey, aKeyLen);
 
     nsTArray<uint8_t> authSecret(aAuthSecretLen);
-    authSecret.ReplaceElementsAt(0, aAuthSecretLen,
-                                 aAuthSecret, aAuthSecretLen);
+    authSecret.InsertElementsAt(0, aAuthSecret, aAuthSecretLen);
 
     RefPtr<GetSubscriptionResultRunnable> r =
       new GetSubscriptionResultRunnable(proxy,
                                         aStatus,
                                         aEndpoint,
                                         mScope,
                                         rawP256dhKey,
                                         authSecret);
--- a/dom/push/test/test_data.html
+++ b/dom/push/test/test_data.html
@@ -50,36 +50,41 @@ http://creativecommons.org/licenses/publ
   function subscribe(swr) {
     return swr.pushManager.subscribe();
   }
 
   function sendRequestToWorker(request) {
     return new Promise((resolve, reject) => {
       var channel = new MessageChannel();
       channel.port1.onmessage = e => {
-        (e.data.error ? reject : resolve)(e.data);
+        ((e.data && e.data.error) ? reject : resolve)(e.data);
       };
       registration.active.postMessage(request, [channel.port2]);
     });
   }
 
   function comparePublicKey(pushSubscription) {
-    // FIXME(kitcambridge): Enable when `ServiceWorkerMessageEvent` is
-    // implemented (bug 1143717).
-    return Promise.resolve(pushSubscription);
-    /*
-    return sendRequestToWorker({ type: "publicKey" }).then(data => {
+    return sendRequestToWorker("publicKey").then(data => {
       return registration.pushManager.getSubscription().then(
         pushSubscription => {
           isDeeply(pushSubscription.getKey("p256dh"), data,
             "Mismatched key share");
           return pushSubscription;
       });
     });
-    */
+  }
+  function compareAuth(pushSubscription) {
+    return sendRequestToWorker("authKey").then(data => {
+      return registration.pushManager.getSubscription().then(
+        pushSubscription => {
+          isDeeply(pushSubscription.getKey("auth"), data,
+            "Mismatched key share");
+          return pushSubscription;
+      });
+    });
   }
 
   function waitForMessage(pushSubscription, message) {
     return Promise.all([
       controlledFrame.contentWindow.waitOnPushMessage(pushSubscription),
       webpush(pushSubscription, message),
     ]).then(([message]) => message);
   }
@@ -157,25 +162,25 @@ http://creativecommons.org/licenses/publ
   }
 
   function unregister() {
     return registration.unregister();
   }
 
   function runTest() {
     start()
-    .then(createControlledIFrame)
-    .then(subscribe)
-    .then(comparePublicKey)
-    .then(sendPushMessageFromPage)
-    .then(unsubscribe)
-    .then(unregister)
-    .catch(function(e) {
-      ok(false, "Some test failed with error " + e);
-    }).then(SimpleTest.finish);
+      .then(createControlledIFrame)
+      .then(subscribe)
+      .then(comparePublicKey)
+      .then(compareAuth)
+      .then(sendPushMessageFromPage)
+      .then(unsubscribe)
+      .then(unregister)
+      .catch(e => ok(false, "Some test failed with error " + e))
+      .then(SimpleTest.finish);
   }
 
   SpecialPowers.pushPrefEnv({"set": [
     ["dom.push.enabled", true],
     ["dom.serviceWorkers.exemptFromPerDomainMax", true],
     ["dom.serviceWorkers.enabled", true],
     ["dom.serviceWorkers.testing.enabled", true]
     ]}, runTest);
--- a/dom/push/test/worker.js
+++ b/dom/push/test/worker.js
@@ -40,18 +40,19 @@ function handlePush(event) {
       result[0].postMessage(message);
       return;
     }
     result[0].postMessage({type: "finished", okay: "no"});
   });
 }
 
 function handleMessage(event) {
-  // FIXME(kitcambridge): Enable when `ServiceWorkerMessageEvent` is
-  // implemented (bug 1143717).
-  /*
-  if (event.data.type == "publicKey") {
-    self.registration.pushManager.getSubscription().then(subscription => {
-      event.ports[0].postMessage(subscription.getKey("p256dh"));
-    });
-  }
-  */
+  self.registration.pushManager.getSubscription().then(subscription => {
+    if (event.data === "publicKey") {
+      return subscription.getKey("p256dh");
+    }
+    if (event.data === "authKey") {
+      return subscription.getKey("auth");
+    }
+    throw new Error('Unknown message: ' + event.data);
+  }).then(data => event.ports[0].postMessage(data),
+          e => event.ports[0].postMessage({ error: e }));
 }