summaryrefslogtreecommitdiffstats
path: root/uriloader/exthandler/tests/mochitest/head.js
blob: 4b973da06d08a6bbb00a625ead99e415b846407f (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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
var { FileUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/FileUtils.sys.mjs"
);
var { HandlerServiceTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/HandlerServiceTestUtils.sys.mjs"
);

var gMimeSvc = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
var gHandlerSvc = Cc["@mozilla.org/uriloader/handler-service;1"].getService(
  Ci.nsIHandlerService
);

function createMockedHandlerApp() {
  // Mock the executable
  let mockedExecutable = new FileUtils.File(
    PathUtils.join(PathUtils.tempDir, "mockedExecutable")
  );
  if (!mockedExecutable.exists()) {
    mockedExecutable.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o755);
  }

  // Mock the handler app
  let mockedHandlerApp = Cc[
    "@mozilla.org/uriloader/local-handler-app;1"
  ].createInstance(Ci.nsILocalHandlerApp);
  mockedHandlerApp.executable = mockedExecutable;
  mockedHandlerApp.detailedDescription = "Mocked handler app";

  registerCleanupFunction(function () {
    // remove the mocked executable from disk.
    if (mockedExecutable.exists()) {
      mockedExecutable.remove(true);
    }
  });

  return mockedHandlerApp;
}

function createMockedObjects(createHandlerApp) {
  // Mock the mime info
  let internalMockedMIME = gMimeSvc.getFromTypeAndExtension(
    "text/x-test-handler",
    null
  );
  internalMockedMIME.alwaysAskBeforeHandling = true;
  internalMockedMIME.preferredAction = Ci.nsIHandlerInfo.useHelperApp;
  internalMockedMIME.appendExtension("abc");
  if (createHandlerApp) {
    let mockedHandlerApp = createMockedHandlerApp();
    internalMockedMIME.description = mockedHandlerApp.detailedDescription;
    internalMockedMIME.possibleApplicationHandlers.appendElement(
      mockedHandlerApp
    );
    internalMockedMIME.preferredApplicationHandler = mockedHandlerApp;
  }

  // Proxy for the mocked MIME info for faking the read-only attributes
  let mockedMIME = new Proxy(internalMockedMIME, {
    get(target, property) {
      switch (property) {
        case "hasDefaultHandler":
          return true;
        case "defaultDescription":
          return "Default description";
        default:
          return target[property];
      }
    },
  });

  // Mock the launcher:
  let mockedLauncher = {
    MIMEInfo: mockedMIME,
    source: Services.io.newURI("http://www.mozilla.org/"),
    suggestedFileName: "test_download_dialog.abc",
    targetFileIsExecutable: false,
    saveToDisk() {},
    cancel() {},
    setDownloadToLaunch() {},
    setWebProgressListener() {},
    saveDestinationAvailable() {},
    contentLength: 42,
    targetFile: null, // never read
    // PRTime is microseconds since epoch, Date.now() returns milliseconds:
    timeDownloadStarted: Date.now() * 1000,
    QueryInterface: ChromeUtils.generateQI([
      "nsICancelable",
      "nsIHelperAppLauncher",
    ]),
  };

  registerCleanupFunction(function () {
    // remove the mocked mime info from database.
    let mockHandlerInfo = gMimeSvc.getFromTypeAndExtension(
      "text/x-test-handler",
      null
    );
    if (gHandlerSvc.exists(mockHandlerInfo)) {
      gHandlerSvc.remove(mockHandlerInfo);
    }
  });

  return mockedLauncher;
}

function createTemporarySaveDirectory() {
  var saveDir = Services.dirsvc.get("TmpD", Ci.nsIFile);
  saveDir.append("testsavedir");
  if (!saveDir.exists()) {
    saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755);
  }
  return saveDir;
}

async function openHelperAppDialog(launcher) {
  let helperAppDialog = Cc[
    "@mozilla.org/helperapplauncherdialog;1"
  ].createInstance(Ci.nsIHelperAppLauncherDialog);

  let helperAppDialogShownPromise = BrowserTestUtils.domWindowOpenedAndLoaded();
  try {
    helperAppDialog.show(launcher, window, "foopy");
  } catch (ex) {
    ok(
      false,
      "Trying to show unknownContentType.xhtml failed with exception: " + ex
    );
    console.error(ex);
  }
  let dlg = await helperAppDialogShownPromise;

  is(
    dlg.location.href,
    "chrome://mozapps/content/downloads/unknownContentType.xhtml",
    "Got correct dialog"
  );

  return dlg;
}

/**
 * Wait for a subdialog event indicating a dialog either opened
 * or was closed.
 *
 * First argument is the browser in which to listen. If a tabbrowser,
 * we listen to subdialogs for any tab of that browser.
 */
async function waitForSubDialog(browser, url, state) {
  let eventStr = state ? "dialogopen" : "dialogclose";

  let eventTarget;

  // Tabbrowser?
  if (browser.tabContainer) {
    eventTarget = browser.tabContainer.ownerDocument.documentElement;
  } else {
    // Individual browser. Get its box:
    let tabDialogBox = browser.ownerGlobal.gBrowser.getTabDialogBox(browser);
    eventTarget = tabDialogBox.getTabDialogManager()._dialogStack;
  }

  let checkFn;

  if (state) {
    checkFn = dialogEvent => dialogEvent.detail.dialog?._openedURL == url;
  }

  let event = await BrowserTestUtils.waitForEvent(
    eventTarget,
    eventStr,
    true,
    checkFn
  );

  let { dialog } = event.detail;

  // If the dialog is closing wait for it to be fully closed before resolving
  if (!state) {
    await dialog._closingPromise;
  }

  return event.detail.dialog;
}

/**
 * Wait for protocol permission dialog open/close.
 * @param {MozBrowser} browser - Browser element the dialog belongs to.
 * @param {boolean} state - true: dialog open, false: dialog close
 * @returns {Promise<SubDialog>} - Returns a promise which resolves with the
 * SubDialog object of the dialog which closed or opened.
 */
async function waitForProtocolPermissionDialog(browser, state) {
  return waitForSubDialog(
    browser,
    "chrome://mozapps/content/handling/permissionDialog.xhtml",
    state
  );
}

/**
 * Wait for protocol app chooser dialog open/close.
 * @param {MozBrowser} browser - Browser element the dialog belongs to.
 * @param {boolean} state - true: dialog open, false: dialog close
 * @returns {Promise<SubDialog>} - Returns a promise which resolves with the
 * SubDialog object of the dialog which closed or opened.
 */
async function waitForProtocolAppChooserDialog(browser, state) {
  return waitForSubDialog(
    browser,
    "chrome://mozapps/content/handling/appChooser.xhtml",
    state
  );
}

async function promiseDownloadFinished(list, stopFromOpening) {
  return new Promise(resolve => {
    list.addView({
      onDownloadChanged(download) {
        if (stopFromOpening) {
          download.launchWhenSucceeded = false;
        }
        info("Download changed!");
        if (download.succeeded || download.error) {
          info("Download succeeded or errored");
          list.removeView(this);
          resolve(download);
        }
      },
    });
  });
}

function setupMailHandler() {
  let mailHandlerInfo = HandlerServiceTestUtils.getHandlerInfo("mailto");
  let gOldMailHandlers = [];

  // Remove extant web handlers because they have icons that
  // we fetch from the web, which isn't allowed in tests.
  let handlers = mailHandlerInfo.possibleApplicationHandlers;
  for (let i = handlers.Count() - 1; i >= 0; i--) {
    try {
      let handler = handlers.queryElementAt(i, Ci.nsIWebHandlerApp);
      gOldMailHandlers.push(handler);
      // If we get here, this is a web handler app. Remove it:
      handlers.removeElementAt(i);
    } catch (ex) {}
  }

  let previousHandling = mailHandlerInfo.alwaysAskBeforeHandling;
  mailHandlerInfo.alwaysAskBeforeHandling = true;

  // Create a dummy web mail handler so we always know the mailto: protocol.
  // Without this, the test fails on VMs without a default mailto: handler,
  // because no dialog is ever shown, as we ignore subframe navigations to
  // protocols that cannot be handled.
  let dummy = Cc["@mozilla.org/uriloader/web-handler-app;1"].createInstance(
    Ci.nsIWebHandlerApp
  );
  dummy.name = "Handler 1";
  dummy.uriTemplate = "https://example.com/first/%s";
  mailHandlerInfo.possibleApplicationHandlers.appendElement(dummy);

  gHandlerSvc.store(mailHandlerInfo);
  registerCleanupFunction(() => {
    // Re-add the original protocol handlers:
    let mailHandlers = mailHandlerInfo.possibleApplicationHandlers;
    for (let i = handlers.Count() - 1; i >= 0; i--) {
      try {
        // See if this is a web handler. If it is, it'll throw, otherwise,
        // we will remove it.
        mailHandlers.queryElementAt(i, Ci.nsIWebHandlerApp);
        mailHandlers.removeElementAt(i);
      } catch (ex) {}
    }
    for (let h of gOldMailHandlers) {
      mailHandlers.appendElement(h);
    }
    mailHandlerInfo.alwaysAskBeforeHandling = previousHandling;
    gHandlerSvc.store(mailHandlerInfo);
  });
}

let gDownloadDir;

async function setDownloadDir() {
  let tmpDir = PathUtils.join(
    PathUtils.tempDir,
    "testsavedir" + Math.floor(Math.random() * 2 ** 32)
  );
  // Create this dir if it doesn't exist (ignores existing dirs)
  await IOUtils.makeDirectory(tmpDir);
  registerCleanupFunction(async function () {
    Services.prefs.clearUserPref("browser.download.folderList");
    Services.prefs.clearUserPref("browser.download.dir");
    try {
      await IOUtils.remove(tmpDir, { recursive: true });
    } catch (e) {
      console.error(e);
    }
  });
  Services.prefs.setIntPref("browser.download.folderList", 2);
  Services.prefs.setCharPref("browser.download.dir", tmpDir);
  return tmpDir;
}

add_setup(async function test_common_initialize() {
  gDownloadDir = await setDownloadDir();
  Services.prefs.setCharPref("browser.download.loglevel", "Debug");
  registerCleanupFunction(function () {
    Services.prefs.clearUserPref("browser.download.loglevel");
  });
});

async function removeAllDownloads() {
  let publicList = await Downloads.getList(Downloads.PUBLIC);
  let downloads = await publicList.getAll();
  for (let download of downloads) {
    await publicList.remove(download);
    if (await IOUtils.exists(download.target.path)) {
      await download.finalize(true);
    }
  }
}

// Helpers for external protocol sandbox tests.
const EXT_PROTO_URI_MAILTO = "mailto:test@example.com";

/**
 * Creates and iframe and navigate to an external protocol from the iframe.
 * @param {MozBrowser} browser - Browser to spawn iframe in.
 * @param {string} sandboxAttr - Sandbox attribute value for the iframe.
 * @param {'trustedClick'|'untrustedClick'|'trustedLocationAPI'|'untrustedLocationAPI'|'frameSrc'|'frameSrcRedirect'} triggerMethod
 *  - How to trigger the navigation to the external protocol.
 */
async function navigateExternalProtoFromIframe(
  browser,
  sandboxAttr,
  useCSPSandbox = false,
  triggerMethod = "trustedClick"
) {
  if (
    ![
      "trustedClick",
      "untrustedClick",
      "trustedLocationAPI",
      "untrustedLocationAPI",
      "frameSrc",
      "frameSrcRedirect",
    ].includes(triggerMethod)
  ) {
    throw new Error("Invalid trigger method " + triggerMethod);
  }

  // Construct the url to use as iframe src.
  let testPath = getRootDirectory(gTestPath).replace(
    "chrome://mochitests/content",
    "https://example.com"
  );
  let frameSrc = testPath + "/protocol_custom_sandbox_helper.sjs";

  // Load the external protocol directly via the frame src field.
  if (triggerMethod == "frameSrc") {
    frameSrc = EXT_PROTO_URI_MAILTO;
  } else if (triggerMethod == "frameSrcRedirect") {
    let url = new URL(frameSrc);
    url.searchParams.set("redirectCustomProtocol", "true");
    frameSrc = url.href;
  }

  // If enabled set the sandbox attributes via CSP header instead. To do
  // this we need to pass the sandbox flags to the test server via query
  // params.
  if (useCSPSandbox) {
    let url = new URL(frameSrc);
    url.searchParams.set("cspSandbox", sandboxAttr);
    frameSrc = url.href;

    // If we use CSP sandbox attributes we shouldn't set any via iframe attribute.
    sandboxAttr = null;
  }

  // Create a sandboxed iframe and navigate to the external protocol.
  await SpecialPowers.spawn(
    browser,
    [sandboxAttr, frameSrc, EXT_PROTO_URI_MAILTO, triggerMethod],
    async (sandbox, src, extProtoURI, trigger) => {
      let frame = content.document.createElement("iframe");

      if (sandbox != null) {
        frame.sandbox = sandbox;
      }

      frame.src = src;

      let useFrameSrc = trigger == "frameSrc" || trigger == "frameSrcRedirect";

      // Create frame load promise.
      let frameLoadPromise;
      // We won't get a load event if we directly put the external protocol in
      // the frame src.
      if (!useFrameSrc) {
        frameLoadPromise = ContentTaskUtils.waitForEvent(frame, "load", false);
      }

      content.document.body.appendChild(frame);
      await frameLoadPromise;

      if (!useFrameSrc) {
        // Trigger the external protocol navigation in the iframe. We test
        // navigation by clicking links and navigation via the history API.
        await SpecialPowers.spawn(
          frame,
          [extProtoURI, trigger],
          async (uri, trigger2) => {
            let link = content.document.createElement("a");
            link.innerText = "CLICK ME";
            link.id = "extProtoLink";
            content.document.body.appendChild(link);

            if (trigger2 == "trustedClick" || trigger2 == "untrustedClick") {
              link.href = uri;
            } else if (
              trigger2 == "trustedLocationAPI" ||
              trigger2 == "untrustedLocationAPI"
            ) {
              link.setAttribute("onclick", `location.href = '${uri}'`);
            }

            if (
              trigger2 == "untrustedClick" ||
              trigger2 == "untrustedLocationAPI"
            ) {
              link.click();
            } else if (
              trigger2 == "trustedClick" ||
              trigger2 == "trustedLocationAPI"
            ) {
              await ContentTaskUtils.waitForCondition(
                () => link,
                "wait for link to be present"
              );
              await EventUtils.synthesizeMouseAtCenter(link, {}, content);
            }
          }
        );
      }
    }
  );
}

/**
 * Wait for the sandbox error message which is shown in the web console when an
 * external protocol navigation from a sandboxed context is blocked.
 * @returns {Promise} - Promise which resolves once message has been logged.
 */
function waitForExtProtocolSandboxError() {
  return new Promise(resolve => {
    Services.console.registerListener(function onMessage(msg) {
      let { message, logLevel } = msg;
      if (logLevel != Ci.nsIConsoleMessage.error) {
        return;
      }
      if (
        !message.includes(
          `Blocked navigation to custom protocol “${EXT_PROTO_URI_MAILTO}” from a sandboxed context.`
        )
      ) {
        return;
      }
      Services.console.unregisterListener(onMessage);
      resolve();
    });
  });
}

/**
 * Run the external protocol sandbox test using iframes.
 * @param {Object} options
 * @param {boolean} options.blocked - Whether the navigation should be blocked.
 * @param {string} options.sandbox -   See {@link navigateExternalProtoFromIframe}.
 * @param {string} options.useCSPSandbox -  See {@link navigateExternalProtoFromIframe}.
 * @param {string} options.triggerMethod - See {@link navigateExternalProtoFromIframe}.
 * @returns {Promise} - Promise which resolves once the test has finished.
 */
function runExtProtocolSandboxTest(options) {
  let { blocked, sandbox, useCSPSandbox = false, triggerMethod } = options;

  let testPath = getRootDirectory(gTestPath).replace(
    "chrome://mochitests/content",
    "https://example.com"
  );

  info("runSandboxTest options: " + JSON.stringify(options));
  return BrowserTestUtils.withNewTab(
    testPath + "/protocol_custom_sandbox_helper.sjs",
    async browser => {
      if (blocked) {
        let errorPromise = waitForExtProtocolSandboxError();
        await navigateExternalProtoFromIframe(
          browser,
          sandbox,
          useCSPSandbox,
          triggerMethod
        );
        await errorPromise;

        ok(
          errorPromise,
          "Should not show the dialog for iframe with sandbox " + sandbox
        );
      } else {
        let dialogWindowOpenPromise = waitForProtocolAppChooserDialog(
          browser,
          true
        );
        await navigateExternalProtoFromIframe(
          browser,
          sandbox,
          useCSPSandbox,
          triggerMethod
        );
        let dialog = await dialogWindowOpenPromise;

        ok(dialog, "Should show the dialog for sandbox " + sandbox);

        // Close dialog before closing the tab to avoid intermittent failures.
        let dialogWindowClosePromise = waitForProtocolAppChooserDialog(
          browser,
          false
        );

        dialog.close();
        await dialogWindowClosePromise;
      }
    }
  );
}