summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/content-security-policy/inheritance/iframe-all-local-schemes.sub.html
blob: 4b787e0c1888eae0e7c7e8dd75ad154f585af274 (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
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<meta http-equiv="Content-Security-Policy" content="img-src 'none'">

<body>

<script>
  function wait_for_error_from_frame(frame, test) {
    window.addEventListener('message', test.step_func(e => {
      if (e.source != frame.contentWindow)
        return;
      assert_equals(e.data, "error");
      frame.remove();
      test.done();
    }));
  }

  function wait_for_error_from_window(opened_window, test) {
    window.addEventListener('message', test.step_func(e => {
      if (e.source != opened_window)
        return;
      assert_equals(e.data, "error");
      opened_window.close();
      test.done();
    }));
  }

  async_test(t => {
    var i = document.createElement('iframe');
    document.body.appendChild(i);

    var img = document.createElement('img');
    img.onerror = t.step_func_done(_ => i.remove());
    img.onload = t.unreached_func();
    i.contentDocument.body.appendChild(img);
    img.src = "{{location[server]}}/images/red-16x16.png";
  }, "<iframe>'s about:blank inherits policy.");

  async_test(t => {
    var w = window.open("about:blank");

    let then = t.step_func(() => {
      then = () => {};
      var img = w.document.createElement('img');
      img.onerror = t.step_func_done(_ => w.close());
      img.onload = t.unreached_func();
      w.document.body.appendChild(img);
      img.src = "{{location[server]}}/images/red-16x16.png";
    });

    // There are now interoperable way to wait for the initial about:blank
    // document to load. Chrome loads it synchronously, hence we can't wait for
    // w.onload. On the other side Firefox loads the initial empty document
    // later and we can wait for the onload event.
    w.onload = then;
    setTimeout(then, 200);

    // Navigations to about:blank happens synchronously. There is no need to
    // wait for the document to load.
  }, "window about:blank inherits policy.");

  async_test(t => {
    var i = document.createElement('iframe');
    i.srcdoc = `
      <img src='{{location[server]}}/images/red-16x16.png'
        onload='window.top.postMessage("load", "*");'
        onerror='window.top.postMessage("error", "*");'
      >
    `;

    wait_for_error_from_frame(i, t);

    document.body.appendChild(i);
  }, "<iframe srcdoc>'s inherits policy.");

  async_test(t => {
    var i = document.createElement('iframe');
    var b = new Blob(
      [`
        <img src='{{location[server]}}/images/red-16x16.png'
          onload='window.top.postMessage("load", "*");'
          onerror='window.top.postMessage("error", "*");'
        >
      `], {type:"text/html"});
    i.src = URL.createObjectURL(b);

    wait_for_error_from_frame(i, t);

    document.body.appendChild(i);
  }, "<iframe src='blob:...'>'s inherits policy.");

  async_test(t => {
    var b = new Blob(
      [`
        <img src='{{location[server]}}/images/red-16x16.png'
          onload='window.opener.postMessage("load", "*");'
          onerror='window.opener.postMessage("error", "*");'
        >
      `], {type:"text/html"});
    let url = URL.createObjectURL(b);
    var w = window.open(url);
    wait_for_error_from_window(w, t);
  }, "window url='blob:...' inherits policy.");

  async_test(t => {
    var i = document.createElement('iframe');
    i.src = `data:text/html,<img src='{{location[server]}}/images/red-16x16.png'
      onload='window.top.postMessage("load", "*");'
      onerror='window.top.postMessage("error", "*");'
    >`;

    wait_for_error_from_frame(i, t);

    document.body.appendChild(i);
  }, "<iframe src='data:...'>'s inherits policy.");

  // Opening a window toward a data-url isn't allowed anymore. Hence, it can't
  // be tested.

  async_test(t => {
    var i = document.createElement('iframe');
    i.src = `javascript:"<img src='{{location[server]}}/images/red-16x16.png'
      onload='window.top.postMessage(\\"load\\", \\"*\\");'
      onerror='window.top.postMessage(\\"error\\", \\"*\\");'
    >"`;

    wait_for_error_from_frame(i, t);

    document.body.appendChild(i);
  }, "<iframe src='javascript:...'>'s inherits policy (static <img> is blocked)");

  async_test(t => {
    let url = `javascript:"<img src='{{location[server]}}/images/red-16x16.png'
      onload='window.opener.postMessage(\\"load\\", \\"*\\");'
      onerror='window.opener.postMessage(\\"error\\", \\"*\\");'
    >"`;

    let w = window.open(url);
    wait_for_error_from_window(w, t);
  }, "window url='javascript:...'>'s inherits policy (static <img> is blocked)");

  // Same as the previous javascript-URL test, but instead of loading the <img>
  // from the new document, this one is created from the initial empty document,
  // while evaluating the javascript-url.
  // See https://crbug.com/1064676
  async_test(t => {
    let url = `javascript:
      let img = document.createElement('img');
      img.onload = () => window.top.postMessage('load', '*');
      img.onerror = () => window.top.postMessage('error', '*');
      img.src = '{{location[server]}}/images/red-16x16.png';
      document.body.appendChild(img);
    `;
    var i = document.createElement('iframe');
    i.src = encodeURI(url.replace(/\n/g, ""));
    wait_for_error_from_frame(i, t);

    document.body.appendChild(i);
  }, "<iframe src='javascript:...'>'s inherits policy (dynamically inserted <img> is blocked)");

  async_test(t => {
    var i = document.createElement('iframe');
    var b = new Blob(
      [`
        <img src='{{location[server]}}/images/red-16x16.png'
          onload='window.top.postMessage("load", "*");'
          onerror='window.top.postMessage("error", "*");'
        >
      `], {type:"text/html"});
    i.src = URL.createObjectURL(b);
    i.sandbox = 'allow-scripts';

    wait_for_error_from_frame(i, t);

    document.body.appendChild(i);
  }, "<iframe sandbox src='blob:...'>'s inherits policy. (opaque origin sandbox)");

</script>