Bug 1281884 - strict_min_version and strict_max_version aren't respected in "Load Temporary Add-on". r=aswan
☠☠ backed out by 794db78767b9 ☠ ☠
authorBob Silverberg <bsilverberg@mozilla.com>
Mon, 04 Jul 2016 09:58:59 -0400
changeset 400788 a11a0035c85e643cf68ed03cd9c6a540367c0756
parent 400787 05e5ceb799a1cd892e42ea9e09005a076fe2f002
child 400789 9e8fb23fc565df8b8f002941e7dc12bced5df054
push id26284
push usermwein@mozilla.com
push dateMon, 15 Aug 2016 20:48:20 +0000
reviewersaswan
bugs1281884
milestone51.0a1
Bug 1281884 - strict_min_version and strict_max_version aren't respected in "Load Temporary Add-on". r=aswan Replace test_disabled_addon_can_be_enabled_after_reload in test_reload.js with test_reload_to_invalid_version_fails. MozReview-Commit-ID: 9OEBnbwNplC
toolkit/mozapps/extensions/internal/XPIProvider.jsm
toolkit/mozapps/extensions/test/xpcshell/test_reload.js
toolkit/mozapps/extensions/test/xpcshell/test_webextension_install.js
--- a/toolkit/mozapps/extensions/internal/XPIProvider.jsm
+++ b/toolkit/mozapps/extensions/internal/XPIProvider.jsm
@@ -984,19 +984,18 @@ var loadManifestFromWebManifest = Task.a
 
   addon.defaultLocale = getLocale(extension.defaultLocale);
   addon.locales = Array.from(locales.keys(), getLocale);
 
   delete addon.defaultLocale.locales;
 
   addon.targetApplications = [{
     id: TOOLKIT_ID,
-    minVersion: (bss.strict_min_version ||
-                 AddonManagerPrivate.webExtensionsMinPlatformVersion),
-    maxVersion: bss.strict_max_version || "*",
+    minVersion: bss.strict_min_version,
+    maxVersion: bss.strict_max_version,
   }];
 
   addon.targetPlatforms = [];
   addon.userDisabled = false;
   addon.softDisabled = addon.blocklistState == Blocklist.STATE_SOFTBLOCKED;
 
   return addon;
 });
@@ -3990,16 +3989,23 @@ this.XPIProvider = {
    *         same ID is already temporarily installed
    */
   installTemporaryAddon: Task.async(function*(aFile) {
     if (aFile.exists() && aFile.isFile()) {
       flushJarCache(aFile);
     }
     let addon = yield loadManifestFromFile(aFile, TemporaryInstallLocation);
 
+    if (!addon.isCompatible) {
+      let app = addon.matchingTargetApplication;
+      throw new Error(`Add-on ${addon.id} is not compatible with application version. ` +
+                      `add-on minVersion: ${app.minVersion}, ` +
+                      `add-on maxVersion: ${app.maxVersion}`);
+    }
+
     if (!addon.bootstrap) {
       throw new Error("Only restartless (bootstrap) add-ons"
                     + " can be temporarily installed:", addon.id);
     }
     let installReason = BOOTSTRAP_REASONS.ADDON_INSTALL;
     let oldAddon = yield new Promise(
                    resolve => XPIDatabase.getVisibleAddonForID(addon.id, resolve));
     if (oldAddon) {
@@ -7009,16 +7015,20 @@ AddonInternal.prototype = {
     return matchedOS && !needsABI;
   },
 
   isCompatibleWith: function(aAppVersion, aPlatformVersion) {
     let app = this.matchingTargetApplication;
     if (!app)
       return false;
 
+    // set reasonable defaults for minVersion and maxVersion
+    let minVersion = app.minVersion || "0";
+    let maxVersion = app.maxVersion || "*";
+
     if (!aAppVersion)
       aAppVersion = Services.appinfo.version;
     if (!aPlatformVersion)
       aPlatformVersion = Services.appinfo.platformVersion;
 
     let version;
     if (app.id == Services.appinfo.ID)
       version = aAppVersion;
@@ -7045,24 +7055,24 @@ AddonInternal.prototype = {
       // Extremely old extensions should not be compatible by default.
       let minCompatVersion;
       if (app.id == Services.appinfo.ID)
         minCompatVersion = XPIProvider.minCompatibleAppVersion;
       else if (app.id == TOOLKIT_ID)
         minCompatVersion = XPIProvider.minCompatiblePlatformVersion;
 
       if (minCompatVersion &&
-          Services.vc.compare(minCompatVersion, app.maxVersion) > 0)
+          Services.vc.compare(minCompatVersion, maxVersion) > 0)
         return false;
 
-      return Services.vc.compare(version, app.minVersion) >= 0;
-    }
-
-    return (Services.vc.compare(version, app.minVersion) >= 0) &&
-           (Services.vc.compare(version, app.maxVersion) <= 0)
+      return Services.vc.compare(version, minVersion) >= 0;
+    }
+
+    return (Services.vc.compare(version, minVersion) >= 0) &&
+           (Services.vc.compare(version, maxVersion) <= 0)
   },
 
   get matchingTargetApplication() {
     let app = null;
     for (let targetApp of this.targetApplications) {
       if (targetApp.id == Services.appinfo.ID)
         return targetApp;
       if (targetApp.id == TOOLKIT_ID)
--- a/toolkit/mozapps/extensions/test/xpcshell/test_reload.js
+++ b/toolkit/mozapps/extensions/test/xpcshell/test_reload.js
@@ -85,48 +85,67 @@ add_task(function* test_cannot_reload_pe
   yield promiseRestartManager();
   const addon = yield installAddon(sampleAddon.name, sampleAddon.id);
 
   yield Assert.rejects(addon.reload(), /Only temporary add-ons can be reloaded/);
 
   yield tearDownAddon(addon);
 });
 
-add_task(function* test_disabled_addon_can_be_enabled_after_reload() {
+add_task(function* test_reload_to_invalid_version_fails() {
   yield promiseRestartManager();
   let tempdir = gTmpD.clone();
 
-  // Create an add-on with strictCompatibility which should cause it
-  // to be appDisabled.
-  const unpackedAddon = writeInstallRDFToDir(
-    Object.assign({}, manifestSample, {
-      strictCompatibility: true,
-      targetApplications: [{
-        id: "xpcshell@tests.mozilla.org",
-        minVersion: "0.1",
-        maxVersion: "0.1"
-      }],
-    }), tempdir, manifestSample.id, "bootstrap.js");
+  // The initial version of the add-on will be compatible, and will therefore load
+  const addonId = "invalid_version_cannot_be_reloaded@tests.mozilla.org";
+  let manifest = {
+    name: "invalid_version_cannot_be_reloaded",
+    description: "test invalid_version_cannot_be_reloaded",
+    manifest_version: 2,
+    version: "1.0",
+    applications: {
+      gecko: {
+        id: addonId,
+      }
+    },
+  };
+
+  let addonDir = writeWebManifestForExtension(manifest, tempdir, "invalid_version");
+
+  yield AddonManager.installTemporaryAddon(addonDir);
+  let addon = yield promiseAddonByID(addonId);
 
-  yield AddonManager.installTemporaryAddon(unpackedAddon);
-  const addon = yield promiseAddonByID(manifestSample.id);
   notEqual(addon, null);
-  equal(addon.appDisabled, true);
+  equal(addon.id, addonId);
+  equal(addon.version, "1.0");
+  equal(addon.appDisabled, false);
+  equal(addon.userDisabled, false);
+
+  // update the manifest to make the add-on version incompatible, so the reload will reject
+  manifest.applications.gecko.strict_min_version = "1";
+  manifest.applications.gecko.strict_max_version = "1";
+  manifest.version = "2.0";
+
+  addonDir = writeWebManifestForExtension(manifest, tempdir, "invalid_version");
 
-  // Remove strictCompatibility from the manifest.
-  writeInstallRDFToDir(manifestSample, tempdir, manifestSample.id);
+  let expectedMsg = new RegExp("Add-on invalid_version_cannot_be_reloaded@tests.mozilla.org is not compatible with application version. " +
+                               "add-on minVersion: 1, add-on maxVersion: 1");
+  yield Assert.rejects(addon.reload(),
+                       expectedMsg,
+                       "Reload rejects when application version does not fall between minVersion and maxVersion");
 
-  yield addon.reload();
-
-  const reloadedAddon = yield promiseAddonByID(manifestSample.id);
+  let reloadedAddon = yield promiseAddonByID(addonId);
   notEqual(reloadedAddon, null);
+  equal(reloadedAddon.id, addonId);
+  equal(reloadedAddon.version, "1.0");
   equal(reloadedAddon.appDisabled, false);
+  equal(reloadedAddon.userDisabled, false);
 
   yield tearDownAddon(reloadedAddon);
-  unpackedAddon.remove(true);
+  addonDir.remove(true);
 });
 
 add_task(function* test_manifest_changes_are_refreshed() {
   yield promiseRestartManager();
   let tempdir = gTmpD.clone();
 
   const unpackedAddon = writeInstallRDFToDir(
     Object.assign({}, manifestSample, {
--- a/toolkit/mozapps/extensions/test/xpcshell/test_webextension_install.js
+++ b/toolkit/mozapps/extensions/test/xpcshell/test_webextension_install.js
@@ -194,8 +194,165 @@ add_task(function* test_two_ids() {
 
   let addon = yield promiseAddonByID(BAD_ID);
   do_check_eq(addon, null);
   addon = yield promiseAddonByID(GOOD_ID);
   do_check_neq(addon, null);
 
   addon.uninstall();
 });
+
+// Test that strict_min_version and strict_max_version are enforced for
+// loading temporary extension.
+add_task(function* test_strict_min_max() {
+  // the app version being compared to is 1.9.2
+  const addonId = "strict_min_max@tests.mozilla.org";
+  const MANIFEST = {
+    name: "strict min max test",
+    description: "test strict min and max with temporary loading",
+    manifest_version: 2,
+    version: "1.0",
+  };
+
+  // bad max good min
+  let apps = {
+    applications: {
+      gecko: {
+        id: addonId,
+        strict_min_version: "1",
+        strict_max_version: "1"
+      },
+    },
+  }
+  let testManifest = Object.assign(apps, MANIFEST);
+
+  let addonDir = writeWebManifestForExtension(testManifest, gTmpD,
+                                              "the-addon-sub-dir");
+
+  let expectedMsg = new RegExp("Add-on strict_min_max@tests.mozilla.org is not compatible with application version. " +
+                               "add-on minVersion: 1, add-on maxVersion: 1");
+  yield Assert.rejects(AddonManager.installTemporaryAddon(addonDir),
+                       expectedMsg,
+                       "Install rejects when specified maxVersion is not valid");
+
+  let addon = yield promiseAddonByID(addonId);
+  do_check_eq(addon, null);
+
+  // bad min good max
+  apps = {
+    applications: {
+      gecko: {
+        id: addonId,
+        strict_min_version: "2",
+        strict_max_version: "2"
+      },
+    },
+  }
+  testManifest = Object.assign(apps, MANIFEST);
+
+  addonDir = writeWebManifestForExtension(testManifest, gTmpD,
+                                          "the-addon-sub-dir");
+
+  expectedMsg = new RegExp("Add-on strict_min_max@tests.mozilla.org is not compatible with application version. " +
+                               "add-on minVersion: 2, add-on maxVersion: 2");
+  yield Assert.rejects(AddonManager.installTemporaryAddon(addonDir),
+                       expectedMsg,
+                       "Install rejects when specified minVersion is not valid");
+
+  addon = yield promiseAddonByID(addonId);
+  do_check_eq(addon, null);
+
+  // bad both
+  apps = {
+    applications: {
+      gecko: {
+        id: addonId,
+        strict_min_version: "2",
+        strict_max_version: "1"
+      },
+    },
+  }
+  testManifest = Object.assign(apps, MANIFEST);
+
+  addonDir = writeWebManifestForExtension(testManifest, gTmpD,
+                                          "the-addon-sub-dir");
+
+  expectedMsg = new RegExp("Add-on strict_min_max@tests.mozilla.org is not compatible with application version. " +
+                               "add-on minVersion: 2, add-on maxVersion: 1");
+  yield Assert.rejects(AddonManager.installTemporaryAddon(addonDir),
+                       expectedMsg,
+                       "Install rejects when specified minVersion and maxVersion are not valid");
+
+  addon = yield promiseAddonByID(addonId);
+  do_check_eq(addon, null);
+  addonDir.remove(true);
+
+  // good both
+  apps = {
+    applications: {
+      gecko: {
+        id: addonId,
+        strict_min_version: "1",
+        strict_max_version: "2"
+      },
+    },
+  }
+  testManifest = Object.assign(apps, MANIFEST);
+
+  addonDir = writeWebManifestForExtension(testManifest, gTmpD,
+                                          "strict_min_max");
+
+  yield AddonManager.installTemporaryAddon(addonDir);
+  addon = yield promiseAddonByID(addonId);
+
+  do_check_neq(addon, null);
+  do_check_eq(addon.id, addonId);
+  addon.uninstall();
+  addonDir.remove(true);
+
+  // good only min
+  let newId = "strict_min_only@tests.mozilla.org";
+  apps = {
+    applications: {
+      gecko: {
+        id: newId,
+        strict_min_version: "1",
+      },
+    },
+  }
+  testManifest = Object.assign(apps, MANIFEST);
+
+  addonDir = writeWebManifestForExtension(testManifest, gTmpD,
+                                          "strict_min_only");
+
+  yield AddonManager.installTemporaryAddon(addonDir);
+  addon = yield promiseAddonByID(newId);
+
+  do_check_neq(addon, null);
+  do_check_eq(addon.id, newId);
+
+  addon.uninstall();
+  addonDir.remove(true);
+
+  // good only max
+  newId = "strict_max_only@tests.mozilla.org";
+  apps = {
+    applications: {
+      gecko: {
+        id: newId,
+        strict_max_version: "2",
+      },
+    },
+  }
+  testManifest = Object.assign(apps, MANIFEST);
+
+  addonDir = writeWebManifestForExtension(testManifest, gTmpD,
+                                          "strict_max_only");
+
+  yield AddonManager.installTemporaryAddon(addonDir);
+  addon = yield promiseAddonByID(newId);
+
+  do_check_neq(addon, null);
+  do_check_eq(addon.id, newId);
+
+  addon.uninstall();
+  addonDir.remove(true);
+});