-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGuestScriptContext.js
492 lines (431 loc) · 17.6 KB
/
GuestScriptContext.js
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
/**
* This file contains the worker code.
* This file is responsible for containing user-scripts and disallowing any potentially harmful action.
* The links explain the approach and the global context for web workers, respectively.
*
* @see http://stackoverflow.com/questions/10653809/making-webworkers-a-safe-environment
* @see https://developer.mozilla.org/en-US/docs/Web/Reference/Functions_and_classes_available_to_workers
*/
"use strict";
// ################################################################################################################
// Make Worker context secure by removing all kinds of potentially dangerous globals
var global = this;
/**
* Only allow these globals.
* All other globals are potentially harmful or not portable between run-time environments.
*/
self.whiteList = {
"self": 1,
"postMessage": 1,
"global": 1,
"whiteList": 1,
"eval": 1,
"Array": 1,
"Boolean": 1,
"Date": 1,
"Function": 1,
"Number" : 1,
"Object": 1,
"RegExp": 1,
"String": 1,
"Error": 1,
"EvalError": 1,
"RangeError": 1,
"ReferenceError": 1,
"SyntaxError": 1,
"TypeError": 1,
"URIError": 1,
"decodeURI": 1,
"decodeURIComponent": 1,
"encodeURI": 1,
"encodeURIComponent": 1,
"isFinite": 1,
"isNaN": 1,
"parseFloat": 1,
"parseInt": 1,
"Infinity": 1,
"JSON": 1,
"Math": 1,
"NaN": 1,
"undefined": 1,
"Intl": 1,
"console": 1,
"setTimeout": 1,
"constructor":1,
// globals from utility libraries
"printStackTrace": 1,
"require": 1, // require is not configurable, but luckily its harmless without importScripts
"define": 1, // require is not configurable, but luckily its harmless without importScripts
"requirejs": 1, // require is not configurable, but luckily its harmless without importScripts
// helper & static context globals
"currentSenderId": 1,
"postAction": 1,
"postCommand": 1,
"postPrivilegedCommand": 1, // can only be used with a key, that should never be available to user code
"postInstanceMessage": 1,
"lockDown": 1, // lockDown will be set to null explicitely to check for execution
"runScript": 1, // glorified eval
"reportError": 1,
"exposeGlobals": 1,
"createGlobalEvents": 1,
// user-given globals
"initScriptContext": 1, // initialize context for interacting with the host
"onInitializationFinished": 1 // post-lockDown initialization
};
/**
* Lock down the context and disallow everything that is a potential security risk.
*
* @param {Array.<String>} ... Additional whitelist objects, imported from libraries.
* @see http://stackoverflow.com/questions/10653809/making-webworkers-a-safe-environment
*/
self.lockDown = function(additionalWhiteListSymbols) {
// add external libraries to safe list
for (var i = 0; i < additionalWhiteListSymbols.length; ++i) {
var arg = additionalWhiteListSymbols[i];
whiteList[arg] = 1;
}
// create a map of all forbidden commands
var unsecureGlobal = {};
// Remove all potentially harmful functions from the global context:
// add all "orphaned" globals (properties that are not revealed by getOwnPropertyNames, possibly due to some bug)
var allBadProps = {};
for (var prop in global) {
unsecureGlobal[prop] = global[prop];
allBadProps[prop] = global[prop];
}
// add all other possible globals
Object.getOwnPropertyNames( global ).forEach( function( prop ) {
allBadProps[prop] = global[prop];
});
// override all found props
Object.getOwnPropertyNames(allBadProps).forEach( function( prop ) {
if( !whiteList.hasOwnProperty( prop ) ) {
// lock potentially insecure global
Object.defineProperty( global, prop, {
get : function() {
throw new Error("Security Exception - cannot access: "+prop);
},
configurable : false
});
}
});
// add all other globals, hidden in the global prototype
Object.getOwnPropertyNames( global.__proto__ ).forEach( function( prop ) {
if( !whiteList.hasOwnProperty( prop ) ) {
Object.defineProperty( global.__proto__, prop, {
get : function() {
throw new Error("Security Exception - cannot access: "+prop);
},
configurable : false
});
}
});
// unset this function
Object.defineProperty(global, "lockDown", {
writable: false,
configurable: false,
enumrable: false,
value: null
});
return unsecureGlobal;
};
/**
* Joining large arrays can potentially crash browser tabs.
* @see http://stackoverflow.com/questions/10653809/making-webworkers-a-safe-environment
*/
Object.defineProperty( Array.prototype, "join", {
writable: false,
configurable: false,
enumrable: false,
value: function(old){
return function(arg){
if( this.length > 500 || (arg && arg.length > 500 ) ) {
throw "Exception: too many items";
}
return old.apply( this, arguments );
};
}(Array.prototype.join)
});
// ################################################################################################################
// Run user script
/**
* Sends custom action to host.
*/
Object.defineProperty(global, "postAction", {
writable: false,
configurable: false,
enumrable: false,
value: function(args) {
postCommand("action", args);
}
});
/**
* Sends a given command to the host.
*/
Object.defineProperty(global, "postCommand", {
writable: false,
configurable: false,
enumrable: false,
value: function(cmd, args) {
var msg = {senderId: currentSenderId, command: cmd, args: args};
postMessage(msg);
}
});
/**
* Sends a given message to host, including currentSenderId.
*/
Object.defineProperty(global, "postInstanceMessage", {
writable: false,
configurable: false,
enumrable: false,
value: function(msg) {
msg.senderId = currentSenderId;
postMessage(msg);
}
});
/**
* Fancy version of eval.
*/
Object.defineProperty(global, "runScript", {
writable: false,
configurable: false,
enumrable: false,
value: function(code) {
try {
// run user code
return eval(code);
} catch (err) {
reportError(err);
}
}
});
/**
* Report error back to host.
*
* TODO: Test on node.
* TODO: Use built-ins, if available: http://stackoverflow.com/questions/11386492/accessing-line-number-in-v8-javascript-chrome-node-js
*/
Object.defineProperty(global, "reportError", {
writable: false,
configurable: false,
enumrable: false,
value: function(err) {
var trace = printStackTrace({e: err});
var args = {message: err.message, stacktrace: [] };
var beforeEval = true;
// Parse the printStackTrace format (no idea, why they did not do this in the first place...)
// Each line has a format similar to: "functionName@url:line:column" (however, URL can (but does not have to) contain colons and/or @'s)
//var frameRegex = /([^@]+)@([^\:]+)\:([^\:]+)\:([^\:]+)/;
for (var i = 0; i < trace.length; ++i) {
var frameInfo = trace[i];
var functionName = frameInfo;
var url = ""
var line = -1, column = -1;
var locationIndex = frameInfo.indexOf('@')+1;
if (locationIndex > 0) {
functionName = frameInfo.substring(0, locationIndex-1);
var location = frameInfo.substring(locationIndex);
var colIdx = frameInfo.lastIndexOf(':')+1;
var lineIdx = frameInfo.lastIndexOf(':', colIdx-2)+1;
if (colIdx > 0 && lineIdx > 0) {
url = frameInfo.substring(locationIndex, lineIdx-1);
line = frameInfo.substring(lineIdx, colIdx-1);
column = frameInfo.substring(colIdx);
}
else {
// no line or column given (probably native code)
url = location;
}
}
// add to stacktrace array
args.stacktrace.push({fileName: url, functionName: functionName, line: line, column: column, infoString: frameInfo});
}
// warn developer
console.warn(trace.join("\n"));
//console.warn(err.stack);
//console.warn(squishy.objToString(args.stacktrace));
// run-time error
postInstanceMessage({command: "error_eval", args: args});
}
});
// ################################################################################################################
// Other utility methods
/**
* Creates global events from an array of eventIds and a function that returns the name of the event handler function, given its id.
*/
Object.defineProperty(global, "createGlobalEvents", {
writable: false,
configurable: false,
enumrable: false,
value: function(eventIds, getEventHandlerNameById) {
// create event handlers
var eventHandlers = {};
// create global getter which looks up the event handler in the events map
var createEventHandler = function(eventId, eventHandlerName) {
Object.defineProperty(global, eventHandlerName, {
configurable: true,
get: function() {
return eventHandlers[eventId];
},
set: function(value) {
eventHandlers[eventId] = value;
}
});
};
// create an event handler for every event
for (var i = 0; i < eventIds.length; ++i) {
var eventId = eventIds[i];
var eventHandlerName = getEventHandlerNameById(eventId);
createEventHandler(eventId, eventHandlerName);
// add event handler stub to event map
eventHandlers[eventId] = function() {};
}
return eventHandlers;
}
});
/**
* Expose all variables in the given object to the global context.
*/
Object.defineProperty(global, "exposeGlobals", {
writable: false,
configurable: false,
enumrable: false,
value: function(globals) {
// expose context-specific globals & white-list them, too
for (var key in globals) {
if (globals.hasOwnProperty(key)) {
// add global
Object.defineProperty(global, key, {
writable: false,
configurable: false,
enumrable: false,
value: globals[key]
});
// make sure, lockDown won't touch it
whiteList[key] = 1;
}
}
}
});
// ################################################################################################################
// Context initialization: Register the onmessage event handler.
/**
* This function is initially and anonymously called to initialize this Worker.
*/
(function(local) {
// register the message handler which acts as a simple command interpreter.
onmessage = function(event) {
if (!event.data) return;
// remember command sender's id to identify the origin when messages are received by host
self.currentSenderId = event.data.senderId;
// get command name and arguments
var cmd = event.data.command;
var args = event.data.args;
switch (cmd) {
case "init":
// initialize this guest context
if (!lockDown) return; // already initialized
var baseUrl = args.baseUrl;
// import some standard libraries
importScripts(baseUrl + "lib/stacktrace.js");
importScripts(baseUrl + "lib/require.js");
// initialize custom globals
try {
var guestGlobals = eval(args.guestGlobals);
}
catch (err) {
throw new Error("Unable to eval guestGlobals: " + err.stack);
}
var initScriptContext = guestGlobals.initScriptContext || function() {};
var onInitializationFinished = guestGlobals.onInitializationFinished || function() {};
var globals = guestGlobals.globals || {};
var commandHandlers = guestGlobals.commandHandlers || {};
// check for handler configuration
local.commandHandlers = {};
Object.keys(commandHandlers).forEach(function(prop) {
if (!commandHandlers.hasOwnProperty(prop)) return;
var handler = commandHandlers[prop];
if (handler instanceof Function) {
local.commandHandlers[prop] = handler;
}
else if (handler.handlerFunction instanceof Function) {
local.commandHandlers[prop] = handler.handlerFunction;
local.commandHandlers[prop].isPrivileged = handler.isPrivileged;
}
else {
console.warn("Invalid commandHandler \"" + prop + "\" is not a function, nor an object with a \"handlerFunction\" property that is a function.");
}
});
// register handler to run user scripts (not privileged!)
local.commandHandlers["run"] = function(args) { runScript(args.code); };
/**
* Simulator-specific initialization of the Worker context.
* This code is called before lockDown.
*/
Object.defineProperty(global, "initScriptContext", {
writable: false,
configurable: false,
enumrable: false,
value: initScriptContext
});
/**
* This code is called after lockDown.
*/
Object.defineProperty(global, "onInitializationFinished", {
writable: false,
configurable: false,
enumrable: false,
value: onInitializationFinished
});
// expose globals
exposeGlobals(globals);
// ScriptContext-specific initialization code
initScriptContext(baseUrl, function() {
// lock down the context and make it secure
if (lockDown) {
local.unsecureGlobal = lockDown(arguments);
local.unsecureGlobal = null; // apparently, copying some globals into another object and calling them from there is against the rules
// ScriptContext-specific second pass of initialization (post lockDown initialization)
onInitializationFinished(function() {
// this code is called after the second initialization pass has finished
postCommand("ready");
});
}
});
break;
default:
if (lockDown) return; // the context has not been locked down yet. I.e. initialization has not finished yet.
// check for custom command handler
var handler = local.commandHandlers[cmd];
if (handler) {
// securityToken is used to to send privileged messages to the host, such as start or stop, while preventing user code from doing the same.
// We don't want the raw key to be lingering on the stack, due to ECMA's non-standardized stackframe API.
// Instead, we hide it in a closure whose stackframe is generally impossible (or at least harder) to obtain.
var postPrivilegedCommand = (function(securityToken) { return function(commandName, args) {
var msg = {senderId: currentSenderId, securityToken: securityToken, command: commandName, args: args};
postMessage(msg);
}})(event.data.securityToken);
event.data.securityToken = null;
postPrivilegedCommand("start"); // signal host that command execution has started
try {
if (handler.isPrivileged) {
// pass postPrivilegedCommand only to privileged handlers
handler(args, postPrivilegedCommand);
}
else {
handler(args);
}
}
catch (err) {
reportError(err);
}
postPrivilegedCommand("stop"); // signal host that command execution has finished
}
else {
// developer error
console.warn("invalid command received by worker: " + cmd);
}
break;
};
};
})({});