File: reporting-to-endpoint.https.html

package info (click to toggle)
firefox-esr 78.15.0esr-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,301,156 kB
  • sloc: cpp: 5,665,905; javascript: 4,798,386; ansic: 2,878,233; python: 977,004; asm: 270,347; xml: 181,456; java: 111,756; sh: 72,926; makefile: 21,819; perl: 13,380; cs: 4,725; yacc: 4,565; objc: 3,026; pascal: 1,787; lex: 1,720; ada: 1,681; exp: 505; php: 436; lisp: 260; awk: 152; ruby: 103; csh: 80; sed: 53; sql: 45
file content (171 lines) | stat: -rw-r--r-- 5,379 bytes parent folder | download | duplicates (4)
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
<!doctype html>
<html>
<meta name="timeout" content="long">
<body>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/utils.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script>
// This file consists of tests for COEP reporting. The tests make COEP
// violations and see whether reports are sent to the network as specified.
// We only have basic tests in this file - one for each kind of reports,
// because we can also test the reporting functionality with ReportingObserver,
// and that way is faster, easier to debug, and less flaky.
//
// For more detailed tests and tests with workers, see tests in other files
// such as
//  - reporting-navigation.https.html
//  - reporting-subresource-corp.https.html
//  - cache-storage-reporting*.https.html
// .
const { REMOTE_ORIGIN } = get_host_info();
const BASE = new URL("resources", location).pathname

function wait(ms) {
  return new Promise(resolve => step_timeout(resolve, ms));
}

async function pollReports(endpoint, reports) {
  while (true) {
    await wait(200);
    const res = await fetch(`resources/report.py?endpoint=${endpoint}`, {cache: 'no-store'});
    if (res.status !== 200) {
      continue;
    }
    for (const report of await res.json()) {
      reports.push(report);
    }
  }
}

const reports = [];
const reportsForReportOnly = [];
pollReports('endpoint', reports);
pollReports('report-only-endpoint', reportsForReportOnly);

function checkCorpReportExistence(reports, blockedUrl, contextUrl) {
  blockedUrl = new URL(blockedUrl, location).href;
  contextUrl = new URL(contextUrl, location).href;
  for (const report of reports) {
    if (report.type !== 'coep' || report.url !== contextUrl ||
        report.body.type !== 'corp') {
      continue;
    }
    if (report.body['blocked-url'] === blockedUrl) {
      return;
    }
  }
  assert_unreached(`A report whose blocked-url is ${blockedUrl} and url is ${contextUrl} is not found.`);
}

function checkNavigationReportExistence(reports, blockedUrl, contextUrl) {
  blockedUrl = new URL(blockedUrl, location).href;
  contextUrl = new URL(contextUrl, location).href;
  for (const report of reports) {
    if (report.type !== 'coep' || report.url !== contextUrl ||
        report.body.type !== 'navigation') {
      continue;
    }
    if (report.body['blocked-url'] === blockedUrl) {
      return;
    }
  }
  assert_unreached(`A report whose blocked-url is ${blockedUrl} and url is ${contextUrl} is not found.`);
}

async_test(async (t) => {
  try {
    const iframe = document.createElement('iframe');
    t.add_cleanup(() => iframe.remove());

    iframe.src = `resources/reporting-empty-frame.html`
    document.body.appendChild(iframe);
    await new Promise(resolve => {
      iframe.addEventListener('load', resolve, {once: true});
    });

    const url = `${REMOTE_ORIGIN}/common/text-plain.txt?${token()}`;
    const init = { mode: 'no-cors', cache: 'no-store' };
    // The response comes from cross-origin, and doesn't have a CORP
    // header, so it is blocked.
    iframe.contentWindow.fetch(url, init).catch(() => {});

    // Wait 3 seconds for reports to settle.
    await wait(3000);

    checkCorpReportExistence(reports, url, iframe.src);
    checkCorpReportExistence(reportsForReportOnly, url, iframe.src);

    t.done();
  } catch (e) {
    t.step(() => { throw e });
  }
}, 'subresource CORP');

async_test(async (t) => {
  try {
    const iframe = document.createElement('iframe');
    t.add_cleanup(() => iframe.remove());

    iframe.src = `resources/reporting-empty-frame.html`
    document.body.appendChild(iframe);
    await new Promise(resolve => {
      iframe.addEventListener('load', resolve, {once: true});
    });

    const w = iframe.contentWindow;

    function attachFrame(url) {
      const frame = w.document.createElement('iframe');
      frame.src = url;
      w.document.body.appendChild(frame);
    }

    const url = `${REMOTE_ORIGIN}/common/blank.html?${token()}`;
    // The nested frame comes from cross-origin and doesn't have a CORP
    // header, so it is blocked.
    attachFrame(url);

    // Wait 3 seconds for reports to settle.
    await wait(3000);

    checkCorpReportExistence(reports, url, iframe.src);
    checkCorpReportExistence(reportsForReportOnly, url, iframe.src);

    t.done();
  } catch (e) {
    t.step(() => { throw e });
  }
}, 'navigation CORP');

async_test(async (t) => {
  try {
    const iframe = document.createElement('iframe');
    t.add_cleanup(() => iframe.remove());

    iframe.src = 'resources/reporting-empty-frame.html';
    const targetUrl = `/common/blank.html?${token()}`;
    iframe.addEventListener('load', () => {
      const nested = iframe.contentDocument.createElement('iframe');
      nested.src = targetUrl;
      // |nested| doesn't have COEP whereas |iframe| has, so it is blocked.
      iframe.contentDocument.body.appendChild(nested);
    }, {once: true});

    document.body.appendChild(iframe);

    // Wait 3 seconds for reports to settle.
    await wait(3000);

    checkNavigationReportExistence(reports, targetUrl, iframe.src);
    checkNavigationReportExistence(
      reportsForReportOnly, targetUrl, iframe.src);

    t.done();
  } catch (e) {
    t.step(() => { throw e });
  }
}, 'COEP violation on nested frame navigation');

</script>$