-
Notifications
You must be signed in to change notification settings - Fork 0
/
sandbox.mjs
419 lines (374 loc) · 14.4 KB
/
sandbox.mjs
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
import assert from 'assert/strict'
const mapValues = (o, f) => Object.fromEntries(Object.entries(o).map(([k, v]) => [k, f(v)]))
const unexpected = () => { throw new Error('Did not expect to get here') }
export class Sandbox {
#wet // The app side of the membrane
#dry // The host side of the membrane
#ioJournal // A recording of all IO between wet and dry side
#dryGlobals
#wetGlobals
#controlChannelDry
#mode
#journalReplayCursor
constructor (opts) {
const { globalThis } = opts ?? {}
this.#dryGlobals = globalThis ?? {}
this.#ioJournal = []
this.#wet = new SerializingMembrane(this.#sendToDry.bind(this), 'wet', 'dry')
this.#dry = new SerializingMembrane(this.#sendToWet.bind(this), 'dry', 'wet')
this.#mode = 'live'
// Set up globals
this.#dry.defineWellKnownLocal('globalThis', this.#createGlobalThis())
this.#wetGlobals = this.#wet.defineWellKnownRemote('globalThis')
// Connect to wet controlChannel from dry side
this.#wet.defineWellKnownLocal('controlChannel', this.#controlChannelWet())
this.#controlChannelDry = this.#dry.defineWellKnownRemote('controlChannel')
}
evaluateCommonJsModule(moduleText, filename) {
// Send to wet side to be evaluated
return this.#controlChannelDry.evaluateCommonJsModule(moduleText, filename)
}
takeSnapshot(captures) {
const exportList = mapValues(captures, v => this.#dry.serialize(v));
const ioJournal = this.#ioJournal;
// Basically all host objects are treated as ephemeral until proven
// otherwise. Even the well-known ones are not guaranteed to exist in the
// target environment, although they probably will.
const ephemerals = [...this.#dry.objectsByLocalId.entries()].map(([id, obj]) => [id, typeof obj]);
const json = JSON.stringify({ ioJournal, exportList, ephemerals })
const buffer = Buffer.from(json, 'utf-8')
return buffer;
}
static restoreFromSnapshot(snapshot, opts) {
const sandbox = new Sandbox(opts)
return sandbox.#restoreFromSnapshot(snapshot)
}
#createGlobalThis() {
// Using a proxy to prevent the app accessing any globals it likes
return new Proxy({}, {
get: (_, p) => this.#dryGlobals[p],
set: (_, p) => false,
has: (_, p) => true,
})
}
#controlChannelWet() {
return {
evaluateCommonJsModule: this.#evaluateCommonJsModule.bind(this)
}
}
#evaluateCommonJsModule(moduleText, filename) {
const wrapped = `(function(globalThis){with(globalThis){return function(module,exports,require){${moduleText}}}})`;
const wrapper = globalThis.eval(wrapped)
const wetModule = { exports: {} };
const wetExports = wetModule.exports;
const require = undefined;
wrapper(this.#wetGlobals)(wetModule, wetExports, require);
Object.assign(wetExports, wetModule.exports);
return wetExports
}
#restoreFromSnapshot(snapshot) {
const json = snapshot.toString('utf-8')
const { ioJournal, exportList, ephemerals } = JSON.parse(json)
this.#ioJournal = ioJournal;
this.#mode = 'replay'
this.#journalReplayCursor = 0
while (this.#journalReplayCursor < this.#ioJournal.length) {
const entry = this.#ioJournal[this.#journalReplayCursor]
// The root loop is a set of actions to be performed against the sandbox.
// We don't support async/await in sandboxes yet so the sandbox cannot
// spontaneously call out to the host
entry.type === 'action-to-wet' || unexpected();
this.#sendToWet(entry.action)
}
// Local IDs on the wet side that existed before the snapshot and are now
// invalid.
for (const [id, type] of ephemerals) {
// If the ID is still alive, skip it. This applies to well-known objects
if (this.#dry.objectsByLocalId.has(id)) continue;
this.#dry.defineEphemeral(id, type)
}
this.#mode = 'live'
const captures = mapValues(exportList, v => {
// Resolve value inside app
const wet = this.#wet.deserialize(v)
// Move back across the boundary (message traveling the opposite direction)
const msg = this.#wet.serialize(wet)
const dry = this.#dry.deserialize(msg)
return dry
});
return captures;
}
#sendToWet(action) {
if (this.#mode === 'live') {
this.#ioJournalAppend({ type: 'action-to-wet', action })
const result = this.#wet.receiveAction(action);
this.#ioJournalAppend({ type: 'return-from-wet', result })
return result;
} else {
this.#expectNextJournalEntry({ type: 'action-to-wet', action })
const result = this.#wet.receiveAction(action)
this.#expectNextJournalEntry({ type: 'return-from-wet', result })
return result;
}
}
#sendToDry(action) {
if (this.#mode === 'live') {
this.#ioJournalAppend({ type: 'action-to-dry', action })
const result = this.#dry.receiveAction(action);
this.#ioJournalAppend({ type: 'return-from-dry', result })
return result;
} else {
this.#expectNextJournalEntry({ type: 'action-to-dry', action })
const entry = this.#nextJournalEntry()
entry.type === 'return-from-dry' || unexpected();
return entry.result
}
}
#ioJournalAppend(event) {
event.index = this.#ioJournal.length
this.#logJournalEvent(event)
this.#ioJournal.push(event)
}
#logJournalEvent(event) {
return;
if (event.type.startsWith('action')) {
console.log(this.#mode, event.index, event.type, event.action?.type, event.action?.target?.id, event.action)
} else if (event.type.startsWith('return')) {
console.log(this.#mode, event.index, event.type, event.result)
} else {
console.log(this.#mode, event.index, event.type, event)
}
}
#expectNextJournalEntry(expect) {
expect.index = this.#journalReplayCursor
// Hack because the journal has undefined values represented as missing.
expect = JSON.parse(JSON.stringify(expect))
const entry = this.#nextJournalEntry()
assert.deepEqual(expect, entry)
}
#nextJournalEntry() {
const entry = this.#ioJournal[this.#journalReplayCursor++] ?? unexpected()
this.#logJournalEvent(entry)
return entry;
}
}
/** A synchronous JavaScript membrane where all IO flows through `sendAction`
* and `receiveAction`, where actions are POD. */
class SerializingMembrane {
/**
* @param sendAction A callback when the membrane needs to send an action to
* the remote side
*/
constructor (sendAction, localDebugId, remoteDebugId) {
this.sendAction = sendAction
this.serializedByObject = new WeakMap() // In the form that we would send to the other side
this.objectsByLocalId = new Map()
this.objectsByRemoteId = new Map()
this.nextLocalId = 1
this.localDebugId = localDebugId
this.remoteDebugId = remoteDebugId
}
/**
* Handle an action sent by the other side
* @returns A serialized representation of the result
*/
receiveAction(action) {
switch (action.type) {
case 'apply': {
const target = this.deserialize(action.target);
const thisArg = this.deserialize(action.thisArg);
const args = action.args.map(arg => this.deserialize(arg));
const result = Reflect.apply(target, thisArg, args);
return this.serialize(result);
}
case 'get': {
const target = this.deserialize(action.target);
const receiver = this.deserialize(action.receiver);
const propertyKey = this.deserialize(action.propertyKey);
const result = Reflect.get(target, propertyKey, receiver);
return this.serialize(result);
}
case 'has': {
const target = this.deserialize(action.target);
const propertyKey = this.deserialize(action.propertyKey);
const result = Reflect.has(target, propertyKey);
return this.serialize(result);
}
case 'set': {
const target = this.deserialize(action.target);
const receiver = this.deserialize(action.receiver);
const value = this.deserialize(action.value);
const propertyKey = this.deserialize(action.propertyKey);
const result = Reflect.set(target, propertyKey, value, receiver);
return this.serialize(result);
}
default: unexpected()
}
}
defineWellKnownLocal(id, localObj) {
this.objectsByLocalId.set(id, localObj)
// Cache the serialized form of how this would be sent to the other side
this.serializedByObject.set(localObj, {
type: 'src-obj',
objectType: 'object',
id
})
}
// Register a local ID for an object that no longer exists on this host (it
// may have existed before the snapshot)
defineEphemeral(id, type) {
if (type === 'function') {
this.objectsByLocalId.set(id, revokedFunctionProxy)
} else {
this.objectsByLocalId.set(id, revokedObjectProxy)
}
}
defineWellKnownRemote(id) {
return this.#proxyRemoteObject(id, 'object')
}
serialize(value) {
switch (typeof value) {
case 'undefined':
case 'boolean':
case 'number':
case 'string':
return { type: 'literal', value };
case 'function':
case 'object': {
if (value === null) return { type: 'literal', value }
// Note: Local objects will be in this Map if they've been sent
// previously. Remote objects will always be in this Map because we map
// them when we receive them, so if control passes these 2 lines then
// it's definitely a local object.
let serialized = this.serializedByObject.get(value)
if (serialized) return serialized;
const id = this.nextLocalId++
const type = 'src-obj'
const objectType = typeof value // function or object
serialized = { type, id, objectType };
this.serializedByObject.set(value, serialized)
this.objectsByLocalId.set(id, value)
return serialized
}
case 'symbol': {
switch (value) {
case Symbol.asyncIterator: return { type: 'well-known-symbol', name: 'asyncIterator' }
case Symbol.hasInstance: return { type: 'well-known-symbol', name: 'hasInstance' }
case Symbol.isConcatSpreadable: return { type: 'well-known-symbol', name: 'isConcatSpreadable' }
case Symbol.iterator: return { type: 'well-known-symbol', name: 'iterator' }
case Symbol.match: return { type: 'well-known-symbol', name: 'match' }
case Symbol.matchAll: return { type: 'well-known-symbol', name: 'matchAll' }
case Symbol.replace: return { type: 'well-known-symbol', name: 'replace' }
case Symbol.search: return { type: 'well-known-symbol', name: 'search' }
case Symbol.species: return { type: 'well-known-symbol', name: 'species' }
case Symbol.split: return { type: 'well-known-symbol', name: 'split' }
case Symbol.toPrimitive: return { type: 'well-known-symbol', name: 'toPrimitive' }
case Symbol.toStringTag: return { type: 'well-known-symbol', name: 'toStringTag' }
case Symbol.unscopables: return { type: 'well-known-symbol', name: 'unscopables' }
default:
unexpected(); // Not implemented yet: user-defined symbols
}
}
default: unexpected()
}
}
deserialize(value) {
switch (value.type) {
case 'literal': return value.value;
case 'src-obj': {
const remoteId = value.id;
if (this.objectsByRemoteId.has(remoteId)) {
return this.objectsByRemoteId.get(remoteId)
}
return this.#proxyRemoteObject(remoteId, value.objectType)
}
case 'dst-obj': {
return this.objectsByLocalId.get(value.id) ?? unexpected();
}
case 'well-known-symbol': {
switch (value.name) {
case 'asyncIterator': return Symbol.asyncIterator
case 'hasInstance': return Symbol.hasInstance
case 'isConcatSpreadable': return Symbol.isConcatSpreadable
case 'iterator': return Symbol.iterator
case 'match': return Symbol.match
case 'matchAll': return Symbol.matchAll
case 'replace': return Symbol.replace
case 'search': return Symbol.search
case 'species': return Symbol.species
case 'split': return Symbol.split
case 'toPrimitive': return Symbol.toPrimitive
case 'toStringTag': return Symbol.toStringTag
case 'unscopables': return Symbol.unscopables
default:
unexpected(); // Not implemented yet: user-defined symbols
}
}
default:
throw new Error('Not implemented')
}
}
#proxyRemoteObject(remoteId, objectType) {
const target = objectType === 'function' ? () => {} : {};
target.debugId = `${this.remoteDebugId}:${remoteId}`;
const obj = new Proxy(target, {
get: (_target, propertyKey, receiver) => {
// Remote object accessed from local side
const result = this.sendAction({
type: 'get',
target: this.serialize(obj),
propertyKey: this.serialize(propertyKey),
receiver: this.serialize(receiver)
})
return this.deserialize(result)
},
has: (_target, propertyKey) => {
// Remote object accessed from local side
const result = this.sendAction({
type: 'has',
target: this.serialize(obj),
propertyKey: this.serialize(propertyKey)
})
return this.deserialize(result)
},
set: (_target, propertyKey, value, receiver) => {
// Remote object accessed from local side
const result = this.sendAction({
type: 'set',
target: this.serialize(obj),
propertyKey: this.serialize(propertyKey),
value: this.serialize(value),
receiver: this.serialize(receiver)
})
return this.deserialize(result)
},
apply: (_target, thisArg, args) => {
// Remote function called from local side
const result = this.sendAction({
type: 'apply',
target: this.serialize(obj),
thisArg: this.serialize(thisArg),
args: args.map(arg => this.serialize(arg))
})
return this.deserialize(result)
}
})
this.objectsByRemoteId.set(remoteId, obj)
this.serializedByObject.set(obj, {
type: 'dst-obj',
objectType,
id: remoteId
})
return obj;
}
}
const revokedObjectProxy = (() => {
const { proxy, revoke } = Proxy.revocable({}, {});
revoke();
return proxy;
})();
const revokedFunctionProxy = (() => {
const { proxy, revoke } = Proxy.revocable(() => {}, {});
revoke();
return proxy;
})();