summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/protectionsUI/browser_protectionsUI_suspicious_fingerprinters_subview.js
blob: 220f985223231ff0a66537d768100b4792987445 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

requestLongerTimeout(2);

/**
 * Bug 1863280 - Testing the fingerprinting category of the protection panel
 *               shows the suspicious fingerpinter domain if the fingerprinting
 *               protection is enabled
 */

const TEST_PATH = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  ""
);

const TEST_DOMAIN = "https://www.example.com";
const TEST_3RD_DOMAIN = "https://example.org";

const TEST_PAGE = TEST_DOMAIN + TEST_PATH + "benignPage.html";
const TEST_3RD_CANVAS_FP_PAGE =
  TEST_3RD_DOMAIN + TEST_PATH + "canvas-fingerprinter.html";
const TEST_3RD_FONT_FP_PAGE =
  TEST_3RD_DOMAIN + TEST_PATH + "font-fingerprinter.html";

const FINGERPRINT_BLOCKING_PREF =
  "privacy.trackingprotection.fingerprinting.enabled";
const FINGERPRINT_PROTECTION_PREF = "privacy.fingerprintingProtection";
const FINGERPRINT_PROTECTION_PBM_PREF =
  "privacy.fingerprintingProtection.pbmode";

/**
 * A helper function to check whether or not an element has "notFound" class.
 *
 * @param {String} id The id of the testing element.
 * @returns {Boolean} true when the element has "notFound" class.
 */
function notFound(id) {
  return document.getElementById(id).classList.contains("notFound");
}

/**
 * A helper function to wait until the suspicious fingerprinting content
 * blocking event is dispatched.
 *
 * @param {Window} win The window to listen content blocking events.
 * @returns {Promise} A promise that resolves when the event files.
 */
async function waitForSuspiciousFingerprintingEvent(win) {
  return new Promise(resolve => {
    let listener = {
      onContentBlockingEvent(webProgress, request, event) {
        info(`Received onContentBlockingEvent event: ${event}`);
        if (
          event &
          Ci.nsIWebProgressListener.STATE_BLOCKED_SUSPICIOUS_FINGERPRINTING
        ) {
          win.gBrowser.removeProgressListener(listener);
          resolve();
        }
      },
    };
    win.gBrowser.addProgressListener(listener);
  });
}

/**
 * A helper function that opens a tests page that embeds iframes with given
 * urls.
 *
 * @param {string[]} urls An array of urls that will be embedded in the test page.
 * @param {boolean} usePrivateWin true to indicate testing in a private window.
 * @param {Function} testFn The test function that will be called after the
 * iframes have been loaded.
 */
async function openTestPage(urls, usePrivateWin, testFn) {
  let win = window;

  if (usePrivateWin) {
    win = await BrowserTestUtils.openNewBrowserWindow({ private: true });
  }

  await BrowserTestUtils.withNewTab(
    { gBrowser: win.gBrowser, url: TEST_PAGE },
    async browser => {
      info("Loading all iframes");

      for (const url of urls) {
        await SpecialPowers.spawn(browser, [url], async testUrl => {
          await new content.Promise(resolve => {
            let ifr = content.document.createElement("iframe");
            ifr.onload = resolve;

            content.document.body.appendChild(ifr);
            ifr.src = testUrl;
          });
        });
      }

      info("Running the test");
      await testFn(win, browser);
    }
  );

  if (usePrivateWin) {
    await BrowserTestUtils.closeWindow(win);
  }
}

/**
 * A testing function verifies that fingerprinting category is not shown.
 *
 * @param {*} win The window to run the test.
 */
async function testCategoryNotShown(win) {
  await openProtectionsPanel(false, win);

  let categoryItem = win.document.getElementById(
    "protections-popup-category-fingerprinters"
  );

  // The fingerprinting category should have the 'notFound' class to indicate
  // that no fingerprinter was found in the page.
  ok(
    notFound("protections-popup-category-fingerprinters"),
    "Fingerprinting category is not found"
  );

  ok(
    !BrowserTestUtils.isVisible(categoryItem),
    "Fingerprinting category item is not visible"
  );

  await closeProtectionsPanel(win);
}

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [[FINGERPRINT_BLOCKING_PREF, true]],
  });
});

// Verify that fingerprinting category is not shown if fingerprinting protection
// is disabled.
add_task(async function testFPPDisabled() {
  await SpecialPowers.pushPrefEnv({
    set: [[FINGERPRINT_PROTECTION_PREF, false]],
  });

  await openTestPage(
    [TEST_3RD_CANVAS_FP_PAGE, TEST_3RD_FONT_FP_PAGE],
    false,
    testCategoryNotShown
  );
});

// Verify that fingerprinting category is not shown if no fingerprinting
// activity is detected.
add_task(async function testFPPEnabledWithoutFingerprintingActivity() {
  await SpecialPowers.pushPrefEnv({
    set: [[FINGERPRINT_PROTECTION_PREF, true]],
  });

  // Test the case where the page doesn't load any fingerprinter.
  await openTestPage([], false, testCategoryNotShown);

  // Test the case where the page loads only one fingerprinter. We don't treat
  // this case as suspicious fingerprinting.
  await openTestPage([TEST_3RD_FONT_FP_PAGE], false, testCategoryNotShown);

  // Test the case where the page loads the same fingerprinter multiple times.
  // We don't treat this case as suspicious fingerprinting.
  await openTestPage(
    [TEST_3RD_FONT_FP_PAGE, TEST_3RD_FONT_FP_PAGE],
    false,
    testCategoryNotShown
  );
});

// Verify that fingerprinting category is not shown if no fingerprinting
// activity is detected.
add_task(
  async function testFPPEnabledWithoutSuspiciousFingerprintingActivity() {
    await SpecialPowers.pushPrefEnv({
      set: [[FINGERPRINT_PROTECTION_PREF, true]],
    });

    // Test the case where the page doesn't load any fingerprinter.
    await openTestPage([], false, testCategoryNotShown);

    // Test the case where the page loads only one fingerprinter. We don't treat
    // this case as suspicious fingerprinting.
    await openTestPage([TEST_3RD_FONT_FP_PAGE], false, testCategoryNotShown);

    // Test the case where the page loads the same fingerprinter multiple times.
    // We don't treat this case as suspicious fingerprinting.
    await openTestPage(
      [TEST_3RD_FONT_FP_PAGE, TEST_3RD_FONT_FP_PAGE],
      false,
      testCategoryNotShown
    );
  }
);

// Verify that fingerprinting category is properly shown and the fingerprinting
// subview displays the origin of the suspicious fingerprinter.
add_task(async function testFingerprintingSubview() {
  await SpecialPowers.pushPrefEnv({
    set: [[FINGERPRINT_PROTECTION_PREF, true]],
  });

  await openTestPage(
    [TEST_3RD_CANVAS_FP_PAGE, TEST_3RD_FONT_FP_PAGE],
    false,
    async (win, _) => {
      await openProtectionsPanel(false, win);

      let categoryItem = win.document.getElementById(
        "protections-popup-category-fingerprinters"
      );

      // Explicitly waiting for the category item becoming visible.
      await BrowserTestUtils.waitForMutationCondition(categoryItem, {}, () =>
        BrowserTestUtils.isVisible(categoryItem)
      );

      ok(
        BrowserTestUtils.isVisible(categoryItem),
        "Fingerprinting category item is visible"
      );

      // Click the fingerprinting category and wait until the fingerprinter view is
      // shown.
      let fingerprintersView = win.document.getElementById(
        "protections-popup-fingerprintersView"
      );
      let viewShown = BrowserTestUtils.waitForEvent(
        fingerprintersView,
        "ViewShown"
      );
      categoryItem.click();
      await viewShown;

      ok(true, "Fingerprinter view was shown");

      // Ensure the fingerprinter is listed on the tracker list.
      let listItems = Array.from(
        fingerprintersView.querySelectorAll(".protections-popup-list-item")
      );
      is(listItems.length, 1, "We have 1 fingerprinter in the list");

      let listItem = listItems.find(
        item => item.querySelector("label").value == "https://example.org"
      );
      ok(listItem, "Has an item for example.org");
      ok(BrowserTestUtils.isVisible(listItem), "List item is visible");

      // Back to the popup main view.
      let mainView = win.document.getElementById("protections-popup-mainView");
      viewShown = BrowserTestUtils.waitForEvent(mainView, "ViewShown");
      let backButton = fingerprintersView.querySelector(".subviewbutton-back");
      backButton.click();
      await viewShown;

      ok(true, "Main view was shown");

      await closeProtectionsPanel(win);
    }
  );
});

// Verify the case where the fingerprinting protection is only enabled in PBM.
add_task(async function testFingerprintingSubviewInPBM() {
  // Only enabled fingerprinting protection in PBM.
  await SpecialPowers.pushPrefEnv({
    set: [
      [FINGERPRINT_PROTECTION_PREF, false],
      [FINGERPRINT_PROTECTION_PBM_PREF, true],
    ],
  });

  // Verify that fingerprinting category isn't shown on a normal window.
  await openTestPage(
    [TEST_3RD_CANVAS_FP_PAGE, TEST_3RD_FONT_FP_PAGE],
    false,
    testCategoryNotShown
  );

  // Verify that fingerprinting category is shown on a private window.
  await openTestPage(
    [TEST_3RD_CANVAS_FP_PAGE, TEST_3RD_FONT_FP_PAGE],
    true,
    async (win, _) => {
      await openProtectionsPanel(false, win);

      let categoryItem = win.document.getElementById(
        "protections-popup-category-fingerprinters"
      );

      // Explicitly waiting for the category item becoming visible.
      await BrowserTestUtils.waitForMutationCondition(categoryItem, {}, () =>
        BrowserTestUtils.isVisible(categoryItem)
      );

      ok(
        BrowserTestUtils.isVisible(categoryItem),
        "Fingerprinting category item is visible"
      );

      // Click the fingerprinting category and wait until the fingerprinter view is
      // shown.
      let fingerprintersView = win.document.getElementById(
        "protections-popup-fingerprintersView"
      );
      let viewShown = BrowserTestUtils.waitForEvent(
        fingerprintersView,
        "ViewShown"
      );
      categoryItem.click();
      await viewShown;

      ok(true, "Fingerprinter view was shown");

      // Ensure the fingerprinter is listed on the tracker list.
      let listItems = Array.from(
        fingerprintersView.querySelectorAll(".protections-popup-list-item")
      );
      is(listItems.length, 1, "We have 1 fingerprinter in the list");

      let listItem = listItems.find(
        item => item.querySelector("label").value == "https://example.org"
      );
      ok(listItem, "Has an item for example.org");
      ok(BrowserTestUtils.isVisible(listItem), "List item is visible");

      // Back to the popup main view.
      let mainView = win.document.getElementById("protections-popup-mainView");
      viewShown = BrowserTestUtils.waitForEvent(mainView, "ViewShown");
      let backButton = fingerprintersView.querySelector(".subviewbutton-back");
      backButton.click();
      await viewShown;

      ok(true, "Main view was shown");

      await closeProtectionsPanel(win);
    }
  );
});

// Verify that fingerprinting category will be shown after loading
// fingerprinters.
add_task(async function testDynamicallyLoadFingerprinter() {
  await SpecialPowers.pushPrefEnv({
    set: [[FINGERPRINT_PROTECTION_PREF, true]],
  });

  await openTestPage([TEST_3RD_FONT_FP_PAGE], false, async (win, browser) => {
    await openProtectionsPanel(false, win);

    let categoryItem = win.document.getElementById(
      "protections-popup-category-fingerprinters"
    );

    // The fingerprinting category should have the 'notFound' class to indicate
    // that no suspicious fingerprinter was found in the page.
    ok(
      notFound("protections-popup-category-fingerprinters"),
      "Fingerprinting category is not found"
    );

    ok(
      !BrowserTestUtils.isVisible(categoryItem),
      "Fingerprinting category item is not visible"
    );

    // Add an iframe that triggers suspicious fingerprinting and wait until the
    // content event files.

    let contentBlockingEventPromise = waitForSuspiciousFingerprintingEvent(win);
    await SpecialPowers.spawn(browser, [TEST_3RD_CANVAS_FP_PAGE], test_url => {
      let ifr = content.document.createElement("iframe");

      content.document.body.appendChild(ifr);
      ifr.src = test_url;
    });
    await contentBlockingEventPromise;

    // Click the fingerprinting category and wait until the fingerprinter view
    // is shown.
    let fingerprintersView = win.document.getElementById(
      "protections-popup-fingerprintersView"
    );
    let viewShown = BrowserTestUtils.waitForEvent(
      fingerprintersView,
      "ViewShown"
    );
    categoryItem.click();
    await viewShown;

    ok(true, "Fingerprinter view was shown");

    // Ensure the fingerprinter is listed on the tracker list.
    let listItems = Array.from(
      fingerprintersView.querySelectorAll(".protections-popup-list-item")
    );
    is(listItems.length, 1, "We have 1 fingerprinter in the list");

    let listItem = listItems.find(
      item => item.querySelector("label").value == "https://example.org"
    );
    ok(listItem, "Has an item for example.org");
    ok(BrowserTestUtils.isVisible(listItem), "List item is visible");

    // Back to the popup main view.
    let mainView = win.document.getElementById("protections-popup-mainView");
    viewShown = BrowserTestUtils.waitForEvent(mainView, "ViewShown");
    let backButton = fingerprintersView.querySelector(".subviewbutton-back");
    backButton.click();
    await viewShown;

    ok(true, "Main view was shown");

    await closeProtectionsPanel(win);
  });
});