summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/content-security-policy/resource-hints/prefetch-generate-directives.html
blob: 75c9485f0d77dcb51d9cdefa370dc0163c1ef2e3 (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
<!DOCTYPE html>
<html>
<head>
<meta name="timeout" content="long">
<script src='/resources/testharness.js'></script>
<script src='/resources/testharnessreport.js'></script>
<script src='/common/utils.js'></script>
<script src='/content-security-policy/support/testharness-helper.js'></script>
<script>

const directives = {
  'script-src': true,
  'img-src': true,
  'connect-src': true,
  'object-src': true,
  'font-src': true,
  'manifest-src': true,
  'media-src': true,
  'style-src': true,
  'child-src': true,
  'frame-src': true,
  'worker-src': true,
  'base-uri': false,
};

function prefetch_with_csp_in_a_popup(byDirective, t) {
  // Allow inline scripts so that we can run the postMessage script...
  if (byDirective["script-src"] === "*")
    byDirective["script-src"] = "* 'unsafe-inline'";
  else
    byDirective["script-src"] = "'unsafe-inline'";

  const url = new URL('/content-security-policy/support/prefetch-with-csp.html', location.href);
  const csp = Object.entries(byDirective).map(([key, value]) => `${key} ${value}`).join(";");
  url.searchParams.set("pipe", `header(Content-Security-Policy, ${csp})`);
  const uid = token();
  url.searchParams.set("uid", uid);
  const bc = new BroadcastChannel(uid);
  const popup = window.open(url.href);
  t.add_cleanup(() => popup.close());
  return new Promise(resolve => {
    bc.addEventListener("message", ({data}) => {
      resolve(data);
    });
  });
}

for (const directive in directives) {
  promise_test(async t => {
    const byDirective = Object.fromEntries(Object.keys(directives).map(d => [d, "'none'"]));
    byDirective[directive] = "*";
    byDirective["default-src"] = "'none'";
    const prefetch_ok = await prefetch_with_csp_in_a_popup(byDirective, t);
    assert_equals(prefetch_ok, directives[directive], directive);
  }, `Test that ${directive} enabled with everything else disabled allows prefetching`);

  promise_test(async t => {
    const byDirective = {
      "default-src": "'none'",
      [directive]: "*",
    };
    const prefetch_ok = await prefetch_with_csp_in_a_popup(byDirective, t);
    assert_equals(prefetch_ok, directives[directive], directive);
  }, `Test that ${directive} enabled with default-src disabled allows prefetching`);
}

promise_test(async t => {
    const byDirective = {
      "default-src": "'none'",
      "script-src-elem": "* 'unsafe-inline'",
      "script-src": "'none'",
    };
    const prefetch_ok = await prefetch_with_csp_in_a_popup(byDirective, t);
    assert_true(prefetch_ok);
  }, `Test that permissive script-src-elem supersedes script-src`);

promise_test(async t => {
  const byDirective = {
    "default-src": "'none'",
    "script-src-elem": "'unsafe-inline'",
    "script-src": "*",
  };
  const prefetch_ok = await prefetch_with_csp_in_a_popup(byDirective, t);
  assert_true(prefetch_ok);
}, `Test that permissive script-src supersedes script-src-elem`);

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