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

"use strict";

const TRACKING_PAGE =
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  "http://tracking.example.org/browser/browser/base/content/test/protectionsUI/trackingPage.html";
const BENIGN_PAGE =
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  "http://tracking.example.org/browser/browser/base/content/test/protectionsUI/benignPage.html";
const COOKIE_PAGE =
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  "http://not-tracking.example.com/browser/browser/base/content/test/protectionsUI/cookiePage.html";

const CM_PREF = "privacy.trackingprotection.cryptomining.enabled";
const FP_PREF = "privacy.trackingprotection.fingerprinting.enabled";
const TP_PREF = "privacy.trackingprotection.enabled";
const CB_PREF = "network.cookie.cookieBehavior";

const PREF_REPORT_BREAKAGE_URL = "browser.contentblocking.reportBreakage.url";

let { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
let { CommonUtils } = ChromeUtils.importESModule(
  "resource://services-common/utils.sys.mjs"
);
let { Preferences } = ChromeUtils.importESModule(
  "resource://gre/modules/Preferences.sys.mjs"
);

add_setup(async function () {
  await UrlClassifierTestUtils.addTestTrackers();

  registerCleanupFunction(() => {
    // Clear prefs that are touched in this test again for sanity.
    Services.prefs.clearUserPref(TP_PREF);
    Services.prefs.clearUserPref(CB_PREF);
    Services.prefs.clearUserPref(FP_PREF);
    Services.prefs.clearUserPref(CM_PREF);
    Services.prefs.clearUserPref(PREF_REPORT_BREAKAGE_URL);

    UrlClassifierTestUtils.cleanupTestTrackers();
  });

  await SpecialPowers.pushPrefEnv({
    set: [
      [
        "urlclassifier.features.fingerprinting.blacklistHosts",
        "fingerprinting.example.com",
      ],
      [
        "urlclassifier.features.fingerprinting.annotate.blacklistHosts",
        "fingerprinting.example.com",
      ],
      ["privacy.trackingprotection.cryptomining.enabled", true],
      [
        "urlclassifier.features.cryptomining.blacklistHosts",
        "cryptomining.example.com",
      ],
      [
        "urlclassifier.features.cryptomining.annotate.blacklistHosts",
        "cryptomining.example.com",
      ],
    ],
  });
});

add_task(async function testReportBreakageCancel() {
  Services.prefs.setBoolPref(TP_PREF, true);

  await BrowserTestUtils.withNewTab(TRACKING_PAGE, async function () {
    await openProtectionsPanel();
    await TestUtils.waitForCondition(() =>
      gProtectionsHandler._protectionsPopup.hasAttribute("blocking")
    );

    let siteNotWorkingButton = document.getElementById(
      "protections-popup-tp-switch-breakage-link"
    );
    ok(
      BrowserTestUtils.is_visible(siteNotWorkingButton),
      "site not working button is visible"
    );
    let siteNotWorkingView = document.getElementById(
      "protections-popup-siteNotWorkingView"
    );
    let viewShown = BrowserTestUtils.waitForEvent(
      siteNotWorkingView,
      "ViewShown"
    );
    siteNotWorkingButton.click();
    await viewShown;

    let sendReportButton = document.getElementById(
      "protections-popup-siteNotWorkingView-sendReport"
    );
    let sendReportView = document.getElementById(
      "protections-popup-sendReportView"
    );
    viewShown = BrowserTestUtils.waitForEvent(sendReportView, "ViewShown");
    sendReportButton.click();
    await viewShown;

    ok(true, "Report breakage view was shown");

    viewShown = BrowserTestUtils.waitForEvent(siteNotWorkingView, "ViewShown");
    let cancelButton = document.getElementById(
      "protections-popup-sendReportView-cancel"
    );
    cancelButton.click();
    await viewShown;

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

  Services.prefs.clearUserPref(TP_PREF);
});

add_task(async function testReportBreakageSiteException() {
  Services.prefs.setBoolPref(TP_PREF, true);

  let url = TRACKING_PAGE + "?a=b&1=abc&unicode=🦊";

  await BrowserTestUtils.withNewTab(url, async browser => {
    let loaded = BrowserTestUtils.browserLoaded(browser, false);
    gProtectionsHandler.disableForCurrentPage();
    await loaded;

    await openProtectionsPanel();

    let siteFixedButton = document.getElementById(
      "protections-popup-tp-switch-breakage-fixed-link"
    );
    ok(
      BrowserTestUtils.is_visible(siteFixedButton),
      "site fixed button is visible"
    );
    let sendReportView = document.getElementById(
      "protections-popup-sendReportView"
    );
    let viewShown = BrowserTestUtils.waitForEvent(sendReportView, "ViewShown");
    siteFixedButton.click();
    await viewShown;

    ok(true, "Report breakage view was shown");

    await testReportBreakageSubmit(
      TRACKING_PAGE,
      "trackingprotection",
      false,
      true
    );

    // Pass false for shouldReload - there's no need since the tab is going away.
    gProtectionsHandler.enableForCurrentPage(false);
  });

  Services.prefs.clearUserPref(TP_PREF);
});

add_task(async function testNoTracking() {
  await BrowserTestUtils.withNewTab(BENIGN_PAGE, async function () {
    await openProtectionsPanel();

    let siteNotWorkingButton = document.getElementById(
      "protections-popup-tp-switch-breakage-link"
    );
    ok(
      BrowserTestUtils.is_hidden(siteNotWorkingButton),
      "site not working button is not visible"
    );
  });
});

add_task(async function testReportBreakageError() {
  Services.prefs.setBoolPref(TP_PREF, true);
  // Make sure that we correctly strip the query.
  let url = TRACKING_PAGE + "?a=b&1=abc&unicode=🦊";
  await BrowserTestUtils.withNewTab(url, async function () {
    await openAndTestReportBreakage(TRACKING_PAGE, "trackingprotection", true);
  });

  Services.prefs.clearUserPref(TP_PREF);
});

add_task(async function testTP() {
  Services.prefs.setBoolPref(TP_PREF, true);
  // Make sure that we correctly strip the query.
  let url = TRACKING_PAGE + "?a=b&1=abc&unicode=🦊";
  await BrowserTestUtils.withNewTab(url, async function () {
    await openAndTestReportBreakage(TRACKING_PAGE, "trackingprotection");
  });

  Services.prefs.clearUserPref(TP_PREF);
});

add_task(async function testCR() {
  Services.prefs.setIntPref(
    CB_PREF,
    Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER
  );
  // Make sure that we correctly strip the query.
  let url = COOKIE_PAGE + "?a=b&1=abc&unicode=🦊";
  await BrowserTestUtils.withNewTab(url, async function () {
    await openAndTestReportBreakage(COOKIE_PAGE, "cookierestrictions");
  });

  Services.prefs.clearUserPref(CB_PREF);
});

add_task(async function testFP() {
  Services.prefs.setIntPref(CB_PREF, Ci.nsICookieService.BEHAVIOR_ACCEPT);
  Services.prefs.setBoolPref(FP_PREF, true);
  // Make sure that we correctly strip the query.
  let url = TRACKING_PAGE + "?a=b&1=abc&unicode=🦊";
  await BrowserTestUtils.withNewTab(url, async function (browser) {
    let promise = waitForContentBlockingEvent();
    await SpecialPowers.spawn(browser, [], function () {
      content.postMessage("fingerprinting", "*");
    });
    await promise;

    await openAndTestReportBreakage(TRACKING_PAGE, "fingerprinting", true);
  });

  Services.prefs.clearUserPref(FP_PREF);
  Services.prefs.clearUserPref(CB_PREF);
});

add_task(async function testCM() {
  Services.prefs.setIntPref(CB_PREF, Ci.nsICookieService.BEHAVIOR_ACCEPT);
  Services.prefs.setBoolPref(CM_PREF, true);
  // Make sure that we correctly strip the query.
  let url = TRACKING_PAGE + "?a=b&1=abc&unicode=🦊";
  await BrowserTestUtils.withNewTab(url, async function (browser) {
    let promise = waitForContentBlockingEvent();
    await SpecialPowers.spawn(browser, [], function () {
      content.postMessage("cryptomining", "*");
    });
    await promise;

    await openAndTestReportBreakage(TRACKING_PAGE, "cryptomining", true);
  });

  Services.prefs.clearUserPref(CM_PREF);
  Services.prefs.clearUserPref(CB_PREF);
});

async function openAndTestReportBreakage(url, tags, error = false) {
  await openProtectionsPanel();

  let siteNotWorkingButton = document.getElementById(
    "protections-popup-tp-switch-breakage-link"
  );
  ok(
    BrowserTestUtils.is_visible(siteNotWorkingButton),
    "site not working button is visible"
  );
  let siteNotWorkingView = document.getElementById(
    "protections-popup-siteNotWorkingView"
  );
  let viewShown = BrowserTestUtils.waitForEvent(
    siteNotWorkingView,
    "ViewShown"
  );
  siteNotWorkingButton.click();
  await viewShown;

  let sendReportButton = document.getElementById(
    "protections-popup-siteNotWorkingView-sendReport"
  );
  let sendReportView = document.getElementById(
    "protections-popup-sendReportView"
  );
  viewShown = BrowserTestUtils.waitForEvent(sendReportView, "ViewShown");
  sendReportButton.click();
  await viewShown;

  ok(true, "Report breakage view was shown");

  await testReportBreakageSubmit(url, tags, error, false);
}

// This function assumes that the breakage report view is ready.
async function testReportBreakageSubmit(url, tags, error, hasException) {
  // Setup a mock server for receiving breakage reports.
  let server = new HttpServer();
  server.start(-1);
  let i = server.identity;
  let path =
    i.primaryScheme + "://" + i.primaryHost + ":" + i.primaryPort + "/";

  Services.prefs.setStringPref(PREF_REPORT_BREAKAGE_URL, path);

  let comments = document.getElementById(
    "protections-popup-sendReportView-collection-comments"
  );
  is(comments.value, "", "Comments textarea should initially be empty");

  let submitButton = document.getElementById(
    "protections-popup-sendReportView-submit"
  );
  let reportURL = document.getElementById(
    "protections-popup-sendReportView-collection-url"
  ).value;

  is(reportURL, url, "Shows the correct URL in the report UI.");

  // Make sure that sending the report closes the identity popup.
  let popuphidden = BrowserTestUtils.waitForEvent(
    gProtectionsHandler._protectionsPopup,
    "popuphidden"
  );

  // Check that we're receiving a good report.
  await new Promise(resolve => {
    server.registerPathHandler("/", async (request, response) => {
      is(request.method, "POST", "request was a post");

      // Extract and "parse" the form data in the request body.
      let body = CommonUtils.readBytesFromInputStream(request.bodyInputStream);
      let boundary = request
        .getHeader("Content-Type")
        .match(/boundary=-+([^-]*)/i)[1];
      let regex = new RegExp("-+" + boundary + "-*\\s+");
      let sections = body.split(regex);

      let prefs = [
        "privacy.trackingprotection.enabled",
        "privacy.trackingprotection.pbmode.enabled",
        "urlclassifier.trackingTable",
        "network.http.referer.defaultPolicy",
        "network.http.referer.defaultPolicy.pbmode",
        "network.cookie.cookieBehavior",
        "privacy.annotate_channels.strict_list.enabled",
        "privacy.restrict3rdpartystorage.expiration",
        "privacy.trackingprotection.fingerprinting.enabled",
        "privacy.trackingprotection.cryptomining.enabled",
      ];
      let prefsBody = "";

      for (let pref of prefs) {
        prefsBody += `${pref}: ${Preferences.get(pref)}\r\n`;
      }

      Assert.deepEqual(
        sections,
        [
          "",
          `Content-Disposition: form-data; name=\"title\"\r\n\r\n${
            Services.io.newURI(reportURL).host
          }\r\n`,
          'Content-Disposition: form-data; name="body"\r\n\r\n' +
            `Full URL: ${reportURL + "?"}\r\n` +
            `userAgent: ${navigator.userAgent}\r\n\r\n` +
            "**Preferences**\r\n" +
            `${prefsBody}\r\n` +
            `hasException: ${hasException}\r\n\r\n` +
            "**Comments**\r\n" +
            "This is a comment\r\n",
          'Content-Disposition: form-data; name="labels"\r\n\r\n' +
            `${hasException ? "" : tags}\r\n`,
          "",
        ],
        "Should send the correct form data"
      );

      if (error) {
        response.setStatusLine(request.httpVersion, 500, "Request failed");
      } else {
        response.setStatusLine(request.httpVersion, 201, "Entry created");
      }

      resolve();
    });

    comments.value = "This is a comment";
    submitButton.click();
  });

  let errorMessage = document.getElementById(
    "protections-popup-sendReportView-report-error"
  );
  if (error) {
    await TestUtils.waitForCondition(() =>
      BrowserTestUtils.is_visible(errorMessage)
    );
    is(
      comments.value,
      "This is a comment",
      "Comment not cleared in case of an error"
    );
    gProtectionsHandler._protectionsPopup.hidePopup();
  } else {
    ok(BrowserTestUtils.is_hidden(errorMessage), "Error message not shown");
  }

  await popuphidden;

  // Stop the server.
  await new Promise(r => server.stop(r));

  Services.prefs.clearUserPref(PREF_REPORT_BREAKAGE_URL);
}