summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/test/browser/browser_partitionkey_bloburl.js
blob: 3dc3f36742b141a6b9c0fed27b7178edc4a1436e (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const BASE_URI =
  "https://example.net/browser/toolkit/components/antitracking/test/browser/blobPartitionPage.html";
const EMPTY_URI =
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  "https://example.com/browser/toolkit/components/antitracking/test/browser/empty.html";

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [["privacy.partition.bloburl_per_partition_key", true]],
  });
});

// Ensuring Blob URL cannot be resolved under a different
// top-level domain other than its original creation top-level domain
add_task(async function test_different_tld_with_iframe() {
  let tab1 = await BrowserTestUtils.openNewForegroundTab(gBrowser, BASE_URI);
  let browser1 = gBrowser.getBrowserForTab(tab1);
  let blobURL = await SpecialPowers.spawn(browser1, [], function () {
    return content.URL.createObjectURL(new content.Blob(["hello world!"]));
  });

  let tab2 = await BrowserTestUtils.openNewForegroundTab(gBrowser, EMPTY_URI);
  let browser2 = gBrowser.getBrowserForTab(tab2);

  await SpecialPowers.spawn(
    browser2,
    [
      {
        page: BASE_URI,
        blob: blobURL,
      },
    ],
    async obj => {
      let ifr = content.document.createElement("iframe");
      ifr.setAttribute("id", "ifr");
      ifr.setAttribute("src", obj.page);

      info("Iframe loading...");
      await new content.Promise(resolve => {
        ifr.onload = resolve;
        content.document.body.appendChild(ifr);
      });

      let value = await new content.Promise(resolve => {
        content.addEventListener(
          "message",
          e => {
            resolve(e.data == "error");
          },
          { once: true }
        );
        ifr.contentWindow.postMessage(obj.blob, "*");
      });

      ok(value, "Blob URL was unable to be resolved");
    }
  );

  BrowserTestUtils.removeTab(tab1);
  BrowserTestUtils.removeTab(tab2);
});

// Ensuring if Blob URL can be resolved if a domain1 creates a blob URL
// and domain1 trys to resolve blob URL within an iframe of itself
add_task(async function test_same_tld_with_iframe() {
  let tab1 = await BrowserTestUtils.openNewForegroundTab(gBrowser, BASE_URI);
  let browser1 = gBrowser.getBrowserForTab(tab1);
  let blobURL = await SpecialPowers.spawn(browser1, [], function () {
    return content.URL.createObjectURL(new content.Blob(["hello world!"]));
  });

  let tab2 = await BrowserTestUtils.openNewForegroundTab(gBrowser, BASE_URI);
  let browser2 = gBrowser.getBrowserForTab(tab2);

  await SpecialPowers.spawn(
    browser2,
    [
      {
        page: BASE_URI,
        blob: blobURL,
      },
    ],
    async obj => {
      let ifr = content.document.createElement("iframe");
      ifr.setAttribute("id", "ifr");
      ifr.setAttribute("src", obj.page);

      info("Iframe loading...");
      await new content.Promise(resolve => {
        ifr.onload = resolve;
        content.document.body.appendChild(ifr);
      });

      let value = await new content.Promise(resolve => {
        content.addEventListener(
          "message",
          e => {
            resolve(e.data == "hello world!");
          },
          { once: true }
        );
        ifr.contentWindow.postMessage(obj.blob, "*");
      });

      ok(value, "Blob URL was able to be resolved");
    }
  );

  BrowserTestUtils.removeTab(tab1);
  BrowserTestUtils.removeTab(tab2);
});

// Ensuring Blob URL can be resolved in an iframe
// under the same top-level domain where it creates.
add_task(async function test_no_iframes_same_tld() {
  let tab1 = await BrowserTestUtils.openNewForegroundTab(gBrowser, BASE_URI);
  let browser1 = gBrowser.getBrowserForTab(tab1);

  let blobURL = await SpecialPowers.spawn(browser1, [], function () {
    return content.URL.createObjectURL(new content.Blob(["hello world!"]));
  });

  let tab2 = await BrowserTestUtils.openNewForegroundTab(gBrowser, BASE_URI);
  let browser2 = gBrowser.getBrowserForTab(tab2);

  let status = await SpecialPowers.spawn(
    browser2,
    [blobURL],
    function (blobURL) {
      return new content.Promise(resolve => {
        var xhr = new content.XMLHttpRequest();
        xhr.open("GET", blobURL);
        xhr.onloadend = function () {
          resolve(xhr.response == "hello world!");
        };

        xhr.send();
      });
    }
  );

  ok(status, "Blob URL was able to be resolved");

  BrowserTestUtils.removeTab(tab1);
  BrowserTestUtils.removeTab(tab2);
});

// Ensuring Blob URL can be resolved in a sandboxed
// iframe under the top-level domain where it creates.
add_task(async function test_same_tld_with_iframe() {
  let tab1 = await BrowserTestUtils.openNewForegroundTab(gBrowser, BASE_URI);
  let browser1 = gBrowser.getBrowserForTab(tab1);
  let blobURL = await SpecialPowers.spawn(browser1, [], function () {
    return content.URL.createObjectURL(new content.Blob(["hello world!"]));
  });

  await SpecialPowers.spawn(
    browser1,
    [
      {
        page: BASE_URI,
        blob: blobURL,
      },
    ],
    async obj => {
      let ifr = content.document.createElement("iframe");
      ifr.setAttribute("id", "ifr");
      ifr.setAttribute("sandbox", "allow-scripts allow-same-origin");
      ifr.setAttribute("src", obj.page);

      info("Iframe loading...");
      await new content.Promise(resolve => {
        ifr.onload = resolve;
        content.document.body.appendChild(ifr);
      });

      let value = await new content.Promise(resolve => {
        content.addEventListener(
          "message",
          e => {
            resolve(e.data == "hello world!");
          },
          { once: true }
        );
        ifr.contentWindow.postMessage(obj.blob, "*");
      });

      ok(value, "Blob URL was able to be resolved");
    }
  );

  BrowserTestUtils.removeTab(tab1);
});