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
|
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
Cu.import("resource://gre/modules/Promise.jsm");
Cu.import("resource://gre/modules/Task.jsm");
Cu.import("resource://gre/modules/Services.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "WebChannel",
"resource://gre/modules/WebChannel.jsm");
const HTTP_PATH = "http://example.com";
const HTTP_ENDPOINT = "/browser/browser/base/content/test/general/browser_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: function* () {
return new Promise(function(resolve, reject) {
let tab;
let channel = new WebChannel("generic", Services.io.newURI(HTTP_PATH, null, null));
channel.listen(function (id, message, target) {
is(id, "generic");
is(message.something.nested, "hello");
channel.stopListening();
gBrowser.removeTab(tab);
resolve();
});
tab = gBrowser.addTab(HTTP_PATH + HTTP_ENDPOINT + "?generic");
});
}
},
{
desc: "WebChannel generic message in a private window.",
run: function* () {
let promiseTestDone = new Promise(function(resolve, reject) {
let channel = new WebChannel("generic", Services.io.newURI(HTTP_PATH, null, null));
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 = yield BrowserTestUtils.openNewBrowserWindow({private: true});
yield BrowserTestUtils.openNewForegroundTab(privateWindow.gBrowser, url);
yield promiseTestDone;
yield BrowserTestUtils.closeWindow(privateWindow);
}
},
{
desc: "WebChannel two way communication",
run: function* () {
return new Promise(function(resolve, reject) {
let tab;
let channel = new WebChannel("twoway", Services.io.newURI(HTTP_PATH, null, null));
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 = gBrowser.addTab(HTTP_PATH + HTTP_ENDPOINT + "?twoway");
});
}
},
{
desc: "WebChannel two way communication in an iframe",
run: function* () {
let parentChannel = new WebChannel("echo", Services.io.newURI(HTTP_PATH, null, null));
let iframeChannel = new WebChannel("twoway", Services.io.newURI(HTTP_IFRAME_PATH, null, null));
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();
}
});
});
yield BrowserTestUtils.withNewTab({
gBrowser: gBrowser,
url: HTTP_PATH + HTTP_ENDPOINT + "?iframe"
}, function* () {
yield promiseTestDone;
parentChannel.stopListening();
iframeChannel.stopListening();
});
}
},
{
desc: "WebChannel response to a redirected iframe",
run: function* () {
/**
* 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, null, null));
let postRedirectChannel = new WebChannel("post_redirect", Services.io.newURI(HTTP_REDIRECTED_IFRAME_PATH, null, null));
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}`));
}
});
});
yield BrowserTestUtils.withNewTab({
gBrowser: gBrowser,
url: HTTP_PATH + HTTP_ENDPOINT + "?iframe_pre_redirect"
}, function* () {
yield promiseTestDone;
preRedirectChannel.stopListening();
postRedirectChannel.stopListening();
});
}
},
{
desc: "WebChannel multichannel",
run: function* () {
return new Promise(function(resolve, reject) {
let tab;
let channel = new WebChannel("multichannel", Services.io.newURI(HTTP_PATH, null, null));
channel.listen(function (id, message, sender) {
is(id, "multichannel");
gBrowser.removeTab(tab);
resolve();
});
tab = gBrowser.addTab(HTTP_PATH + HTTP_ENDPOINT + "?multichannel");
});
}
},
{
desc: "WebChannel unsolicited send, using system principal",
run: function* () {
let channel = new WebChannel("echo", Services.io.newURI(HTTP_PATH, null, null));
// 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()
});
});
yield BrowserTestUtils.withNewTab({
gBrowser,
url: HTTP_PATH + HTTP_ENDPOINT + "?unsolicited"
}, function* (targetBrowser) {
channel.send({ command: "unsolicited" }, {
browser: targetBrowser,
principal: Services.scriptSecurityManager.getSystemPrincipal()
});
yield messagePromise;
channel.stopListening();
});
}
},
{
desc: "WebChannel unsolicited send, using target origin's principal",
run: function* () {
let targetURI = Services.io.newURI(HTTP_PATH, null, null);
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();
});
});
yield BrowserTestUtils.withNewTab({
gBrowser,
url: HTTP_PATH + HTTP_ENDPOINT + "?unsolicited"
}, function* (targetBrowser) {
channel.send({ command: "unsolicited" }, {
browser: targetBrowser,
principal: Services.scriptSecurityManager.getNoAppCodebasePrincipal(targetURI)
});
yield messagePromise;
channel.stopListening();
});
}
},
{
desc: "WebChannel unsolicited send with principal mismatch",
run: function* () {
let targetURI = Services.io.newURI(HTTP_PATH, null, null);
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}`));
}
});
});
yield BrowserTestUtils.withNewTab({
gBrowser: gBrowser,
url: HTTP_PATH + HTTP_ENDPOINT + "?unsolicited"
}, function* (targetBrowser) {
let mismatchURI = Services.io.newURI(HTTP_MISMATCH_PATH, null, null);
let mismatchPrincipal = Services.scriptSecurityManager.getNoAppCodebasePrincipal(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" }, {
browser: targetBrowser,
principal: mismatchPrincipal
});
let targetPrincipal = Services.scriptSecurityManager.getNoAppCodebasePrincipal(targetURI);
// send the `done` message to the correct principal. It
// should be echoed back.
channel.send({ command: "done" }, {
browser: targetBrowser,
principal: targetPrincipal
});
yield messagePromise;
channel.stopListening();
});
}
},
{
desc: "WebChannel non-window target",
run: function* () {
/**
* 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, null, null));
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}`));
}
});
});
yield BrowserTestUtils.withNewTab({
gBrowser,
url: HTTP_PATH + HTTP_ENDPOINT + "?bubbles"
}, function* () {
yield testDonePromise;
channel.stopListening();
});
}
},
{
desc: "WebChannel disallows non-string message from non-whitelisted origin",
run: function* () {
/**
* 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, null, null));
let testDonePromise = new Promise((resolve, reject) => {
channel.listen((id, message, sender) => {
is(id, "objects");
is(message.type, "string");
resolve();
});
});
yield BrowserTestUtils.withNewTab({
gBrowser,
url: HTTP_PATH + HTTP_ENDPOINT + "?object"
}, function* () {
yield testDonePromise;
channel.stopListening();
});
}
},
{
desc: "WebChannel allows both string and non-string message from whitelisted origin",
run: function* () {
/**
* 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, null, null));
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);
yield BrowserTestUtils.withNewTab({
gBrowser,
url: HTTP_PATH + HTTP_ENDPOINT + "?object"
}, function* () {
yield testDonePromise;
Services.prefs.setCharPref(webchannelWhitelistPref, origWhitelist);
channel.stopListening();
});
}
}
]; // gTests
function test() {
waitForExplicitFinish();
Task.spawn(function* () {
for (let testCase of gTests) {
info("Running: " + testCase.desc);
yield testCase.run();
}
}).then(finish, ex => {
ok(false, "Unexpected Exception: " + ex);
finish();
});
}
|