tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_postmessage.html (2656B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <meta charset="utf-8">
      5  <title>Test for Bug 1574017</title>
      6  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      7  <script src="/tests/SimpleTest/ChromeTask.js"></script>
      8  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
      9  <script type="application/javascript">
     10  async function waitForErrorMessage(err) {
     11    return ChromeTask.spawn(err, async err => {
     12      await new Promise(resolve => {
     13        let console = Services.console;
     14        console.reset();
     15        console.registerListener(function listener(aMessage) {
     16          var sourceName = `http://mochi.test:8888/tests/dom/tests/mochitest/bugs/test_postmessage.html`;
     17          if (
     18            aMessage.message.includes(err) &&
     19            aMessage instanceof Ci.nsIScriptError &&
     20            aMessage.sourceName == sourceName
     21          ) {
     22            console.unregisterListener(listener);
     23            resolve();
     24          }
     25        });
     26      });
     27    });
     28  }
     29  add_task(async function testNoCallerURI() {
     30    var Cu = SpecialPowers.Cu;
     31    var princ = SpecialPowers.wrap(window.document).nodePrincipal;
     32    var sandbox = Cu.Sandbox(princ, { sameZoneAs: this });
     33    SpecialPowers.unwrap(sandbox).win = window.frames.diffDomain;
     34    var err = `Failed to execute ‘postMessage’ on ‘DOMWindow’: The target origin provided (‘https://example.com’) does not match the recipient window’s origin (‘https://example.org’).`;
     35    let consolePromise = waitForErrorMessage(err);
     36    Cu.evalInSandbox(
     37      'win.postMessage("We should not be able to post this message", "https://example.com");',
     38      sandbox
     39    );
     40    await consolePromise;
     41    ok(true, "Error message was logged correctly to the console");
     42    Cu.nukeSandbox(sandbox);
     43  });
     44  add_task(async function testWithWindowID() {
     45    var err = `Failed to execute ‘postMessage’ on ‘DOMWindow’: The target origin provided (‘https://example.ru’) does not match the recipient window’s origin (‘https://example.org’).`;
     46    let consolePromise = waitForErrorMessage(err);
     47    window.frames.diffDomain.postMessage(
     48      "The window should not receive this",
     49      "https://example.ru"
     50    );
     51    await consolePromise;
     52    ok(true, "Error message was logged correctly to the console");
     53  });
     54  </script>
     55 </head>
     56 <body>
     57 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1574017">Mozilla Bug 1574017</a>
     58 <p id="display"></p>
     59 <div id="content" style="display: none">
     60 </div>
     61 <pre id="test">
     62 </pre>
     63 <iframe id="diffDomain" name="diffDomain" src="https://example.org/tests/dom/tests/mochitest/bugs/file_empty.html"></iframe>
     64 </body>
     65 </html>