summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/xpcshell/test_signed_install.js
blob: 065463864dd69ddb97950301dca0c5e22afdbf0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Enable signature checks for these tests
gUseRealCertChecks = true;

const DATA = "data/signing_checks/";
const ADDONS = {
  unsigned: "unsigned.xpi",
  signed1: "signed1.xpi",
  signed2: "signed2.xpi",
  privileged: "privileged.xpi",

  // Bug 1509093
  // sha256Signed: "signed_bootstrap_sha256_1.xpi",
};

// The ID in signed1.xpi and signed2.xpi
const ID = "test@somewhere.com";
const PR_USEC_PER_MSEC = 1000;

let testserver = createHttpServer({ hosts: ["example.com"] });

Services.prefs.setCharPref(
  "extensions.update.background.url",
  "http://example.com/update.json"
);
Services.prefs.setBoolPref(PREF_EM_CHECK_UPDATE_SECURITY, false);

// Creates an add-on with a broken signature by changing an existing file
function createBrokenAddonModify(file) {
  let brokenFile = gTmpD.clone();
  brokenFile.append("broken.xpi");
  file.copyTo(brokenFile.parent, brokenFile.leafName);

  var stream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
    Ci.nsIStringInputStream
  );
  stream.setData("FOOBAR", -1);
  var zipW = Cc["@mozilla.org/zipwriter;1"].createInstance(Ci.nsIZipWriter);
  zipW.open(brokenFile, FileUtils.MODE_RDWR | FileUtils.MODE_APPEND);
  zipW.removeEntry("test.txt", false);
  zipW.addEntryStream(
    "test.txt",
    new Date() * PR_USEC_PER_MSEC,
    Ci.nsIZipWriter.COMPRESSION_NONE,
    stream,
    false
  );
  zipW.close();

  return brokenFile;
}

// Creates an add-on with a broken signature by adding a new file
function createBrokenAddonAdd(file) {
  let brokenFile = gTmpD.clone();
  brokenFile.append("broken.xpi");
  file.copyTo(brokenFile.parent, brokenFile.leafName);

  var stream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
    Ci.nsIStringInputStream
  );
  stream.setData("FOOBAR", -1);
  var zipW = Cc["@mozilla.org/zipwriter;1"].createInstance(Ci.nsIZipWriter);
  zipW.open(brokenFile, FileUtils.MODE_RDWR | FileUtils.MODE_APPEND);
  zipW.addEntryStream(
    "test2.txt",
    new Date() * PR_USEC_PER_MSEC,
    Ci.nsIZipWriter.COMPRESSION_NONE,
    stream,
    false
  );
  zipW.close();

  return brokenFile;
}

// Creates an add-on with a broken signature by removing an existing file
function createBrokenAddonRemove(file) {
  let brokenFile = gTmpD.clone();
  brokenFile.append("broken.xpi");
  file.copyTo(brokenFile.parent, brokenFile.leafName);

  var stream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
    Ci.nsIStringInputStream
  );
  stream.setData("FOOBAR", -1);
  var zipW = Cc["@mozilla.org/zipwriter;1"].createInstance(Ci.nsIZipWriter);
  zipW.open(brokenFile, FileUtils.MODE_RDWR | FileUtils.MODE_APPEND);
  zipW.removeEntry("test.txt", false);
  zipW.close();

  return brokenFile;
}

function serveUpdate(filename) {
  const RESPONSE = {
    addons: {
      [ID]: {
        updates: [
          {
            version: "2.0",
            update_link: `http://example.com/${filename}`,
            applications: {
              gecko: {
                strict_min_version: "4",
                advisory_max_version: "6",
              },
            },
          },
        ],
      },
    },
  };
  AddonTestUtils.registerJSON(testserver, "/update.json", RESPONSE);
}

async function test_install_broken(
  file,
  expectedError,
  expectNullAddon = true
) {
  let install = await AddonManager.getInstallForFile(file);
  await Assert.rejects(
    install.install(),
    /Install failed/,
    "Install of an improperly signed extension should throw"
  );

  Assert.equal(install.state, AddonManager.STATE_DOWNLOAD_FAILED);
  Assert.equal(install.error, expectedError);

  if (expectNullAddon) {
    Assert.equal(install.addon, null);
  }
}

async function test_install_working(file, expectedSignedState) {
  let install = await AddonManager.getInstallForFile(file);
  await install.install();

  Assert.equal(install.state, AddonManager.STATE_INSTALLED);
  Assert.notEqual(install.addon, null);
  Assert.equal(install.addon.signedState, expectedSignedState);

  await install.addon.uninstall();
}

async function test_update_broken(file1, file2, expectedError) {
  // First install the older version
  await Promise.all([
    promiseInstallFile(file1),
    promiseWebExtensionStartup(ID),
  ]);

  testserver.registerFile("/" + file2.leafName, file2);
  serveUpdate(file2.leafName);

  let addon = await promiseAddonByID(ID);
  let update = await promiseFindAddonUpdates(addon);
  let install = update.updateAvailable;
  await Assert.rejects(
    install.install(),
    /Install failed/,
    "Update to an improperly signed extension should throw"
  );

  Assert.equal(install.state, AddonManager.STATE_DOWNLOAD_FAILED);
  Assert.equal(install.error, expectedError);
  Assert.equal(install.addon, null);

  testserver.registerFile("/" + file2.leafName, null);
  testserver.registerPathHandler("/update.json", null);

  await addon.uninstall();
}

async function test_update_working(file1, file2, expectedSignedState) {
  // First install the older version
  await promiseInstallFile(file1);

  testserver.registerFile("/" + file2.leafName, file2);
  serveUpdate(file2.leafName);

  let addon = await promiseAddonByID(ID);
  let update = await promiseFindAddonUpdates(addon);
  let install = update.updateAvailable;
  await Promise.all([install.install(), promiseWebExtensionStartup(ID)]);

  Assert.equal(install.state, AddonManager.STATE_INSTALLED);
  Assert.notEqual(install.addon, null);
  Assert.equal(install.addon.signedState, expectedSignedState);

  testserver.registerFile("/" + file2.leafName, null);
  testserver.registerPathHandler("/update.json", null);

  await install.addon.uninstall();
}

add_task(async function setup() {
  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "4", "4");
  await promiseStartupManager();
});

// Try to install a broken add-on
add_task(async function test_install_invalid_modified() {
  let file = createBrokenAddonModify(do_get_file(DATA + ADDONS.signed1));
  await test_install_broken(file, AddonManager.ERROR_CORRUPT_FILE);
  file.remove(true);
});

add_task(async function test_install_invalid_added() {
  let file = createBrokenAddonAdd(do_get_file(DATA + ADDONS.signed1));
  await test_install_broken(file, AddonManager.ERROR_CORRUPT_FILE);
  file.remove(true);
});

add_task(async function test_install_invalid_removed() {
  let file = createBrokenAddonRemove(do_get_file(DATA + ADDONS.signed1));
  await test_install_broken(file, AddonManager.ERROR_CORRUPT_FILE);
  file.remove(true);
});

// Try to install an unsigned add-on
add_task(async function test_install_invalid_unsigned() {
  let file = do_get_file(DATA + ADDONS.unsigned);
  await test_install_broken(file, AddonManager.ERROR_SIGNEDSTATE_REQUIRED);
});

// Try to install a signed add-on
add_task(async function test_install_valid() {
  let file = do_get_file(DATA + ADDONS.signed1);
  await test_install_working(file, AddonManager.SIGNEDSTATE_SIGNED);
});

add_task(
  {
    pref_set: [["xpinstall.signatures.dev-root", true]],
    // `xpinstall.signatures.dev-root` is not taken into account on release
    // builds because `MOZ_REQUIRE_SIGNING` is set to `true`.
    skip_if: () => AppConstants.MOZ_REQUIRE_SIGNING,
  },
  async function test_install_valid_file_with_different_root_cert() {
    const TEST_CASES = [
      {
        title: "XPI without ID in manifest",
        xpi: "data/webext-implicit-id.xpi",
        expectedMessage:
          /Cannot find id for addon .+ Preference xpinstall.signatures.dev-root is set/,
      },
      {
        title: "XPI with ID in manifest",
        xpi: DATA + ADDONS.signed1,
        expectedMessage: /Add-on test@somewhere.com is not correctly signed/,
      },
    ];

    for (const { title, xpi, expectedMessage } of TEST_CASES) {
      info(`test_install_valid_file_with_different_root_cert: ${title}`);

      const file = do_get_file(xpi);

      const awaitConsole = new Promise(resolve => {
        Services.console.registerListener(function listener(message) {
          if (expectedMessage.test(message.message)) {
            Services.console.unregisterListener(listener);
            resolve();
          }
        });
      });

      await test_install_broken(
        file,
        AddonManager.ERROR_CORRUPT_FILE,
        // We don't expect the `addon` property on the `install` object to be
        // `null` because that seems to happen later (when the signature is
        // checked).
        false
      );

      await awaitConsole;
    }
  }
);

// Try to install an add-on signed with SHA-256
add_task(async function test_install_valid_sha256() {
  // Bug 1509093
  // let file = do_get_file(DATA + ADDONS.sha256Signed);
  // await test_install_working(file, AddonManager.SIGNEDSTATE_SIGNED);
});

// Try to install an add-on with the "Mozilla Extensions" OU
add_task(async function test_install_valid_privileged() {
  let file = do_get_file(DATA + ADDONS.privileged);
  await test_install_working(file, AddonManager.SIGNEDSTATE_PRIVILEGED);
});

// Try to update to a broken add-on
add_task(async function test_update_invalid_modified() {
  let file1 = do_get_file(DATA + ADDONS.signed1);
  let file2 = createBrokenAddonModify(do_get_file(DATA + ADDONS.signed2));
  await test_update_broken(file1, file2, AddonManager.ERROR_CORRUPT_FILE);
  file2.remove(true);
});

add_task(async function test_update_invalid_added() {
  let file1 = do_get_file(DATA + ADDONS.signed1);
  let file2 = createBrokenAddonAdd(do_get_file(DATA + ADDONS.signed2));
  await test_update_broken(file1, file2, AddonManager.ERROR_CORRUPT_FILE);
  file2.remove(true);
});

add_task(async function test_update_invalid_removed() {
  let file1 = do_get_file(DATA + ADDONS.signed1);
  let file2 = createBrokenAddonRemove(do_get_file(DATA + ADDONS.signed2));
  await test_update_broken(file1, file2, AddonManager.ERROR_CORRUPT_FILE);
  file2.remove(true);
});

// Try to update to an unsigned add-on
add_task(async function test_update_invalid_unsigned() {
  let file1 = do_get_file(DATA + ADDONS.signed1);
  let file2 = do_get_file(DATA + ADDONS.unsigned);
  await test_update_broken(
    file1,
    file2,
    AddonManager.ERROR_SIGNEDSTATE_REQUIRED
  );
});

// Try to update to a signed add-on
add_task(async function test_update_valid() {
  let file1 = do_get_file(DATA + ADDONS.signed1);
  let file2 = do_get_file(DATA + ADDONS.signed2);
  await test_update_working(file1, file2, AddonManager.SIGNEDSTATE_SIGNED);
});

add_task(() => promiseShutdownManager());