summaryrefslogtreecommitdiffstats
path: root/dom/security/test/csp/test_child-src_worker-redirect.html
blob: a1b1c9c2b41efa2a8ed3c6f5c263eed7dfbe06e3 (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
<!DOCTYPE HTML>
<html>
  <head>
    <title>Bug 1045891</title>
    <!-- Including SimpleTest.js so we can use waitForExplicitFinish !-->
    <script src="/tests/SimpleTest/SimpleTest.js"></script>
    <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  </head>
  <body>
    <p id="display"></p>
    <div id="content" style="visibility: hidden">
    </div>

    <script class="testbody" type="text/javascript">
      /*
       * Description of the test:
       *   We load a page with a given CSP and verify that child frames and workers are correctly
       *   evaluated through the "child-src" directive.
       */

      SimpleTest.waitForExplicitFinish();

      var WORKER_REDIRECT_TEST_FILE = "file_child-src_worker-redirect.html";
      var SHARED_WORKER_REDIRECT_TEST_FILE = "file_child-src_shared_worker-redirect.html";

      var tests = {
        'same-src-worker_redir-same': {
          id: "same-src-worker_redir-same",
          file: WORKER_REDIRECT_TEST_FILE,
          result : "allowed",
          redir: "same",
          policy : "default-src 'none'; script-src 'self' 'unsafe-inline'; child-src http://mochi.test:8888"
        },
        'same-src-worker_redir-other': {
          id: "same-src-worker_redir-other",
          file: WORKER_REDIRECT_TEST_FILE,
          result : "blocked",
          redir: "other",
          policy : "default-src 'none'; script-src 'self' 'unsafe-inline'; child-src http://mochi.test:8888"
        },
        'star-src-worker_redir-same': {
          id: "star-src-worker_redir-same",
          file: WORKER_REDIRECT_TEST_FILE,
          redir: "same",
          result : "allowed",
          policy : "default-src 'none'; script-src 'self' 'unsafe-inline'; child-src *"
        },
        'other-src-worker_redir-same': {
          id: "other-src-worker_redir-same",
          file: WORKER_REDIRECT_TEST_FILE,
          redir: "same",
          result : "blocked",
          policy : "default-src 'none'; script-src 'self' 'unsafe-inline'; child-src https://www.example.org"
        },
        /* shared workers */
        'same-src-shared_worker_redir-same': {
          id: "same-src-shared_worker_redir-same",
          file: SHARED_WORKER_REDIRECT_TEST_FILE,
          result : "allowed",
          redir: "same",
          policy : "default-src 'none'; script-src 'self' 'unsafe-inline'; child-src http://mochi.test:8888"
        },
        'same-src-shared_worker_redir-other': {
          id: "same-src-shared_worker_redir-other",
          file: SHARED_WORKER_REDIRECT_TEST_FILE,
          result : "blocked",
          redir: "other",
          policy : "default-src 'none'; script-src 'self' 'unsafe-inline'; child-src http://mochi.test:8888"
        },
        'star-src-shared_worker_redir-same': {
          id: "star-src-shared_worker_redir-same",
          file: SHARED_WORKER_REDIRECT_TEST_FILE,
          redir: "same",
          result : "allowed",
          policy : "default-src 'none'; script-src 'self' 'unsafe-inline'; child-src *"
        },
        'other-src-shared_worker_redir-same': {
          id: "other-src-shared_worker_redir-same",
          file: SHARED_WORKER_REDIRECT_TEST_FILE,
          redir: "same",
          result : "blocked",
          policy : "default-src 'none'; script-src 'self' 'unsafe-inline'; child-src https://www.example.org"
        },
      };

      finished = {};

      function recvMessage(ev) {
        is(ev.data.message, tests[ev.data.id].result, "CSP child-src worker test " + ev.data.id);
        finished[ev.data.id] = ev.data.message;

        if (Object.keys(finished).length == Object.keys(tests).length) {
          window.removeEventListener('message', recvMessage);
          SimpleTest.finish();
        }
      }

      window.addEventListener('message', recvMessage);

      function loadNextTest() {
        for (item in tests) {
          test = tests[item];
          var src = "file_testserver.sjs";
          // append the file that should be served
          src += "?file=" + escape("tests/dom/security/test/csp/" + test.file);
          // append the CSP that should be used to serve the file
          src += "&csp=" + escape(test.policy);
          // add whether redirect is to same or different
          src += "&redir=" + escape(test.policy);
          // add our identifier
          src += "#" + escape(test.id);

          content = document.getElementById('content');
          testframe = document.createElement("iframe");
          testframe.setAttribute('id', test.id);
          content.appendChild(testframe);
          testframe.src = src;
        }
      }

      // start running the tests
      loadNextTest();
    </script>
  </body>
</html>