ses 0.16.1 → 0.17.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/NEWS.md +11 -1
- package/package.json +4 -4
- 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/NEWS.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
User-visible changes in SES:
|
|
2
2
|
|
|
3
|
-
# v0.
|
|
3
|
+
# v0.17.1 (2023-08-07)
|
|
4
4
|
|
|
5
5
|
- Censors the pattern `{...import(specifier)`}.
|
|
6
6
|
We previously censored `import(specifier)` and expressly allowed
|
|
@@ -8,6 +8,16 @@ User-visible changes in SES:
|
|
|
8
8
|
The relaxation for the latter form in version 0.13.0 inadvertently allowed
|
|
9
9
|
import with the spread operator.
|
|
10
10
|
|
|
11
|
+
# v0.17.0 (2022-10-24)
|
|
12
|
+
|
|
13
|
+
- Previous versions of SES would leak the proxy used to isolate evaluated
|
|
14
|
+
code to functions added to the global object by guest code.
|
|
15
|
+
The value of `this` in such functions should be `undefined`, but that is not
|
|
16
|
+
possible to emulate in this shim.
|
|
17
|
+
This version changes the value of `this` in such functions to be the same as
|
|
18
|
+
`globalThis` of the compartment, as would be correct in sloppy mode.
|
|
19
|
+
- Removes experimental support for "known scope proxies".
|
|
20
|
+
|
|
11
21
|
# v0.16.0 (2022-10-19)
|
|
12
22
|
|
|
13
23
|
- When hardening a typed array, detects and locks down properties named as
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ses",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.1",
|
|
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",
|
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
|
+
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Proxy,
|
|
3
|
+
String,
|
|
4
|
+
TypeError,
|
|
5
|
+
ReferenceError,
|
|
6
|
+
create,
|
|
7
|
+
freeze,
|
|
8
|
+
getOwnPropertyDescriptors,
|
|
9
|
+
globalThis,
|
|
10
|
+
immutableObject,
|
|
11
|
+
} from './commons.js';
|
|
12
|
+
import { assert } from './error/assert.js';
|
|
13
|
+
|
|
14
|
+
const { details: d, quote: q } = assert;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* alwaysThrowHandler
|
|
18
|
+
* This is an object that throws if any property is called. It's used as
|
|
19
|
+
* a proxy handler which throws on any trap called.
|
|
20
|
+
* It's made from a proxy with a get trap that throws. It's safe to
|
|
21
|
+
* create one and share it between all Proxy handlers.
|
|
22
|
+
*/
|
|
23
|
+
export const alwaysThrowHandler = new Proxy(
|
|
24
|
+
immutableObject,
|
|
25
|
+
freeze({
|
|
26
|
+
get(_shadow, prop) {
|
|
27
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
28
|
+
assert.fail(
|
|
29
|
+
d`Please report unexpected scope handler trap: ${q(String(prop))}`,
|
|
30
|
+
);
|
|
31
|
+
},
|
|
32
|
+
}),
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
/*
|
|
36
|
+
* scopeProxyHandlerProperties
|
|
37
|
+
* scopeTerminatorHandler manages a strictScopeTerminator Proxy which serves as
|
|
38
|
+
* the final scope boundary that will always return "undefined" in order
|
|
39
|
+
* to prevent access to "start compartment globals".
|
|
40
|
+
*/
|
|
41
|
+
const scopeProxyHandlerProperties = {
|
|
42
|
+
get(_shadow, _prop) {
|
|
43
|
+
return undefined;
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
set(_shadow, prop, _value) {
|
|
47
|
+
// We should only hit this if the has() hook returned true matches the v8
|
|
48
|
+
// ReferenceError message "Uncaught ReferenceError: xyz is not defined"
|
|
49
|
+
throw new ReferenceError(`${String(prop)} is not defined`);
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
has(_shadow, prop) {
|
|
53
|
+
// we must at least return true for all properties on the realm globalThis
|
|
54
|
+
return prop in globalThis;
|
|
55
|
+
},
|
|
56
|
+
|
|
57
|
+
// note: this is likely a bug of safari
|
|
58
|
+
// https://bugs.webkit.org/show_bug.cgi?id=195534
|
|
59
|
+
getPrototypeOf() {
|
|
60
|
+
return null;
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
// Chip has seen this happen single stepping under the Chrome/v8 debugger.
|
|
64
|
+
// TODO record how to reliably reproduce, and to test if this fix helps.
|
|
65
|
+
// TODO report as bug to v8 or Chrome, and record issue link here.
|
|
66
|
+
getOwnPropertyDescriptor(_target, prop) {
|
|
67
|
+
// Coerce with `String` in case prop is a symbol.
|
|
68
|
+
const quotedProp = q(String(prop));
|
|
69
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
70
|
+
console.warn(
|
|
71
|
+
`getOwnPropertyDescriptor trap on scopeTerminatorHandler for ${quotedProp}`,
|
|
72
|
+
new TypeError().stack,
|
|
73
|
+
);
|
|
74
|
+
return undefined;
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// The scope handler's prototype is a proxy that throws if any trap other
|
|
79
|
+
// than get/set/has are run (like getOwnPropertyDescriptors, apply,
|
|
80
|
+
// getPrototypeOf).
|
|
81
|
+
export const strictScopeTerminatorHandler = freeze(
|
|
82
|
+
create(
|
|
83
|
+
alwaysThrowHandler,
|
|
84
|
+
getOwnPropertyDescriptors(scopeProxyHandlerProperties),
|
|
85
|
+
),
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
export const strictScopeTerminator = new Proxy(
|
|
89
|
+
immutableObject,
|
|
90
|
+
strictScopeTerminatorHandler,
|
|
91
|
+
);
|
package/src/whitelist.js
CHANGED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { FERAL_FUNCTION, arrayJoin } from './commons.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* buildOptimizer()
|
|
5
|
-
* Given an array of indentifier, the optimizer return a `const` declaration
|
|
6
|
-
* destructring `this`.
|
|
7
|
-
*
|
|
8
|
-
* @param {Array<string>} constants
|
|
9
|
-
*/
|
|
10
|
-
function buildOptimizer(constants) {
|
|
11
|
-
// No need to build an optimizer when there are no constants.
|
|
12
|
-
if (constants.length === 0) return '';
|
|
13
|
-
// Use 'this' to avoid going through the scope proxy, which is unecessary
|
|
14
|
-
// since the optimizer only needs references to the safe global.
|
|
15
|
-
return `const {${arrayJoin(constants, ',')}} = this;`;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* makeEvaluateFactory()
|
|
20
|
-
* The factory create 'evaluate' functions with the correct optimizer
|
|
21
|
-
* inserted.
|
|
22
|
-
*
|
|
23
|
-
* @param {Array<string>} [constants]
|
|
24
|
-
*/
|
|
25
|
-
export const makeEvaluateFactory = (constants = []) => {
|
|
26
|
-
const optimizer = buildOptimizer(constants);
|
|
27
|
-
|
|
28
|
-
// Create a function in sloppy mode, so that we can use 'with'. It returns
|
|
29
|
-
// a function in strict mode that evaluates the provided code using direct
|
|
30
|
-
// eval, and thus in strict mode in the same scope. We must be very careful
|
|
31
|
-
// to not create new names in this scope
|
|
32
|
-
|
|
33
|
-
// 1: we use 'with' (around a Proxy) to catch all free variable names. The
|
|
34
|
-
// `this` value holds the Proxy which safely wraps the safeGlobal
|
|
35
|
-
// 2: 'optimizer' catches constant variable names for speed.
|
|
36
|
-
// 3: The inner strict function is effectively passed two parameters:
|
|
37
|
-
// a) its arguments[0] is the source to be directly evaluated.
|
|
38
|
-
// b) its 'this' is the this binding seen by the code being
|
|
39
|
-
// directly evaluated (the globalObject).
|
|
40
|
-
// 4: The outer sloppy function is passed one parameter, the scope proxy.
|
|
41
|
-
// as the `this` parameter.
|
|
42
|
-
|
|
43
|
-
// Notes:
|
|
44
|
-
// - everything in the 'optimizer' string is looked up in the proxy
|
|
45
|
-
// (including an 'arguments[0]', which points at the Proxy).
|
|
46
|
-
// - keywords like 'function' which are reserved keywords, and cannot be
|
|
47
|
-
// used as a variable, so they are not part of the optimizer.
|
|
48
|
-
// - when 'eval' is looked up in the proxy, and it's the first time it is
|
|
49
|
-
// looked up after allowNextEvalToBeUnsafe is turned on, the proxy returns
|
|
50
|
-
// the powerful, unsafe eval intrinsic, and flips allowNextEvalToBeUnsafe
|
|
51
|
-
// back to false. Any reference to 'eval' in that string will get the tamed
|
|
52
|
-
// evaluator.
|
|
53
|
-
|
|
54
|
-
// TODO https://github.com/endojs/endo/issues/816
|
|
55
|
-
// The optimizer currently runs under sloppy mode, and although we doubt that
|
|
56
|
-
// there is any vulnerability introduced just by running the optimizer
|
|
57
|
-
// sloppy, we are much more confident in the semantics of strict mode.
|
|
58
|
-
// The motivation for having the optimizer in sloppy mode is that it can be
|
|
59
|
-
// reused for multiple evaluations, but in practice we have no such calls.
|
|
60
|
-
// We could probably both move the optimizer into the inner function
|
|
61
|
-
// and we could also simplify makeEvaluateFactory to simply evaluate.
|
|
62
|
-
return FERAL_FUNCTION(`
|
|
63
|
-
with (this) {
|
|
64
|
-
${optimizer}
|
|
65
|
-
return function() {
|
|
66
|
-
'use strict';
|
|
67
|
-
return eval(arguments[0]);
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
`);
|
|
71
|
-
};
|
package/src/scope-handler.js
DELETED
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
FERAL_EVAL,
|
|
3
|
-
Proxy,
|
|
4
|
-
String,
|
|
5
|
-
TypeError,
|
|
6
|
-
create,
|
|
7
|
-
freeze,
|
|
8
|
-
getOwnPropertyDescriptor,
|
|
9
|
-
getOwnPropertyDescriptors,
|
|
10
|
-
globalThis,
|
|
11
|
-
immutableObject,
|
|
12
|
-
objectHasOwnProperty,
|
|
13
|
-
reflectGet,
|
|
14
|
-
reflectSet,
|
|
15
|
-
seal,
|
|
16
|
-
} from './commons.js';
|
|
17
|
-
import { assert } from './error/assert.js';
|
|
18
|
-
|
|
19
|
-
const { details: d, quote: q } = assert;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* alwaysThrowHandler
|
|
23
|
-
* This is an object that throws if any property is called. It's used as
|
|
24
|
-
* a proxy handler which throws on any trap called.
|
|
25
|
-
* It's made from a proxy with a get trap that throws. It's safe to
|
|
26
|
-
* create one and share it between all scopeHandlers.
|
|
27
|
-
*/
|
|
28
|
-
const alwaysThrowHandler = new Proxy(
|
|
29
|
-
immutableObject,
|
|
30
|
-
freeze({
|
|
31
|
-
get(_shadow, prop) {
|
|
32
|
-
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
33
|
-
assert.fail(
|
|
34
|
-
d`Please report unexpected scope handler trap: ${q(String(prop))}`,
|
|
35
|
-
);
|
|
36
|
-
},
|
|
37
|
-
}),
|
|
38
|
-
);
|
|
39
|
-
|
|
40
|
-
/*
|
|
41
|
-
* createScopeHandler()
|
|
42
|
-
* ScopeHandler manages a Proxy which serves as the global scope for the
|
|
43
|
-
* safeEvaluate operation (the Proxy is the argument of a 'with' binding).
|
|
44
|
-
* As described in createSafeEvaluator(), it has several functions:
|
|
45
|
-
* - allow the very first (and only the very first) use of 'eval' to map to
|
|
46
|
-
* the real (unsafe) eval function, so it acts as a 'direct eval' and can
|
|
47
|
-
* access its lexical scope (which maps to the 'with' binding, which the
|
|
48
|
-
* ScopeHandler also controls).
|
|
49
|
-
* - ensure that all subsequent uses of 'eval' map to the safeEvaluator,
|
|
50
|
-
* which lives as the 'eval' property of the safeGlobal.
|
|
51
|
-
* - route all other property lookups at the safeGlobal.
|
|
52
|
-
* - hide the unsafeGlobal which lives on the scope chain above the 'with'.
|
|
53
|
-
* - ensure the Proxy invariants despite some global properties being frozen.
|
|
54
|
-
*/
|
|
55
|
-
export const createScopeHandler = (
|
|
56
|
-
globalObject,
|
|
57
|
-
globalLexicals = {},
|
|
58
|
-
{ sloppyGlobalsMode = false } = {},
|
|
59
|
-
) => {
|
|
60
|
-
// This flag allow us to determine if the eval() call is an done by the
|
|
61
|
-
// compartment's code or if it is user-land invocation, so we can react
|
|
62
|
-
// differently.
|
|
63
|
-
// Using a flag on an object with a single mutable property allows a safe
|
|
64
|
-
// evaluator to signal to the scope proxy without consuming a stack frame.
|
|
65
|
-
// Consuming a stack frame could possibly allow an attacker to control the
|
|
66
|
-
// stack depth before calling `evaluate` to cause a RangeError before this
|
|
67
|
-
// flag can be reset, leaving the unsafe evaluator available.
|
|
68
|
-
const scopeController = {
|
|
69
|
-
allowNextEvalToBeUnsafe: false,
|
|
70
|
-
};
|
|
71
|
-
seal(scopeController);
|
|
72
|
-
|
|
73
|
-
const scopeProxyHandlerProperties = {
|
|
74
|
-
get(_shadow, prop) {
|
|
75
|
-
if (typeof prop === 'symbol') {
|
|
76
|
-
return undefined;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// Special treatment for eval. The very first lookup of 'eval' gets the
|
|
80
|
-
// unsafe (real direct) eval, so it will get the lexical scope that uses
|
|
81
|
-
// the 'with' context.
|
|
82
|
-
if (prop === 'eval') {
|
|
83
|
-
// test that it is true rather than merely truthy
|
|
84
|
-
if (scopeController.allowNextEvalToBeUnsafe === true) {
|
|
85
|
-
// revoke before use
|
|
86
|
-
scopeController.allowNextEvalToBeUnsafe = false;
|
|
87
|
-
return FERAL_EVAL;
|
|
88
|
-
}
|
|
89
|
-
// fall through
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// Properties of the globalLexicals.
|
|
93
|
-
if (prop in globalLexicals) {
|
|
94
|
-
// Use reflect to defeat accessors that could be present on the
|
|
95
|
-
// globalLexicals object itself as `this`.
|
|
96
|
-
// This is done out of an overabundance of caution, as the SES shim
|
|
97
|
-
// only use the globalLexicals carry globalLexicals and live binding
|
|
98
|
-
// traps.
|
|
99
|
-
// The globalLexicals are captured as a snapshot of what's passed to
|
|
100
|
-
// the Compartment constructor, wherein all accessors and setters are
|
|
101
|
-
// eliminated and the result frozen.
|
|
102
|
-
// The live binding traps do use accessors, and none of those accessors
|
|
103
|
-
// make use of their receiver.
|
|
104
|
-
// Live binding traps provide no avenue for user code to observe the
|
|
105
|
-
// receiver.
|
|
106
|
-
return reflectGet(globalLexicals, prop, globalObject);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// Properties of the global.
|
|
110
|
-
return reflectGet(globalObject, prop);
|
|
111
|
-
},
|
|
112
|
-
|
|
113
|
-
set(_shadow, prop, value) {
|
|
114
|
-
// Properties of the globalLexicals.
|
|
115
|
-
if (prop in globalLexicals) {
|
|
116
|
-
const desc = getOwnPropertyDescriptor(globalLexicals, prop);
|
|
117
|
-
if (objectHasOwnProperty(desc, 'value')) {
|
|
118
|
-
// Work around a peculiar behavior in the specs, where
|
|
119
|
-
// value properties are defined on the receiver.
|
|
120
|
-
return reflectSet(globalLexicals, prop, value);
|
|
121
|
-
}
|
|
122
|
-
// Ensure that the 'this' value on setters resolves
|
|
123
|
-
// to the safeGlobal, not to the globalLexicals object.
|
|
124
|
-
return reflectSet(globalLexicals, prop, value, globalObject);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// Properties of the global.
|
|
128
|
-
return reflectSet(globalObject, prop, value);
|
|
129
|
-
},
|
|
130
|
-
|
|
131
|
-
// we need has() to return false for some names to prevent the lookup from
|
|
132
|
-
// climbing the scope chain and eventually reaching the unsafeGlobal
|
|
133
|
-
// object (globalThis), which is bad.
|
|
134
|
-
|
|
135
|
-
// todo: we'd like to just have has() return true for everything, and then
|
|
136
|
-
// use get() to raise a ReferenceError for anything not on the safe global.
|
|
137
|
-
// But we want to be compatible with ReferenceError in the normal case and
|
|
138
|
-
// the lack of ReferenceError in the 'typeof' case. Must either reliably
|
|
139
|
-
// distinguish these two cases (the trap behavior might be different), or
|
|
140
|
-
// we rely on a mandatory source-to-source transform to change 'typeof abc'
|
|
141
|
-
// to XXX. We already need a mandatory parse to prevent the 'import',
|
|
142
|
-
// since it's a special form instead of merely being a global variable/
|
|
143
|
-
|
|
144
|
-
// note: if we make has() return true always, then we must implement a
|
|
145
|
-
// set() trap to avoid subverting the protection of strict mode (it would
|
|
146
|
-
// accept assignments to undefined globals, when it ought to throw
|
|
147
|
-
// ReferenceError for such assignments)
|
|
148
|
-
|
|
149
|
-
has(_shadow, prop) {
|
|
150
|
-
// unsafeGlobal: hide all properties of the current global
|
|
151
|
-
// at the expense of 'typeof' being wrong for those properties. For
|
|
152
|
-
// example, in the browser, evaluating 'document = 3', will add
|
|
153
|
-
// a property to globalObject instead of throwing a ReferenceError.
|
|
154
|
-
|
|
155
|
-
// !!!!! WARNING: DANGER ZONE !!!!!!
|
|
156
|
-
// The order of the conditions in the `||` expression below is of the
|
|
157
|
-
// utmost importance. Under no circumstances should `eval` be checked
|
|
158
|
-
// after `globalObject`. The prototype of the global object is under
|
|
159
|
-
// full control of user code and may be replaced by a proxy with a
|
|
160
|
-
// `has` trap. If we allow that trap to trigger while the
|
|
161
|
-
// `allowNextEvalToBeUnsafe` flag is down, it could allow user code
|
|
162
|
-
// to get a hold of `FERAL_EVAL`, resulting in a complete escape of
|
|
163
|
-
// the compartment.
|
|
164
|
-
// !!!!! WARNING: DANGER ZONE !!!!!!
|
|
165
|
-
return (
|
|
166
|
-
sloppyGlobalsMode ||
|
|
167
|
-
(scopeController.allowNextEvalToBeUnsafe && prop === 'eval') ||
|
|
168
|
-
prop in globalLexicals ||
|
|
169
|
-
prop in globalObject ||
|
|
170
|
-
prop in globalThis
|
|
171
|
-
);
|
|
172
|
-
},
|
|
173
|
-
|
|
174
|
-
// note: this is likely a bug of safari
|
|
175
|
-
// https://bugs.webkit.org/show_bug.cgi?id=195534
|
|
176
|
-
|
|
177
|
-
getPrototypeOf() {
|
|
178
|
-
return null;
|
|
179
|
-
},
|
|
180
|
-
|
|
181
|
-
// Chip has seen this happen single stepping under the Chrome/v8 debugger.
|
|
182
|
-
// TODO record how to reliably reproduce, and to test if this fix helps.
|
|
183
|
-
// TODO report as bug to v8 or Chrome, and record issue link here.
|
|
184
|
-
|
|
185
|
-
getOwnPropertyDescriptor(_target, prop) {
|
|
186
|
-
// Coerce with `String` in case prop is a symbol.
|
|
187
|
-
const quotedProp = q(String(prop));
|
|
188
|
-
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
189
|
-
console.warn(
|
|
190
|
-
`getOwnPropertyDescriptor trap on scopeHandler for ${quotedProp}`,
|
|
191
|
-
new TypeError().stack,
|
|
192
|
-
);
|
|
193
|
-
return undefined;
|
|
194
|
-
},
|
|
195
|
-
};
|
|
196
|
-
|
|
197
|
-
// The scope handler's prototype is a proxy that throws if any trap other
|
|
198
|
-
// than get/set/has are run (like getOwnPropertyDescriptors, apply,
|
|
199
|
-
// getPrototypeOf).
|
|
200
|
-
const scopeHandler = freeze(
|
|
201
|
-
create(
|
|
202
|
-
alwaysThrowHandler,
|
|
203
|
-
getOwnPropertyDescriptors(scopeProxyHandlerProperties),
|
|
204
|
-
),
|
|
205
|
-
);
|
|
206
|
-
|
|
207
|
-
return {
|
|
208
|
-
scopeController,
|
|
209
|
-
scopeHandler,
|
|
210
|
-
};
|
|
211
|
-
};
|