File: iframe-test.js

package info (click to toggle)
thunderbird 1%3A140.4.0esr-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,609,432 kB
  • sloc: cpp: 7,672,442; javascript: 5,901,613; ansic: 3,898,954; python: 1,413,343; xml: 653,997; asm: 462,286; java: 180,927; sh: 113,489; makefile: 20,460; perl: 14,288; objc: 13,059; yacc: 4,583; pascal: 3,352; lex: 1,720; ruby: 1,222; exp: 762; sql: 715; awk: 580; php: 436; lisp: 430; sed: 70; csh: 10
file content (241 lines) | stat: -rw-r--r-- 9,064 bytes parent folder | download | duplicates (14)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// To use the functions below, be sure to include the following files in your
// test:
// - "/common/get-host-info.sub.js" to get the different origin values.
// - "common.js" to have the origins easily available.
// - "/common/dispatcher/dispatcher.js" for cross-origin messaging.
// - "/common/utils.js" for token().

function getBaseExecutorPath(origin) {
  return origin + '/common/dispatcher/executor.html';
}

function getHeadersPipe(headers) {
  const coop_header = headers.coop ?
    `|header(Cross-Origin-Opener-Policy,${encodeURIComponent(headers.coop)})` : '';
  const coep_header = headers.coep ?
    `|header(Cross-Origin-Embedder-Policy,${encodeURIComponent(headers.coep)})` : '';
  return coop_header + coep_header;
}

function getExecutorPath(uuid, origin, headers) {
  return getBaseExecutorPath(origin) +
    `?uuid=${uuid}` +
    `&pipe=${getHeadersPipe(headers)}`;
}

function evaluate(target_token, script) {
  const reply_token = token();
  send(target_token, `send('${reply_token}', ${script});`);
  return receive(reply_token);
}

// Return true if an opened iframe can access |property| on a stored
// window.popup object without throwing an error.
function iframeCanAccessProperty(iframe_token, property) {
  const reply_token = token();
  send(iframe_token,
    `try {
       const unused = window.popup['${property}'];
       send('${reply_token}', 'true')
     } catch (errors) {
       send('${reply_token}', 'false')
     }`);
  return receive(reply_token);
}

// Returns the script necessary to open a popup, given the method in
// `popup_via`. Supported methods are 'window_open' that leverages
// window.open(), 'anchor' that creates an <a> HTML element and clicks on it,
// and 'form' that creates a form and submits it.
function popupOpeningScript(popup_via, popup_url, popup_origin, headers,
    popup_token) {
  if (popup_via === 'window_open')
    return `window.popup = window.open('${popup_url}', '${popup_token}');`;

  if (popup_via === 'anchor') {
    return `
      const anchor = document.createElement('a');
      anchor.href = '${popup_url}';
      anchor.rel = "opener";
      anchor.target = '${popup_token}';
      anchor.innerText = "anchor";
      document.body.appendChild(anchor);
      anchor.click();
      `;
  }

  if (popup_via === "form") {
    return `
      const form = document.createElement("form");
      form.action = '${getBaseExecutorPath(popup_origin.origin)}';
      form.target = '${popup_token}';
      form.method = 'GET';
      const add_param = (name, value) => {
        const input = document.createElement("input");
        input.name = name;
        input.value = value;
        form.appendChild(input);
      };
      add_param("uuid", "${popup_token}");
      add_param("pipe", "${getHeadersPipe(headers)}");
      document.body.appendChild(form);
      form.submit();
      `;
  }

  assert_not_reached('Unrecognized popup opening method.');
}

function promise_test_parallel(promise, description) {
  async_test(test => {
    promise(test)
        .then(() => test.done())
        .catch(test.step_func(error => { throw error; }));
  }, description);
};

// Verifies that a popup with origin `popup_origin` and headers `headers` has
// the expected `opener_state` after being opened from an iframe with origin
// `iframe_origin`.
function iframe_test(description, iframe_origin, popup_origin, headers,
    expected_opener_state) {
  for (const popup_via of ['window_open', 'anchor','form']) {
    promise_test_parallel(async t => {
      const iframe_token = token();
      const popup_token = token();
      const reply_token = token();

      const frame = document.createElement("iframe");
      const iframe_url = getExecutorPath(
        iframe_token,
        iframe_origin.origin,
        {});

      frame.src = iframe_url;
      document.body.append(frame);

      send(iframe_token, `send('${reply_token}', 'Iframe loaded');`);
      assert_equals(await receive(reply_token), 'Iframe loaded');

      const popup_url = getExecutorPath(
        popup_token,
        popup_origin.origin,
        headers);

      // We open popup and then ping it, it will respond after loading.
      send(iframe_token, popupOpeningScript(popup_via, popup_url, popup_origin,
                                            headers, popup_token));
      send(popup_token, `send('${reply_token}', 'Popup loaded');`);
      assert_equals(await receive(reply_token), 'Popup loaded');

      // Make sure the popup and the iframe are removed once the test has run,
      // keeping a clean state.
      add_completion_callback(() => {
        frame.remove();
        send(popup_token, `close()`);
      });

      // Give some time for things to settle across processes etc. before
      // proceeding with verifications.
      await new Promise(resolve => { t.step_timeout(resolve, 500); });

      // Verify that the opener is in the state we expect it to be in.
      switch (expected_opener_state) {
        case 'preserved': {
          assert_equals(
            await evaluate(popup_token, 'opener != null'), "true",
            'Popup has an opener?');
          assert_equals(
            await evaluate(popup_token, `name === '${popup_token}'`), "true",
            'Popup has a name?');

          // When the popup was created using window.open, we've kept a handle
          // and we can do extra verifications.
          if (popup_via === 'window_open') {
            assert_equals(
              await evaluate(iframe_token, 'popup != null'), "true",
              'Popup handle is non-null in iframe?');
            assert_equals(
              await evaluate(iframe_token, 'popup.closed'), "false",
              'Popup appears closed from iframe?');
            assert_equals(
              await iframeCanAccessProperty(iframe_token, "document"),
              popup_origin === iframe_origin ? "true" : "false",
              'Iframe has dom access to the popup?');
            assert_equals(
              await iframeCanAccessProperty(iframe_token, "frames"), "true",
              'Iframe has cross origin access to the popup?');
          }
          break;
        }
        case 'restricted': {
          assert_equals(
            await evaluate(popup_token, 'opener != null'), "true",
            'Popup has an opener?');
          assert_equals(
            await evaluate(popup_token, `name === ''`), "true",
            'Popup name is cleared?');

          // When the popup was created using window.open, we've kept a handle
          // and we can do extra verifications.
          if (popup_via === 'window_open') {
            assert_equals(
              await evaluate(iframe_token, 'popup != null'), "true",
              'Popup handle is non-null in iframe?');
            assert_equals(
              await evaluate(iframe_token, 'popup.closed'), "false",
              'Popup appears closed from iframe?');
            assert_equals(
              await iframeCanAccessProperty(iframe_token, "document"), "false",
              'Iframe has dom access to the popup?');
            assert_equals(
              await iframeCanAccessProperty(iframe_token, "frames"), "false",
              'Iframe has cross origin access to the popup?');
            assert_equals(
              await iframeCanAccessProperty(iframe_token, "closed"), "true",
              'Iframe has limited cross origin access to the popup?');
          }
          break;
        }
        case 'severed': {
          assert_equals(await evaluate(popup_token, 'opener != null'), "false",
                       'Popup has an opener?');
          assert_equals(
            await evaluate(popup_token, `name === ''`), "true",
            'Popup name is cleared?');

          // When the popup was created using window.open, we've kept a handle
          // and we can do extra verifications.
          if (popup_via === 'window_open') {
            assert_equals(
              await evaluate(iframe_token, 'popup != null'), "true",
              'Popup handle is non-null in iframe?');
            assert_equals(
              await evaluate(iframe_token, 'popup.closed'), "true",
              'Popup appears closed from iframe?');
          }
          break;
        }
        case 'noopener': {
          assert_equals(await evaluate(popup_token, 'opener != null'), "false",
                        'Popup has an opener?');
          assert_equals(
            await evaluate(popup_token, `name === ''`), "true",
            'Popup name is cleared?');

          // When the popup was created using window.open, we've kept a handle
          // and we can do extra verifications.
          if (popup_via === 'window_open') {
            assert_equals(
              await evaluate(iframe_token, 'popup == null'), "true",
              'Popup handle is null in iframe?');
          }
          break;
        }
        default:
          assert_not_reached('Unrecognized opener state: ' +
            expected_opener_state);
      }
    }, `${description} with ${popup_via}`);
  }
}