ses 0.16.0 → 0.17.0
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 +27 -0
- package/NEWS.md +10 -0
- package/dist/lockdown.cjs +584 -452
- package/dist/lockdown.mjs +584 -452
- package/dist/lockdown.umd.js +584 -452
- package/dist/lockdown.umd.min.js +1 -1
- package/dist/ses.cjs +584 -452
- package/dist/ses.mjs +584 -452
- package/dist/ses.umd.js +584 -452
- package/dist/ses.umd.min.js +1 -1
- package/package.json +5 -5
- package/src/commons.js +1 -0
- package/src/compartment-evaluate.js +2 -7
- package/src/compartment-shim.js +3 -11
- package/src/eval-scope.js +90 -0
- package/src/global-object.js +40 -1
- package/src/make-evaluate.js +110 -0
- package/src/make-safe-evaluator.js +28 -35
- package/src/scope-constants.js +17 -13
- package/src/sloppy-globals-scope-terminator.js +53 -0
- package/src/strict-scope-terminator.js +91 -0
- package/src/whitelist.js +0 -1
- package/src/make-evaluate-factory.js +0 -71
- package/src/scope-handler.js +0 -211
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ses",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.0",
|
|
4
4
|
"description": "Hardened JavaScript for Fearless Cooperation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lockdown",
|
|
@@ -53,15 +53,15 @@
|
|
|
53
53
|
"lint-fix": "eslint --fix .",
|
|
54
54
|
"lint:js": "eslint .",
|
|
55
55
|
"lint:types": "tsc",
|
|
56
|
-
"
|
|
56
|
+
"prepare": "yarn run clean && yarn build",
|
|
57
57
|
"qt": "ava",
|
|
58
58
|
"test": "tsd && ava",
|
|
59
59
|
"test:platform-compatibility": "node test/package/test.cjs"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
|
-
"@endo/compartment-mapper": "^0.7.
|
|
62
|
+
"@endo/compartment-mapper": "^0.7.15",
|
|
63
63
|
"@endo/eslint-config": "^0.5.1",
|
|
64
|
-
"@endo/static-module-record": "^0.7.
|
|
64
|
+
"@endo/static-module-record": "^0.7.14",
|
|
65
65
|
"@endo/test262-runner": "^0.1.28",
|
|
66
66
|
"ava": "^3.12.1",
|
|
67
67
|
"babel-eslint": "^10.0.3",
|
|
@@ -179,5 +179,5 @@
|
|
|
179
179
|
],
|
|
180
180
|
"timeout": "2m"
|
|
181
181
|
},
|
|
182
|
-
"gitHead": "
|
|
182
|
+
"gitHead": "8fb324d8f13a0c6939dc0c1feb831f72298f1853"
|
|
183
183
|
}
|
package/src/commons.js
CHANGED
|
@@ -28,11 +28,7 @@ export const provideCompartmentEvaluator = (compartmentFields, options) => {
|
|
|
28
28
|
// shared evaluator so we need to build a new one
|
|
29
29
|
|
|
30
30
|
let { globalTransforms } = compartmentFields;
|
|
31
|
-
const {
|
|
32
|
-
globalObject,
|
|
33
|
-
globalLexicals,
|
|
34
|
-
knownScopeProxies,
|
|
35
|
-
} = compartmentFields;
|
|
31
|
+
const { globalObject, globalLexicals } = compartmentFields;
|
|
36
32
|
|
|
37
33
|
let localObject = globalLexicals;
|
|
38
34
|
if (__moduleShimLexicals__ !== undefined) {
|
|
@@ -58,7 +54,6 @@ export const provideCompartmentEvaluator = (compartmentFields, options) => {
|
|
|
58
54
|
globalLexicals: localObject,
|
|
59
55
|
globalTransforms,
|
|
60
56
|
sloppyGlobalsMode,
|
|
61
|
-
knownScopeProxies,
|
|
62
57
|
}));
|
|
63
58
|
}
|
|
64
59
|
|
|
@@ -66,7 +61,7 @@ export const provideCompartmentEvaluator = (compartmentFields, options) => {
|
|
|
66
61
|
};
|
|
67
62
|
|
|
68
63
|
export const compartmentEvaluate = (compartmentFields, source, options) => {
|
|
69
|
-
// Perform this check first to avoid
|
|
64
|
+
// Perform this check first to avoid unnecessary sanitizing.
|
|
70
65
|
// TODO Maybe relax string check and coerce instead:
|
|
71
66
|
// https://github.com/tc39/proposal-dynamic-code-brand-checks
|
|
72
67
|
if (typeof source !== 'string') {
|
package/src/compartment-shim.js
CHANGED
|
@@ -7,7 +7,6 @@ import {
|
|
|
7
7
|
ReferenceError,
|
|
8
8
|
TypeError,
|
|
9
9
|
WeakMap,
|
|
10
|
-
WeakSet,
|
|
11
10
|
arrayFilter,
|
|
12
11
|
arrayJoin,
|
|
13
12
|
assign,
|
|
@@ -18,9 +17,9 @@ import {
|
|
|
18
17
|
promiseThen,
|
|
19
18
|
weakmapGet,
|
|
20
19
|
weakmapSet,
|
|
21
|
-
weaksetHas,
|
|
22
20
|
} from './commons.js';
|
|
23
21
|
import {
|
|
22
|
+
setGlobalObjectSymbolUnscopables,
|
|
24
23
|
setGlobalObjectConstantProperties,
|
|
25
24
|
setGlobalObjectMutableProperties,
|
|
26
25
|
setGlobalObjectEvaluators,
|
|
@@ -119,12 +118,6 @@ export const CompartmentPrototype = {
|
|
|
119
118
|
return '[object Compartment]';
|
|
120
119
|
},
|
|
121
120
|
|
|
122
|
-
/* eslint-disable-next-line no-underscore-dangle */
|
|
123
|
-
__isKnownScopeProxy__(value) {
|
|
124
|
-
const { knownScopeProxies } = weakmapGet(privateFields, this);
|
|
125
|
-
return weaksetHas(knownScopeProxies, value);
|
|
126
|
-
},
|
|
127
|
-
|
|
128
121
|
module(specifier) {
|
|
129
122
|
if (typeof specifier !== 'string') {
|
|
130
123
|
throw new TypeError('first argument of module() must be a string');
|
|
@@ -277,6 +270,8 @@ export const makeCompartmentConstructor = (
|
|
|
277
270
|
|
|
278
271
|
const globalObject = {};
|
|
279
272
|
|
|
273
|
+
setGlobalObjectSymbolUnscopables(globalObject);
|
|
274
|
+
|
|
280
275
|
// We must initialize all constant properties first because
|
|
281
276
|
// `makeSafeEvaluator` may use them to create optimized bindings
|
|
282
277
|
// in the evaluator.
|
|
@@ -284,13 +279,11 @@ export const makeCompartmentConstructor = (
|
|
|
284
279
|
// evaluator is no longer eagerly created
|
|
285
280
|
setGlobalObjectConstantProperties(globalObject);
|
|
286
281
|
|
|
287
|
-
const knownScopeProxies = new WeakSet();
|
|
288
282
|
const { safeEvaluate } = makeSafeEvaluator({
|
|
289
283
|
globalObject,
|
|
290
284
|
globalLexicals,
|
|
291
285
|
globalTransforms,
|
|
292
286
|
sloppyGlobalsMode: false,
|
|
293
|
-
knownScopeProxies,
|
|
294
287
|
});
|
|
295
288
|
|
|
296
289
|
setGlobalObjectMutableProperties(globalObject, {
|
|
@@ -313,7 +306,6 @@ export const makeCompartmentConstructor = (
|
|
|
313
306
|
name: `${name}`,
|
|
314
307
|
globalTransforms,
|
|
315
308
|
globalObject,
|
|
316
|
-
knownScopeProxies,
|
|
317
309
|
globalLexicals,
|
|
318
310
|
safeEvaluate,
|
|
319
311
|
resolveHook,
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { FERAL_EVAL, create, defineProperties, freeze } from './commons.js';
|
|
2
|
+
|
|
3
|
+
import { assert } from './error/assert.js';
|
|
4
|
+
|
|
5
|
+
const { details: d } = assert;
|
|
6
|
+
|
|
7
|
+
// We attempt to frustrate stack bumping attacks on the safe evaluator
|
|
8
|
+
// (`make-safe-evaluator.js`).
|
|
9
|
+
// A stack bumping attack forces an API call to throw a stack overflow
|
|
10
|
+
// `RangeError` at an inopportune time.
|
|
11
|
+
// The attacker arranges for the stack to be sufficiently deep that the API
|
|
12
|
+
// consumes exactly enough stack frames to throw an exception.
|
|
13
|
+
//
|
|
14
|
+
// For the safe evaluator, an exception thrown between adding and then deleting
|
|
15
|
+
// `eval` on `evalScope` could leak the real `eval` to an attacker's lexical
|
|
16
|
+
// scope.
|
|
17
|
+
// This would be sufficiently disastrous that we guard against it twice.
|
|
18
|
+
// First, we delete `eval` from `evalScope` immediately before rendering it to
|
|
19
|
+
// the guest program's lexical scope.
|
|
20
|
+
//
|
|
21
|
+
// If the attacker manages to arrange for `eval` to throw an exception after we
|
|
22
|
+
// call `allowNextEvalToBeUnsafe` but before the guest program accesses `eval`,
|
|
23
|
+
// it would be able to access `eval` once more in its own code.
|
|
24
|
+
// Although they could do no harm with a direct `eval`, they would be able to
|
|
25
|
+
// escape to the true global scope with an indirect `eval`.
|
|
26
|
+
//
|
|
27
|
+
// prepareStack(depth, () => {
|
|
28
|
+
// (eval)('');
|
|
29
|
+
// });
|
|
30
|
+
// const unsafeEval = (eval);
|
|
31
|
+
// const safeEval = (eval);
|
|
32
|
+
// const realGlobal = unsafeEval('globalThis');
|
|
33
|
+
//
|
|
34
|
+
// To protect against that case, we also delete `eval` from the `evalScope` in
|
|
35
|
+
// a `finally` block surrounding the call to the safe evaluator.
|
|
36
|
+
// The only way to reach this case is if `eval` remains on `evalScope` due to
|
|
37
|
+
// an attack, so we assume that attack would have have invalided our isolation
|
|
38
|
+
// and revoke all future access to the evaluator.
|
|
39
|
+
//
|
|
40
|
+
// To defeat a stack bumping attack, we must use fewer stack frames to recover
|
|
41
|
+
// in that `finally` block than we used in the `try` block.
|
|
42
|
+
// We have no reliable guarantees about how many stack frames a block of
|
|
43
|
+
// JavaScript will consume.
|
|
44
|
+
// Function inlining, tail-call optimization, variations in the size of a stack
|
|
45
|
+
// frame, and block scopes may affect the depth of the stack.
|
|
46
|
+
// The only number of acceptable stack frames to use in the finally block is
|
|
47
|
+
// zero.
|
|
48
|
+
// We only use property assignment and deletion in the safe evaluator's
|
|
49
|
+
// `finally` block.
|
|
50
|
+
// We use `delete evalScope.eval` to withhold the evaluator.
|
|
51
|
+
// We assign an envelope object over `evalScopeKit.revoked` to revoke the
|
|
52
|
+
// evaluator.
|
|
53
|
+
//
|
|
54
|
+
// This is why we supply a meaningfully named function for
|
|
55
|
+
// `allowNextEvalToBeUnsafe` but do not provide a corresponding
|
|
56
|
+
// `revokeAccessToUnsafeEval` or even simply `revoke`.
|
|
57
|
+
// These recovery routines are expressed inline in the safe evaluator.
|
|
58
|
+
|
|
59
|
+
export const makeEvalScopeKit = () => {
|
|
60
|
+
const evalScope = create(null);
|
|
61
|
+
const oneTimeEvalProperties = freeze({
|
|
62
|
+
eval: {
|
|
63
|
+
get() {
|
|
64
|
+
delete evalScope.eval;
|
|
65
|
+
return FERAL_EVAL;
|
|
66
|
+
},
|
|
67
|
+
enumerable: false,
|
|
68
|
+
configurable: true,
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const evalScopeKit = {
|
|
73
|
+
evalScope,
|
|
74
|
+
allowNextEvalToBeUnsafe() {
|
|
75
|
+
if (evalScopeKit.revoked !== null) {
|
|
76
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
77
|
+
assert.fail(
|
|
78
|
+
d`a handler did not reset allowNextEvalToBeUnsafe ${this.revoked.err}`,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
// Allow next reference to eval produce the unsafe FERAL_EVAL.
|
|
82
|
+
// We avoid defineProperty because it consumes an extra stack frame taming
|
|
83
|
+
// its return value.
|
|
84
|
+
defineProperties(evalScope, oneTimeEvalProperties);
|
|
85
|
+
},
|
|
86
|
+
revoked: null,
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
return evalScopeKit;
|
|
90
|
+
};
|
package/src/global-object.js
CHANGED
|
@@ -1,8 +1,47 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
TypeError,
|
|
3
|
+
assign,
|
|
4
|
+
create,
|
|
5
|
+
defineProperty,
|
|
6
|
+
entries,
|
|
7
|
+
freeze,
|
|
8
|
+
objectHasOwnProperty,
|
|
9
|
+
unscopablesSymbol,
|
|
10
|
+
} from './commons.js';
|
|
2
11
|
import { makeEvalFunction } from './make-eval-function.js';
|
|
3
12
|
import { makeFunctionConstructor } from './make-function-constructor.js';
|
|
4
13
|
import { constantProperties, universalPropertyNames } from './whitelist.js';
|
|
5
14
|
|
|
15
|
+
/**
|
|
16
|
+
* The host's ordinary global object is not provided by a `with` block, so
|
|
17
|
+
* assigning to Symbol.unscopables has no effect.
|
|
18
|
+
* Since this shim uses `with` blocks to create a confined lexical scope for
|
|
19
|
+
* guest programs, we cannot emulate the proper behavior.
|
|
20
|
+
* With this shim, assigning Symbol.unscopables causes the given lexical
|
|
21
|
+
* names to fall through to the terminal scope proxy.
|
|
22
|
+
* But, we can install this setter to prevent a program from proceding on
|
|
23
|
+
* this false assumption.
|
|
24
|
+
*
|
|
25
|
+
* @param {Object} globalObject
|
|
26
|
+
*/
|
|
27
|
+
export const setGlobalObjectSymbolUnscopables = globalObject => {
|
|
28
|
+
defineProperty(
|
|
29
|
+
globalObject,
|
|
30
|
+
unscopablesSymbol,
|
|
31
|
+
freeze(
|
|
32
|
+
assign(create(null), {
|
|
33
|
+
set: freeze(() => {
|
|
34
|
+
throw new TypeError(
|
|
35
|
+
`Cannot set Symbol.unscopables of a Compartment's globalThis`,
|
|
36
|
+
);
|
|
37
|
+
}),
|
|
38
|
+
enumerable: false,
|
|
39
|
+
configurable: false,
|
|
40
|
+
}),
|
|
41
|
+
),
|
|
42
|
+
);
|
|
43
|
+
};
|
|
44
|
+
|
|
6
45
|
/**
|
|
7
46
|
* setGlobalObjectConstantProperties()
|
|
8
47
|
* Initializes a new global object using a process similar to ECMA specifications
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { FERAL_FUNCTION, arrayJoin, apply } from './commons.js';
|
|
4
|
+
import { getScopeConstants } from './scope-constants.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* buildOptimizer()
|
|
8
|
+
* Given an array of identifiers, the optimizer returns a `const` declaration
|
|
9
|
+
* destructuring `this.${name}`.
|
|
10
|
+
*
|
|
11
|
+
* @param {Array<string>} constants
|
|
12
|
+
* @param {string} name
|
|
13
|
+
*/
|
|
14
|
+
function buildOptimizer(constants, name) {
|
|
15
|
+
// No need to build an optimizer when there are no constants.
|
|
16
|
+
if (constants.length === 0) return '';
|
|
17
|
+
// Use 'this' to avoid going through the scope proxy, which is unnecessary
|
|
18
|
+
// since the optimizer only needs references to the safe global.
|
|
19
|
+
// Destructure the constants from the target scope object.
|
|
20
|
+
return `const {${arrayJoin(constants, ',')}} = this.${name};`;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* makeEvaluate()
|
|
25
|
+
* Create an 'evaluate' function with the correct optimizer inserted.
|
|
26
|
+
*
|
|
27
|
+
* @param {object} context
|
|
28
|
+
* @param {object} context.evalScope
|
|
29
|
+
* @param {object} context.globalLexicals
|
|
30
|
+
* @param {object} context.globalObject
|
|
31
|
+
* @param {object} context.scopeTerminator
|
|
32
|
+
*/
|
|
33
|
+
export const makeEvaluate = context => {
|
|
34
|
+
const { globalObjectConstants, globalLexicalConstants } = getScopeConstants(
|
|
35
|
+
context.globalObject,
|
|
36
|
+
context.globalLexicals,
|
|
37
|
+
);
|
|
38
|
+
const globalObjectOptimizer = buildOptimizer(
|
|
39
|
+
globalObjectConstants,
|
|
40
|
+
'globalObject',
|
|
41
|
+
);
|
|
42
|
+
const globalLexicalOptimizer = buildOptimizer(
|
|
43
|
+
globalLexicalConstants,
|
|
44
|
+
'globalLexicals',
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
// Create a function in sloppy mode, so that we can use 'with'. It returns
|
|
48
|
+
// a function in strict mode that evaluates the provided code using direct
|
|
49
|
+
// eval, and thus in strict mode in the same scope. We must be very careful
|
|
50
|
+
// to not create new names in this scope
|
|
51
|
+
|
|
52
|
+
// 1: we use multiple nested 'with' to catch all free variable names. The
|
|
53
|
+
// `this` value of the outer sloppy function holds the different scope
|
|
54
|
+
// layers, from inner to outer:
|
|
55
|
+
// a) `evalScope` which must release the `FERAL_EVAL` as 'eval' once for
|
|
56
|
+
// every invocation of the inner `evaluate` function in order to
|
|
57
|
+
// trigger direct eval. The direct eval semantics is what allows the
|
|
58
|
+
// evaluated code to lookup free variable names on the other scope
|
|
59
|
+
// objects and not in global scope.
|
|
60
|
+
// b) `globalLexicals` which provide a way to introduce free variables
|
|
61
|
+
// that are not available on the globalObject.
|
|
62
|
+
// c) `globalObject` is the global scope object of the evaluator, aka the
|
|
63
|
+
// Compartment's `globalThis`.
|
|
64
|
+
// d) `scopeTerminator` is a proxy object which prevents free variable
|
|
65
|
+
// lookups to escape to the start compartment's global object.
|
|
66
|
+
// 2: `optimizer`s catch constant variable names for speed.
|
|
67
|
+
// 3: The inner strict `evaluate` function should be passed two parameters:
|
|
68
|
+
// a) its arguments[0] is the source to be directly evaluated.
|
|
69
|
+
// b) its 'this' is the this binding seen by the code being
|
|
70
|
+
// directly evaluated (the globalObject).
|
|
71
|
+
|
|
72
|
+
// Notes:
|
|
73
|
+
// - The `optimizer` strings only lookup values on the `globalObject` and
|
|
74
|
+
// `globalLexicals` objects by construct. Keywords like 'function' are
|
|
75
|
+
// reserved and cannot be used as a variable, so they are excluded from the
|
|
76
|
+
// optimizer. Furthermore to prevent shadowing 'eval', while a valid
|
|
77
|
+
// identifier, that name is also explicitly excluded.
|
|
78
|
+
// - when 'eval' is looked up in the `evalScope`, the powerful unsafe eval
|
|
79
|
+
// intrinsic is returned after automatically removing it from the
|
|
80
|
+
// `evalScope`. Any further reference to 'eval' in the evaluate string will
|
|
81
|
+
// get the tamed evaluator from the `globalObject`, if any.
|
|
82
|
+
|
|
83
|
+
// TODO https://github.com/endojs/endo/issues/816
|
|
84
|
+
// The optimizer currently runs under sloppy mode, and although we doubt that
|
|
85
|
+
// there is any vulnerability introduced just by running the optimizer
|
|
86
|
+
// sloppy, we are much more confident in the semantics of strict mode.
|
|
87
|
+
// The `evaluate` function can be and is reused across multiple evaluations.
|
|
88
|
+
// Since the optimizer should not be re-evaluated every time, it cannot be
|
|
89
|
+
// inside the `evaluate` closure. While we could potentially nest an
|
|
90
|
+
// intermediate layer of `() => {'use strict'; ${optimizers}; ...`, it
|
|
91
|
+
// doesn't seem worth the overhead and complexity.
|
|
92
|
+
const evaluateFactory = FERAL_FUNCTION(`
|
|
93
|
+
with (this.scopeTerminator) {
|
|
94
|
+
with (this.globalObject) {
|
|
95
|
+
with (this.globalLexicals) {
|
|
96
|
+
with (this.evalScope) {
|
|
97
|
+
${globalObjectOptimizer}
|
|
98
|
+
${globalLexicalOptimizer}
|
|
99
|
+
return function() {
|
|
100
|
+
'use strict';
|
|
101
|
+
return eval(arguments[0]);
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
`);
|
|
108
|
+
|
|
109
|
+
return apply(evaluateFactory, context, []);
|
|
110
|
+
};
|
|
@@ -1,17 +1,12 @@
|
|
|
1
1
|
// Portions adapted from V8 - Copyright 2016 the V8 project authors.
|
|
2
2
|
// https://github.com/v8/v8/blob/master/src/builtins/builtins-function.cc
|
|
3
3
|
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
proxyRevocable,
|
|
9
|
-
weaksetAdd,
|
|
10
|
-
} from './commons.js';
|
|
11
|
-
import { getScopeConstants } from './scope-constants.js';
|
|
12
|
-
import { createScopeHandler } from './scope-handler.js';
|
|
4
|
+
import { apply, freeze } from './commons.js';
|
|
5
|
+
import { strictScopeTerminator } from './strict-scope-terminator.js';
|
|
6
|
+
import { createSloppyGlobalsScopeTerminator } from './sloppy-globals-scope-terminator.js';
|
|
7
|
+
import { makeEvalScopeKit } from './eval-scope.js';
|
|
13
8
|
import { applyTransforms, mandatoryTransforms } from './transforms.js';
|
|
14
|
-
import {
|
|
9
|
+
import { makeEvaluate } from './make-evaluate.js';
|
|
15
10
|
import { assert } from './error/assert.js';
|
|
16
11
|
|
|
17
12
|
const { details: d } = assert;
|
|
@@ -26,37 +21,33 @@ const { details: d } = assert;
|
|
|
26
21
|
* @param {Object} [options.globalLexicals]
|
|
27
22
|
* @param {Array<Transform>} [options.globalTransforms]
|
|
28
23
|
* @param {bool} [options.sloppyGlobalsMode]
|
|
29
|
-
* @param {WeakSet} [options.knownScopeProxies]
|
|
30
24
|
*/
|
|
31
25
|
export const makeSafeEvaluator = ({
|
|
32
26
|
globalObject,
|
|
33
27
|
globalLexicals = {},
|
|
34
28
|
globalTransforms = [],
|
|
35
29
|
sloppyGlobalsMode = false,
|
|
36
|
-
knownScopeProxies = new WeakSet(),
|
|
37
30
|
} = {}) => {
|
|
38
|
-
const
|
|
39
|
-
globalObject
|
|
31
|
+
const scopeTerminator = sloppyGlobalsMode
|
|
32
|
+
? createSloppyGlobalsScopeTerminator(globalObject)
|
|
33
|
+
: strictScopeTerminator;
|
|
34
|
+
const evalScopeKit = makeEvalScopeKit();
|
|
35
|
+
const { evalScope } = evalScopeKit;
|
|
36
|
+
|
|
37
|
+
const evaluateContext = freeze({
|
|
38
|
+
evalScope,
|
|
40
39
|
globalLexicals,
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
);
|
|
45
|
-
const { proxy: scopeProxy, revoke: revokeScopeProxy } = proxyRevocable(
|
|
46
|
-
immutableObject,
|
|
47
|
-
scopeHandler,
|
|
48
|
-
);
|
|
49
|
-
weaksetAdd(knownScopeProxies, scopeProxy);
|
|
40
|
+
globalObject,
|
|
41
|
+
scopeTerminator,
|
|
42
|
+
});
|
|
50
43
|
|
|
51
44
|
// Defer creating the actual evaluator to first use.
|
|
52
45
|
// Creating a compartment should be possible in no-eval environments
|
|
53
46
|
// It also allows more global constants to be captured by the optimizer
|
|
54
47
|
let evaluate;
|
|
55
|
-
const
|
|
48
|
+
const provideEvaluate = () => {
|
|
56
49
|
if (!evaluate) {
|
|
57
|
-
|
|
58
|
-
const evaluateFactory = makeEvaluateFactory(constants);
|
|
59
|
-
evaluate = apply(evaluateFactory, scopeProxy, []);
|
|
50
|
+
evaluate = makeEvaluate(evaluateContext);
|
|
60
51
|
}
|
|
61
52
|
};
|
|
62
53
|
|
|
@@ -66,7 +57,7 @@ export const makeSafeEvaluator = ({
|
|
|
66
57
|
* @param {Array<Transform>} [options.localTransforms]
|
|
67
58
|
*/
|
|
68
59
|
const safeEvaluate = (source, { localTransforms = [] } = {}) => {
|
|
69
|
-
|
|
60
|
+
provideEvaluate();
|
|
70
61
|
|
|
71
62
|
// Execute the mandatory transforms last to ensure that any rewritten code
|
|
72
63
|
// meets those mandatory requirements.
|
|
@@ -76,9 +67,12 @@ export const makeSafeEvaluator = ({
|
|
|
76
67
|
mandatoryTransforms,
|
|
77
68
|
]);
|
|
78
69
|
|
|
79
|
-
scopeController.allowNextEvalToBeUnsafe = true;
|
|
80
70
|
let err;
|
|
81
71
|
try {
|
|
72
|
+
// Allow next reference to eval produce the unsafe FERAL_EVAL.
|
|
73
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
74
|
+
evalScopeKit.allowNextEvalToBeUnsafe();
|
|
75
|
+
|
|
82
76
|
// Ensure that "this" resolves to the safe global.
|
|
83
77
|
return apply(evaluate, globalObject, [source]);
|
|
84
78
|
} catch (e) {
|
|
@@ -86,15 +80,14 @@ export const makeSafeEvaluator = ({
|
|
|
86
80
|
err = e;
|
|
87
81
|
throw e;
|
|
88
82
|
} finally {
|
|
89
|
-
const unsafeEvalWasStillExposed =
|
|
90
|
-
|
|
83
|
+
const unsafeEvalWasStillExposed = 'eval' in evalScope;
|
|
84
|
+
delete evalScope.eval;
|
|
91
85
|
if (unsafeEvalWasStillExposed) {
|
|
92
|
-
// Barring a defect in the SES shim, the
|
|
86
|
+
// Barring a defect in the SES shim, the evalScope should allow the
|
|
93
87
|
// powerful, unsafe `eval` to be used by `evaluate` exactly once, as the
|
|
94
88
|
// very first name that it attempts to access from the lexical scope.
|
|
95
89
|
// A defect in the SES shim could throw an exception after we set
|
|
96
|
-
// `
|
|
97
|
-
// calls `eval` internally.
|
|
90
|
+
// `evalScope.eval` and before `evaluate` calls `eval` internally.
|
|
98
91
|
// If we get here, SES is very broken.
|
|
99
92
|
// This condition is one where this vat is now hopelessly confused, and
|
|
100
93
|
// the vat as a whole should be aborted.
|
|
@@ -104,7 +97,7 @@ export const makeSafeEvaluator = ({
|
|
|
104
97
|
// variable resolution via the scopeHandler, and throw an error with
|
|
105
98
|
// diagnostic info including the thrown error if any from evaluating the
|
|
106
99
|
// source code.
|
|
107
|
-
|
|
100
|
+
evalScopeKit.revoked = { err };
|
|
108
101
|
// TODO A GOOD PLACE TO PANIC(), i.e., kill the vat incarnation.
|
|
109
102
|
// See https://github.com/Agoric/SES-shim/issues/490
|
|
110
103
|
// eslint-disable-next-line @endo/no-polymorphic-call
|
package/src/scope-constants.js
CHANGED
|
@@ -77,7 +77,7 @@ const keywords = [
|
|
|
77
77
|
|
|
78
78
|
/**
|
|
79
79
|
* identifierPattern
|
|
80
|
-
* Simplified validation of
|
|
80
|
+
* Simplified validation of identifier names: may only contain alphanumeric
|
|
81
81
|
* characters (or "$" or "_"), and may not start with a digit. This is safe
|
|
82
82
|
* and does not reduces the compatibility of the shim. The motivation for
|
|
83
83
|
* this limitation was to decrease the complexity of the implementation,
|
|
@@ -144,32 +144,36 @@ function isImmutableDataProperty(obj, name) {
|
|
|
144
144
|
* safe and only prevent performance optimization.
|
|
145
145
|
*
|
|
146
146
|
* @param {Object} globalObject
|
|
147
|
-
* @param {Object}
|
|
147
|
+
* @param {Object} globalLexicals
|
|
148
148
|
*/
|
|
149
|
-
export const getScopeConstants = (globalObject,
|
|
149
|
+
export const getScopeConstants = (globalObject, globalLexicals = {}) => {
|
|
150
150
|
// getOwnPropertyNames() does ignore Symbols so we don't need to
|
|
151
151
|
// filter them out.
|
|
152
|
-
const
|
|
153
|
-
const
|
|
152
|
+
const globalObjectNames = getOwnPropertyNames(globalObject);
|
|
153
|
+
const globalLexicalNames = getOwnPropertyNames(globalLexicals);
|
|
154
154
|
|
|
155
155
|
// Collect all valid & immutable identifiers from the endowments.
|
|
156
|
-
const
|
|
157
|
-
|
|
156
|
+
const globalLexicalConstants = arrayFilter(
|
|
157
|
+
globalLexicalNames,
|
|
158
158
|
name =>
|
|
159
|
-
isValidIdentifierName(name) &&
|
|
159
|
+
isValidIdentifierName(name) &&
|
|
160
|
+
isImmutableDataProperty(globalLexicals, name),
|
|
160
161
|
);
|
|
161
162
|
|
|
162
163
|
// Collect all valid & immutable identifiers from the global that
|
|
163
|
-
// are also not present in the
|
|
164
|
-
const
|
|
165
|
-
|
|
164
|
+
// are also not present in the endowments (immutable or not).
|
|
165
|
+
const globalObjectConstants = arrayFilter(
|
|
166
|
+
globalObjectNames,
|
|
166
167
|
name =>
|
|
167
168
|
// Can't define a constant: it would prevent a
|
|
168
169
|
// lookup on the endowments.
|
|
169
|
-
!arrayIncludes(
|
|
170
|
+
!arrayIncludes(globalLexicalNames, name) &&
|
|
170
171
|
isValidIdentifierName(name) &&
|
|
171
172
|
isImmutableDataProperty(globalObject, name),
|
|
172
173
|
);
|
|
173
174
|
|
|
174
|
-
return
|
|
175
|
+
return {
|
|
176
|
+
globalObjectConstants,
|
|
177
|
+
globalLexicalConstants,
|
|
178
|
+
};
|
|
175
179
|
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Proxy,
|
|
3
|
+
create,
|
|
4
|
+
freeze,
|
|
5
|
+
getOwnPropertyDescriptors,
|
|
6
|
+
immutableObject,
|
|
7
|
+
reflectSet,
|
|
8
|
+
} from './commons.js';
|
|
9
|
+
import {
|
|
10
|
+
strictScopeTerminatorHandler,
|
|
11
|
+
alwaysThrowHandler,
|
|
12
|
+
} from './strict-scope-terminator.js';
|
|
13
|
+
|
|
14
|
+
/*
|
|
15
|
+
* createSloppyGlobalsScopeTerminator()
|
|
16
|
+
* strictScopeTerminatorHandler manages a scopeTerminator Proxy which serves as
|
|
17
|
+
* the final scope boundary that will always return "undefined" in order
|
|
18
|
+
* to prevent access to "start compartment globals". When "sloppyGlobalsMode"
|
|
19
|
+
* is true, the Proxy will perform sets on the "globalObject".
|
|
20
|
+
*/
|
|
21
|
+
export const createSloppyGlobalsScopeTerminator = globalObject => {
|
|
22
|
+
const scopeProxyHandlerProperties = {
|
|
23
|
+
// inherit scopeTerminator behavior
|
|
24
|
+
...strictScopeTerminatorHandler,
|
|
25
|
+
|
|
26
|
+
// Redirect set properties to the globalObject.
|
|
27
|
+
set(_shadow, prop, value) {
|
|
28
|
+
return reflectSet(globalObject, prop, value);
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
// Always claim to have a potential property in order to be the recipient of a set
|
|
32
|
+
has(_shadow, _prop) {
|
|
33
|
+
return true;
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// The scope handler's prototype is a proxy that throws if any trap other
|
|
38
|
+
// than get/set/has are run (like getOwnPropertyDescriptors, apply,
|
|
39
|
+
// getPrototypeOf).
|
|
40
|
+
const sloppyGlobalsScopeTerminatorHandler = freeze(
|
|
41
|
+
create(
|
|
42
|
+
alwaysThrowHandler,
|
|
43
|
+
getOwnPropertyDescriptors(scopeProxyHandlerProperties),
|
|
44
|
+
),
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
const sloppyGlobalsScopeTerminator = new Proxy(
|
|
48
|
+
immutableObject,
|
|
49
|
+
sloppyGlobalsScopeTerminatorHandler,
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
return sloppyGlobalsScopeTerminator;
|
|
53
|
+
};
|