summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/cross-origin-embedder-policy/reporting-to-frame-owner.https.html
blob: 331ad898ebd03007683b3fbc3f6d62433bb5da1d (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
<!doctype html>
<html>
<head>
<title>Check COEP reports are sent to iframe for 'new Worker()' failure</title>
</head>
<body>
<script src="/common/get-host-info.sub.js"></script>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script>
<script>
const {ORIGIN} = get_host_info();
const RESOURCES_PATH= new URL("resources", location).pathname;
const iframe_path = "worker-owner-frame.html?pipe=";
const worker_path = "universal-worker.js?pipe=";

const coep_header= {
  "coep-none"         : "",
  "coep-report-only"  :
    "header(Cross-Origin-Embedder-Policy-Report-Only,require-corp)",
  "coep-require-corp" : "|header(Cross-Origin-Embedder-Policy,require-corp)",
};

function checkReport(report, url, blocked_url, disposition) {
  assert_equals(report.type, "coep");
  assert_equals(report.url, url);
  assert_equals(report.body.type, "worker initialization");
  assert_equals(report.body.blockedURL, blocked_url);
  assert_equals(report.body.disposition, disposition);
}

// Test parameters:
// - `owner_coep` the COEP header of the iframe document's response.
// - `worker_coep` the COEP header of the DedicatedWorker's script response.
//
// Test expectations:
// - `length` the length of reports.
// - `disposition`  the disposition in a report's body. Empty string if the
//                  length of reports is expected to be 0.
function check(
  // Test parameters:
  owner_coep,
  worker_coep,
  // Test expectations:
  length,
  disposition) {
  promise_test(async (t) => {
    const worker_url = worker_path + coep_header[worker_coep];
    const iframe_url = iframe_path + coep_header[owner_coep];
    const iframe = await with_iframe("./resources/" + iframe_url);
    t.add_cleanup(() => iframe.remove());

    const iframe_response = new Promise(resolve => window.onmessage = resolve);
    iframe.contentWindow.startWorkerAndObserveReports(worker_url, length > 0);

    const {data} = await iframe_response;
    assert_equals(data.length, length);
    if (data.length > 0) {
      const blocked_url = `${ORIGIN}${RESOURCES_PATH}/${worker_url}`;
      const url = `${ORIGIN}${RESOURCES_PATH}/${iframe_url}`;
      checkReport(
        data[0],
        url,
        blocked_url,
        disposition
      );
    }
  }, `Reporting to ${owner_coep} frame with ${worker_coep} worker`);
}

// -----------------------------------------------------------------------------
//    owner_coep          , worker_coep         , length  , disposition
// -----------------------------------------------------------------------------
check("coep-none"         , "coep-none"         , 0       , "");
check("coep-none"         , "coep-report-only"  , 0       , "");
check("coep-none"         , "coep-require-corp" , 0       , "");
check("coep-report-only"  , "coep-none"         , 1       , "reporting");
check("coep-report-only"  , "coep-report-only"  , 1       , "reporting");
check("coep-report-only"  , "coep-require-corp" , 0       , "");
check("coep-require-corp" , "coep-none"         , 1       , "enforce");
check("coep-require-corp" , "coep-report-only"  , 1       , "enforce");
check("coep-require-corp" , "coep-require-corp" , 0       , "");

</script>
</body>
</html>