ses 0.15.23 → 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 +54 -0
- package/NEWS.md +16 -1
- package/README.md +22 -1
- package/dist/lockdown.cjs +636 -502
- package/dist/lockdown.mjs +636 -502
- package/dist/lockdown.umd.js +636 -502
- package/dist/lockdown.umd.min.js +1 -1
- package/dist/ses.cjs +636 -502
- package/dist/ses.mjs +636 -502
- package/dist/ses.umd.js +636 -502
- package/dist/ses.umd.min.js +1 -1
- package/package.json +6 -6
- package/src/commons.js +2 -16
- package/src/compartment-evaluate.js +2 -7
- package/src/compartment-shim.js +3 -11
- package/src/enablements.js +14 -9
- package/src/eval-scope.js +90 -0
- package/src/global-object.js +40 -1
- package/src/make-evaluate.js +110 -0
- package/src/make-hardener.js +31 -19
- 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
|
-
"test:platform-
|
|
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
|
@@ -66,6 +66,7 @@ export const {
|
|
|
66
66
|
preventExtensions,
|
|
67
67
|
setPrototypeOf,
|
|
68
68
|
values,
|
|
69
|
+
fromEntries,
|
|
69
70
|
} = Object;
|
|
70
71
|
|
|
71
72
|
export const {
|
|
@@ -73,6 +74,7 @@ export const {
|
|
|
73
74
|
toStringTag: toStringTagSymbol,
|
|
74
75
|
iterator: iteratorSymbol,
|
|
75
76
|
matchAll: matchAllSymbol,
|
|
77
|
+
unscopables: unscopablesSymbol,
|
|
76
78
|
keyFor: symbolKeyFor,
|
|
77
79
|
for: symbolFor,
|
|
78
80
|
} = Symbol;
|
|
@@ -81,22 +83,6 @@ export const { isInteger } = Number;
|
|
|
81
83
|
|
|
82
84
|
export const { stringify: stringifyJson } = JSON;
|
|
83
85
|
|
|
84
|
-
// At time of this writing, we still support Node 10 which doesn't have
|
|
85
|
-
// `Object.fromEntries`. If it is absent, this should be an adequate
|
|
86
|
-
// replacement.
|
|
87
|
-
// By the terminology of https://ponyfoo.com/articles/polyfills-or-ponyfills
|
|
88
|
-
// it is a ponyfill rather than a polyfill or shim because we do not
|
|
89
|
-
// install it on `Object`.
|
|
90
|
-
const objectFromEntries = entryPairs => {
|
|
91
|
-
const result = {};
|
|
92
|
-
for (const [prop, val] of entryPairs) {
|
|
93
|
-
result[prop] = val;
|
|
94
|
-
}
|
|
95
|
-
return result;
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
export const fromEntries = Object.fromEntries || objectFromEntries;
|
|
99
|
-
|
|
100
86
|
// Needed only for the Safari bug workaround below
|
|
101
87
|
const { defineProperty: originalDefineProperty } = Object;
|
|
102
88
|
|
|
@@ -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,
|
package/src/enablements.js
CHANGED
|
@@ -165,22 +165,27 @@ export const severeEnablements = {
|
|
|
165
165
|
...moderateEnablements,
|
|
166
166
|
|
|
167
167
|
/**
|
|
168
|
-
* Rollup(as used at least by vega) and webpack
|
|
168
|
+
* Rollup (as used at least by vega) and webpack
|
|
169
169
|
* (as used at least by regenerator) both turn exports into assignments
|
|
170
170
|
* to a big `exports` object that inherits directly from
|
|
171
|
-
* `Object.prototype`.Some of the exported names we've seen include
|
|
171
|
+
* `Object.prototype`. Some of the exported names we've seen include
|
|
172
172
|
* `hasOwnProperty`, `constructor`, and `toString`. But the strategy used
|
|
173
|
-
* by rollup and webpack
|
|
174
|
-
* into an assignment rejected by the override mistake.That's why
|
|
175
|
-
*
|
|
173
|
+
* by rollup and webpack potentionally turns any exported name
|
|
174
|
+
* into an assignment rejected by the override mistake. That's why
|
|
175
|
+
* the `severe` enablements takes the extreme step of enabling
|
|
176
|
+
* everything on `Object.prototype`.
|
|
176
177
|
*
|
|
177
178
|
* In addition, code doing inheritance manually will often override
|
|
178
179
|
* the `constructor` property on the new prototype by assignment. We've
|
|
179
180
|
* seen this several times.
|
|
180
181
|
*
|
|
181
182
|
* The cost of enabling all these is that they create a miserable debugging
|
|
182
|
-
* experience
|
|
183
|
-
*
|
|
183
|
+
* experience specifically on Node.
|
|
184
|
+
* https://github.com/Agoric/agoric-sdk/issues/2324
|
|
185
|
+
* explains how it confused the Node console.
|
|
186
|
+
*
|
|
187
|
+
* (TODO Reexamine the vscode situation. I think it may have improved
|
|
188
|
+
* since the following paragraph was written.)
|
|
184
189
|
*
|
|
185
190
|
* The vscode debugger's object inspector shows the own data properties of
|
|
186
191
|
* an object, which is typically what you want, but also shows both getter
|
|
@@ -197,8 +202,8 @@ export const severeEnablements = {
|
|
|
197
202
|
* The widely used Buffer defined at https://github.com/feross/buffer
|
|
198
203
|
* on initialization, manually creates the equivalent of a subclass of
|
|
199
204
|
* `TypedArray`, which it then initializes by assignment. These assignments
|
|
200
|
-
* include enough of the `TypeArray` methods that here,
|
|
201
|
-
* them all.
|
|
205
|
+
* include enough of the `TypeArray` methods that here, the `severe`
|
|
206
|
+
* enablements just enable them all.
|
|
202
207
|
*/
|
|
203
208
|
'%TypedArrayPrototype%': '*',
|
|
204
209
|
};
|
|
@@ -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
|
+
};
|
package/src/make-hardener.js
CHANGED
|
@@ -66,40 +66,52 @@ const getTypedArrayToStringTag = typedArrayToStringTag.get;
|
|
|
66
66
|
assert(getTypedArrayToStringTag);
|
|
67
67
|
|
|
68
68
|
// Exported for tests.
|
|
69
|
-
/**
|
|
69
|
+
/**
|
|
70
|
+
* Duplicates packages/marshal/src/helpers/passStyle-helpers.js to avoid a dependency.
|
|
71
|
+
*
|
|
72
|
+
* @param {unknown} object
|
|
73
|
+
*/
|
|
70
74
|
export const isTypedArray = object => {
|
|
71
75
|
// The object must pass a brand check or toStringTag will return undefined.
|
|
72
76
|
const tag = apply(getTypedArrayToStringTag, object, []);
|
|
73
77
|
return tag !== undefined;
|
|
74
78
|
};
|
|
75
79
|
|
|
80
|
+
/**
|
|
81
|
+
* Tests if a property key is an integer-valued canonical numeric index.
|
|
82
|
+
* https://tc39.es/ecma262/#sec-canonicalnumericindexstring
|
|
83
|
+
*
|
|
84
|
+
* @param {string | symbol} propertyKey
|
|
85
|
+
*/
|
|
86
|
+
const isCanonicalIntegerIndexString = propertyKey => {
|
|
87
|
+
const n = +String(propertyKey);
|
|
88
|
+
return isInteger(n) && String(n) === propertyKey;
|
|
89
|
+
};
|
|
90
|
+
|
|
76
91
|
/**
|
|
77
92
|
* @template T
|
|
78
93
|
* @param {ArrayLike<T>} array
|
|
79
94
|
*/
|
|
80
95
|
const freezeTypedArray = array => {
|
|
81
|
-
const descs = getOwnPropertyDescriptors(array);
|
|
82
|
-
|
|
83
96
|
preventExtensions(array);
|
|
84
97
|
|
|
85
98
|
// Downgrade writable expandos to readonly, even if non-configurable.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
//
|
|
94
|
-
//
|
|
95
|
-
//
|
|
96
|
-
// Assignment to these indexes silently fails and defining an indexed
|
|
97
|
-
// property throws an error.
|
|
98
|
-
// So, we only need to make non-index properties non-writable and
|
|
99
|
-
// non-configurable.
|
|
99
|
+
// We get each descriptor individually rather than using
|
|
100
|
+
// getOwnPropertyDescriptors in order to fail safe when encountering
|
|
101
|
+
// an obscure GraalJS issue where getOwnPropertyDescriptor returns
|
|
102
|
+
// undefined for a property that does exist.
|
|
103
|
+
arrayForEach(ownKeys(array), (/** @type {string | symbol} */ name) => {
|
|
104
|
+
const desc = getOwnPropertyDescriptor(array, name);
|
|
105
|
+
assert(desc);
|
|
106
|
+
// TypedArrays are integer-indexed exotic objects, which define special
|
|
107
|
+
// treatment for property names in canonical numeric form:
|
|
108
|
+
// integers in range are permanently writable and non-configurable.
|
|
100
109
|
// https://tc39.es/ecma262/#sec-integer-indexed-exotic-objects
|
|
101
|
-
|
|
102
|
-
|
|
110
|
+
//
|
|
111
|
+
// This is analogous to the data of a hardened Map or Set,
|
|
112
|
+
// so we carve out this exceptional behavior but make all other
|
|
113
|
+
// properties non-configurable.
|
|
114
|
+
if (!isCanonicalIntegerIndexString(name)) {
|
|
103
115
|
defineProperty(array, name, {
|
|
104
116
|
...desc,
|
|
105
117
|
writable: false,
|
|
@@ -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
|