summaryrefslogtreecommitdiffstats
path: root/browser/components/originattributes/test/browser/browser_blobURLIsolation.js
blob: 4777223c2827f8b77eb4e2535490c1168e4855df (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
/**
 * 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);