Bug 1635637 - Add contextId to MessageSender, prevent onConnect in same Context r=robwu a=pascalc FIREFOX_76_0_1_BUILD1 FIREFOX_76_0_1_RELEASE
authorTomislav Jovanovic <tomica@gmail.com>
Wed, 06 May 2020 17:53:00 +0000
changeset 589272 e2de5f11bc0afd9a3024d32b83cb9f0ada95717a
parent 589271 2aa3ab8e2feeb3f8b67684ace2f3db7d8126b460
child 589273 8b26a00b2372f4ee9a95a6d9132bdc537b518acd
push id2328
push userarchaeopteryx@coole-files.de
push dateThu, 07 May 2020 11:40:07 +0000
treeherdermozilla-release@e2de5f11bc0a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrobwu, pascalc
bugs1635637
milestone76.0.1
Bug 1635637 - Add contextId to MessageSender, prevent onConnect in same Context r=robwu a=pascalc We already had logic to avoid sending to the same context, but weren't setting the `contextId` as necessary. https://searchfox.org/mozilla-central/rev/dc4560dc/toolkit/components/extensions/ConduitsParent.jsm#251 Also add a test specifically for connect() triggering onConnect in the same Context, since the existing test only covered sendMessage. Differential Revision: https://phabricator.services.mozilla.com/D74099
toolkit/components/extensions/ExtensionParent.jsm
toolkit/components/extensions/test/xpcshell/test_ext_runtime_sendMessage_self.js
--- a/toolkit/components/extensions/ExtensionParent.jsm
+++ b/toolkit/components/extensions/ExtensionParent.jsm
@@ -296,16 +296,17 @@ const NativeMessenger = {
       await extension.wakeupBackground();
     }
 
     sender = {
       id: sender.extensionId,
       envType: sender.envType,
       frameId: sender.frameId,
       url: sender.actor.manager.documentURI.spec,
+      contextId: sender.id,
     };
     apiManager.global.tabGetSender(extension, target, sender);
 
     arg = { portId, extensionId, sender, ...arg };
     let all = await this.conduit.castPortConnect(kind, arg);
     resolvePort();
 
     // If there are no active onConnect listeners.
--- a/toolkit/components/extensions/test/xpcshell/test_ext_runtime_sendMessage_self.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_runtime_sendMessage_self.js
@@ -1,15 +1,15 @@
 /* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* vim: set sts=2 sw=2 et tw=80: */
 
 "use strict";
 
 add_task(
-  async function test_sendMessage_to_self_should_not_trigger_onMessage() {
+  async function test_sendMessage_connect_to_self_should_not_trigger_onMessage_onConnect() {
     async function background() {
       browser.runtime.onMessage.addListener(msg => {
         browser.test.assertEq("msg from child", msg);
         browser.test.notifyPass(
           "sendMessage did not call same-frame onMessage"
         );
       });
 
@@ -21,16 +21,31 @@ add_task(
         browser.runtime.sendMessage("should only reach another frame");
       });
 
       await browser.test.assertRejects(
         browser.runtime.sendMessage("should not trigger same-frame onMessage"),
         "Could not establish connection. Receiving end does not exist."
       );
 
+      browser.runtime.onConnect.addListener(port => {
+        browser.test.fail("Should not receive runtime.onConnect from self");
+      });
+
+      await new Promise(resolve => {
+        let port = browser.runtime.connect();
+        port.onDisconnect.addListener(() => {
+          browser.test.assertEq(
+            "Could not establish connection. Receiving end does not exist.",
+            port.error.message
+          );
+          resolve();
+        });
+      });
+
       let anotherFrame = document.createElement("iframe");
       anotherFrame.src = browser.extension.getURL("extensionpage.html");
       document.body.appendChild(anotherFrame);
     }
 
     function lastScript() {
       browser.runtime.onMessage.addListener(msg => {
         browser.test.assertEq("should only reach another frame", msg);