-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjailed.js
808 lines (672 loc) · 23.3 KB
/
jailed.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
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
/**
* @fileoverview Jailed - safe yet flexible sandbox
* @version 0.3.1
*
* @license MIT, see http://github.com/asvd/jailed
* Copyright (c) 2014 asvd <heliosframework@gmail.com>
*
* Main library script, the only one to be loaded by a developer into
* the application. Other scrips shipped along will be loaded by the
* library either here (application site), or into the plugin site
* (Worker/child process):
*
* _JailedSite.js loaded into both applicaiton and plugin sites
* _frame.html sandboxed frame (web)
* _frame.js sandboxed frame code (web)
* _pluginWebWorker.js platform-dependent plugin routines (web / worker)
* _pluginWebIframe.js platform-dependent plugin routines (web / iframe)
* _pluginNode.js platform-dependent plugin routines (Node.js)
* _pluginCore.js common plugin site protocol implementation
*/
var __jailed__path__;
if (typeof window == 'undefined') {
// Node.js
__jailed__path__ = __dirname + '/';
} else {
// web
var scripts = document.getElementsByTagName('script');
__jailed__path__ = scripts[scripts.length-1].src
.split('?')[0]
.split('/')
.slice(0, -1)
.join('/')+'/';
}
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['exports'], factory);
} else if (typeof exports !== 'undefined') {
factory(exports);
} else {
factory((root.jailed = {}));
}
}(this, function (exports) {
var isNode = ((typeof process !== 'undefined') &&
(!process.browser) &&
(process.release.name.search(/node|io.js/) !== -1));
/**
* A special kind of event:
* - which can only be emitted once;
* - executes a set of subscribed handlers upon emission;
* - if a handler is subscribed after the event was emitted, it
* will be invoked immideately.
*
* Used for the events which only happen once (or do not happen at
* all) during a single plugin lifecycle - connect, disconnect and
* connection failure
*/
var Whenable = function() {
this._emitted = false;
this._handlers = [];
}
/**
* Emits the Whenable event, calls all the handlers already
* subscribed, switches the object to the 'emitted' state (when
* all future subscibed listeners will be immideately issued
* instead of being stored)
*/
Whenable.prototype.emit = function(){
if (!this._emitted) {
this._emitted = true;
var handler;
while(handler = this._handlers.pop()) {
setTimeout(handler,0);
}
}
}
/**
* Saves the provided function as a handler for the Whenable
* event. This handler will then be called upon the event emission
* (if it has not been emitted yet), or will be scheduled for
* immediate issue (if the event has already been emmitted before)
*
* @param {Function} handler to subscribe for the event
*/
Whenable.prototype.whenEmitted = function(handler){
handler = this._checkHandler(handler);
if (this._emitted) {
setTimeout(handler, 0);
} else {
this._handlers.push(handler);
}
}
/**
* Checks if the provided object is suitable for being subscribed
* to the event (= is a function), throws an exception if not
*
* @param {Object} obj to check for being subscribable
*
* @throws {Exception} if object is not suitable for subscription
*
* @returns {Object} the provided object if yes
*/
Whenable.prototype._checkHandler = function(handler){
var type = typeof handler;
if (type != 'function') {
var msg =
'A function may only be subsribed to the event, '
+ type
+ ' was provided instead'
throw new Error(msg);
}
return handler;
}
/**
* Initializes the library site for Node.js environment (loads
* _JailedSite.js)
*/
var initNode = function() {
require('./_JailedSite.js');
}
/**
* Initializes the library site for web environment (loads
* _JailedSite.js)
*/
var platformInit;
var initWeb = function() {
// loads additional script to the application environment
var load = function(path, cb) {
var script = document.createElement('script');
script.src = path;
var clear = function() {
script.onload = null;
script.onerror = null;
script.onreadystatechange = null;
script.parentNode.removeChild(script);
}
var success = function() {
clear();
cb();
}
script.onerror = clear;
script.onload = success;
script.onreadystatechange = function() {
var state = script.readyState;
if (state==='loaded' || state==='complete') {
success();
}
}
document.body.appendChild(script);
}
platformInit = new Whenable;
var origOnload = window.onload || function(){};
window.onload = function(){
origOnload();
load(
__jailed__path__+'_JailedSite.js',
function(){ platformInit.emit(); }
);
}
}
var BasicConnection;
/**
* Creates the platform-dependent BasicConnection object in the
* Node.js environment
*/
var basicConnectionNode = function() {
var childProcess = require('child_process');
/**
* Platform-dependent implementation of the BasicConnection
* object, initializes the plugin site and provides the basic
* messaging-based connection with it
*
* For Node.js the plugin is created as a forked process
*/
BasicConnection = function() {
// in Node.js always has a subprocess
this.dedicatedThread = true;
this._disconnected = false;
this._messageHandler = function(){};
this._disconnectHandler = function(){};
this._process = childProcess.fork(
__jailed__path__+'_pluginNode.js'
);
var me = this;
this._process.on('message', function(m){
me._messageHandler(m);
});
this._process.on('exit', function(m){
me._disconnected = true;
me._disconnectHandler(m);
});
}
/**
* Sets-up the handler to be called upon the BasicConnection
* initialization is completed.
*
* For Node.js the connection is fully initialized within the
* constructor, so simply calls the provided handler.
*
* @param {Function} handler to be called upon connection init
*/
BasicConnection.prototype.whenInit = function(handler) {
handler();
}
/**
* Sends a message to the plugin site
*
* @param {Object} data to send
*/
BasicConnection.prototype.send = function(data) {
if (!this._disconnected) {
this._process.send(data);
}
}
/**
* Adds a handler for a message received from the plugin site
*
* @param {Function} handler to call upon a message
*/
BasicConnection.prototype.onMessage = function(handler) {
this._messageHandler = function(data) {
// broken stack would break the IPC in Node.js
try {
handler(data);
} catch (e) {
console.error();
console.error(e.stack);
}
}
}
/**
* Adds a handler for the event of plugin disconnection
* (= plugin process exit)
*
* @param {Function} handler to call upon a disconnect
*/
BasicConnection.prototype.onDisconnect = function(handler) {
this._disconnectHandler = handler;
}
/**
* Disconnects the plugin (= kills the forked process)
*/
BasicConnection.prototype.disconnect = function() {
this._process.kill('SIGKILL');
this._disconnected = true;
}
}
/**
* Creates the platform-dependent BasicConnection object in the
* web-browser environment
*/
var basicConnectionWeb = function() {
var perm = ['allow-scripts'];
if (__jailed__path__.substr(0,7).toLowerCase() == 'file://') {
// local instance requires extra permission
perm.push('allow-same-origin');
}
// frame element to be cloned
var sample = document.createElement('iframe');
sample.src = __jailed__path__ + '_frame.html';
sample.sandbox = perm.join(' ');
sample.style.display = 'none';
/**
* Platform-dependent implementation of the BasicConnection
* object, initializes the plugin site and provides the basic
* messaging-based connection with it
*
* For the web-browser environment, the plugin is created as a
* Worker in a sandbaxed frame
*/
BasicConnection = function() {
this._init = new Whenable;
this._disconnected = false;
var me = this;
platformInit.whenEmitted(function() {
if (!me._disconnected) {
me._frame = sample.cloneNode(false);
document.body.appendChild(me._frame);
window.addEventListener('message', function (e) {
if (e.source === me._frame.contentWindow) {
if (e.data.type == 'initialized') {
me.dedicatedThread =
e.data.dedicatedThread;
me._init.emit();
} else {
me._messageHandler(e.data);
}
}
});
}
});
}
/**
* Sets-up the handler to be called upon the BasicConnection
* initialization is completed.
*
* For the web-browser environment, the handler is issued when
* the plugin worker successfully imported and executed the
* _pluginWebWorker.js or _pluginWebIframe.js, and replied to
* the application site with the initImprotSuccess message.
*
* @param {Function} handler to be called upon connection init
*/
BasicConnection.prototype.whenInit = function(handler) {
this._init.whenEmitted(handler);
}
/**
* Sends a message to the plugin site
*
* @param {Object} data to send
*/
BasicConnection.prototype.send = function(data) {
this._frame.contentWindow.postMessage(
{type: 'message', data: data}, '*'
);
}
/**
* Adds a handler for a message received from the plugin site
*
* @param {Function} handler to call upon a message
*/
BasicConnection.prototype.onMessage = function(handler) {
this._messageHandler = handler;
}
/**
* Adds a handler for the event of plugin disconnection
* (not used in case of Worker)
*
* @param {Function} handler to call upon a disconnect
*/
BasicConnection.prototype.onDisconnect = function(){};
/**
* Disconnects the plugin (= kills the frame)
*/
BasicConnection.prototype.disconnect = function() {
if (!this._disconnected) {
this._disconnected = true;
if (typeof this._frame != 'undefined') {
this._frame.parentNode.removeChild(this._frame);
} // otherwise farme is not yet created
}
}
}
if (isNode) {
initNode();
basicConnectionNode();
} else {
initWeb();
basicConnectionWeb();
}
/**
* Application-site Connection object constructon, reuses the
* platform-dependent BasicConnection declared above in order to
* communicate with the plugin environment, implements the
* application-site protocol of the interraction: provides some
* methods for loading scripts and executing the given code in the
* plugin
*/
var Connection = function(){
this._platformConnection = new BasicConnection;
this._importCallbacks = {};
this._executeSCb = function(){};
this._executeFCb = function(){};
this._messageHandler = function(){};
var me = this;
this.whenInit = function(cb){
me._platformConnection.whenInit(cb);
};
this._platformConnection.onMessage(function(m) {
switch(m.type) {
case 'message':
me._messageHandler(m.data);
break;
case 'importSuccess':
me._handleImportSuccess(m.url);
break;
case 'importFailure':
me._handleImportFailure(m.url);
break;
case 'executeSuccess':
me._executeSCb();
break;
case 'executeFailure':
me._executeFCb();
break;
}
});
}
/**
* @returns {Boolean} true if a connection obtained a dedicated
* thread (subprocess in Node.js or a subworker in browser) and
* therefore will not hang up on the infinite loop in the
* untrusted code
*/
Connection.prototype.hasDedicatedThread = function() {
return this._platformConnection.dedicatedThread;
}
/**
* Tells the plugin to load a script with the given path, and to
* execute it. Callbacks executed upon the corresponding responce
* message from the plugin site
*
* @param {String} path of a script to load
* @param {Function} sCb to call upon success
* @param {Function} fCb to call upon failure
*/
Connection.prototype.importScript = function(path, sCb, fCb) {
var f = function(){};
this._importCallbacks[path] = {sCb: sCb||f, fCb: fCb||f};
this._platformConnection.send({type: 'import', url: path});
}
/**
* Tells the plugin to load a script with the given path, and to
* execute it in the JAILED environment. Callbacks executed upon
* the corresponding responce message from the plugin site
*
* @param {String} path of a script to load
* @param {Function} sCb to call upon success
* @param {Function} fCb to call upon failure
*/
Connection.prototype.importJailedScript = function(path, sCb, fCb) {
var f = function(){};
this._importCallbacks[path] = {sCb: sCb||f, fCb: fCb||f};
this._platformConnection.send({type: 'importJailed', url: path});
}
/**
* Sends the code to the plugin site in order to have it executed
* in the JAILED enviroment. Assuming the execution may only be
* requested once by the Plugin object, which means a single set
* of callbacks is enough (unlike importing additional scripts)
*
* @param {String} code code to execute
* @param {Function} sCb to call upon success
* @param {Function} fCb to call upon failure
*/
Connection.prototype.execute = function(code, sCb, fCb) {
this._executeSCb = sCb||function(){};
this._executeFCb = fCb||function(){};
this._platformConnection.send({type: 'execute', code: code});
}
/**
* Adds a handler for a message received from the plugin site
*
* @param {Function} handler to call upon a message
*/
Connection.prototype.onMessage = function(handler) {
this._messageHandler = handler;
}
/**
* Adds a handler for a disconnect message received from the
* plugin site
*
* @param {Function} handler to call upon disconnect
*/
Connection.prototype.onDisconnect = function(handler) {
this._platformConnection.onDisconnect(handler);
}
/**
* Sends a message to the plugin
*
* @param {Object} data of the message to send
*/
Connection.prototype.send = function(data) {
this._platformConnection.send({
type: 'message',
data: data
});
}
/**
* Handles import succeeded message from the plugin
*
* @param {String} url of a script loaded by the plugin
*/
Connection.prototype._handleImportSuccess = function(url) {
var sCb = this._importCallbacks[url].sCb;
this._importCallbacks[url] = null;
delete this._importCallbacks[url];
sCb();
}
/**
* Handles import failure message from the plugin
*
* @param {String} url of a script loaded by the plugin
*/
Connection.prototype._handleImportFailure = function(url) {
var fCb = this._importCallbacks[url].fCb;
this._importCallbacks[url] = null;
delete this._importCallbacks[url];
fCb();
}
/**
* Disconnects the plugin when it is not needed anymore
*/
Connection.prototype.disconnect = function() {
this._platformConnection.disconnect();
}
/**
* Plugin constructor, represents a plugin initialized by a script
* with the given path
*
* @param {String} url of a plugin source
* @param {Object} _interface to provide for the plugin
*/
var Plugin = function(url, _interface) {
this._path = url;
this._initialInterface = _interface||{};
this._connect();
};
/**
* DynamicPlugin constructor, represents a plugin initialized by a
* string containing the code to be executed
*
* @param {String} code of the plugin
* @param {Object} _interface to provide to the plugin
*/
var DynamicPlugin = function(code, _interface) {
this._code = code;
this._initialInterface = _interface||{};
this._connect();
};
/**
* Creates the connection to the plugin site
*/
DynamicPlugin.prototype._connect =
Plugin.prototype._connect = function() {
this.remote = null;
this._connect = new Whenable;
this._fail = new Whenable;
this._disconnect = new Whenable;
var me = this;
// binded failure callback
this._fCb = function(){
me._fail.emit();
me.disconnect();
}
this._connection = new Connection;
this._connection.whenInit(function(){
me._init();
});
}
/**
* Creates the Site object for the plugin, and then loads the
* common routines (_JailedSite.js)
*/
DynamicPlugin.prototype._init =
Plugin.prototype._init = function() {
this._site = new JailedSite(this._connection);
var me = this;
this._site.onDisconnect(function() {
me._disconnect.emit();
});
var sCb = function() {
me._loadCore();
}
this._connection.importScript(
__jailed__path__+'_JailedSite.js', sCb, this._fCb
);
}
/**
* Loads the core scirpt into the plugin
*/
DynamicPlugin.prototype._loadCore =
Plugin.prototype._loadCore = function() {
var me = this;
var sCb = function() {
me._sendInterface();
}
this._connection.importScript(
__jailed__path__+'_pluginCore.js', sCb, this._fCb
);
}
/**
* Sends to the remote site a signature of the interface provided
* upon the Plugin creation
*/
DynamicPlugin.prototype._sendInterface =
Plugin.prototype._sendInterface = function() {
var me = this;
this._site.onInterfaceSetAsRemote(function() {
if (!me._connected) {
me._loadPlugin();
}
});
this._site.setInterface(this._initialInterface);
}
/**
* Loads the plugin body (loads the plugin url in case of the
* Plugin)
*/
Plugin.prototype._loadPlugin = function() {
var me = this;
var sCb = function() {
me._requestRemote();
}
this._connection.importJailedScript(this._path, sCb, this._fCb);
}
/**
* Loads the plugin body (executes the code in case of the
* DynamicPlugin)
*/
DynamicPlugin.prototype._loadPlugin = function() {
var me = this;
var sCb = function() {
me._requestRemote();
}
this._connection.execute(this._code, sCb, this._fCb);
}
/**
* Requests the remote interface from the plugin (which was
* probably set by the plugin during its initialization), emits
* the connect event when done, then the plugin is fully usable
* (meaning both the plugin and the application can use the
* interfaces provided to each other)
*/
DynamicPlugin.prototype._requestRemote =
Plugin.prototype._requestRemote = function() {
var me = this;
this._site.onRemoteUpdate(function(){
me.remote = me._site.getRemote();
me._connect.emit();
});
this._site.requestRemote();
}
/**
* @returns {Boolean} true if a plugin runs on a dedicated thread
* (subprocess in Node.js or a subworker in browser) and therefore
* will not hang up on the infinite loop in the untrusted code
*/
DynamicPlugin.prototype.hasDedicatedThread =
Plugin.prototype.hasDedicatedThread = function() {
return this._connection.hasDedicatedThread();
}
/**
* Disconnects the plugin immideately
*/
DynamicPlugin.prototype.disconnect =
Plugin.prototype.disconnect = function() {
this._connection.disconnect();
this._disconnect.emit();
}
/**
* Saves the provided function as a handler for the connection
* failure Whenable event
*
* @param {Function} handler to be issued upon disconnect
*/
DynamicPlugin.prototype.whenFailed =
Plugin.prototype.whenFailed = function(handler) {
this._fail.whenEmitted(handler);
}
/**
* Saves the provided function as a handler for the connection
* success Whenable event
*
* @param {Function} handler to be issued upon connection
*/
DynamicPlugin.prototype.whenConnected =
Plugin.prototype.whenConnected = function(handler) {
this._connect.whenEmitted(handler);
}
/**
* Saves the provided function as a handler for the connection
* failure Whenable event
*
* @param {Function} handler to be issued upon connection failure
*/
DynamicPlugin.prototype.whenDisconnected =
Plugin.prototype.whenDisconnected = function(handler) {
this._disconnect.whenEmitted(handler);
}
exports.Plugin = Plugin;
exports.DynamicPlugin = DynamicPlugin;
}));