Commit ee70042c authored by Rob Wu's avatar Rob Wu Committed by Pier Angelo Vendrame
Browse files

Bug 1939087 - Truncate long name and log warning a=dmeehan

parent 021860c8
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -1377,6 +1377,17 @@ export class ExtensionData {
    );
  }

  // AMO enforces a maximum length of 45 on the name since at least 2017, via
  // https://github.com/mozilla/addons-linter/blame/c4507688899aaafe29c522f1b1aec94b78b8a095/src/schema/updates/manifest.json#L111
  // added in https://github.com/mozilla/addons-linter/pull/1169
  // To avoid breaking add-ons that do not go through AMO (e.g. temporarily
  // loaded extensions), we enforce the limit by truncating and warning if
  // needed, instead enforcing a maxLength on "name" in schemas/manifest.json.
  //
  // We set the limit to 75, which is a safe limit that matches the CWS,
  // see https://bugzilla.mozilla.org/show_bug.cgi?id=1939087#c5
  static EXT_NAME_MAX_LEN = 75;

  async initializeAddonTypeAndID() {
    if (this.type) {
      // Already initialized.
@@ -1496,6 +1507,14 @@ export class ExtensionData {
      }
    }

    if (manifest.name.length > ExtensionData.EXT_NAME_MAX_LEN) {
      // Truncate and warn - see comment in EXT_NAME_MAX_LEN.
      manifest.name = manifest.name.slice(0, ExtensionData.EXT_NAME_MAX_LEN);
      this.manifestWarning(
        `Warning processing "name": must be shorter than ${ExtensionData.EXT_NAME_MAX_LEN}`
      );
    }

    if (
      this.manifestVersion < 3 &&
      manifest.background &&
+1 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@

          "name": {
            "type": "string",
            "description": "Name must be at least 2, at should be at most 75 characters",
            "optional": false,
            "preprocess": "localize"
          },
+22 −0
Original line number Diff line number Diff line
@@ -156,6 +156,28 @@ add_task(
  }
);

add_task(async function test_name_too_long() {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      // This length is 80, which exceeds ExtensionData.EXT_NAME_MAX_LEN:
      name: "123456789_".repeat(8),
    },
  });
  await extension.startup();
  equal(
    extension.extension.name,
    "123456789_123456789_123456789_123456789_123456789_123456789_123456789_12345",
    "Name should be truncated"
  );
  Assert.deepEqual(
    extension.extension.warnings,
    ['Reading manifest: Warning processing "name": must be shorter than 75'],
    "Expected error message when the name is too long"
  );

  await extension.unload();
});

add_task(async function test_simpler_version_format() {
  const TEST_CASES = [
    // Valid cases
+5 −0
Original line number Diff line number Diff line
@@ -560,6 +560,11 @@ async function loadManifestFromWebManifest(aPackage, aLocation) {
      contributors: null,
      locales: [aLocale],
    };
    if (result.name.length > lazy.ExtensionData.EXT_NAME_MAX_LEN) {
      // See comment at EXT_NAME_MAX_LEN in Extension.sys.mjs.
      logger.warn(`Truncating add-on name ${addon.id} for locale ${aLocale}`);
      result.name = result.name.slice(0, lazy.ExtensionData.EXT_NAME_MAX_LEN);
    }
    return result;
  }

+22 −0
Original line number Diff line number Diff line
@@ -51,6 +51,13 @@ add_task(async function test_1() {
          description: "name",
        },
      },
      "_locales/es-ES/messages.json": {
        name: {
          // This length is 80, which exceeds ExtensionData.EXT_NAME_MAX_LEN:
          message: "123456789_".repeat(8),
          description: "name with 80 chars, should truncate to 75",
        },
      },
    },
  });

@@ -101,3 +108,18 @@ add_task(async function test_6() {

  await addon.enable();
});

add_task(async function test_name_too_long() {
  await restartWithLocales(["es-ES"]);

  let addon = await AddonManager.getAddonByID("addon1@tests.mozilla.org");
  Assert.notEqual(addon, null);

  Assert.equal(
    addon.name,
    "123456789_123456789_123456789_123456789_123456789_123456789_123456789_12345",
    "Name should be truncated"
  );

  await addon.enable();
});