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
|
dump('loaded child cpow test\n');
var Cu = Components.utils;
var Ci = Components.interfaces;
(function start() {
[is_remote] = sendRpcMessage("cpows:is_remote");
var tests = [
parent_test,
error_reporting_test,
dom_test,
xray_test,
symbol_test,
compartment_test,
regexp_test,
postmessage_test,
sync_test,
async_test,
rpc_test,
lifetime_test,
cancel_test,
cancel_test2,
dead_test,
unsafe_test,
];
function go() {
if (tests.length == 0) {
sendRpcMessage("cpows:done", {});
return;
}
var test = tests[0];
tests.shift();
test(function() {
go();
});
}
go();
})();
function ok(condition, message) {
dump('condition: ' + condition + ', ' + message + '\n');
if (!condition) {
sendAsyncMessage("cpows:fail", { message: message });
throw 'failed check: ' + message;
}
}
var sync_obj;
var async_obj;
function make_object()
{
let o = { };
o.i = 5;
o.b = true;
o.s = "hello";
o.x = { i: 10 };
o.f = function () { return 99; };
o.ctor = function() { this.a = 3; }
// Doing anything with this Proxy will throw.
var throwing = new Proxy({}, new Proxy({}, {
get: function (trap) { throw trap; }
}));
let array = [1, 2, 3];
let for_json = { "n": 3, "a": array, "s": "hello", o: { "x": 10 } };
let proto = { data: 42 };
let with_proto = Object.create(proto);
let with_null_proto = Object.create(null);
content.document.title = "Hello, Kitty";
return { "data": o,
"throwing": throwing,
"document": content.document,
"array": array,
"for_json": for_json,
"with_proto": with_proto,
"with_null_proto": with_null_proto
};
}
function make_json()
{
return { check: "ok" };
}
function parent_test(finish)
{
function f(check_func) {
// Make sure this doesn't crash.
let array = new Uint32Array(10);
content.crypto.getRandomValues(array);
let result = check_func(10);
ok(result == 20, "calling function in parent worked");
return result;
}
addMessageListener("cpows:from_parent", (msg) => {
let obj = msg.objects.obj;
ok(obj.a == 1, "correct value from parent");
// Test that a CPOW reference to a function in the chrome process
// is callable from unprivileged content. Greasemonkey uses this
// functionality.
let func = msg.objects.func;
let sb = Cu.Sandbox('http://www.example.com', {});
sb.func = func;
ok(sb.eval('func()') == 101, "can call parent's function in child");
finish();
});
sendRpcMessage("cpows:parent_test", {}, {func: f});
}
function error_reporting_test(finish) {
sendRpcMessage("cpows:error_reporting_test", {}, {});
finish();
}
function dom_test(finish)
{
let element = content.document.createElement("div");
element.id = "it_works";
content.document.body.appendChild(element);
sendRpcMessage("cpows:dom_test", {}, {element: element});
Components.utils.schedulePreciseGC(function() {
sendRpcMessage("cpows:dom_test_after_gc");
finish();
});
}
function xray_test(finish)
{
let element = content.document.createElement("div");
element.wrappedJSObject.foo = "hello";
sendRpcMessage("cpows:xray_test", {}, {element: element});
finish();
}
function symbol_test(finish)
{
let iterator = Symbol.iterator;
let named = Symbol.for("cpow-test");
let object = {
[iterator]: iterator,
[named]: named,
};
let test = ['a'];
sendRpcMessage("cpows:symbol_test", {}, {object: object, test: test});
finish();
}
// Parent->Child references should go X->parent.privilegedJunkScope->child.privilegedJunkScope->Y
// Child->Parent references should go X->child.privilegedJunkScope->parent.unprivilegedJunkScope->Y
function compartment_test(finish)
{
// This test primarily checks various compartment invariants for CPOWs, and
// doesn't make sense to run in-process.
if (!is_remote) {
finish();
return;
}
let sb = Cu.Sandbox('http://www.example.com', { wantGlobalProperties: ['XMLHttpRequest'] });
sb.eval('function getUnprivilegedObject() { var xhr = new XMLHttpRequest(); xhr.expando = 42; return xhr; }');
function testParentObject(obj) {
let results = [];
function is(a, b, msg) { results.push({ result: a === b ? "PASS" : "FAIL", message: msg }) };
function ok(x, msg) { results.push({ result: x ? "PASS" : "FAIL", message: msg }) };
let cpowLocation = Cu.getCompartmentLocation(obj);
ok(/Privileged Junk/.test(cpowLocation),
"child->parent CPOWs should live in the privileged junk scope: " + cpowLocation);
is(obj(), 42, "child->parent CPOW is invokable");
try {
obj.expando;
ok(false, "child->parent CPOW cannot access properties");
} catch (e) {
ok(true, "child->parent CPOW cannot access properties");
}
return results;
}
sendRpcMessage("cpows:compartment_test", {}, { getUnprivilegedObject: sb.getUnprivilegedObject,
testParentObject: testParentObject });
finish();
}
function regexp_test(finish)
{
sendRpcMessage("cpows:regexp_test", {}, { regexp: /myRegExp/g });
finish();
}
function postmessage_test(finish)
{
sendRpcMessage("cpows:postmessage_test", {}, { win: content.window });
finish();
}
function sync_test(finish)
{
dump('beginning cpow sync test\n');
sync_obj = make_object();
sendRpcMessage("cpows:sync",
make_json(),
make_object());
finish();
}
function async_test(finish)
{
dump('beginning cpow async test\n');
async_obj = make_object();
sendAsyncMessage("cpows:async",
make_json(),
async_obj);
addMessageListener("cpows:async_done", finish);
}
var rpc_obj;
function rpc_test(finish)
{
dump('beginning cpow rpc test\n');
rpc_obj = make_object();
rpc_obj.data.reenter = function () {
sendRpcMessage("cpows:reenter", { }, { data: { valid: true } });
return "ok";
}
sendRpcMessage("cpows:rpc",
make_json(),
rpc_obj);
finish();
}
function lifetime_test(finish)
{
if (!is_remote) {
// Only run this test when running out-of-process. Otherwise it
// will fail, since local CPOWs don't follow the same ownership
// rules.
finish();
return;
}
dump("beginning lifetime test\n");
var obj = {"will_die": {"f": 1}};
let [result] = sendRpcMessage("cpows:lifetime_test_1", {}, {obj: obj});
ok(result == 10, "got sync result");
ok(obj.wont_die.f == 2, "got reverse CPOW");
obj.will_die = null;
Components.utils.schedulePreciseGC(function() {
addMessageListener("cpows:lifetime_test_3", (msg) => {
ok(obj.wont_die.f == 2, "reverse CPOW still works");
finish();
});
sendRpcMessage("cpows:lifetime_test_2");
});
}
function cancel_test(finish)
{
if (!is_remote) {
// No point in doing this in single-process mode.
finish();
return;
}
let fin1 = false, fin2 = false;
// CPOW from the parent runs f. When it sends a sync message, the
// CPOW is canceled. The parent starts running again immediately
// after the CPOW is canceled; f also continues running.
function f() {
let res = sendSyncMessage("cpows:cancel_sync_message");
ok(res[0] == 12, "cancel_sync_message result correct");
fin1 = true;
if (fin1 && fin2) finish();
}
sendAsyncMessage("cpows:cancel_test", null, {f: f});
addMessageListener("cpows:cancel_test_done", msg => {
fin2 = true;
if (fin1 && fin2) finish();
});
}
function cancel_test2(finish)
{
if (!is_remote) {
// No point in doing this in single-process mode.
finish();
return;
}
let fin1 = false, fin2 = false;
// CPOW from the parent runs f. When it does a sync XHR, the
// CPOW is canceled. The parent starts running again immediately
// after the CPOW is canceled; f also continues running.
function f() {
let req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].
createInstance(Components.interfaces.nsIXMLHttpRequest);
let fin = false;
let reqListener = () => {
if (req.readyState != req.DONE) {
return;
}
ok(req.status == 200, "XHR succeeded");
fin = true;
};
req.onload = reqListener;
req.open("get", "http://example.com", false);
req.send(null);
ok(fin == true, "XHR happened");
fin1 = true;
if (fin1 && fin2) finish();
}
sendAsyncMessage("cpows:cancel_test2", null, {f: f});
addMessageListener("cpows:cancel_test2_done", msg => {
fin2 = true;
if (fin1 && fin2) finish();
});
}
function unsafe_test(finish)
{
if (!is_remote) {
// Only run this test when running out-of-process.
finish();
return;
}
function f() {}
sendAsyncMessage("cpows:unsafe", null, {f});
addMessageListener("cpows:unsafe_done", msg => {
sendRpcMessage("cpows:safe", null, {f});
addMessageListener("cpows:safe_done", finish);
});
}
function dead_test(finish)
{
if (!is_remote) {
// Only run this test when running out-of-process.
finish();
return;
}
let gcTrigger = function() {
// Force the GC to dead-ify the thing.
content.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils)
.garbageCollect();
}
{
let thing = { value: "Gonna croak" };
sendAsyncMessage("cpows:dead", null, { thing, gcTrigger });
}
addMessageListener("cpows:dead_done", finish);
}
|