diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /dom/tests/browser/browser_xhr_sandbox.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/tests/browser/browser_xhr_sandbox.js')
-rw-r--r-- | dom/tests/browser/browser_xhr_sandbox.js | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/dom/tests/browser/browser_xhr_sandbox.js b/dom/tests/browser/browser_xhr_sandbox.js new file mode 100644 index 0000000000..cca69b916b --- /dev/null +++ b/dom/tests/browser/browser_xhr_sandbox.js @@ -0,0 +1,62 @@ +// This code is evaluated in a sandbox courtesy of toSource(); +var sandboxCode = + function () { + let req = new XMLHttpRequest(); + req.open("GET", "http://mochi.test:8888/browser/dom/tests/browser/", true); + req.onreadystatechange = function () { + if (req.readyState === 4) { + // If we get past the problem above, we end up with a req.status of zero + // (ie, blocked due to CORS) even though we are fetching from the same + // origin as the window itself. + let result; + if (req.status != 200) { + result = "ERROR: got request status of " + req.status; + } else if (!req.responseText.length) { + result = "ERROR: got zero byte response text"; + } else { + result = "ok"; + } + postMessage({ result }, "*"); + } + }; + req.send(null); + }.toSource() + "();"; + +add_task(async function test() { + await SpecialPowers.pushPrefEnv({ + set: [["security.allow_unsafe_parent_loads", true]], + }); + + let newWin = await BrowserTestUtils.openNewBrowserWindow(); + + let frame = newWin.document.createXULElement("iframe"); + frame.setAttribute("type", "content"); + frame.setAttribute( + "src", + "http://mochi.test:8888/browser/dom/tests/browser/browser_xhr_sandbox.js" + ); + + newWin.document.documentElement.appendChild(frame); + await BrowserTestUtils.waitForEvent(frame, "load", true); + + let contentWindow = frame.contentWindow; + let sandbox = new Cu.Sandbox(contentWindow); + + // inject some functions from the window into the sandbox. + // postMessage so the async code in the sandbox can report a result. + sandbox.importFunction( + contentWindow.postMessage.bind(contentWindow), + "postMessage" + ); + sandbox.importFunction(contentWindow.XMLHttpRequest, "XMLHttpRequest"); + Cu.evalInSandbox(sandboxCode, sandbox, "1.8"); + + let sandboxReply = await BrowserTestUtils.waitForEvent( + contentWindow, + "message", + true + ); + is(sandboxReply.data.result, "ok", "check the sandbox code was felipe"); + + await BrowserTestUtils.closeWindow(newWin); +}); |