Bug 1498343 - Add more assertions related to behaviors on redefined globals and prototype methods in the userScript tests. r=mixedpuppy
authorLuca Greco <lgreco@mozilla.com>
Mon, 15 Oct 2018 17:56:34 +0000
changeset 500651 a0aa0b0aa078f7135e7d7032687e4a38e6c378f8
parent 500650 c68fc2d0347185ab54ef90462b9b294f82cc50be
child 500652 681ffcabae79527e14f5f795aaf360eeb2e85399
push id1864
push userffxbld-merge
push dateMon, 03 Dec 2018 15:51:40 +0000
treeherdermozilla-release@f040763d99ad [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmixedpuppy
bugs1498343
milestone64.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 1498343 - Add more assertions related to behaviors on redefined globals and prototype methods in the userScript tests. r=mixedpuppy Depends on D8537 Differential Revision: https://phabricator.services.mozilla.com/D8538
toolkit/components/extensions/test/xpcshell/test_ext_userScripts.js
--- a/toolkit/components/extensions/test/xpcshell/test_ext_userScripts.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_userScripts.js
@@ -320,27 +320,41 @@ add_task(async function test_userScripts
   async function background() {
     const matches = ["http://localhost/*/file_sample.html"];
 
     await browser.runtime.onMessage.addListener(async (msg, sender) => {
       return {bgPageReply: true};
     });
 
     async function userScript() {
-      // Explicitly retrieve the custom exported API methods
-      // to prevent eslint to raise a no-undef validation
-      // error for them.
+      // Redefine Promise and Error globals to verify that it doesn't break the WebExtensions internals
+      // that are going to use them.
+      const {Error} = this;
+      this.Promise.resolve = function() {
+        throw new Error("Promise poisoning");
+      };
+      this.Error = {};
+
+      // Explicitly retrieve the custom exported API methods to prevent eslint to raise a no-undef
+      // validation error for them.
       const {
         US_sync_api,
         US_async_api_with_callback,
         US_send_api_results,
       } = this;
       this.userScriptGlobalVar = "global-sandbox-value";
 
-      const syncAPIResult = US_sync_api("param1", "param2");
+      // Redefine the includes method on the Array prototype, to explicitly verify that the method
+      // redefined in the userScript is not used when accessing arrayParam.includes from the API script.
+      Array.prototype.includes = () => { // eslint-disable-line no-extend-native
+        throw new Error("Unexpected prototype leakage");
+      };
+      const arrayParam = new Array(1, 2, 3); // eslint-disable-line no-array-constructor
+
+      const syncAPIResult = US_sync_api("param1", "param2", arrayParam);
       const cb = (cbParam) => {
         return `callback param: ${JSON.stringify(cbParam)}`;
       };
       const cb2 = cb;
       const asyncAPIResult = await US_async_api_with_callback("param3", cb, cb2);
 
       let expectedError;
 
@@ -374,32 +388,35 @@ add_task(async function test_userScripts
 
   function apiScript() {
     // Redefine Promise and Error globals to verify that it doesn't break the WebExtensions internals
     // that are going to use them.
     this.Promise = {};
     this.Error = {};
 
     browser.userScripts.setScriptAPIs({
-      US_sync_api([param1, param2], scriptMetadata, scriptGlobal) {
+      US_sync_api([param1, param2, arrayParam], scriptMetadata, scriptGlobal) {
         browser.test.assertEq("test-user-script-exported-apis", scriptMetadata.name,
                               "Got the expected value for a string scriptMetadata property");
         browser.test.assertEq(null, scriptMetadata.nullProperty,
                               "Got the expected value for a null scriptMetadata property");
         browser.test.assertTrue(scriptMetadata.arrayProperty &&
                                 scriptMetadata.arrayProperty.length === 1 &&
                                 scriptMetadata.arrayProperty[0] === "el1",
                                 "Got the expected value for an array scriptMetadata property");
         browser.test.assertTrue(scriptMetadata.objectProperty &&
                                 scriptMetadata.objectProperty.nestedProp === "nestedValue",
                                 "Got the expected value for an object scriptMetadata property");
 
         browser.test.assertEq("param1", param1, "Got the expected parameter value");
         browser.test.assertEq("param2", param2, "Got the expected parameter value");
 
+        browser.test.assertEq(3, arrayParam.length, "Got the expected lenght on the array param");
+        browser.test.assertTrue(arrayParam.includes(1), "Got the expected result when calling arrayParam.includes");
+
         browser.test.sendMessage("US_sync_api", {param1, param2});
 
         return "returned_value";
       },
       async US_async_api_with_callback([param, cb, cb2], scriptMetadata, scriptGlobal) {
         browser.test.assertEq("function", typeof cb, "Got a callback function parameter");
         browser.test.assertTrue(cb === cb2, "Got the same cloned function for the same function parameter");