ses 0.14.2 → 0.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +64 -0
- package/NEWS.md +60 -1
- package/README.md +0 -1
- package/dist/lockdown.cjs +1769 -992
- package/dist/lockdown.mjs +1769 -992
- package/dist/lockdown.umd.js +1769 -992
- package/dist/lockdown.umd.min.js +1 -1
- package/dist/ses.cjs +1769 -992
- package/dist/ses.mjs +1769 -992
- package/dist/ses.umd.js +1769 -992
- package/dist/ses.umd.min.js +1 -1
- package/index.d.ts +2 -1
- package/index.js +6 -13
- package/package.json +24 -11
- package/src/commons.js +44 -9
- package/src/compartment-evaluate.js +102 -0
- package/src/compartment-shim.js +78 -106
- package/src/enable-property-overrides.js +5 -4
- package/src/enablements.js +7 -2
- package/src/environment-options.js +125 -0
- package/src/error/assert.js +51 -39
- package/src/error/console.js +32 -14
- package/src/error/fatal-assert.js +2 -0
- package/src/error/stringify-utils.js +26 -3
- package/src/error/tame-console.js +12 -4
- package/src/error/tame-error-constructor.js +18 -17
- package/src/error/tame-v8-error-constructor.js +30 -14
- package/src/get-anonymous-intrinsics.js +11 -8
- package/src/get-source-url.js +4 -4
- package/src/global-object.js +33 -26
- package/src/intrinsics.js +76 -67
- package/src/lockdown-shim.js +111 -119
- package/src/make-eval-function.js +3 -5
- package/src/make-function-constructor.js +3 -4
- package/src/make-safe-evaluator.js +117 -0
- package/src/module-instance.js +31 -17
- package/src/module-link.js +11 -4
- package/src/module-load.js +169 -44
- package/src/module-proxy.js +12 -4
- package/src/scope-constants.js +6 -3
- package/src/scope-handler.js +41 -17
- package/src/tame-date-constructor.js +8 -2
- package/src/tame-domains.js +46 -0
- package/src/tame-function-constructors.js +1 -0
- package/src/tame-locale-methods.js +6 -6
- package/src/tame-math-object.js +2 -2
- package/src/tame-regexp-constructor.js +7 -7
- package/src/transforms.js +13 -6
- package/src/whitelist-intrinsics.js +9 -3
- package/src/evaluate.js +0 -98
package/src/lockdown-shim.js
CHANGED
|
@@ -14,7 +14,16 @@
|
|
|
14
14
|
|
|
15
15
|
// @ts-check
|
|
16
16
|
|
|
17
|
-
import {
|
|
17
|
+
import {
|
|
18
|
+
TypeError,
|
|
19
|
+
arrayFilter,
|
|
20
|
+
arrayMap,
|
|
21
|
+
globalThis,
|
|
22
|
+
is,
|
|
23
|
+
ownKeys,
|
|
24
|
+
stringSplit,
|
|
25
|
+
} from './commons.js';
|
|
26
|
+
import { enJoin } from './error/stringify-utils.js';
|
|
18
27
|
import { makeHardener } from './make-hardener.js';
|
|
19
28
|
import { makeIntrinsicsCollector } from './intrinsics.js';
|
|
20
29
|
import whitelistIntrinsics from './whitelist-intrinsics.js';
|
|
@@ -24,19 +33,28 @@ import tameMathObject from './tame-math-object.js';
|
|
|
24
33
|
import tameRegExpConstructor from './tame-regexp-constructor.js';
|
|
25
34
|
import enablePropertyOverrides from './enable-property-overrides.js';
|
|
26
35
|
import tameLocaleMethods from './tame-locale-methods.js';
|
|
27
|
-
import {
|
|
36
|
+
import {
|
|
37
|
+
setGlobalObjectConstantProperties,
|
|
38
|
+
setGlobalObjectMutableProperties,
|
|
39
|
+
} from './global-object.js';
|
|
40
|
+
import { makeSafeEvaluator } from './make-safe-evaluator.js';
|
|
28
41
|
import { initialGlobalPropertyNames } from './whitelist.js';
|
|
29
42
|
import { tameFunctionToString } from './tame-function-tostring.js';
|
|
43
|
+
import { tameDomains } from './tame-domains.js';
|
|
30
44
|
|
|
31
45
|
import { tameConsole } from './error/tame-console.js';
|
|
32
46
|
import tameErrorConstructor from './error/tame-error-constructor.js';
|
|
33
47
|
import { assert, makeAssert } from './error/assert.js';
|
|
48
|
+
import { makeEnvironmentCaptor } from './environment-options.js';
|
|
49
|
+
import { getAnonymousIntrinsics } from './get-anonymous-intrinsics.js';
|
|
50
|
+
import { makeCompartmentConstructor } from './compartment-shim.js';
|
|
34
51
|
|
|
35
52
|
/** @typedef {import('../index.js').LockdownOptions} LockdownOptions */
|
|
36
53
|
|
|
37
54
|
const { details: d, quote: q } = assert;
|
|
38
55
|
|
|
39
|
-
|
|
56
|
+
/** @type {Error=} */
|
|
57
|
+
let priorLockdown;
|
|
40
58
|
|
|
41
59
|
// Build a harden() with an empty fringe.
|
|
42
60
|
// Gate it on lockdown.
|
|
@@ -47,8 +65,6 @@ let firstOptions;
|
|
|
47
65
|
*/
|
|
48
66
|
const harden = makeHardener();
|
|
49
67
|
|
|
50
|
-
const alreadyHardenedIntrinsics = () => false;
|
|
51
|
-
|
|
52
68
|
/**
|
|
53
69
|
* @callback Transform
|
|
54
70
|
* @param {string} source
|
|
@@ -65,44 +81,17 @@ const alreadyHardenedIntrinsics = () => false;
|
|
|
65
81
|
* @param {Object} [options.globalLexicals]
|
|
66
82
|
*/
|
|
67
83
|
|
|
68
|
-
/**
|
|
69
|
-
* @callback CompartmentConstructorMaker
|
|
70
|
-
* @param {CompartmentConstructorMaker} targetMakeCompartmentConstructor
|
|
71
|
-
* @param {Object} intrinsics
|
|
72
|
-
* @param {(func: Function) => void} markVirtualizedNativeFunction
|
|
73
|
-
* @returns {CompartmentConstructor}
|
|
74
|
-
*/
|
|
75
|
-
|
|
76
84
|
// TODO https://github.com/endojs/endo/issues/814
|
|
77
85
|
// Lockdown currently allows multiple calls provided that the specified options
|
|
78
86
|
// of every call agree. With experience, we have observed that lockdown should
|
|
79
87
|
// only ever need to be called once and that simplifying lockdown will improve
|
|
80
88
|
// the quality of audits.
|
|
81
89
|
|
|
82
|
-
// TODO https://github.com/endojs/endo/issues/815
|
|
83
|
-
// Lockdown receives makeCompartmentConstructor and compartmentPrototype.
|
|
84
|
-
// This is a vestige of an earlier version of SES where makeLockdown was called
|
|
85
|
-
// from two different entry points: one for a layer of SES that had a
|
|
86
|
-
// Compartment that only supported evaluating programs, and a second layer that
|
|
87
|
-
// extended Compartment to support modules, but at the expense of entraining a
|
|
88
|
-
// dependency on Babel.
|
|
89
|
-
// SES currently externalizes the dependency on Babel and one version of
|
|
90
|
-
// makeCompartmentConstructor is sufficient for all uses, so this can be
|
|
91
|
-
// simplified.
|
|
92
|
-
|
|
93
90
|
/**
|
|
94
|
-
* @param {CompartmentConstructorMaker} makeCompartmentConstructor
|
|
95
|
-
* @param {Object} compartmentPrototype
|
|
96
|
-
* @param {() => Object} getAnonymousIntrinsics
|
|
97
91
|
* @param {LockdownOptions} [options]
|
|
98
|
-
* @returns {() =>
|
|
92
|
+
* @returns {() => void} repairIntrinsics
|
|
99
93
|
*/
|
|
100
|
-
export const repairIntrinsics = (
|
|
101
|
-
makeCompartmentConstructor,
|
|
102
|
-
compartmentPrototype,
|
|
103
|
-
getAnonymousIntrinsics,
|
|
104
|
-
options = {},
|
|
105
|
-
) => {
|
|
94
|
+
export const repairIntrinsics = (options = {}) => {
|
|
106
95
|
// First time, absent options default to 'safe'.
|
|
107
96
|
// Subsequent times, absent options default to first options.
|
|
108
97
|
// Thus, all present options must agree with first options.
|
|
@@ -127,23 +116,46 @@ export const repairIntrinsics = (
|
|
|
127
116
|
// is useful. See
|
|
128
117
|
// [`stackFiltering` options](https://github.com/Agoric/SES-shim/blob/master/packages/ses/lockdown-options.md#stackfiltering-options)
|
|
129
118
|
// for an explanation.
|
|
130
|
-
|
|
119
|
+
|
|
120
|
+
const {
|
|
121
|
+
getEnvironmentOption: getenv,
|
|
122
|
+
getCapturedEnvironmentOptionNames,
|
|
123
|
+
} = makeEnvironmentCaptor(globalThis);
|
|
124
|
+
|
|
131
125
|
const {
|
|
126
|
+
errorTaming = getenv('LOCKDOWN_ERROR_TAMING', 'safe'),
|
|
127
|
+
errorTrapping = getenv('LOCKDOWN_ERROR_TRAPPING', 'platform'),
|
|
128
|
+
regExpTaming = getenv('LOCKDOWN_REGEXP_TAMING', 'safe'),
|
|
129
|
+
localeTaming = getenv('LOCKDOWN_LOCALE_TAMING', 'safe'),
|
|
130
|
+
consoleTaming = getenv('LOCKDOWN_CONSOLE_TAMING', 'safe'),
|
|
131
|
+
overrideTaming = getenv('LOCKDOWN_OVERRIDE_TAMING', 'moderate'),
|
|
132
|
+
stackFiltering = getenv('LOCKDOWN_STACK_FILTERING', 'concise'),
|
|
133
|
+
domainTaming = getenv('LOCKDOWN_DOMAIN_TAMING', 'safe'),
|
|
134
|
+
overrideDebug = arrayFilter(
|
|
135
|
+
stringSplit(getenv('LOCKDOWN_OVERRIDE_DEBUG', ''), ','),
|
|
136
|
+
/** @param {string} debugName */
|
|
137
|
+
debugName => debugName !== '',
|
|
138
|
+
),
|
|
139
|
+
__allowUnsafeMonkeyPatching__ = getenv(
|
|
140
|
+
'__LOCKDOWN_ALLOW_UNSAFE_MONKEY_PATCHING__',
|
|
141
|
+
'safe',
|
|
142
|
+
),
|
|
132
143
|
dateTaming = 'safe', // deprecated
|
|
133
|
-
errorTaming = 'safe',
|
|
134
144
|
mathTaming = 'safe', // deprecated
|
|
135
|
-
errorTrapping = 'platform',
|
|
136
|
-
regExpTaming = 'safe',
|
|
137
|
-
localeTaming = 'safe',
|
|
138
|
-
consoleTaming = 'safe',
|
|
139
|
-
overrideTaming = 'moderate',
|
|
140
|
-
overrideDebug = [],
|
|
141
|
-
stackFiltering = 'concise',
|
|
142
|
-
__allowUnsafeMonkeyPatching__ = 'safe',
|
|
143
|
-
|
|
144
145
|
...extraOptions
|
|
145
146
|
} = options;
|
|
146
147
|
|
|
148
|
+
const capturedEnvironmentOptionNames = getCapturedEnvironmentOptionNames();
|
|
149
|
+
if (capturedEnvironmentOptionNames.length > 0) {
|
|
150
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
151
|
+
console.warn(
|
|
152
|
+
`SES Lockdown using options from environment variables ${enJoin(
|
|
153
|
+
arrayMap(capturedEnvironmentOptionNames, q),
|
|
154
|
+
'and',
|
|
155
|
+
)}`,
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
147
159
|
// Assert that only supported options were passed.
|
|
148
160
|
// Use Reflect.ownKeys to reject symbol-named properties as well.
|
|
149
161
|
const extraOptionsNames = ownKeys(extraOptions);
|
|
@@ -152,35 +164,21 @@ export const repairIntrinsics = (
|
|
|
152
164
|
d`lockdown(): non supported option ${q(extraOptionsNames)}`,
|
|
153
165
|
);
|
|
154
166
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
firstOptions = {
|
|
167
|
-
dateTaming, // deprecated
|
|
168
|
-
errorTaming,
|
|
169
|
-
mathTaming, // deprecated
|
|
170
|
-
regExpTaming,
|
|
171
|
-
localeTaming,
|
|
172
|
-
consoleTaming,
|
|
173
|
-
overrideTaming,
|
|
174
|
-
overrideDebug,
|
|
175
|
-
stackFiltering,
|
|
176
|
-
__allowUnsafeMonkeyPatching__,
|
|
177
|
-
};
|
|
167
|
+
assert(
|
|
168
|
+
priorLockdown === undefined,
|
|
169
|
+
`Already locked down at ${priorLockdown} (SES_ALREADY_LOCKED_DOWN)`,
|
|
170
|
+
TypeError,
|
|
171
|
+
);
|
|
172
|
+
priorLockdown = new TypeError('Prior lockdown (SES_ALREADY_LOCKED_DOWN)');
|
|
173
|
+
// Tease V8 to generate the stack string and release the closures the stack
|
|
174
|
+
// trace retained:
|
|
175
|
+
priorLockdown.stack;
|
|
178
176
|
|
|
179
177
|
/**
|
|
180
178
|
* Because of packagers and bundlers, etc, multiple invocations of lockdown
|
|
181
179
|
* might happen in separate instantiations of the source of this module.
|
|
182
180
|
* In that case, each one sees its own `firstOptions` variable, so the test
|
|
183
|
-
* above will not detect that lockdown has already happened.
|
|
181
|
+
* above will not detect that lockdown has already happened. We
|
|
184
182
|
* unreliably test some telltale signs that lockdown has run, to avoid
|
|
185
183
|
* trying to lock down a locked down environment. Although the test is
|
|
186
184
|
* unreliable, this is consistent with the SES threat model. SES provides
|
|
@@ -191,12 +189,7 @@ export const repairIntrinsics = (
|
|
|
191
189
|
* tests without actually locking down counts as corrupting code.
|
|
192
190
|
*
|
|
193
191
|
* The specifics of what this tests for may change over time, but it
|
|
194
|
-
* should be consistent with any setting of the lockdown options.
|
|
195
|
-
* do no checking that the state is consistent with current lockdown
|
|
196
|
-
* options. So a call to lockdown with one set of options may silently
|
|
197
|
-
* succeed with a state not reflecting those options, but only
|
|
198
|
-
* if a previous lockdown happened from something other than this
|
|
199
|
-
* instance of this module.
|
|
192
|
+
* should be consistent with any setting of the lockdown options.
|
|
200
193
|
*/
|
|
201
194
|
const seemsToBeLockedDown = () => {
|
|
202
195
|
return (
|
|
@@ -206,36 +199,44 @@ export const repairIntrinsics = (
|
|
|
206
199
|
globalThis.Date.prototype.constructor !== globalThis.Date &&
|
|
207
200
|
typeof globalThis.Date.now === 'function' &&
|
|
208
201
|
// @ts-ignore
|
|
202
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
209
203
|
is(globalThis.Date.prototype.constructor.now(), NaN)
|
|
210
204
|
);
|
|
211
205
|
};
|
|
212
206
|
|
|
213
207
|
if (seemsToBeLockedDown()) {
|
|
214
|
-
|
|
215
|
-
|
|
208
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
209
|
+
throw new TypeError(
|
|
210
|
+
`Already locked down but not by this SES instance (SES_MULTIPLE_INSTANCES)`,
|
|
211
|
+
);
|
|
216
212
|
}
|
|
217
213
|
|
|
218
214
|
/**
|
|
219
215
|
* 1. TAME powers & gather intrinsics first.
|
|
220
216
|
*/
|
|
221
|
-
const intrinsicsCollector = makeIntrinsicsCollector();
|
|
222
217
|
|
|
223
|
-
|
|
218
|
+
tameDomains(domainTaming);
|
|
224
219
|
|
|
225
|
-
|
|
220
|
+
const {
|
|
221
|
+
addIntrinsics,
|
|
222
|
+
completePrototypes,
|
|
223
|
+
finalIntrinsics,
|
|
224
|
+
} = makeIntrinsicsCollector();
|
|
226
225
|
|
|
227
|
-
|
|
228
|
-
intrinsicsCollector.addIntrinsics(
|
|
229
|
-
tameErrorConstructor(errorTaming, stackFiltering),
|
|
230
|
-
);
|
|
231
|
-
intrinsicsCollector.addIntrinsics(tameMathObject(mathTaming));
|
|
232
|
-
intrinsicsCollector.addIntrinsics(tameRegExpConstructor(regExpTaming));
|
|
226
|
+
addIntrinsics({ harden });
|
|
233
227
|
|
|
234
|
-
|
|
228
|
+
addIntrinsics(tameFunctionConstructors());
|
|
235
229
|
|
|
236
|
-
|
|
230
|
+
addIntrinsics(tameDateConstructor(dateTaming));
|
|
231
|
+
addIntrinsics(tameErrorConstructor(errorTaming, stackFiltering));
|
|
232
|
+
addIntrinsics(tameMathObject(mathTaming));
|
|
233
|
+
addIntrinsics(tameRegExpConstructor(regExpTaming));
|
|
237
234
|
|
|
238
|
-
|
|
235
|
+
addIntrinsics(getAnonymousIntrinsics());
|
|
236
|
+
|
|
237
|
+
completePrototypes();
|
|
238
|
+
|
|
239
|
+
const intrinsics = finalIntrinsics();
|
|
239
240
|
|
|
240
241
|
// Wrap console unless suppressed.
|
|
241
242
|
// At the moment, the console is considered a host power in the start
|
|
@@ -246,6 +247,7 @@ export const repairIntrinsics = (
|
|
|
246
247
|
optGetStackString = intrinsics['%InitialGetStackString%'];
|
|
247
248
|
}
|
|
248
249
|
const consoleRecord = tameConsole(
|
|
250
|
+
// @ts-ignore tameConsole does its own input validation
|
|
249
251
|
consoleTaming,
|
|
250
252
|
errorTrapping,
|
|
251
253
|
optGetStackString,
|
|
@@ -279,19 +281,26 @@ export const repairIntrinsics = (
|
|
|
279
281
|
|
|
280
282
|
// Initialize the powerful initial global, i.e., the global of the
|
|
281
283
|
// start compartment, from the intrinsics.
|
|
282
|
-
|
|
283
|
-
|
|
284
|
+
|
|
285
|
+
setGlobalObjectConstantProperties(globalThis);
|
|
286
|
+
|
|
287
|
+
const { safeEvaluate } = makeSafeEvaluator({ globalObject: globalThis });
|
|
288
|
+
|
|
289
|
+
setGlobalObjectMutableProperties(globalThis, {
|
|
284
290
|
intrinsics,
|
|
285
|
-
initialGlobalPropertyNames,
|
|
291
|
+
newGlobalPropertyNames: initialGlobalPropertyNames,
|
|
286
292
|
makeCompartmentConstructor,
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
},
|
|
291
|
-
);
|
|
293
|
+
safeEvaluate,
|
|
294
|
+
markVirtualizedNativeFunction,
|
|
295
|
+
});
|
|
292
296
|
|
|
293
297
|
/**
|
|
294
298
|
* 3. HARDEN to share the intrinsics.
|
|
299
|
+
*
|
|
300
|
+
* We define hardenIntrinsics here so that options are in scope, but return
|
|
301
|
+
* it to the caller because we intend to eventually allow vetted shims to run
|
|
302
|
+
* between repairs and the hardening of intrinsics and so we can benchmark
|
|
303
|
+
* repair separately from hardening.
|
|
295
304
|
*/
|
|
296
305
|
|
|
297
306
|
function hardenIntrinsics() {
|
|
@@ -299,6 +308,7 @@ export const repairIntrinsics = (
|
|
|
299
308
|
// TODO consider moving this to the end of the repair phase, and
|
|
300
309
|
// therefore before vetted shims rather than afterwards. It is not
|
|
301
310
|
// clear yet which is better.
|
|
311
|
+
// @ts-ignore enablePropertyOverrides does its own input validation
|
|
302
312
|
enablePropertyOverrides(intrinsics, overrideTaming, overrideDebug);
|
|
303
313
|
|
|
304
314
|
if (__allowUnsafeMonkeyPatching__ !== 'unsafe') {
|
|
@@ -324,27 +334,9 @@ export const repairIntrinsics = (
|
|
|
324
334
|
};
|
|
325
335
|
|
|
326
336
|
/**
|
|
327
|
-
* @param {
|
|
328
|
-
* @param {Object} compartmentPrototype
|
|
329
|
-
* @param {() => Object} getAnonymousIntrinsics
|
|
330
|
-
* @returns {import('../index.js').Lockdown}
|
|
337
|
+
* @param {LockdownOptions} [options]
|
|
331
338
|
*/
|
|
332
|
-
export const
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
getAnonymousIntrinsics,
|
|
336
|
-
) => {
|
|
337
|
-
/**
|
|
338
|
-
* @param {LockdownOptions} [options]
|
|
339
|
-
*/
|
|
340
|
-
const lockdown = (options = {}) => {
|
|
341
|
-
const maybeHardenIntrinsics = repairIntrinsics(
|
|
342
|
-
makeCompartmentConstructor,
|
|
343
|
-
compartmentPrototype,
|
|
344
|
-
getAnonymousIntrinsics,
|
|
345
|
-
options,
|
|
346
|
-
);
|
|
347
|
-
return maybeHardenIntrinsics();
|
|
348
|
-
};
|
|
349
|
-
return lockdown;
|
|
339
|
+
export const lockdown = (options = {}) => {
|
|
340
|
+
const hardenIntrinsics = repairIntrinsics(options);
|
|
341
|
+
hardenIntrinsics();
|
|
350
342
|
};
|
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import { performEval } from './evaluate.js';
|
|
2
|
-
|
|
3
1
|
/*
|
|
4
2
|
* makeEvalFunction()
|
|
5
3
|
* A safe version of the native eval function which relies on
|
|
6
|
-
* the safety of
|
|
4
|
+
* the safety of safeEvaluate for confinement.
|
|
7
5
|
*/
|
|
8
|
-
export const makeEvalFunction =
|
|
6
|
+
export const makeEvalFunction = safeEvaluate => {
|
|
9
7
|
// We use the the concise method syntax to create an eval without a
|
|
10
8
|
// [[Construct]] behavior (such that the invocation "new eval()" throws
|
|
11
9
|
// TypeError: eval is not a constructor"), but which still accepts a
|
|
@@ -19,7 +17,7 @@ export const makeEvalFunction = (globalObject, options = {}) => {
|
|
|
19
17
|
// rule. Track.
|
|
20
18
|
return source;
|
|
21
19
|
}
|
|
22
|
-
return
|
|
20
|
+
return safeEvaluate(source);
|
|
23
21
|
},
|
|
24
22
|
}.eval;
|
|
25
23
|
|
|
@@ -5,15 +5,14 @@ import {
|
|
|
5
5
|
defineProperties,
|
|
6
6
|
getPrototypeOf,
|
|
7
7
|
} from './commons.js';
|
|
8
|
-
import { performEval } from './evaluate.js';
|
|
9
8
|
import { assert } from './error/assert.js';
|
|
10
9
|
|
|
11
10
|
/*
|
|
12
11
|
* makeFunctionConstructor()
|
|
13
12
|
* A safe version of the native Function which relies on
|
|
14
|
-
* the safety of
|
|
13
|
+
* the safety of safeEvaluate for confinement.
|
|
15
14
|
*/
|
|
16
|
-
export const makeFunctionConstructor =
|
|
15
|
+
export const makeFunctionConstructor = safeEvaluate => {
|
|
17
16
|
// Define an unused parameter to ensure Function.length === 1
|
|
18
17
|
const newFunction = function Function(_body) {
|
|
19
18
|
// Sanitize all parameters at the entry point.
|
|
@@ -53,7 +52,7 @@ export const makeFunctionConstructor = (globaObject, options = {}) => {
|
|
|
53
52
|
// TODO: since we create an anonymous function, the 'this' value
|
|
54
53
|
// isn't bound to the global object as per specs, but set as undefined.
|
|
55
54
|
const src = `(function anonymous(${parameters}\n) {\n${bodyText}\n})`;
|
|
56
|
-
return
|
|
55
|
+
return safeEvaluate(src);
|
|
57
56
|
};
|
|
58
57
|
|
|
59
58
|
defineProperties(newFunction, {
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// Portions adapted from V8 - Copyright 2016 the V8 project authors.
|
|
2
|
+
// https://github.com/v8/v8/blob/master/src/builtins/builtins-function.cc
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
WeakSet,
|
|
6
|
+
apply,
|
|
7
|
+
immutableObject,
|
|
8
|
+
proxyRevocable,
|
|
9
|
+
weaksetAdd,
|
|
10
|
+
} from './commons.js';
|
|
11
|
+
import { getScopeConstants } from './scope-constants.js';
|
|
12
|
+
import { createScopeHandler } from './scope-handler.js';
|
|
13
|
+
import { applyTransforms, mandatoryTransforms } from './transforms.js';
|
|
14
|
+
import { makeEvaluateFactory } from './make-evaluate-factory.js';
|
|
15
|
+
import { assert } from './error/assert.js';
|
|
16
|
+
|
|
17
|
+
const { details: d } = assert;
|
|
18
|
+
|
|
19
|
+
// TODO: rename localObject to scopeObject
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* makeSafeEvaluator()
|
|
23
|
+
* Build the low-level operation used by all evaluators:
|
|
24
|
+
* eval(), Function(), Compartment.prototype.evaluate().
|
|
25
|
+
*
|
|
26
|
+
* @param {Object} options
|
|
27
|
+
* @param {Object} options.globalObject
|
|
28
|
+
* @param {Object} [options.localObject]
|
|
29
|
+
* @param {Array<Transform>} [options.globalTransforms]
|
|
30
|
+
* @param {bool} [options.sloppyGlobalsMode]
|
|
31
|
+
* @param {WeakSet} [options.knownScopeProxies]
|
|
32
|
+
*/
|
|
33
|
+
export const makeSafeEvaluator = ({
|
|
34
|
+
globalObject,
|
|
35
|
+
localObject = {},
|
|
36
|
+
globalTransforms = [],
|
|
37
|
+
sloppyGlobalsMode = false,
|
|
38
|
+
knownScopeProxies = new WeakSet(),
|
|
39
|
+
} = {}) => {
|
|
40
|
+
const {
|
|
41
|
+
scopeHandler,
|
|
42
|
+
admitOneUnsafeEvalNext,
|
|
43
|
+
resetOneUnsafeEvalNext,
|
|
44
|
+
} = createScopeHandler(globalObject, localObject, {
|
|
45
|
+
sloppyGlobalsMode,
|
|
46
|
+
});
|
|
47
|
+
const { proxy: scopeProxy, revoke: revokeScopeProxy } = proxyRevocable(
|
|
48
|
+
immutableObject,
|
|
49
|
+
scopeHandler,
|
|
50
|
+
);
|
|
51
|
+
weaksetAdd(knownScopeProxies, scopeProxy);
|
|
52
|
+
|
|
53
|
+
// Defer creating the actual evaluator to first use.
|
|
54
|
+
// Creating a compartment should be possible in no-eval environments
|
|
55
|
+
// It also allows more global constants to be captured by the optimizer
|
|
56
|
+
let evaluate;
|
|
57
|
+
const makeEvaluate = () => {
|
|
58
|
+
if (!evaluate) {
|
|
59
|
+
const constants = getScopeConstants(globalObject, localObject);
|
|
60
|
+
const evaluateFactory = makeEvaluateFactory(constants);
|
|
61
|
+
evaluate = apply(evaluateFactory, scopeProxy, []);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @param {string} source
|
|
67
|
+
* @param {Object} [options]
|
|
68
|
+
* @param {Array<Transform>} [options.localTransforms]
|
|
69
|
+
*/
|
|
70
|
+
const safeEvaluate = (source, { localTransforms = [] } = {}) => {
|
|
71
|
+
makeEvaluate();
|
|
72
|
+
|
|
73
|
+
// Execute the mandatory transforms last to ensure that any rewritten code
|
|
74
|
+
// meets those mandatory requirements.
|
|
75
|
+
source = applyTransforms(source, [
|
|
76
|
+
...localTransforms,
|
|
77
|
+
...globalTransforms,
|
|
78
|
+
mandatoryTransforms,
|
|
79
|
+
]);
|
|
80
|
+
|
|
81
|
+
admitOneUnsafeEvalNext();
|
|
82
|
+
let err;
|
|
83
|
+
try {
|
|
84
|
+
// Ensure that "this" resolves to the safe global.
|
|
85
|
+
return apply(evaluate, globalObject, [source]);
|
|
86
|
+
} catch (e) {
|
|
87
|
+
// stash the child-code error in hopes of debugging the internal failure
|
|
88
|
+
err = e;
|
|
89
|
+
throw e;
|
|
90
|
+
} finally {
|
|
91
|
+
if (resetOneUnsafeEvalNext()) {
|
|
92
|
+
// Barring a defect in the SES shim, the scope proxy should allow the
|
|
93
|
+
// powerful, unsafe `eval` to be used by `evaluate` exactly once, as the
|
|
94
|
+
// very first name that it attempts to access from the lexical scope.
|
|
95
|
+
// A defect in the SES shim could throw an exception after our call to
|
|
96
|
+
// `admitOneUnsafeEvalNext()` and before `evaluate` calls `eval`
|
|
97
|
+
// internally.
|
|
98
|
+
// If we get here, SES is very broken.
|
|
99
|
+
// This condition is one where this vat is now hopelessly confused, and
|
|
100
|
+
// the vat as a whole should be aborted.
|
|
101
|
+
// No further code should run.
|
|
102
|
+
// All immediately reachable state should be abandoned.
|
|
103
|
+
// However, that is not yet possible, so we at least prevent further
|
|
104
|
+
// variable resolution via the scopeHandler, and throw an error with
|
|
105
|
+
// diagnostic info including the thrown error if any from evaluating the
|
|
106
|
+
// source code.
|
|
107
|
+
revokeScopeProxy();
|
|
108
|
+
// TODO A GOOD PLACE TO PANIC(), i.e., kill the vat incarnation.
|
|
109
|
+
// See https://github.com/Agoric/SES-shim/issues/490
|
|
110
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
111
|
+
assert.fail(d`handler did not reset allowNextEvalToBeUnsafe ${err}`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
return { safeEvaluate };
|
|
117
|
+
};
|
package/src/module-instance.js
CHANGED
|
@@ -1,17 +1,24 @@
|
|
|
1
1
|
import { assert } from './error/assert.js';
|
|
2
2
|
import { getDeferredExports } from './module-proxy.js';
|
|
3
3
|
import {
|
|
4
|
-
Error,
|
|
5
4
|
ReferenceError,
|
|
6
5
|
SyntaxError,
|
|
7
6
|
TypeError,
|
|
7
|
+
arrayForEach,
|
|
8
|
+
arrayIncludes,
|
|
9
|
+
arrayPush,
|
|
10
|
+
arraySome,
|
|
11
|
+
arraySort,
|
|
8
12
|
create,
|
|
9
13
|
defineProperty,
|
|
10
14
|
entries,
|
|
11
15
|
freeze,
|
|
12
16
|
isArray,
|
|
13
17
|
keys,
|
|
18
|
+
mapGet,
|
|
19
|
+
weakmapGet,
|
|
14
20
|
} from './commons.js';
|
|
21
|
+
import { compartmentEvaluate } from './compartment-evaluate.js';
|
|
15
22
|
|
|
16
23
|
const { quote: q } = assert;
|
|
17
24
|
|
|
@@ -25,7 +32,7 @@ export const makeThirdPartyModuleInstance = (
|
|
|
25
32
|
) => {
|
|
26
33
|
const { exportsProxy, proxiedExports, activate } = getDeferredExports(
|
|
27
34
|
compartment,
|
|
28
|
-
compartmentPrivateFields
|
|
35
|
+
weakmapGet(compartmentPrivateFields, compartment),
|
|
29
36
|
moduleAliases,
|
|
30
37
|
moduleSpecifier,
|
|
31
38
|
);
|
|
@@ -35,13 +42,13 @@ export const makeThirdPartyModuleInstance = (
|
|
|
35
42
|
if (staticModuleRecord.exports) {
|
|
36
43
|
if (
|
|
37
44
|
!isArray(staticModuleRecord.exports) ||
|
|
38
|
-
staticModuleRecord.exports
|
|
45
|
+
arraySome(staticModuleRecord.exports, name => typeof name !== 'string')
|
|
39
46
|
) {
|
|
40
47
|
throw new TypeError(
|
|
41
48
|
`SES third-party static module record "exports" property must be an array of strings for module ${moduleSpecifier}`,
|
|
42
49
|
);
|
|
43
50
|
}
|
|
44
|
-
staticModuleRecord.exports
|
|
51
|
+
arrayForEach(staticModuleRecord.exports, name => {
|
|
45
52
|
let value = proxiedExports[name];
|
|
46
53
|
const updaters = [];
|
|
47
54
|
|
|
@@ -62,7 +69,7 @@ export const makeThirdPartyModuleInstance = (
|
|
|
62
69
|
});
|
|
63
70
|
|
|
64
71
|
notifiers[name] = update => {
|
|
65
|
-
updaters
|
|
72
|
+
arrayPush(updaters, update);
|
|
66
73
|
update(value);
|
|
67
74
|
};
|
|
68
75
|
});
|
|
@@ -76,6 +83,7 @@ export const makeThirdPartyModuleInstance = (
|
|
|
76
83
|
if (!activated) {
|
|
77
84
|
activate();
|
|
78
85
|
activated = true;
|
|
86
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
79
87
|
staticModuleRecord.execute(
|
|
80
88
|
proxiedExports,
|
|
81
89
|
compartment,
|
|
@@ -107,7 +115,7 @@ export const makeModuleInstance = (
|
|
|
107
115
|
__liveExportMap__: liveExportMap = {},
|
|
108
116
|
} = staticModuleRecord;
|
|
109
117
|
|
|
110
|
-
const compartmentFields = privateFields
|
|
118
|
+
const compartmentFields = weakmapGet(privateFields, compartment);
|
|
111
119
|
|
|
112
120
|
const { __shimTransforms__ } = compartmentFields;
|
|
113
121
|
|
|
@@ -143,7 +151,7 @@ export const makeModuleInstance = (
|
|
|
143
151
|
// be notified when this binding is initialized or updated.
|
|
144
152
|
const notifiers = create(null);
|
|
145
153
|
|
|
146
|
-
entries(fixedExportMap)
|
|
154
|
+
arrayForEach(entries(fixedExportMap), ([fixedExportName, [localName]]) => {
|
|
147
155
|
let fixedGetNotify = localGetNotify[localName];
|
|
148
156
|
if (!fixedGetNotify) {
|
|
149
157
|
// fixed binding state
|
|
@@ -166,7 +174,7 @@ export const makeModuleInstance = (
|
|
|
166
174
|
// init with initValue of a declared const binding, and return
|
|
167
175
|
// it.
|
|
168
176
|
if (!tdz) {
|
|
169
|
-
throw new
|
|
177
|
+
throw new TypeError(
|
|
170
178
|
`Internal: binding ${q(localName)} already initialized`,
|
|
171
179
|
);
|
|
172
180
|
}
|
|
@@ -188,7 +196,7 @@ export const makeModuleInstance = (
|
|
|
188
196
|
return;
|
|
189
197
|
}
|
|
190
198
|
if (tdz) {
|
|
191
|
-
optUpdaters
|
|
199
|
+
arrayPush(optUpdaters, updater);
|
|
192
200
|
} else {
|
|
193
201
|
updater(value);
|
|
194
202
|
}
|
|
@@ -213,7 +221,8 @@ export const makeModuleInstance = (
|
|
|
213
221
|
notifiers[fixedExportName] = fixedGetNotify.notify;
|
|
214
222
|
});
|
|
215
223
|
|
|
216
|
-
|
|
224
|
+
arrayForEach(
|
|
225
|
+
entries(liveExportMap),
|
|
217
226
|
([liveExportName, [localName, setProxyTrap]]) => {
|
|
218
227
|
let liveGetNotify = localGetNotify[localName];
|
|
219
228
|
if (!liveGetNotify) {
|
|
@@ -269,7 +278,7 @@ export const makeModuleInstance = (
|
|
|
269
278
|
// Prevent recursion.
|
|
270
279
|
return;
|
|
271
280
|
}
|
|
272
|
-
updaters
|
|
281
|
+
arrayPush(updaters, updater);
|
|
273
282
|
if (!tdz) {
|
|
274
283
|
updater(value);
|
|
275
284
|
}
|
|
@@ -327,7 +336,12 @@ export const makeModuleInstance = (
|
|
|
327
336
|
const candidateAll = create(null);
|
|
328
337
|
candidateAll.default = false;
|
|
329
338
|
for (const [specifier, importUpdaters] of updateRecord) {
|
|
330
|
-
const instance = importedInstances
|
|
339
|
+
const instance = mapGet(importedInstances, specifier);
|
|
340
|
+
// The module instance object is an internal literal, does not bind this,
|
|
341
|
+
// and never revealed outside the SES shim.
|
|
342
|
+
// There are two instantiation sites for instances and they are both in
|
|
343
|
+
// this module.
|
|
344
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
331
345
|
instance.execute(); // bottom up cycle tolerant
|
|
332
346
|
const { notifiers: importNotifiers } = instance;
|
|
333
347
|
for (const [importName, updaters] of importUpdaters) {
|
|
@@ -341,7 +355,7 @@ export const makeModuleInstance = (
|
|
|
341
355
|
importNotify(updater);
|
|
342
356
|
}
|
|
343
357
|
}
|
|
344
|
-
if (exportAlls
|
|
358
|
+
if (arrayIncludes(exportAlls, specifier)) {
|
|
345
359
|
// Make all these imports candidates.
|
|
346
360
|
for (const [importName, importNotify] of entries(importNotifiers)) {
|
|
347
361
|
if (candidateAll[importName] === undefined) {
|
|
@@ -382,15 +396,15 @@ export const makeModuleInstance = (
|
|
|
382
396
|
// since all properties of the exports namespace must be keyed by a string
|
|
383
397
|
// and the string must correspond to a valid identifier, sorting these
|
|
384
398
|
// properties works for this specific case.
|
|
385
|
-
keys(exportsProps)
|
|
386
|
-
|
|
387
|
-
|
|
399
|
+
arrayForEach(arraySort(keys(exportsProps)), k =>
|
|
400
|
+
defineProperty(proxiedExports, k, exportsProps[k]),
|
|
401
|
+
);
|
|
388
402
|
|
|
389
403
|
freeze(proxiedExports);
|
|
390
404
|
activate();
|
|
391
405
|
}
|
|
392
406
|
|
|
393
|
-
let optFunctor =
|
|
407
|
+
let optFunctor = compartmentEvaluate(compartmentFields, functorSource, {
|
|
394
408
|
globalObject: compartment.globalThis,
|
|
395
409
|
transforms: __shimTransforms__,
|
|
396
410
|
__moduleShimLexicals__: localLexicals,
|