summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/tests/browser/browser_web_channel.js
blob: 9dfa59485b08196997695f5d5d823de370a846dc (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

ChromeUtils.defineESModuleGetters(this, {
  WebChannel: "resource://gre/modules/WebChannel.sys.mjs",
});

const HTTP_PATH = "http://example.com";
const HTTP_ENDPOINT =
  getRootDirectory(gTestPath).replace("chrome://mochitests/content", "") +
  "file_web_channel.html";
const HTTP_MISMATCH_PATH = "http://example.org";
const HTTP_IFRAME_PATH = "http://mochi.test:8888";
const HTTP_REDIRECTED_IFRAME_PATH = "http://example.org";

requestLongerTimeout(2); // timeouts in debug builds.

// Keep this synced with /mobile/android/tests/browser/robocop/testWebChannel.js
// as much as possible.  (We only have that since we can't run browser chrome
// tests on Android.  Yet?)
var gTests = [
  {
    desc: "WebChannel generic message",
    run() {
      return new Promise(function (resolve, reject) {
        let tab;
        let channel = new WebChannel("generic", Services.io.newURI(HTTP_PATH));
        channel.listen(function (id, message, target) {
          is(id, "generic");
          is(message.something.nested, "hello");
          channel.stopListening();
          gBrowser.removeTab(tab);
          resolve();
        });

        tab = BrowserTestUtils.addTab(
          gBrowser,
          HTTP_PATH + HTTP_ENDPOINT + "?generic"
        );
      });
    },
  },
  {
    desc: "WebChannel generic message in a private window.",
    async run() {
      let promiseTestDone = new Promise(function (resolve, reject) {
        let channel = new WebChannel("generic", Services.io.newURI(HTTP_PATH));
        channel.listen(function (id, message, target) {
          is(id, "generic");
          is(message.something.nested, "hello");
          channel.stopListening();
          resolve();
        });
      });

      const url = HTTP_PATH + HTTP_ENDPOINT + "?generic";
      let privateWindow = await BrowserTestUtils.openNewBrowserWindow({
        private: true,
      });
      await BrowserTestUtils.openNewForegroundTab(privateWindow.gBrowser, url);
      await promiseTestDone;
      await BrowserTestUtils.closeWindow(privateWindow);
    },
  },
  {
    desc: "WebChannel two way communication",
    run() {
      return new Promise(function (resolve, reject) {
        let tab;
        let channel = new WebChannel("twoway", Services.io.newURI(HTTP_PATH));

        channel.listen(function (id, message, sender) {
          is(id, "twoway", "bad id");
          ok(message.command, "command not ok");

          if (message.command === "one") {
            channel.send({ data: { nested: true } }, sender);
          }

          if (message.command === "two") {
            is(message.detail.data.nested, true);
            channel.stopListening();
            gBrowser.removeTab(tab);
            resolve();
          }
        });

        tab = BrowserTestUtils.addTab(
          gBrowser,
          HTTP_PATH + HTTP_ENDPOINT + "?twoway"
        );
      });
    },
  },
  {
    desc: "WebChannel two way communication in an iframe",
    async run() {
      let parentChannel = new WebChannel("echo", Services.io.newURI(HTTP_PATH));
      let iframeChannel = new WebChannel(
        "twoway",
        Services.io.newURI(HTTP_IFRAME_PATH)
      );
      let promiseTestDone = new Promise(function (resolve, reject) {
        parentChannel.listen(function (id, message, sender) {
          reject(new Error("WebChannel message incorrectly sent to parent"));
        });

        iframeChannel.listen(function (id, message, sender) {
          is(id, "twoway", "bad id (2)");
          ok(message.command, "command not ok (2)");

          if (message.command === "one") {
            iframeChannel.send({ data: { nested: true } }, sender);
          }

          if (message.command === "two") {
            is(message.detail.data.nested, true);
            resolve();
          }
        });
      });
      await BrowserTestUtils.withNewTab(
        {
          gBrowser,
          url: HTTP_PATH + HTTP_ENDPOINT + "?iframe",
        },
        async function () {
          await promiseTestDone;
          parentChannel.stopListening();
          iframeChannel.stopListening();
        }
      );
    },
  },
  {
    desc: "WebChannel response to a redirected iframe",
    async run() {
      /**
       * This test checks that WebChannel responses are only sent
       * to an iframe if the iframe has not redirected to another origin.
       * Test flow:
       * 1. create a page, embed an iframe on origin A.
       * 2. the iframe sends a message `redirecting`, then redirects to
       *    origin B.
       * 3. the iframe at origin B is set up to echo any messages back to the
       *    test parent.
       * 4. the test parent receives the `redirecting` message from origin A.
       *    the test parent creates a new channel with origin B.
       * 5. when origin B is ready, it sends a `loaded` message to the test
       *    parent, letting the test parent know origin B is ready to echo
       *    messages.
       * 5. the test parent tries to send a response to origin A. If the
       *    WebChannel does not perform a valid origin check, the response
       *    will be received by origin B. If the WebChannel does perform
       *    a valid origin check, the response will not be sent.
       * 6. the test parent sends a `done` message to origin B, which origin
       *    B echoes back. If the response to origin A is not echoed but
       *    the message to origin B is, then hooray, the test passes.
       */

      let preRedirectChannel = new WebChannel(
        "pre_redirect",
        Services.io.newURI(HTTP_IFRAME_PATH)
      );
      let postRedirectChannel = new WebChannel(
        "post_redirect",
        Services.io.newURI(HTTP_REDIRECTED_IFRAME_PATH)
      );

      let promiseTestDone = new Promise(function (resolve, reject) {
        preRedirectChannel.listen(function (id, message, preRedirectSender) {
          if (message.command === "redirecting") {
            postRedirectChannel.listen(function (
              aId,
              aMessage,
              aPostRedirectSender
            ) {
              is(aId, "post_redirect");
              isnot(aMessage.command, "no_response_expected");

              if (aMessage.command === "loaded") {
                // The message should not be received on the preRedirectChannel
                // because the target window has redirected.
                preRedirectChannel.send(
                  { command: "no_response_expected" },
                  preRedirectSender
                );
                postRedirectChannel.send(
                  { command: "done" },
                  aPostRedirectSender
                );
              } else if (aMessage.command === "done") {
                resolve();
              } else {
                reject(new Error(`Unexpected command ${aMessage.command}`));
              }
            });
          } else {
            reject(new Error(`Unexpected command ${message.command}`));
          }
        });
      });

      await BrowserTestUtils.withNewTab(
        {
          gBrowser,
          url: HTTP_PATH + HTTP_ENDPOINT + "?iframe_pre_redirect",
        },
        async function () {
          await promiseTestDone;
          preRedirectChannel.stopListening();
          postRedirectChannel.stopListening();
        }
      );
    },
  },
  {
    desc: "WebChannel multichannel",
    run() {
      return new Promise(function (resolve, reject) {
        let tab;
        let channel = new WebChannel(
          "multichannel",
          Services.io.newURI(HTTP_PATH)
        );

        channel.listen(function (id, message, sender) {
          is(id, "multichannel");
          gBrowser.removeTab(tab);
          resolve();
        });

        tab = BrowserTestUtils.addTab(
          gBrowser,
          HTTP_PATH + HTTP_ENDPOINT + "?multichannel"
        );
      });
    },
  },
  {
    desc: "WebChannel unsolicited send, using system principal",
    async run() {
      let channel = new WebChannel("echo", Services.io.newURI(HTTP_PATH));

      // an unsolicted message is sent from Chrome->Content which is then
      // echoed back. If the echo is received here, then the content
      // received the message.
      let messagePromise = new Promise(function (resolve, reject) {
        channel.listen(function (id, message, sender) {
          is(id, "echo");
          is(message.command, "unsolicited");

          resolve();
        });
      });

      await BrowserTestUtils.withNewTab(
        {
          gBrowser,
          url: HTTP_PATH + HTTP_ENDPOINT + "?unsolicited",
        },
        async function (targetBrowser) {
          channel.send(
            { command: "unsolicited" },
            {
              browsingContext: targetBrowser.browsingContext,
              principal: Services.scriptSecurityManager.getSystemPrincipal(),
            }
          );
          await messagePromise;
          channel.stopListening();
        }
      );
    },
  },
  {
    desc: "WebChannel unsolicited send, using target origin's principal",
    async run() {
      let targetURI = Services.io.newURI(HTTP_PATH);
      let channel = new WebChannel("echo", targetURI);

      // an unsolicted message is sent from Chrome->Content which is then
      // echoed back. If the echo is received here, then the content
      // received the message.
      let messagePromise = new Promise(function (resolve, reject) {
        channel.listen(function (id, message, sender) {
          is(id, "echo");
          is(message.command, "unsolicited");

          resolve();
        });
      });

      await BrowserTestUtils.withNewTab(
        {
          gBrowser,
          url: HTTP_PATH + HTTP_ENDPOINT + "?unsolicited",
        },
        async function (targetBrowser) {
          channel.send(
            { command: "unsolicited" },
            {
              browsingContext: targetBrowser.browsingContext,
              principal: Services.scriptSecurityManager.createContentPrincipal(
                targetURI,
                {}
              ),
            }
          );

          await messagePromise;
          channel.stopListening();
        }
      );
    },
  },
  {
    desc: "WebChannel unsolicited send with principal mismatch",
    async run() {
      let targetURI = Services.io.newURI(HTTP_PATH);
      let channel = new WebChannel("echo", targetURI);

      // two unsolicited messages are sent from Chrome->Content. The first,
      // `unsolicited_no_response_expected` is sent to the wrong principal
      // and should not be echoed back. The second, `done`, is sent to the
      // correct principal and should be echoed back.
      let messagePromise = new Promise(function (resolve, reject) {
        channel.listen(function (id, message, sender) {
          is(id, "echo");

          if (message.command === "done") {
            resolve();
          } else {
            reject(new Error(`Unexpected command ${message.command}`));
          }
        });
      });

      await BrowserTestUtils.withNewTab(
        {
          gBrowser,
          url: HTTP_PATH + HTTP_ENDPOINT + "?unsolicited",
        },
        async function (targetBrowser) {
          let mismatchURI = Services.io.newURI(HTTP_MISMATCH_PATH);
          let mismatchPrincipal =
            Services.scriptSecurityManager.createContentPrincipal(
              mismatchURI,
              {}
            );

          // send a message to the wrong principal. It should not be delivered
          // to content, and should not be echoed back.
          channel.send(
            { command: "unsolicited_no_response_expected" },
            {
              browsingContext: targetBrowser.browsingContext,
              principal: mismatchPrincipal,
            }
          );

          let targetPrincipal =
            Services.scriptSecurityManager.createContentPrincipal(
              targetURI,
              {}
            );

          // send the `done` message to the correct principal. It
          // should be echoed back.
          channel.send(
            { command: "done" },
            {
              browsingContext: targetBrowser.browsingContext,
              principal: targetPrincipal,
            }
          );

          await messagePromise;
          channel.stopListening();
        }
      );
    },
  },
  {
    desc: "WebChannel non-window target",
    async run() {
      /**
       * This test ensures messages can be received from and responses
       * sent to non-window elements.
       *
       * First wait for the non-window element to send a "start" message.
       * Then send the non-window element a "done" message.
       * The non-window element will echo the "done" message back, if it
       * receives the message.
       * Listen for the response. If received, good to go!
       */
      let channel = new WebChannel(
        "not_a_window",
        Services.io.newURI(HTTP_PATH)
      );

      let testDonePromise = new Promise(function (resolve, reject) {
        channel.listen(function (id, message, sender) {
          if (message.command === "start") {
            channel.send({ command: "done" }, sender);
          } else if (message.command === "done") {
            resolve();
          } else {
            reject(new Error(`Unexpected command ${message.command}`));
          }
        });
      });

      await BrowserTestUtils.withNewTab(
        {
          gBrowser,
          url: HTTP_PATH + HTTP_ENDPOINT + "?bubbles",
        },
        async function () {
          await testDonePromise;
          channel.stopListening();
        }
      );
    },
  },
  {
    desc: "WebChannel disallows non-string message from non-whitelisted origin",
    async run() {
      /**
       * This test ensures that non-string messages can't be sent via WebChannels.
       * We create a page (on a non-whitelisted origin) which should send us two
       * messages immediately. The first message has an object for it's detail,
       * and the second has a string. We check that we only get the second
       * message.
       */
      let channel = new WebChannel("objects", Services.io.newURI(HTTP_PATH));
      let testDonePromise = new Promise((resolve, reject) => {
        channel.listen((id, message, sender) => {
          is(id, "objects");
          is(message.type, "string");
          resolve();
        });
      });
      await BrowserTestUtils.withNewTab(
        {
          gBrowser,
          url: HTTP_PATH + HTTP_ENDPOINT + "?object",
        },
        async function () {
          await testDonePromise;
          channel.stopListening();
        }
      );
    },
  },
  {
    desc: "WebChannel allows both string and non-string message from whitelisted origin",
    async run() {
      /**
       * Same process as above, but we whitelist the origin before loading the page,
       * and expect to get *both* messages back (each exactly once).
       */
      let channel = new WebChannel("objects", Services.io.newURI(HTTP_PATH));

      let testDonePromise = new Promise((resolve, reject) => {
        let sawObject = false;
        let sawString = false;
        channel.listen((id, message, sender) => {
          is(id, "objects");
          if (message.type === "object") {
            ok(!sawObject);
            sawObject = true;
          } else if (message.type === "string") {
            ok(!sawString);
            sawString = true;
          } else {
            reject(new Error(`Unknown message type: ${message.type}`));
          }
          if (sawObject && sawString) {
            resolve();
          }
        });
      });
      const webchannelWhitelistPref = "webchannel.allowObject.urlWhitelist";
      let origWhitelist = Services.prefs.getCharPref(webchannelWhitelistPref);
      let newWhitelist = origWhitelist + " " + HTTP_PATH;
      Services.prefs.setCharPref(webchannelWhitelistPref, newWhitelist);
      await BrowserTestUtils.withNewTab(
        {
          gBrowser,
          url: HTTP_PATH + HTTP_ENDPOINT + "?object",
        },
        async function () {
          await testDonePromise;
          Services.prefs.setCharPref(webchannelWhitelistPref, origWhitelist);
          channel.stopListening();
        }
      );
    },
  },
  {
    desc: "WebChannel errors handling the message are delivered back to content",
    async run() {
      const ERRNO_UNKNOWN_ERROR = 999; // WebChannel.sys.mjs doesn't export this.

      // The channel where we purposely fail responding to a command.
      let channel = new WebChannel("error", Services.io.newURI(HTTP_PATH));
      // The channel where we see the response when the content sees the error
      let echoChannel = new WebChannel("echo", Services.io.newURI(HTTP_PATH));

      let testDonePromise = new Promise((resolve, reject) => {
        // listen for the confirmation that content saw the error.
        echoChannel.listen((id, message, sender) => {
          is(id, "echo");
          is(message.error, "oh no");
          is(message.errno, ERRNO_UNKNOWN_ERROR);
          resolve();
        });

        // listen for a message telling us to simulate an error.
        channel.listen((id, message, sender) => {
          is(id, "error");
          is(message.command, "oops");
          throw new Error("oh no");
        });
      });
      await BrowserTestUtils.withNewTab(
        {
          gBrowser,
          url: HTTP_PATH + HTTP_ENDPOINT + "?error_thrown",
        },
        async function () {
          await testDonePromise;
          channel.stopListening();
          echoChannel.stopListening();
        }
      );
    },
  },
  {
    desc: "WebChannel errors due to an invalid channel are delivered back to content",
    async run() {
      const ERRNO_NO_SUCH_CHANNEL = 2; // WebChannel.sys.mjs doesn't export this.
      // The channel where we see the response when the content sees the error
      let echoChannel = new WebChannel("echo", Services.io.newURI(HTTP_PATH));

      let testDonePromise = new Promise((resolve, reject) => {
        // listen for the confirmation that content saw the error.
        echoChannel.listen((id, message, sender) => {
          is(id, "echo");
          is(message.error, "No Such Channel");
          is(message.errno, ERRNO_NO_SUCH_CHANNEL);
          resolve();
        });
      });
      await BrowserTestUtils.withNewTab(
        {
          gBrowser,
          url: HTTP_PATH + HTTP_ENDPOINT + "?error_invalid_channel",
        },
        async function () {
          await testDonePromise;
          echoChannel.stopListening();
        }
      );
    },
  },
]; // gTests

function test() {
  waitForExplicitFinish();

  (async function () {
    await SpecialPowers.pushPrefEnv({
      set: [["dom.security.https_first_pbm", false]],
    });

    for (let testCase of gTests) {
      info("Running: " + testCase.desc);
      await testCase.run();
    }
  })().then(finish, ex => {
    ok(false, "Unexpected Exception: " + ex);
    finish();
  });
}