From 6bf0a5cb5034a7e684dcc3500e841785237ce2dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 19:32:43 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- .../test/browser/browser_blobURLIsolation.js | 123 +++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 browser/components/originattributes/test/browser/browser_blobURLIsolation.js (limited to 'browser/components/originattributes/test/browser/browser_blobURLIsolation.js') diff --git a/browser/components/originattributes/test/browser/browser_blobURLIsolation.js b/browser/components/originattributes/test/browser/browser_blobURLIsolation.js new file mode 100644 index 0000000000..4777223c28 --- /dev/null +++ b/browser/components/originattributes/test/browser/browser_blobURLIsolation.js @@ -0,0 +1,123 @@ +/** + * Bug 1264573 - A test case for blob url isolation. + */ + +const TEST_PAGE = + "http://mochi.test:8888/browser/browser/components/" + + "originattributes/test/browser/file_firstPartyBasic.html"; +const SCRIPT_WORKER_BLOBIFY = "worker_blobify.js"; + +function page_blobify(browser, input) { + return SpecialPowers.spawn(browser, [input], function (contentInput) { + return { + blobURL: content.URL.createObjectURL(new content.Blob([contentInput])), + }; + }); +} + +function page_deblobify(browser, blobURL) { + return SpecialPowers.spawn( + browser, + [blobURL], + async function (contentBlobURL) { + if ("error" in contentBlobURL) { + return contentBlobURL; + } + contentBlobURL = contentBlobURL.blobURL; + + function blobURLtoBlob(aBlobURL) { + return new content.Promise(function (resolve) { + let xhr = new content.XMLHttpRequest(); + xhr.open("GET", aBlobURL, true); + xhr.onload = function () { + resolve(xhr.response); + }; + xhr.onerror = function () { + resolve("xhr error"); + }; + xhr.responseType = "blob"; + xhr.send(); + }); + } + + function blobToString(blob) { + return new content.Promise(function (resolve) { + let fileReader = new content.FileReader(); + fileReader.onload = function () { + resolve(fileReader.result); + }; + fileReader.readAsText(blob); + }); + } + + let blob = await blobURLtoBlob(contentBlobURL); + if (blob == "xhr error") { + return "xhr error"; + } + + return blobToString(blob); + } + ); +} + +function workerIO(browser, what, message) { + return SpecialPowers.spawn( + browser, + [ + { + scriptFile: SCRIPT_WORKER_BLOBIFY, + message: { message, what }, + }, + ], + function (args) { + if (!content.worker) { + content.worker = new content.Worker(args.scriptFile); + } + let promise = new content.Promise(function (resolve) { + let listenFunction = function (event) { + content.worker.removeEventListener("message", listenFunction); + resolve(event.data); + }; + content.worker.addEventListener("message", listenFunction); + }); + content.worker.postMessage(args.message); + return promise; + } + ); +} + +let worker_blobify = (browser, input) => workerIO(browser, "blobify", input); +let worker_deblobify = (browser, blobURL) => + workerIO(browser, "deblobify", blobURL); + +function doTest(blobify, deblobify) { + let blobURL = null; + return async function (browser) { + if (blobURL === null) { + let input = Math.random().toString(); + blobURL = await blobify(browser, input); + return input; + } + let result = await deblobify(browser, blobURL); + blobURL = null; + return result; + }; +} + +let tests = []; +for (let blobify of [page_blobify, worker_blobify]) { + for (let deblobify of [page_deblobify, worker_deblobify]) { + tests.push(doTest(blobify, deblobify)); + } +} + +async function setup() { + await SpecialPowers.pushPrefEnv({ + set: [ + ["privacy.partition.bloburl_per_agent_cluster", false], + ["dom.security.https_first", false], + ], + }); +} + +IsolationTestTools.runTests(TEST_PAGE, tests, null, setup); -- cgit v1.2.3