summaryrefslogtreecommitdiffstats
path: root/toolkit/components/remotebrowserutils/tests/browser/browser_documentChannel.js
blob: f32216010be53ece41d6f7590f36c31000c032a6 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* eslint-env webextensions */
"use strict";

const PRINT_POSTDATA = httpURL("print_postdata.sjs");
const FILE_DUMMY = fileURL("dummy_page.html");
const DATA_URL = "data:text/html,Hello%2C World!";
const DATA_STRING = "Hello, World!";

async function performLoad(browser, opts, action) {
  let loadedPromise = BrowserTestUtils.browserLoaded(
    browser,
    false,
    opts.url,
    opts.maybeErrorPage
  );
  await action();
  await loadedPromise;
}

const EXTENSION_DATA = {
  manifest: {
    name: "Simple extension test",
    version: "1.0",
    manifest_version: 2,
    description: "",

    permissions: ["proxy", "webRequest", "webRequestBlocking", "<all_urls>"],
  },

  files: {
    "dummy.html": "<html>webext dummy</html>",
    "redirect.html": "<html>webext redirect</html>",
  },

  extUrl: "",

  async background() {
    browser.test.log("background script running");
    browser.webRequest.onAuthRequired.addListener(
      async details => {
        browser.test.log("webRequest onAuthRequired");

        // A blocking request that returns a promise exercises a codepath that
        // sets the notificationCallbacks on the channel to a JS object that we
        // can't do directly QueryObject on with expected results.
        // This triggered a crash which was fixed in bug 1528188.
        return new Promise((resolve, reject) => {
          setTimeout(resolve, 0);
        });
      },
      { urls: ["*://*/*"] },
      ["blocking"]
    );
    browser.webRequest.onBeforeRequest.addListener(
      async details => {
        browser.test.log("webRequest onBeforeRequest");
        let isRedirect =
          details.originUrl == browser.runtime.getURL("redirect.html") &&
          details.url.endsWith("print_postdata.sjs");
        let url = this.extUrl ? this.extUrl : details.url + "?redirected";
        return isRedirect ? { redirectUrl: url } : {};
      },
      { urls: ["*://*/*"] },
      ["blocking"]
    );
    browser.test.onMessage.addListener(async ({ method, url }) => {
      if (method == "setRedirectUrl") {
        this.extUrl = url;
      }
      browser.test.sendMessage("done");
    });
  },
};

async function withExtensionDummy(callback) {
  let extension = ExtensionTestUtils.loadExtension(EXTENSION_DATA);
  await extension.startup();
  let rv = await callback(`moz-extension://${extension.uuid}/`, extension);
  await extension.unload();
  return rv;
}

async function postFrom(start, target) {
  return BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: start,
    },
    async function (browser) {
      info("Test tab ready: postFrom " + start);

      // Create the form element in our loaded URI.
      await SpecialPowers.spawn(browser, [{ target }], function ({ target }) {
        content.document.body.innerHTML = `
        <form method="post" action="${target}">
          <input type="text" name="initialRemoteType" value="${Services.appinfo.remoteType}">
          <input type="submit" id="submit">
        </form>`;
      });

      // Perform a form POST submit load.
      info("Performing POST submission");
      await performLoad(
        browser,
        {
          url(url) {
            let enable =
              url.startsWith(PRINT_POSTDATA) ||
              url == target ||
              url == DATA_URL;
            if (!enable) {
              info(`url ${url} is invalid to perform load`);
            }
            return enable;
          },
          maybeErrorPage: true,
        },
        async () => {
          await SpecialPowers.spawn(browser, [], () => {
            content.document.querySelector("#submit").click();
          });
        }
      );

      // Check that the POST data was submitted.
      info("Fetching results");
      return SpecialPowers.spawn(browser, [], () => {
        return {
          remoteType: Services.appinfo.remoteType,
          location: "" + content.location.href,
          body: content.document.body.textContent,
        };
      });
    }
  );
}

async function loadAndGetProcessID(browser, target) {
  info(`Performing GET load: ${target}`);
  await performLoad(
    browser,
    {
      maybeErrorPage: true,
    },
    () => {
      BrowserTestUtils.startLoadingURIString(browser, target);
    }
  );

  info(`Navigated to: ${target}`);
  browser = gBrowser.selectedBrowser;
  let processID = await SpecialPowers.spawn(browser, [], () => {
    return Services.appinfo.processID;
  });
  return processID;
}

async function testLoadAndRedirect(
  target,
  expectedProcessSwitch,
  testRedirect
) {
  let start = httpURL(`dummy_page.html`);
  return BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: start,
    },
    async function (_browser) {
      info("Test tab ready: getFrom " + start);

      let browser = gBrowser.selectedBrowser;
      let firstProcessID = await SpecialPowers.spawn(browser, [], () => {
        return Services.appinfo.processID;
      });

      info(`firstProcessID: ${firstProcessID}`);

      let secondProcessID = await loadAndGetProcessID(browser, target);

      info(`secondProcessID: ${secondProcessID}`);
      Assert.equal(firstProcessID != secondProcessID, expectedProcessSwitch);

      if (!testRedirect) {
        return;
      }

      let thirdProcessID = await loadAndGetProcessID(browser, add307(target));

      info(`thirdProcessID: ${thirdProcessID}`);
      Assert.equal(firstProcessID != thirdProcessID, expectedProcessSwitch);
      Assert.ok(secondProcessID == thirdProcessID);
    }
  );
}

add_task(async function test_enabled() {
  // Force only one webIsolated content process to ensure same-origin loads
  // always end in the same process.
  await SpecialPowers.pushPrefEnv({
    set: [["dom.ipc.processCount.webIsolated", 1]],
  });

  // URIs should correctly switch processes & the POST
  // should succeed.
  info("ENABLED -- FILE -- raw URI load");
  let resp = await postFrom(FILE_DUMMY, PRINT_POSTDATA);
  ok(E10SUtils.isWebRemoteType(resp.remoteType), "process switch");
  is(resp.location, PRINT_POSTDATA, "correct location");
  is(resp.body, "initialRemoteType=file", "correct POST body");

  info("ENABLED -- FILE -- 307-redirect URI load");
  let resp307 = await postFrom(FILE_DUMMY, add307(PRINT_POSTDATA));
  ok(E10SUtils.isWebRemoteType(resp307.remoteType), "process switch");
  is(resp307.location, PRINT_POSTDATA, "correct location");
  is(resp307.body, "initialRemoteType=file", "correct POST body");

  // Same with extensions
  await withExtensionDummy(async extOrigin => {
    info("ENABLED -- EXTENSION -- raw URI load");
    let respExt = await postFrom(extOrigin + "dummy.html", PRINT_POSTDATA);
    ok(E10SUtils.isWebRemoteType(respExt.remoteType), "process switch");
    is(respExt.location, PRINT_POSTDATA, "correct location");
    is(respExt.body, "initialRemoteType=extension", "correct POST body");

    info("ENABLED -- EXTENSION -- extension-redirect URI load");
    let respExtRedirect = await postFrom(
      extOrigin + "redirect.html",
      PRINT_POSTDATA
    );
    ok(E10SUtils.isWebRemoteType(respExtRedirect.remoteType), "process switch");
    is(
      respExtRedirect.location,
      PRINT_POSTDATA + "?redirected",
      "correct location"
    );
    is(
      respExtRedirect.body,
      "initialRemoteType=extension?redirected",
      "correct POST body"
    );

    info("ENABLED -- EXTENSION -- 307-redirect URI load");
    let respExt307 = await postFrom(
      extOrigin + "dummy.html",
      add307(PRINT_POSTDATA)
    );
    ok(E10SUtils.isWebRemoteType(respExt307.remoteType), "process switch");
    is(respExt307.location, PRINT_POSTDATA, "correct location");
    is(respExt307.body, "initialRemoteType=extension", "correct POST body");
  });
});

async function sendMessage(ext, method, url) {
  ext.sendMessage({ method, url });
  await ext.awaitMessage("done");
}

// TODO: Currently no test framework for ftp://.
add_task(async function test_protocol() {
  // TODO: Processes should be switched due to navigation of different origins.
  await testLoadAndRedirect("data:,foo", false, true);

  // Redirecting to file:// is not allowed.
  await testLoadAndRedirect(FILE_DUMMY, true, false);

  await withExtensionDummy(async (extOrigin, extension) => {
    await sendMessage(extension, "setRedirectUrl", DATA_URL);

    let respExtRedirect = await postFrom(
      extOrigin + "redirect.html",
      PRINT_POSTDATA
    );

    ok(E10SUtils.isWebRemoteType(respExtRedirect.remoteType), "process switch");
    is(respExtRedirect.location, DATA_URL, "correct location");
    is(respExtRedirect.body, DATA_STRING, "correct POST body");
  });
});