ses 0.14.3 → 0.14.4
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 +16 -0
- package/NEWS.md +15 -1
- package/dist/lockdown.cjs +278 -191
- package/dist/lockdown.mjs +278 -191
- package/dist/lockdown.umd.js +278 -191
- package/dist/lockdown.umd.min.js +1 -1
- package/dist/ses.cjs +278 -191
- package/dist/ses.mjs +278 -191
- package/dist/ses.umd.js +278 -191
- package/dist/ses.umd.min.js +1 -1
- package/package.json +6 -6
- package/src/compartment-evaluate.js +58 -28
- package/src/compartment-shim.js +44 -27
- package/src/global-object.js +33 -26
- package/src/lockdown-shim.js +15 -9
- 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/scope-handler.js +13 -2
- package/src/evaluate.js +0 -102
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ses",
|
|
3
|
-
"version": "0.14.
|
|
3
|
+
"version": "0.14.4",
|
|
4
4
|
"description": "Hardened JavaScript for Fearless Cooperation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lockdown",
|
|
@@ -59,10 +59,10 @@
|
|
|
59
59
|
"test:platform-compatability": "node test/package/test.cjs"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
|
-
"@endo/compartment-mapper": "^0.5.
|
|
63
|
-
"@endo/eslint-config": "^0.3.
|
|
64
|
-
"@endo/static-module-record": "^0.6.
|
|
65
|
-
"@endo/test262-runner": "^0.1.
|
|
62
|
+
"@endo/compartment-mapper": "^0.5.4",
|
|
63
|
+
"@endo/eslint-config": "^0.3.16",
|
|
64
|
+
"@endo/static-module-record": "^0.6.4",
|
|
65
|
+
"@endo/test262-runner": "^0.1.10",
|
|
66
66
|
"ava": "^3.12.1",
|
|
67
67
|
"babel-eslint": "^10.0.3",
|
|
68
68
|
"c8": "^7.7.3",
|
|
@@ -179,5 +179,5 @@
|
|
|
179
179
|
],
|
|
180
180
|
"timeout": "2m"
|
|
181
181
|
},
|
|
182
|
-
"gitHead": "
|
|
182
|
+
"gitHead": "e92151ee140769c4de6d8e8533cf9abaac6781e9"
|
|
183
183
|
}
|
|
@@ -11,7 +11,59 @@ import {
|
|
|
11
11
|
evadeImportExpressionTest,
|
|
12
12
|
rejectSomeDirectEvalExpressions,
|
|
13
13
|
} from './transforms.js';
|
|
14
|
-
import {
|
|
14
|
+
import { makeSafeEvaluator } from './make-safe-evaluator.js';
|
|
15
|
+
|
|
16
|
+
export const provideCompartmentEvaluator = (compartmentFields, options) => {
|
|
17
|
+
const {
|
|
18
|
+
sloppyGlobalsMode = false,
|
|
19
|
+
__moduleShimLexicals__ = undefined,
|
|
20
|
+
} = options;
|
|
21
|
+
|
|
22
|
+
let safeEvaluate;
|
|
23
|
+
|
|
24
|
+
if (__moduleShimLexicals__ === undefined && !sloppyGlobalsMode) {
|
|
25
|
+
({ safeEvaluate } = compartmentFields);
|
|
26
|
+
} else {
|
|
27
|
+
// The scope proxy or global lexicals are different from the
|
|
28
|
+
// shared evaluator so we need to build a new one
|
|
29
|
+
|
|
30
|
+
let { globalTransforms } = compartmentFields;
|
|
31
|
+
const {
|
|
32
|
+
globalObject,
|
|
33
|
+
globalLexicals,
|
|
34
|
+
knownScopeProxies,
|
|
35
|
+
} = compartmentFields;
|
|
36
|
+
|
|
37
|
+
let localObject = globalLexicals;
|
|
38
|
+
if (__moduleShimLexicals__ !== undefined) {
|
|
39
|
+
// When using `evaluate` for ESM modules, as should only occur from the
|
|
40
|
+
// module-shim's module-instance.js, we do not reveal the SES-shim's
|
|
41
|
+
// module-to-program translation, as this is not standardizable behavior.
|
|
42
|
+
// However, the `localTransforms` will come from the `__shimTransforms__`
|
|
43
|
+
// Compartment option in this case, which is a non-standardizable escape
|
|
44
|
+
// hatch so programs designed specifically for the SES-shim
|
|
45
|
+
// implementation may opt-in to use the same transforms for `evaluate`
|
|
46
|
+
// and `import`, at the expense of being tightly coupled to SES-shim.
|
|
47
|
+
globalTransforms = undefined;
|
|
48
|
+
|
|
49
|
+
localObject = create(null, getOwnPropertyDescriptors(globalLexicals));
|
|
50
|
+
defineProperties(
|
|
51
|
+
localObject,
|
|
52
|
+
getOwnPropertyDescriptors(__moduleShimLexicals__),
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
({ safeEvaluate } = makeSafeEvaluator({
|
|
57
|
+
globalObject,
|
|
58
|
+
localObject,
|
|
59
|
+
globalTransforms,
|
|
60
|
+
sloppyGlobalsMode,
|
|
61
|
+
knownScopeProxies,
|
|
62
|
+
}));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return { safeEvaluate };
|
|
66
|
+
};
|
|
15
67
|
|
|
16
68
|
export const compartmentEvaluate = (compartmentFields, source, options) => {
|
|
17
69
|
// Perform this check first to avoid unecessary sanitizing.
|
|
@@ -24,8 +76,6 @@ export const compartmentEvaluate = (compartmentFields, source, options) => {
|
|
|
24
76
|
// Extract options, and shallow-clone transforms.
|
|
25
77
|
const {
|
|
26
78
|
transforms = [],
|
|
27
|
-
sloppyGlobalsMode = false,
|
|
28
|
-
__moduleShimLexicals__ = undefined,
|
|
29
79
|
__evadeHtmlCommentTest__ = false,
|
|
30
80
|
__evadeImportExpressionTest__ = false,
|
|
31
81
|
__rejectSomeDirectEvalExpressions__ = true, // Note default on
|
|
@@ -41,32 +91,12 @@ export const compartmentEvaluate = (compartmentFields, source, options) => {
|
|
|
41
91
|
arrayPush(localTransforms, rejectSomeDirectEvalExpressions);
|
|
42
92
|
}
|
|
43
93
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if (__moduleShimLexicals__ !== undefined) {
|
|
49
|
-
// When using `evaluate` for ESM modules, as should only occur from the
|
|
50
|
-
// module-shim's module-instance.js, we do not reveal the SES-shim's
|
|
51
|
-
// module-to-program translation, as this is not standardizable behavior.
|
|
52
|
-
// However, the `localTransforms` will come from the `__shimTransforms__`
|
|
53
|
-
// Compartment option in this case, which is a non-standardizable escape
|
|
54
|
-
// hatch so programs designed specifically for the SES-shim
|
|
55
|
-
// implementation may opt-in to use the same transforms for `evaluate`
|
|
56
|
-
// and `import`, at the expense of being tightly coupled to SES-shim.
|
|
57
|
-
globalTransforms = undefined;
|
|
58
|
-
|
|
59
|
-
localObject = create(null, getOwnPropertyDescriptors(globalLexicals));
|
|
60
|
-
defineProperties(
|
|
61
|
-
localObject,
|
|
62
|
-
getOwnPropertyDescriptors(__moduleShimLexicals__),
|
|
63
|
-
);
|
|
64
|
-
}
|
|
94
|
+
const { safeEvaluate } = provideCompartmentEvaluator(
|
|
95
|
+
compartmentFields,
|
|
96
|
+
options,
|
|
97
|
+
);
|
|
65
98
|
|
|
66
|
-
return
|
|
67
|
-
globalTransforms,
|
|
99
|
+
return safeEvaluate(source, {
|
|
68
100
|
localTransforms,
|
|
69
|
-
sloppyGlobalsMode,
|
|
70
|
-
knownScopeProxies,
|
|
71
101
|
});
|
|
72
102
|
};
|
package/src/compartment-shim.js
CHANGED
|
@@ -20,7 +20,10 @@ import {
|
|
|
20
20
|
weakmapSet,
|
|
21
21
|
weaksetHas,
|
|
22
22
|
} from './commons.js';
|
|
23
|
-
import {
|
|
23
|
+
import {
|
|
24
|
+
setGlobalObjectConstantProperties,
|
|
25
|
+
setGlobalObjectMutableProperties,
|
|
26
|
+
} from './global-object.js';
|
|
24
27
|
import { isValidIdentifierName } from './scope-constants.js';
|
|
25
28
|
import { sharedGlobalPropertyNames } from './whitelist.js';
|
|
26
29
|
import { load } from './module-load.js';
|
|
@@ -28,6 +31,7 @@ import { link } from './module-link.js';
|
|
|
28
31
|
import { getDeferredExports } from './module-proxy.js';
|
|
29
32
|
import { assert } from './error/assert.js';
|
|
30
33
|
import { compartmentEvaluate } from './compartment-evaluate.js';
|
|
34
|
+
import { makeSafeEvaluator } from './make-safe-evaluator.js';
|
|
31
35
|
|
|
32
36
|
const { quote: q } = assert;
|
|
33
37
|
|
|
@@ -206,7 +210,7 @@ export const makeCompartmentConstructor = (
|
|
|
206
210
|
name = '<unknown>',
|
|
207
211
|
transforms = [],
|
|
208
212
|
__shimTransforms__ = [],
|
|
209
|
-
globalLexicals = {},
|
|
213
|
+
globalLexicals: globalLexicalsOption = {},
|
|
210
214
|
resolveHook,
|
|
211
215
|
importHook,
|
|
212
216
|
moduleMapHook,
|
|
@@ -244,23 +248,8 @@ export const makeCompartmentConstructor = (
|
|
|
244
248
|
}
|
|
245
249
|
}
|
|
246
250
|
|
|
247
|
-
const globalObject = {};
|
|
248
|
-
initGlobalObject(
|
|
249
|
-
globalObject,
|
|
250
|
-
intrinsics,
|
|
251
|
-
sharedGlobalPropertyNames,
|
|
252
|
-
targetMakeCompartmentConstructor,
|
|
253
|
-
this.constructor.prototype,
|
|
254
|
-
{
|
|
255
|
-
globalTransforms,
|
|
256
|
-
markVirtualizedNativeFunction,
|
|
257
|
-
},
|
|
258
|
-
);
|
|
259
|
-
|
|
260
|
-
assign(globalObject, endowments);
|
|
261
|
-
|
|
262
251
|
const invalidNames = arrayFilter(
|
|
263
|
-
getOwnPropertyNames(
|
|
252
|
+
getOwnPropertyNames(globalLexicalsOption),
|
|
264
253
|
identifier => !isValidIdentifierName(identifier),
|
|
265
254
|
);
|
|
266
255
|
if (invalidNames.length) {
|
|
@@ -271,23 +260,51 @@ export const makeCompartmentConstructor = (
|
|
|
271
260
|
)}; these names would not be lexically mentionable`,
|
|
272
261
|
);
|
|
273
262
|
}
|
|
263
|
+
// The caller continues to own the globalLexicals object they passed to
|
|
264
|
+
// the compartment constructor, but the compartment only respects the
|
|
265
|
+
// original values and they are constants in the scope of evaluated
|
|
266
|
+
// programs and executed modules.
|
|
267
|
+
// This shallow copy captures only the values of enumerable own
|
|
268
|
+
// properties, erasing accessors.
|
|
269
|
+
// The snapshot is frozen to ensure that the properties are immutable
|
|
270
|
+
// when transferred-by-property-descriptor onto local scope objects.
|
|
271
|
+
const globalLexicals = freeze({ ...globalLexicalsOption });
|
|
272
|
+
|
|
273
|
+
const globalObject = {};
|
|
274
|
+
|
|
275
|
+
// We must initialize all constant properties first because
|
|
276
|
+
// `makeSafeEvaluator` may use them to create optimized bindings
|
|
277
|
+
// in the evaluator.
|
|
278
|
+
// TODO: consider merging into a single initialization if internal
|
|
279
|
+
// evaluator is no longer eagerly created
|
|
280
|
+
setGlobalObjectConstantProperties(globalObject);
|
|
274
281
|
|
|
275
282
|
const knownScopeProxies = new WeakSet();
|
|
283
|
+
const { safeEvaluate } = makeSafeEvaluator({
|
|
284
|
+
globalObject,
|
|
285
|
+
localObject: globalLexicals,
|
|
286
|
+
globalTransforms,
|
|
287
|
+
sloppyGlobalsMode: false,
|
|
288
|
+
knownScopeProxies,
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
setGlobalObjectMutableProperties(globalObject, {
|
|
292
|
+
intrinsics,
|
|
293
|
+
newGlobalPropertyNames: sharedGlobalPropertyNames,
|
|
294
|
+
makeCompartmentConstructor: targetMakeCompartmentConstructor,
|
|
295
|
+
safeEvaluate,
|
|
296
|
+
markVirtualizedNativeFunction,
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
assign(globalObject, endowments);
|
|
276
300
|
|
|
277
301
|
weakmapSet(privateFields, this, {
|
|
278
302
|
name,
|
|
279
303
|
globalTransforms,
|
|
280
304
|
globalObject,
|
|
281
305
|
knownScopeProxies,
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
// original values and they are constants in the scope of evaluated
|
|
285
|
-
// programs and executed modules.
|
|
286
|
-
// This shallow copy captures only the values of enumerable own
|
|
287
|
-
// properties, erasing accessors.
|
|
288
|
-
// The snapshot is frozen to ensure that the properties are immutable
|
|
289
|
-
// when transferred-by-property-descriptor onto local scope objects.
|
|
290
|
-
globalLexicals: freeze({ ...globalLexicals }),
|
|
306
|
+
globalLexicals,
|
|
307
|
+
safeEvaluate,
|
|
291
308
|
resolveHook,
|
|
292
309
|
importHook,
|
|
293
310
|
moduleMap,
|
package/src/global-object.js
CHANGED
|
@@ -4,29 +4,14 @@ import { makeFunctionConstructor } from './make-function-constructor.js';
|
|
|
4
4
|
import { constantProperties, universalPropertyNames } from './whitelist.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* (
|
|
10
|
-
* `
|
|
11
|
-
* `sharedGlobalPropertyNames`.
|
|
7
|
+
* setGlobalObjectConstantProperties()
|
|
8
|
+
* Initializes a new global object using a process similar to ECMA specifications
|
|
9
|
+
* (SetDefaultGlobalBindings). This process is split between this function and
|
|
10
|
+
* `setGlobalObjectMutableProperties`.
|
|
12
11
|
*
|
|
13
12
|
* @param {Object} globalObject
|
|
14
|
-
* @param {Object} intrinsics
|
|
15
|
-
* @param {Object} newGlobalPropertyNames
|
|
16
|
-
* @param {Function} makeCompartmentConstructor
|
|
17
|
-
* @param {Object} compartmentPrototype
|
|
18
|
-
* @param {Object} [options]
|
|
19
|
-
* @param {Array<Transform>} [options.globalTransforms]
|
|
20
|
-
* @param {(Object) => void} [options.markVirtualizedNativeFunction]
|
|
21
13
|
*/
|
|
22
|
-
export const
|
|
23
|
-
globalObject,
|
|
24
|
-
intrinsics,
|
|
25
|
-
newGlobalPropertyNames,
|
|
26
|
-
makeCompartmentConstructor,
|
|
27
|
-
compartmentPrototype,
|
|
28
|
-
{ globalTransforms, markVirtualizedNativeFunction },
|
|
29
|
-
) => {
|
|
14
|
+
export const setGlobalObjectConstantProperties = globalObject => {
|
|
30
15
|
for (const [name, constant] of entries(constantProperties)) {
|
|
31
16
|
defineProperty(globalObject, name, {
|
|
32
17
|
value: constant,
|
|
@@ -35,7 +20,33 @@ export const initGlobalObject = (
|
|
|
35
20
|
configurable: false,
|
|
36
21
|
});
|
|
37
22
|
}
|
|
23
|
+
};
|
|
38
24
|
|
|
25
|
+
/**
|
|
26
|
+
* setGlobalObjectMutableProperties()
|
|
27
|
+
* Create new global object using a process similar to ECMA specifications
|
|
28
|
+
* (portions of SetRealmGlobalObject and SetDefaultGlobalBindings).
|
|
29
|
+
* `newGlobalPropertyNames` should be either `initialGlobalPropertyNames` or
|
|
30
|
+
* `sharedGlobalPropertyNames`.
|
|
31
|
+
*
|
|
32
|
+
* @param {Object} globalObject
|
|
33
|
+
* @param {Object} param1
|
|
34
|
+
* @param {Object} param1.intrinsics
|
|
35
|
+
* @param {Object} param1.newGlobalPropertyNames
|
|
36
|
+
* @param {Function} param1.makeCompartmentConstructor
|
|
37
|
+
* @param {(string, Object?) => any} param1.safeEvaluate
|
|
38
|
+
* @param {(Object) => void} param1.markVirtualizedNativeFunction
|
|
39
|
+
*/
|
|
40
|
+
export const setGlobalObjectMutableProperties = (
|
|
41
|
+
globalObject,
|
|
42
|
+
{
|
|
43
|
+
intrinsics,
|
|
44
|
+
newGlobalPropertyNames,
|
|
45
|
+
makeCompartmentConstructor,
|
|
46
|
+
safeEvaluate,
|
|
47
|
+
markVirtualizedNativeFunction,
|
|
48
|
+
},
|
|
49
|
+
) => {
|
|
39
50
|
for (const [name, intrinsicName] of entries(universalPropertyNames)) {
|
|
40
51
|
if (objectHasOwnProperty(intrinsics, intrinsicName)) {
|
|
41
52
|
defineProperty(globalObject, name, {
|
|
@@ -60,12 +71,8 @@ export const initGlobalObject = (
|
|
|
60
71
|
|
|
61
72
|
const perCompartmentGlobals = {
|
|
62
73
|
globalThis: globalObject,
|
|
63
|
-
eval: makeEvalFunction(
|
|
64
|
-
|
|
65
|
-
}),
|
|
66
|
-
Function: makeFunctionConstructor(globalObject, {
|
|
67
|
-
globalTransforms,
|
|
68
|
-
}),
|
|
74
|
+
eval: makeEvalFunction(safeEvaluate),
|
|
75
|
+
Function: makeFunctionConstructor(safeEvaluate),
|
|
69
76
|
};
|
|
70
77
|
|
|
71
78
|
perCompartmentGlobals.Compartment = makeCompartmentConstructor(
|
package/src/lockdown-shim.js
CHANGED
|
@@ -24,7 +24,11 @@ import tameMathObject from './tame-math-object.js';
|
|
|
24
24
|
import tameRegExpConstructor from './tame-regexp-constructor.js';
|
|
25
25
|
import enablePropertyOverrides from './enable-property-overrides.js';
|
|
26
26
|
import tameLocaleMethods from './tame-locale-methods.js';
|
|
27
|
-
import {
|
|
27
|
+
import {
|
|
28
|
+
setGlobalObjectConstantProperties,
|
|
29
|
+
setGlobalObjectMutableProperties,
|
|
30
|
+
} from './global-object.js';
|
|
31
|
+
import { makeSafeEvaluator } from './make-safe-evaluator.js';
|
|
28
32
|
import { initialGlobalPropertyNames } from './whitelist.js';
|
|
29
33
|
import { tameFunctionToString } from './tame-function-tostring.js';
|
|
30
34
|
import { tameDomains } from './tame-domains.js';
|
|
@@ -289,16 +293,18 @@ export const repairIntrinsics = (
|
|
|
289
293
|
|
|
290
294
|
// Initialize the powerful initial global, i.e., the global of the
|
|
291
295
|
// start compartment, from the intrinsics.
|
|
292
|
-
|
|
293
|
-
|
|
296
|
+
|
|
297
|
+
setGlobalObjectConstantProperties(globalThis);
|
|
298
|
+
|
|
299
|
+
const { safeEvaluate } = makeSafeEvaluator({ globalObject: globalThis });
|
|
300
|
+
|
|
301
|
+
setGlobalObjectMutableProperties(globalThis, {
|
|
294
302
|
intrinsics,
|
|
295
|
-
initialGlobalPropertyNames,
|
|
303
|
+
newGlobalPropertyNames: initialGlobalPropertyNames,
|
|
296
304
|
makeCompartmentConstructor,
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
},
|
|
301
|
-
);
|
|
305
|
+
safeEvaluate,
|
|
306
|
+
markVirtualizedNativeFunction,
|
|
307
|
+
});
|
|
302
308
|
|
|
303
309
|
/**
|
|
304
310
|
* 3. HARDEN to share the intrinsics.
|
|
@@ -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/scope-handler.js
CHANGED
|
@@ -39,7 +39,7 @@ const alwaysThrowHandler = new Proxy(
|
|
|
39
39
|
/*
|
|
40
40
|
* createScopeHandler()
|
|
41
41
|
* ScopeHandler manages a Proxy which serves as the global scope for the
|
|
42
|
-
*
|
|
42
|
+
* safeEvaluate operation (the Proxy is the argument of a 'with' binding).
|
|
43
43
|
* As described in createSafeEvaluator(), it has several functions:
|
|
44
44
|
* - allow the very first (and only the very first) use of 'eval' to map to
|
|
45
45
|
* the real (unsafe) eval function, so it acts as a 'direct eval' and can
|
|
@@ -152,9 +152,20 @@ export const createScopeHandler = (
|
|
|
152
152
|
// at the expense of 'typeof' being wrong for those properties. For
|
|
153
153
|
// example, in the browser, evaluating 'document = 3', will add
|
|
154
154
|
// a property to globalObject instead of throwing a ReferenceError.
|
|
155
|
+
|
|
156
|
+
// !!!!! WARNING: DANGER ZONE !!!!!!
|
|
157
|
+
// The order of the conditions in the `||` expression below is of the
|
|
158
|
+
// utmost importance. Under no circumstances should `eval` be checked
|
|
159
|
+
// after `globalObject`. The prototype of the global object is under
|
|
160
|
+
// full control of user code and may be replaced by a proxy with a
|
|
161
|
+
// `has` trap. If we allow that trap to trigger while the
|
|
162
|
+
// `allowNextEvalToBeUnsafe` flag is down, it could allow user code
|
|
163
|
+
// to get a hold of `FERAL_EVAL`, resulting in a complete escape of
|
|
164
|
+
// the compartment.
|
|
165
|
+
// !!!!! WARNING: DANGER ZONE !!!!!!
|
|
155
166
|
return (
|
|
156
167
|
sloppyGlobalsMode ||
|
|
157
|
-
prop === 'eval' ||
|
|
168
|
+
(allowNextEvalToBeUnsafe && prop === 'eval') ||
|
|
158
169
|
prop in localObject ||
|
|
159
170
|
prop in globalObject ||
|
|
160
171
|
prop in globalThis
|
package/src/evaluate.js
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
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
|
-
/**
|
|
20
|
-
* performEval()
|
|
21
|
-
* The low-level operation used by all evaluators:
|
|
22
|
-
* eval(), Function(), Evalutator.prototype.evaluate().
|
|
23
|
-
*
|
|
24
|
-
* @param {string} source
|
|
25
|
-
* @param {Object} globalObject
|
|
26
|
-
* @param {Objeect} localObject
|
|
27
|
-
* @param {Object} [options]
|
|
28
|
-
* @param {Array<Transform>} [options.localTransforms]
|
|
29
|
-
* @param {Array<Transform>} [options.globalTransforms]
|
|
30
|
-
* @param {bool} [options.sloppyGlobalsMode]
|
|
31
|
-
* @param {WeakSet} [options.knownScopeProxies]
|
|
32
|
-
*/
|
|
33
|
-
export const performEval = (
|
|
34
|
-
source,
|
|
35
|
-
globalObject,
|
|
36
|
-
localObject = {},
|
|
37
|
-
{
|
|
38
|
-
localTransforms = [],
|
|
39
|
-
globalTransforms = [],
|
|
40
|
-
sloppyGlobalsMode = false,
|
|
41
|
-
knownScopeProxies = new WeakSet(),
|
|
42
|
-
} = {},
|
|
43
|
-
) => {
|
|
44
|
-
// Execute the mandatory transforms last to ensure that any rewritten code
|
|
45
|
-
// meets those mandatory requirements.
|
|
46
|
-
source = applyTransforms(source, [
|
|
47
|
-
...localTransforms,
|
|
48
|
-
...globalTransforms,
|
|
49
|
-
mandatoryTransforms,
|
|
50
|
-
]);
|
|
51
|
-
|
|
52
|
-
const {
|
|
53
|
-
scopeHandler,
|
|
54
|
-
admitOneUnsafeEvalNext,
|
|
55
|
-
resetOneUnsafeEvalNext,
|
|
56
|
-
} = createScopeHandler(globalObject, localObject, {
|
|
57
|
-
sloppyGlobalsMode,
|
|
58
|
-
});
|
|
59
|
-
const { proxy: scopeProxy, revoke: revokeScopeProxy } = proxyRevocable(
|
|
60
|
-
immutableObject,
|
|
61
|
-
scopeHandler,
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
const constants = getScopeConstants(globalObject, localObject);
|
|
65
|
-
const evaluateFactory = makeEvaluateFactory(constants);
|
|
66
|
-
const evaluate = apply(evaluateFactory, scopeProxy, []);
|
|
67
|
-
|
|
68
|
-
admitOneUnsafeEvalNext();
|
|
69
|
-
let err;
|
|
70
|
-
try {
|
|
71
|
-
// Ensure that "this" resolves to the safe global.
|
|
72
|
-
weaksetAdd(knownScopeProxies, scopeProxy);
|
|
73
|
-
return apply(evaluate, globalObject, [source]);
|
|
74
|
-
} catch (e) {
|
|
75
|
-
// stash the child-code error in hopes of debugging the internal failure
|
|
76
|
-
err = e;
|
|
77
|
-
throw e;
|
|
78
|
-
} finally {
|
|
79
|
-
if (resetOneUnsafeEvalNext()) {
|
|
80
|
-
// Barring a defect in the SES shim, the scope proxy should allow the
|
|
81
|
-
// powerful, unsafe `eval` to be used by `evaluate` exactly once, as the
|
|
82
|
-
// very first name that it attempts to access from the lexical scope.
|
|
83
|
-
// A defect in the SES shim could throw an exception after our call to
|
|
84
|
-
// `admitOneUnsafeEvalNext()` and before `evaluate` calls `eval`
|
|
85
|
-
// internally.
|
|
86
|
-
// If we get here, SES is very broken.
|
|
87
|
-
// This condition is one where this vat is now hopelessly confused, and
|
|
88
|
-
// the vat as a whole should be aborted.
|
|
89
|
-
// No further code should run.
|
|
90
|
-
// All immediately reachable state should be abandoned.
|
|
91
|
-
// However, that is not yet possible, so we at least prevent further
|
|
92
|
-
// variable resolution via the scopeHandler, and throw an error with
|
|
93
|
-
// diagnostic info including the thrown error if any from evaluating the
|
|
94
|
-
// source code.
|
|
95
|
-
revokeScopeProxy();
|
|
96
|
-
// TODO A GOOD PLACE TO PANIC(), i.e., kill the vat incarnation.
|
|
97
|
-
// See https://github.com/Agoric/SES-shim/issues/490
|
|
98
|
-
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
99
|
-
assert.fail(d`handler did not reset allowNextEvalToBeUnsafe ${err}`);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
};
|