summaryrefslogtreecommitdiffstats
path: root/docshell/test/mochitest/test_content_javascript_loads.html
blob: eabc1d314ee7ac2bc893bac1e7e5fd1b087898fc (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
<!DOCTYPE html>
<html>
<head>
  <title>Test for Bug 1647519</title>
  <meta charset="utf-8">
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1647519">Mozilla Bug 1647519</a>

<script type="application/javascript">
"use strict";

function promiseMessage(source, filter = event => true) {
  return new Promise(resolve => {
    function listener(event) {
      if (event.source == source && filter(event)) {
        window.removeEventListener("message", listener);
        resolve(event);
      }
    }
    window.addEventListener("message", listener);
  });
}

async function runTests(resourcePath) {
  /* globals Assert, content */
  let doc = content.document;

  // Sends a message to the given target window and waits for a response a few
  // times to (more or less) ensure that a `javascript:` load request has had
  // time to succeed, if it were going to.
  async function doSomeRoundTrips(target) {
    for (let i = 0; i < 3; i++) {
      // Note: The ping message needs to be sent from a script running in the
      // content scope or there will be no source window for the reply to be
      // sent to.
      await content.wrappedJSObject.ping(target);
    }
  }

  function promiseEvent(target, name) {
    return new Promise(resolve => {
      target.addEventListener(name, resolve, { once: true });
    });
  }

  function createIframe(host, id) {
    let iframe = doc.createElement("iframe");
    iframe.id = id;
    iframe.name = id;
    iframe.src = `https://${host}${resourcePath}file_content_javascript_loads_frame.html`;
    doc.body.appendChild(iframe);
    return promiseEvent(iframe, "load");
  }

  const ID_SAME_ORIGIN = "frame-same-origin";
  const ID_SAME_BASE_DOMAIN = "frame-same-base-domain";
  const ID_CROSS_BASE_DOMAIN = "frame-cross-base-domain";

  await Promise.all([
    createIframe("example.com", ID_SAME_ORIGIN),
    createIframe("test1.example.com", ID_SAME_BASE_DOMAIN),
    createIframe("example.org", ID_CROSS_BASE_DOMAIN),
  ]);

  let gotJSLoadFrom = null;
  let pendingJSLoadID = null;
  content.addEventListener("message", event => {
    if ("javascriptLoadID" in event.data) {
      Assert.equal(
        event.data.javascriptLoadID,
        pendingJSLoadID,
        "Message from javascript: load should have the expected ID"
      );
      Assert.equal(
        gotJSLoadFrom,
        null,
        "Should not have seen a previous load message this cycle"
      );
      gotJSLoadFrom = event.source.name;
    }
  });

  async function watchForJSLoads(frameName, expected, task) {
    let loadId = Math.random();

    let jsURI =
      "javascript:" +
      encodeURI(`parent.postMessage({ javascriptLoadID: ${loadId} }, "*")`);

    pendingJSLoadID = loadId;
    gotJSLoadFrom = null;

    await task(jsURI);

    await doSomeRoundTrips(content.wrappedJSObject[frameName]);

    if (expected) {
      Assert.equal(
        gotJSLoadFrom,
        frameName,
        `Should have seen javascript: URI loaded into ${frameName}`
      );
    } else {
      Assert.equal(
        gotJSLoadFrom,
        null,
        "Should not have seen javascript: URI loaded"
      );
    }
  }

  let frames = [
    { name: ID_SAME_ORIGIN, expectLoad: true },
    { name: ID_SAME_BASE_DOMAIN, expectLoad: false },
    { name: ID_CROSS_BASE_DOMAIN, expectLoad: false },
  ];
  for (let { name, expectLoad } of frames) {
    info(`Checking loads for frame "${name}". Expecting loads: ${expectLoad}`);

    info("Checking location setter");
    await watchForJSLoads(name, expectLoad, jsURI => {
      // Note: We need to do this from the content scope since security checks
      // depend on the JS caller scope.
      content.wrappedJSObject.setFrameLocation(name, jsURI);
    });

    info("Checking targeted <a> load");
    await watchForJSLoads(name, expectLoad, jsURI => {
      let a = doc.createElement("a");
      a.target = name;
      a.href = jsURI;
      doc.body.appendChild(a);
      a.click();
      a.remove();
    });

    info("Checking targeted window.open load");
    await watchForJSLoads(name, expectLoad, jsURI => {
      content.wrappedJSObject.open(jsURI, name);
    });
  }
}

add_task(async function() {
  const resourcePath = location.pathname.replace(/[^\/]+$/, "");

  let win = window.open(
    `https://example.com${resourcePath}file_content_javascript_loads_root.html`
  );
  await promiseMessage(win, event => event.data == "ready");

  await SpecialPowers.spawn(win, [resourcePath], runTests);

  win.close();
});
</script>

</body>
</html>