summaryrefslogtreecommitdiffstats
path: root/dom/security/test/general/browser_test_http_download.js
blob: 35e3fdfc4b000af549e44d2e36d7c08cec99cd1c (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
/* Any copyright is dedicated to the Public Domain.
 * https://creativecommons.org/publicdomain/zero/1.0/ */

ChromeUtils.defineESModuleGetters(this, {
  Downloads: "resource://gre/modules/Downloads.sys.mjs",
  DownloadsCommon: "resource:///modules/DownloadsCommon.sys.mjs",
});

const HandlerService = Cc[
  "@mozilla.org/uriloader/handler-service;1"
].getService(Ci.nsIHandlerService);

const MIMEService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);

// Using insecure HTTP URL for a test cases around HTTP downloads
let INSECURE_BASE_URL =
  getRootDirectory(gTestPath).replace(
    "chrome://mochitests/content/",
    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
    "http://example.com/"
  ) + "http_download_page.html";

function promiseFocus() {
  return new Promise(resolve => {
    waitForFocus(resolve);
  });
}

async function task_openPanel() {
  await promiseFocus();

  let promise = BrowserTestUtils.waitForPopupEvent(
    DownloadsPanel.panel,
    "shown"
  );
  DownloadsPanel.showPanel();
  await promise;
}

const downloadMonitoringView = {
  _listeners: [],
  onDownloadAdded(download) {
    for (let listener of this._listeners) {
      listener(download);
    }
    this._listeners = [];
  },
  waitForDownload(listener) {
    this._listeners.push(listener);
  },
};

/**
 * Waits until a download is triggered.
 * Unless the always_ask_before_handling_new_types pref is true, the download
 * will simply be saved, so resolve when the view is notified of the new
 * download. Otherwise, it waits until a prompt is shown, selects the choosen
 * <action>, then accepts the dialog
 * @param [action] Which action to select, either:
 *        "handleInternally", "save" or "open".
 * @returns {Promise} Resolved once done.
 */

function shouldTriggerDownload(action = "save") {
  if (
    Services.prefs.getBoolPref(
      "browser.download.always_ask_before_handling_new_types"
    )
  ) {
    return new Promise((resolve, reject) => {
      Services.wm.addListener({
        onOpenWindow(xulWin) {
          Services.wm.removeListener(this);
          let win = xulWin.docShell.domWindow;
          waitForFocus(() => {
            if (
              win.location ==
              "chrome://mozapps/content/downloads/unknownContentType.xhtml"
            ) {
              let dialog = win.document.getElementById("unknownContentType");
              let button = dialog.getButton("accept");
              let actionRadio = win.document.getElementById(action);
              actionRadio.click();
              button.disabled = false;
              dialog.acceptDialog();
              resolve();
            } else {
              reject();
            }
          }, win);
        },
      });
    });
  }
  return new Promise(res => {
    downloadMonitoringView.waitForDownload(res);
  });
}

const CONSOLE_ERROR_MESSAGE = "We blocked a download that’s not secure";

function shouldConsoleError() {
  // Waits until CONSOLE_ERROR_MESSAGE was logged
  return new Promise((resolve, reject) => {
    function listener(msgObj) {
      let text = msgObj.message;
      if (text.includes(CONSOLE_ERROR_MESSAGE)) {
        Services.console.unregisterListener(listener);
        resolve();
      }
    }
    Services.console.registerListener(listener);
  });
}

async function resetDownloads() {
  // Removes all downloads from the download List
  const types = new Set();
  let publicList = await Downloads.getList(Downloads.PUBLIC);
  let downloads = await publicList.getAll();
  for (let download of downloads) {
    if (download.contentType) {
      types.add(download.contentType);
    }
    publicList.remove(download);
    await download.finalize(true);
  }

  if (types.size) {
    // reset handlers for the contentTypes of any files previously downloaded
    for (let type of types) {
      const mimeInfo = MIMEService.getFromTypeAndExtension(type, "");
      info("resetting handler for type: " + type);
      HandlerService.remove(mimeInfo);
    }
  }
}

function shouldNotifyDownloadUI() {
  return new Promise(res => {
    downloadMonitoringView.waitForDownload(async aDownload => {
      let { error } = aDownload;
      if (
        error.becauseBlockedByReputationCheck &&
        error.reputationCheckVerdict == Downloads.Error.BLOCK_VERDICT_INSECURE
      ) {
        // It's an insecure Download, now Check that it has been cleaned up properly
        if ((await IOUtils.stat(aDownload.target.path)).size != 0) {
          throw new Error(`Download target is not empty!`);
        }
        if ((await IOUtils.stat(aDownload.target.path)).size != 0) {
          throw new Error(`Download partFile was not cleaned up properly`);
        }
        // Assert that the Referrer is presnt
        if (!aDownload.source.referrerInfo) {
          throw new Error("The Blocked download is missing the ReferrerInfo");
        }

        res(aDownload);
      } else {
        ok(false, "No error for download that was expected to error!");
      }
    });
  });
}

async function runTest(url, link, checkFunction, description) {
  await SpecialPowers.pushPrefEnv({
    set: [["dom.block_download_insecure", true]],
  });
  await resetDownloads();

  let tab = BrowserTestUtils.addTab(gBrowser, url);
  gBrowser.selectedTab = tab;

  let browser = gBrowser.getBrowserForTab(tab);
  await BrowserTestUtils.browserLoaded(browser);

  info("Checking: " + description);

  let checkPromise = checkFunction();
  // Click the Link to trigger the download
  SpecialPowers.spawn(gBrowser.selectedBrowser, [link], contentLink => {
    content.document.getElementById(contentLink).click();
  });

  await checkPromise;

  ok(true, description);
  BrowserTestUtils.removeTab(tab);

  await SpecialPowers.popPrefEnv();
}

add_setup(async () => {
  let list = await Downloads.getList(Downloads.ALL);
  list.addView(downloadMonitoringView);
  registerCleanupFunction(() => list.removeView(downloadMonitoringView));
});

// Test Blocking
add_task(async function test_blocking() {
  for (let prefVal of [true, false]) {
    await SpecialPowers.pushPrefEnv({
      set: [["browser.download.always_ask_before_handling_new_types", prefVal]],
    });
    await runTest(
      INSECURE_BASE_URL,
      "http-link",
      () =>
        Promise.all([
          shouldTriggerDownload(),
          shouldNotifyDownloadUI(),
          shouldConsoleError(),
        ]),
      "Insecure (HTTP) toplevel -> Insecure (HTTP) download should Error"
    );
    await SpecialPowers.popPrefEnv();
  }
});

// Test Manual Unblocking
add_task(async function test_manual_unblocking() {
  for (let prefVal of [true, false]) {
    await SpecialPowers.pushPrefEnv({
      set: [["browser.download.always_ask_before_handling_new_types", prefVal]],
    });
    await runTest(
      INSECURE_BASE_URL,
      "http-link",
      async () => {
        let [, download] = await Promise.all([
          shouldTriggerDownload(),
          shouldNotifyDownloadUI(),
        ]);
        await download.unblock();
        Assert.equal(
          download.error,
          null,
          "There should be no error after unblocking"
        );
      },
      "A blocked download should succeed to download after a manual unblock"
    );
    await SpecialPowers.popPrefEnv();
  }
});

// Test Unblock Download Visible
add_task(async function test_unblock_download_visible() {
  for (let prefVal of [true, false]) {
    await SpecialPowers.pushPrefEnv({
      set: [["browser.download.always_ask_before_handling_new_types", prefVal]],
    });
    await promiseFocus();
    await runTest(
      INSECURE_BASE_URL,
      "http-link",
      async () => {
        let panelHasOpened = BrowserTestUtils.waitForPopupEvent(
          DownloadsPanel.panel,
          "shown"
        );
        info("awaiting that the download is triggered and added to the list");
        await Promise.all([shouldTriggerDownload(), shouldNotifyDownloadUI()]);
        info("awaiting that the Download list shows itself");
        await panelHasOpened;
        DownloadsPanel.hidePanel();
        ok(true, "The Download Panel should have opened on blocked download");
      },
      "A blocked download should open the download panel"
    );
    await SpecialPowers.popPrefEnv();
  }
});