Bug 1558784 - Truncate abuse report properties that may be longer than allowed by the API endpoint. r=mixedpuppy r=jcristau
authorLuca Greco <lgreco@mozilla.com>
Wed, 12 Jun 2019 20:39:53 +0000
changeset 536968 f2b815c3bbaf5b4cb37c6e4c2cac927e4bc01f8d
parent 536967 c6010ac4cc04f39ff3360e77329684e8215051fe
child 536969 093b0526796014c0ce4074dd5423788130778ff3
push id2082
push userffxbld-merge
push dateMon, 01 Jul 2019 08:34:18 +0000
treeherdermozilla-release@2fb19d0466d2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmixedpuppy, jcristau
bugs1558784
milestone68.0
Bug 1558784 - Truncate abuse report properties that may be longer than allowed by the API endpoint. r=mixedpuppy r=jcristau Differential Revision: https://phabricator.services.mozilla.com/D34768
toolkit/mozapps/extensions/AbuseReporter.jsm
toolkit/mozapps/extensions/test/xpcshell/test_AbuseReporter.js
--- a/toolkit/mozapps/extensions/AbuseReporter.jsm
+++ b/toolkit/mozapps/extensions/AbuseReporter.jsm
@@ -4,16 +4,20 @@
 
 const EXPORTED_SYMBOLS = [ "AbuseReporter", "AbuseReportError" ];
 
 const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 
 Cu.importGlobalProperties(["fetch"]);
 
 const PREF_ABUSE_REPORT_URL  = "extensions.abuseReport.url";
+
+// Maximum length of the string properties sent to the API endpoint.
+const MAX_STRING_LENGTH = 255;
+
 // Minimum time between report submissions (in ms).
 const MIN_MS_BETWEEN_SUBMITS = 30000;
 
 XPCOMUtils.defineLazyModuleGetters(this, {
   AddonManager: "resource://gre/modules/AddonManager.jsm",
   AMTelemetry: "resource://gre/modules/AddonManager.jsm",
   AppConstants: "resource://gre/modules/AppConstants.jsm",
   ClientID: "resource://gre/modules/ClientID.jsm",
@@ -114,21 +118,25 @@ const AbuseReporter = {
    *
    * @param {AddonWrapper} addon
    *        The addon object to collect the detail from.
    *
    * @return {object}
    *         An object that contains the collected details.
    */
   async getReportData(addon) {
+    const truncateString = (text) =>
+      typeof text == "string" ? text.slice(0, MAX_STRING_LENGTH) : text;
+
     const data = {
       addon: addon.id,
       addon_version: addon.version,
-      addon_summary: addon.description,
-      addon_install_origin: addon.sourceURI && addon.sourceURI.spec,
+      addon_name: truncateString(addon.name),
+      addon_summary: truncateString(addon.description),
+      addon_install_origin: addon.sourceURI && truncateString(addon.sourceURI.spec),
       install_date: addon.installDate && addon.installDate.toISOString(),
     };
 
     // Map addon.installTelemetryInfo values to the supported addon_install_method
     // values supported by the API endpoint (See API endpoint docs at
     // https://addons-server.readthedocs.io/en/latest/topics/api/abuse.html).
     let install_method = "other";
     if (addon.installTelemetryInfo) {
--- a/toolkit/mozapps/extensions/test/xpcshell/test_AbuseReporter.js
+++ b/toolkit/mozapps/extensions/test/xpcshell/test_AbuseReporter.js
@@ -396,8 +396,46 @@ add_task(async function test_submission_
 
   await extension.unload();
 
   // Unblock pending requests on the server request handler side, so that the
   // test file can shutdown (otherwise the test run will be stuck after this
   // task completed).
   resolvePendingResponses();
 });
+
+add_task(async function test_truncated_string_properties() {
+  const generateString = len => (new Array(len)).fill("a").join("");
+
+  const LONG_STRINGS_ADDON_ID = "addon-with-long-strings-props@mochi.test";
+  const {extension} = await installTestExtension({
+    manifest: {
+      name: generateString(400),
+      description: generateString(400),
+      applications: {gecko: {id: LONG_STRINGS_ADDON_ID}},
+    },
+  });
+
+  // Override the test api server request handler, to be able to
+  // intercept the properties actually submitted.
+  let reportSubmitted;
+  apiRequestHandler = ({data, request, response}) => {
+    reportSubmitted = JSON.parse(data);
+    handleSubmitRequest({request, response});
+  };
+
+  const report = await AbuseReporter.createAbuseReport(
+    LONG_STRINGS_ADDON_ID, REPORT_OPTIONS);
+
+  await report.submit({message: "fake-message", reason: "fake-reason"});
+
+  const expected = {
+    addon_name: generateString(255),
+    addon_summary: generateString(255),
+  };
+
+  Assert.deepEqual({
+    addon_name: reportSubmitted.addon_name,
+    addon_summary: reportSubmitted.addon_summary,
+  }, expected, "Got the long strings truncated as expected");
+
+  await extension.unload();
+});