summaryrefslogtreecommitdiffstats
path: root/dom/security/test/general/browser_test_report_blocking.js
blob: ebd75140979337cf348de9a49d5997595fdb523c (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
"use strict";

const { TelemetryArchiveTesting } = ChromeUtils.importESModule(
  "resource://testing-common/TelemetryArchiveTesting.sys.mjs"
);

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

const kTestXFrameOptionsURI = kTestPath + "file_framing_error_pages_xfo.html";
const kTestCspURI = kTestPath + "file_framing_error_pages_csp.html";
const kTestXFrameOptionsURIFrame =
  kTestPath + "file_framing_error_pages.sjs?xfo";
const kTestCspURIFrame = kTestPath + "file_framing_error_pages.sjs?csp";

const kTestExpectedPingXFO = [
  [["payload", "error_type"], "xfo"],
  [["payload", "xfo_header"], "deny"],
  [["payload", "csp_header"], ""],
  [["payload", "frame_hostname"], "example.com"],
  [["payload", "top_hostname"], "example.com"],
  [
    ["payload", "frame_uri"],
    "https://example.com/browser/dom/security/test/general/file_framing_error_pages.sjs",
  ],
  [
    ["payload", "top_uri"],
    "https://example.com/browser/dom/security/test/general/file_framing_error_pages_xfo.html",
  ],
];

const kTestExpectedPingCSP = [
  [["payload", "error_type"], "csp"],
  [["payload", "xfo_header"], ""],
  [["payload", "csp_header"], "'none'"],
  [["payload", "frame_hostname"], "example.com"],
  [["payload", "top_hostname"], "example.com"],
  [
    ["payload", "frame_uri"],
    "https://example.com/browser/dom/security/test/general/file_framing_error_pages.sjs",
  ],
  [
    ["payload", "top_uri"],
    "https://example.com/browser/dom/security/test/general/file_framing_error_pages_csp.html",
  ],
];

const TEST_CASES = [
  {
    type: "xfo",
    test_uri: kTestXFrameOptionsURI,
    frame_uri: kTestXFrameOptionsURIFrame,
    expected_ping: kTestExpectedPingXFO,
  },
  {
    type: "csp",
    test_uri: kTestCspURI,
    frame_uri: kTestCspURIFrame,
    expected_ping: kTestExpectedPingCSP,
  },
];

add_setup(async function () {
  Services.telemetry.setEventRecordingEnabled("security.ui.xfocsperror", true);

  await SpecialPowers.pushPrefEnv({
    set: [
      ["security.xfocsp.errorReporting.enabled", true],
      ["security.xfocsp.errorReporting.automatic", false],
    ],
  });
});

add_task(async function testReportingCases() {
  for (const test of TEST_CASES) {
    await testReporting(test);
  }
});

async function testReporting(test) {
  // Clear telemetry event before testing.
  Services.telemetry.clearEvents();

  let telemetryChecker = new TelemetryArchiveTesting.Checker();
  await telemetryChecker.promiseInit();

  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "about:blank"
  );
  let browser = tab.linkedBrowser;

  let loaded = BrowserTestUtils.browserLoaded(
    browser,
    true,
    test.frame_uri,
    true
  );
  BrowserTestUtils.startLoadingURIString(browser, test.test_uri);
  await loaded;

  let { type } = test;

  let frameBC = await SpecialPowers.spawn(browser, [], async _ => {
    const iframe = content.document.getElementById("testframe");
    return iframe.browsingContext;
  });

  await SpecialPowers.spawn(frameBC, [type], async obj => {
    // Wait until the reporting UI is visible.
    await ContentTaskUtils.waitForCondition(() => {
      let reportUI = content.document.getElementById("blockingErrorReporting");
      return ContentTaskUtils.isVisible(reportUI);
    });

    let reportCheckBox = content.document.getElementById(
      "automaticallyReportBlockingInFuture"
    );
    is(
      reportCheckBox.checked,
      false,
      "The checkbox of the reporting ui should be not checked."
    );

    // Click on the checkbox.
    await EventUtils.synthesizeMouseAtCenter(reportCheckBox, {}, content);
  });
  BrowserTestUtils.removeTab(tab);

  // Open the error page again
  tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:blank");
  browser = tab.linkedBrowser;

  loaded = BrowserTestUtils.browserLoaded(browser, true, test.frame_uri, true);
  BrowserTestUtils.startLoadingURIString(browser, test.test_uri);
  await loaded;

  frameBC = await SpecialPowers.spawn(browser, [], async _ => {
    const iframe = content.document.getElementById("testframe");
    return iframe.browsingContext;
  });

  await SpecialPowers.spawn(frameBC, [], async _ => {
    // Wait until the reporting UI is visible.
    await ContentTaskUtils.waitForCondition(() => {
      let reportUI = content.document.getElementById("blockingErrorReporting");
      return ContentTaskUtils.isVisible(reportUI);
    });

    let reportCheckBox = content.document.getElementById(
      "automaticallyReportBlockingInFuture"
    );
    is(
      reportCheckBox.checked,
      true,
      "The checkbox of the reporting ui should be checked."
    );

    // Click on the checkbox again to disable the reporting.
    await EventUtils.synthesizeMouseAtCenter(reportCheckBox, {}, content);

    is(
      reportCheckBox.checked,
      false,
      "The checkbox of the reporting ui should be unchecked."
    );
  });
  BrowserTestUtils.removeTab(tab);

  // Open the error page again to see if the reporting is disabled.
  tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:blank");
  browser = tab.linkedBrowser;

  loaded = BrowserTestUtils.browserLoaded(browser, true, test.frame_uri, true);
  BrowserTestUtils.startLoadingURIString(browser, test.test_uri);
  await loaded;

  frameBC = await SpecialPowers.spawn(browser, [], async _ => {
    const iframe = content.document.getElementById("testframe");
    return iframe.browsingContext;
  });

  await SpecialPowers.spawn(frameBC, [], async _ => {
    // Wait until the reporting UI is visible.
    await ContentTaskUtils.waitForCondition(() => {
      let reportUI = content.document.getElementById("blockingErrorReporting");
      return ContentTaskUtils.isVisible(reportUI);
    });

    let reportCheckBox = content.document.getElementById(
      "automaticallyReportBlockingInFuture"
    );
    is(
      reportCheckBox.checked,
      false,
      "The checkbox of the reporting ui should be unchecked."
    );
  });
  BrowserTestUtils.removeTab(tab);

  // Finally, check if the ping has been archived.
  await new Promise(resolve => {
    telemetryChecker
      .promiseFindPing("xfocsp-error-report", test.expected_ping)
      .then(
        found => {
          ok(found, "Telemetry ping submitted successfully");
          resolve();
        },
        err => {
          ok(false, "Exception finding telemetry ping: " + err);
          resolve();
        }
      );
  });
}