ses 0.16.0 → 0.18.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 +46 -0
- package/NEWS.md +20 -0
- package/README.md +3 -6
- package/dist/lockdown.cjs +716 -590
- package/dist/lockdown.mjs +716 -590
- package/dist/lockdown.umd.js +716 -590
- package/dist/lockdown.umd.min.js +1 -1
- package/dist/ses.cjs +716 -590
- package/dist/ses.mjs +716 -590
- package/dist/ses.umd.js +716 -590
- package/dist/ses.umd.min.js +1 -1
- package/index.d.ts +1 -1
- package/package.json +7 -7
- package/src/commons.js +1 -0
- package/src/compartment-evaluate.js +6 -13
- package/src/compartment-shim.js +3 -41
- package/src/environment-options.js +4 -7
- package/src/error/assert.js +4 -0
- package/src/error/types.js +59 -0
- package/src/eval-scope.js +87 -0
- package/src/global-object.js +40 -1
- package/src/lockdown-shim.js +13 -18
- package/src/make-evaluate.js +110 -0
- package/src/make-function-constructor.js +6 -8
- package/src/make-safe-evaluator.js +32 -40
- package/src/module-instance.js +6 -6
- package/src/module-link.js +22 -36
- package/src/module-load.js +4 -7
- package/src/scope-constants.js +17 -13
- package/src/sloppy-globals-scope-terminator.js +53 -0
- package/src/strict-scope-terminator.js +88 -0
- package/src/tame-locale-methods.js +6 -10
- package/src/whitelist.js +0 -1
- package/src/make-evaluate-factory.js +0 -71
- package/src/scope-handler.js +0 -211
package/index.d.ts
CHANGED
|
@@ -89,7 +89,6 @@ export type ImportHook = (moduleSpecifier: string) => Promise<StaticModuleType>;
|
|
|
89
89
|
export interface CompartmentOptions {
|
|
90
90
|
name?: string;
|
|
91
91
|
transforms?: Array<Transform>;
|
|
92
|
-
globalLexicals?: Record<string, any>;
|
|
93
92
|
moduleMapHook?: ModuleMapHook;
|
|
94
93
|
importHook?: ImportHook;
|
|
95
94
|
resolveHook?: ResolveHook;
|
|
@@ -196,6 +195,7 @@ export interface Assert {
|
|
|
196
195
|
template: TemplateStringsArray | string[],
|
|
197
196
|
...args: any
|
|
198
197
|
): DetailsToken;
|
|
198
|
+
Fail(template: TemplateStringsArray | string[], ...args: any): never;
|
|
199
199
|
quote(payload: any, spaces?: string | number): ToStringable;
|
|
200
200
|
makeAssert: MakeAssert;
|
|
201
201
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ses",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.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.
|
|
62
|
+
"@endo/compartment-mapper": "^0.8.0",
|
|
63
63
|
"@endo/eslint-config": "^0.5.1",
|
|
64
|
-
"@endo/static-module-record": "^0.7.
|
|
64
|
+
"@endo/static-module-record": "^0.7.15",
|
|
65
65
|
"@endo/test262-runner": "^0.1.28",
|
|
66
66
|
"ava": "^3.12.1",
|
|
67
67
|
"babel-eslint": "^10.0.3",
|
|
@@ -75,8 +75,8 @@
|
|
|
75
75
|
"prettier": "^1.19.1",
|
|
76
76
|
"sinon": "8.0.4",
|
|
77
77
|
"terser": "^4.8.0",
|
|
78
|
-
"tsd": "^0.
|
|
79
|
-
"typescript": "~4.
|
|
78
|
+
"tsd": "^0.24.1",
|
|
79
|
+
"typescript": "~4.8.4"
|
|
80
80
|
},
|
|
81
81
|
"files": [
|
|
82
82
|
"LICENSE*",
|
|
@@ -179,5 +179,5 @@
|
|
|
179
179
|
],
|
|
180
180
|
"timeout": "2m"
|
|
181
181
|
},
|
|
182
|
-
"gitHead": "
|
|
182
|
+
"gitHead": "da16a94856482e36296b7cae16d715aa63344928"
|
|
183
183
|
}
|
package/src/commons.js
CHANGED
|
@@ -3,7 +3,6 @@ import {
|
|
|
3
3
|
TypeError,
|
|
4
4
|
arrayPush,
|
|
5
5
|
create,
|
|
6
|
-
defineProperties,
|
|
7
6
|
getOwnPropertyDescriptors,
|
|
8
7
|
} from './commons.js';
|
|
9
8
|
import {
|
|
@@ -28,13 +27,9 @@ export const provideCompartmentEvaluator = (compartmentFields, options) => {
|
|
|
28
27
|
// shared evaluator so we need to build a new one
|
|
29
28
|
|
|
30
29
|
let { globalTransforms } = compartmentFields;
|
|
31
|
-
const {
|
|
32
|
-
globalObject,
|
|
33
|
-
globalLexicals,
|
|
34
|
-
knownScopeProxies,
|
|
35
|
-
} = compartmentFields;
|
|
30
|
+
const { globalObject } = compartmentFields;
|
|
36
31
|
|
|
37
|
-
let
|
|
32
|
+
let moduleLexicals;
|
|
38
33
|
if (__moduleShimLexicals__ !== undefined) {
|
|
39
34
|
// When using `evaluate` for ESM modules, as should only occur from the
|
|
40
35
|
// module-shim's module-instance.js, we do not reveal the SES-shim's
|
|
@@ -46,19 +41,17 @@ export const provideCompartmentEvaluator = (compartmentFields, options) => {
|
|
|
46
41
|
// and `import`, at the expense of being tightly coupled to SES-shim.
|
|
47
42
|
globalTransforms = undefined;
|
|
48
43
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
localObject,
|
|
44
|
+
moduleLexicals = create(
|
|
45
|
+
null,
|
|
52
46
|
getOwnPropertyDescriptors(__moduleShimLexicals__),
|
|
53
47
|
);
|
|
54
48
|
}
|
|
55
49
|
|
|
56
50
|
({ safeEvaluate } = makeSafeEvaluator({
|
|
57
51
|
globalObject,
|
|
58
|
-
|
|
52
|
+
moduleLexicals,
|
|
59
53
|
globalTransforms,
|
|
60
54
|
sloppyGlobalsMode,
|
|
61
|
-
knownScopeProxies,
|
|
62
55
|
}));
|
|
63
56
|
}
|
|
64
57
|
|
|
@@ -66,7 +59,7 @@ export const provideCompartmentEvaluator = (compartmentFields, options) => {
|
|
|
66
59
|
};
|
|
67
60
|
|
|
68
61
|
export const compartmentEvaluate = (compartmentFields, source, options) => {
|
|
69
|
-
// Perform this check first to avoid
|
|
62
|
+
// Perform this check first to avoid unnecessary sanitizing.
|
|
70
63
|
// TODO Maybe relax string check and coerce instead:
|
|
71
64
|
// https://github.com/tc39/proposal-dynamic-code-brand-checks
|
|
72
65
|
if (typeof source !== 'string') {
|
package/src/compartment-shim.js
CHANGED
|
@@ -7,25 +7,19 @@ import {
|
|
|
7
7
|
ReferenceError,
|
|
8
8
|
TypeError,
|
|
9
9
|
WeakMap,
|
|
10
|
-
WeakSet,
|
|
11
|
-
arrayFilter,
|
|
12
|
-
arrayJoin,
|
|
13
10
|
assign,
|
|
14
11
|
defineProperties,
|
|
15
12
|
entries,
|
|
16
|
-
freeze,
|
|
17
|
-
getOwnPropertyNames,
|
|
18
13
|
promiseThen,
|
|
19
14
|
weakmapGet,
|
|
20
15
|
weakmapSet,
|
|
21
|
-
weaksetHas,
|
|
22
16
|
} from './commons.js';
|
|
23
17
|
import {
|
|
18
|
+
setGlobalObjectSymbolUnscopables,
|
|
24
19
|
setGlobalObjectConstantProperties,
|
|
25
20
|
setGlobalObjectMutableProperties,
|
|
26
21
|
setGlobalObjectEvaluators,
|
|
27
22
|
} from './global-object.js';
|
|
28
|
-
import { isValidIdentifierName } from './scope-constants.js';
|
|
29
23
|
import { sharedGlobalPropertyNames } from './whitelist.js';
|
|
30
24
|
import { load } from './module-load.js';
|
|
31
25
|
import { link } from './module-link.js';
|
|
@@ -119,12 +113,6 @@ export const CompartmentPrototype = {
|
|
|
119
113
|
return '[object Compartment]';
|
|
120
114
|
},
|
|
121
115
|
|
|
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
116
|
module(specifier) {
|
|
129
117
|
if (typeof specifier !== 'string') {
|
|
130
118
|
throw new TypeError('first argument of module() must be a string');
|
|
@@ -214,7 +202,6 @@ export const makeCompartmentConstructor = (
|
|
|
214
202
|
name = '<unknown>',
|
|
215
203
|
transforms = [],
|
|
216
204
|
__shimTransforms__ = [],
|
|
217
|
-
globalLexicals: globalLexicalsOption = {},
|
|
218
205
|
resolveHook,
|
|
219
206
|
importHook,
|
|
220
207
|
moduleMapHook,
|
|
@@ -253,30 +240,10 @@ export const makeCompartmentConstructor = (
|
|
|
253
240
|
}
|
|
254
241
|
}
|
|
255
242
|
|
|
256
|
-
const invalidNames = arrayFilter(
|
|
257
|
-
getOwnPropertyNames(globalLexicalsOption),
|
|
258
|
-
identifier => !isValidIdentifierName(identifier),
|
|
259
|
-
);
|
|
260
|
-
if (invalidNames.length) {
|
|
261
|
-
throw new TypeError(
|
|
262
|
-
`Cannot create compartment with invalid names for global lexicals: ${arrayJoin(
|
|
263
|
-
invalidNames,
|
|
264
|
-
', ',
|
|
265
|
-
)}; these names would not be lexically mentionable`,
|
|
266
|
-
);
|
|
267
|
-
}
|
|
268
|
-
// The caller continues to own the globalLexicals object they passed to
|
|
269
|
-
// the compartment constructor, but the compartment only respects the
|
|
270
|
-
// original values and they are constants in the scope of evaluated
|
|
271
|
-
// programs and executed modules.
|
|
272
|
-
// This shallow copy captures only the values of enumerable own
|
|
273
|
-
// properties, erasing accessors.
|
|
274
|
-
// The snapshot is frozen to ensure that the properties are immutable
|
|
275
|
-
// when transferred-by-property-descriptor onto local scope objects.
|
|
276
|
-
const globalLexicals = freeze({ ...globalLexicalsOption });
|
|
277
|
-
|
|
278
243
|
const globalObject = {};
|
|
279
244
|
|
|
245
|
+
setGlobalObjectSymbolUnscopables(globalObject);
|
|
246
|
+
|
|
280
247
|
// We must initialize all constant properties first because
|
|
281
248
|
// `makeSafeEvaluator` may use them to create optimized bindings
|
|
282
249
|
// in the evaluator.
|
|
@@ -284,13 +251,10 @@ export const makeCompartmentConstructor = (
|
|
|
284
251
|
// evaluator is no longer eagerly created
|
|
285
252
|
setGlobalObjectConstantProperties(globalObject);
|
|
286
253
|
|
|
287
|
-
const knownScopeProxies = new WeakSet();
|
|
288
254
|
const { safeEvaluate } = makeSafeEvaluator({
|
|
289
255
|
globalObject,
|
|
290
|
-
globalLexicals,
|
|
291
256
|
globalTransforms,
|
|
292
257
|
sloppyGlobalsMode: false,
|
|
293
|
-
knownScopeProxies,
|
|
294
258
|
});
|
|
295
259
|
|
|
296
260
|
setGlobalObjectMutableProperties(globalObject, {
|
|
@@ -313,8 +277,6 @@ export const makeCompartmentConstructor = (
|
|
|
313
277
|
name: `${name}`,
|
|
314
278
|
globalTransforms,
|
|
315
279
|
globalObject,
|
|
316
|
-
knownScopeProxies,
|
|
317
|
-
globalLexicals,
|
|
318
280
|
safeEvaluate,
|
|
319
281
|
resolveHook,
|
|
320
282
|
importHook,
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { arrayPush, freeze } from './commons.js';
|
|
6
6
|
import { assert } from './error/assert.js';
|
|
7
7
|
|
|
8
|
-
const { details: X, quote: q } = assert;
|
|
8
|
+
const { details: X, Fail, quote: q } = assert;
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* JavaScript module semantics resists attempts to parameterize a module's
|
|
@@ -109,12 +109,9 @@ export const makeEnvironmentCaptor = aGlobal => {
|
|
|
109
109
|
}
|
|
110
110
|
setting === undefined ||
|
|
111
111
|
typeof setting === 'string' ||
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
setting,
|
|
116
|
-
)}, if present, must be a string.`,
|
|
117
|
-
);
|
|
112
|
+
Fail`Environment option value ${q(
|
|
113
|
+
setting,
|
|
114
|
+
)}, if present, must be a string.`;
|
|
118
115
|
return setting;
|
|
119
116
|
};
|
|
120
117
|
freeze(getEnvironmentOption);
|
package/src/error/assert.js
CHANGED
|
@@ -375,6 +375,9 @@ const makeAssert = (optRaise = undefined, unredacted = false) => {
|
|
|
375
375
|
};
|
|
376
376
|
freeze(fail);
|
|
377
377
|
|
|
378
|
+
/** @type {FailTag} */
|
|
379
|
+
const Fail = (template, ...args) => fail(details(template, ...args));
|
|
380
|
+
|
|
378
381
|
// Don't freeze or export `baseAssert` until we add methods.
|
|
379
382
|
// TODO If I change this from a `function` function to an arrow
|
|
380
383
|
// function, I seem to get type errors from TypeScript. Why?
|
|
@@ -433,6 +436,7 @@ const makeAssert = (optRaise = undefined, unredacted = false) => {
|
|
|
433
436
|
string: assertString,
|
|
434
437
|
note,
|
|
435
438
|
details,
|
|
439
|
+
Fail,
|
|
436
440
|
quote,
|
|
437
441
|
makeAssert,
|
|
438
442
|
});
|
package/src/error/types.js
CHANGED
|
@@ -260,6 +260,64 @@
|
|
|
260
260
|
* `string[]` can also be used as a template.
|
|
261
261
|
*/
|
|
262
262
|
|
|
263
|
+
/**
|
|
264
|
+
* @typedef {(template: TemplateStringsArray | string[], ...args: any) => never} FailTag
|
|
265
|
+
* The `Fail` tamplate tag supports replacing patterns like
|
|
266
|
+
* ```js
|
|
267
|
+
* assert(cond, X`...complaint...`);
|
|
268
|
+
* ```
|
|
269
|
+
* or
|
|
270
|
+
* ```js
|
|
271
|
+
* cond || assert.fail(X`...complaint...`);
|
|
272
|
+
* ```
|
|
273
|
+
* with patterns like
|
|
274
|
+
* ```js
|
|
275
|
+
* cond || Fail`...complaint...`;
|
|
276
|
+
* ```
|
|
277
|
+
*
|
|
278
|
+
* However, due to [weakness in current
|
|
279
|
+
* TypeScript](https://github.com/microsoft/TypeScript/issues/51426), the `||`
|
|
280
|
+
* patterns are not as powerful as the `assert(...)` call at enabling static
|
|
281
|
+
* reasoning. Of the `||`, again due to weaknesses in current TypeScript,
|
|
282
|
+
* the
|
|
283
|
+
* ```js
|
|
284
|
+
* cond || Fail`...complaint...`
|
|
285
|
+
* ```
|
|
286
|
+
* pattern is not as powerful as the
|
|
287
|
+
* ```js
|
|
288
|
+
* cond || assert.fail(X`...complaint...`);
|
|
289
|
+
* ```
|
|
290
|
+
* at enabling static resoning. Despite these problems, we do not want to
|
|
291
|
+
* return to the
|
|
292
|
+
* ```js
|
|
293
|
+
* assert(cond, X`...complaint...`)
|
|
294
|
+
* ```
|
|
295
|
+
* style because of the substantial overhead in
|
|
296
|
+
* evaluating the `X` template in the typical `true` case where it is not
|
|
297
|
+
* needed. And we do not want to return to the
|
|
298
|
+
* ```js
|
|
299
|
+
* assert.fail(X`...complaint...`)`
|
|
300
|
+
* ```
|
|
301
|
+
* because of the verbosity and loss of readability. Instead, until/unless
|
|
302
|
+
* https://github.com/microsoft/TypeScript/issues/51426 is fixed, for those
|
|
303
|
+
* new-style assertions where this loss of static reasoning is a problem,
|
|
304
|
+
* instead express the assertion as
|
|
305
|
+
* ```js
|
|
306
|
+
* if (!cond) {
|
|
307
|
+
* Fail`...complaint...`;
|
|
308
|
+
* }
|
|
309
|
+
* ```
|
|
310
|
+
* or, if needed,
|
|
311
|
+
* ```js
|
|
312
|
+
* if (!cond) {
|
|
313
|
+
* // `throw` is noop since `Fail` throws. But linter confused
|
|
314
|
+
* throw Fail`...complaint...`;
|
|
315
|
+
* }
|
|
316
|
+
* ```
|
|
317
|
+
* This avoid the TypeScript bugs that cause the loss of static reasoning,
|
|
318
|
+
* but with no loss of efficiency and little loss of readability.
|
|
319
|
+
*/
|
|
320
|
+
|
|
263
321
|
/**
|
|
264
322
|
* assert that expr is truthy, with an optional details to describe
|
|
265
323
|
* the assertion. It is a tagged template literal like
|
|
@@ -286,6 +344,7 @@
|
|
|
286
344
|
* string: AssertString,
|
|
287
345
|
* note: AssertNote,
|
|
288
346
|
* details: DetailsTag,
|
|
347
|
+
* Fail: FailTag,
|
|
289
348
|
* quote: AssertQuote,
|
|
290
349
|
* makeAssert: MakeAssert,
|
|
291
350
|
* } } Assert
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { FERAL_EVAL, create, defineProperties, freeze } from './commons.js';
|
|
2
|
+
|
|
3
|
+
import { assert } from './error/assert.js';
|
|
4
|
+
|
|
5
|
+
const { Fail } = 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
|
+
Fail`a handler did not reset allowNextEvalToBeUnsafe ${this.revoked.err}`;
|
|
77
|
+
}
|
|
78
|
+
// Allow next reference to eval produce the unsafe FERAL_EVAL.
|
|
79
|
+
// We avoid defineProperty because it consumes an extra stack frame taming
|
|
80
|
+
// its return value.
|
|
81
|
+
defineProperties(evalScope, oneTimeEvalProperties);
|
|
82
|
+
},
|
|
83
|
+
revoked: null,
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
return evalScopeKit;
|
|
87
|
+
};
|
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
|
package/src/lockdown-shim.js
CHANGED
|
@@ -55,7 +55,7 @@ import { makeCompartmentConstructor } from './compartment-shim.js';
|
|
|
55
55
|
|
|
56
56
|
/** @typedef {import('../index.js').LockdownOptions} LockdownOptions */
|
|
57
57
|
|
|
58
|
-
const { details: d, quote: q } = assert;
|
|
58
|
+
const { Fail, details: d, quote: q } = assert;
|
|
59
59
|
|
|
60
60
|
/** @type {Error=} */
|
|
61
61
|
let priorLockdown;
|
|
@@ -82,7 +82,6 @@ const harden = makeHardener();
|
|
|
82
82
|
* @param {Object} [options]
|
|
83
83
|
* @param {Array<Transform>} [options.transforms]
|
|
84
84
|
* @param {Array<Transform>} [options.__shimTransforms__]
|
|
85
|
-
* @param {Object} [options.globalLexicals]
|
|
86
85
|
*/
|
|
87
86
|
|
|
88
87
|
// TODO https://github.com/endojs/endo/issues/814
|
|
@@ -192,26 +191,23 @@ export const repairIntrinsics = (options = {}) => {
|
|
|
192
191
|
);
|
|
193
192
|
}
|
|
194
193
|
|
|
195
|
-
|
|
196
|
-
evalTaming === '
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
d`lockdown(): non supported option evalTaming: ${q(evalTaming)}`,
|
|
200
|
-
);
|
|
194
|
+
evalTaming === 'unsafeEval' ||
|
|
195
|
+
evalTaming === 'safeEval' ||
|
|
196
|
+
evalTaming === 'noEval' ||
|
|
197
|
+
Fail`lockdown(): non supported option evalTaming: ${q(evalTaming)}`;
|
|
201
198
|
|
|
202
199
|
// Assert that only supported options were passed.
|
|
203
200
|
// Use Reflect.ownKeys to reject symbol-named properties as well.
|
|
204
201
|
const extraOptionsNames = ownKeys(extraOptions);
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
d`lockdown(): non supported option ${q(extraOptionsNames)}`,
|
|
208
|
-
);
|
|
202
|
+
extraOptionsNames.length === 0 ||
|
|
203
|
+
Fail`lockdown(): non supported option ${q(extraOptionsNames)}`;
|
|
209
204
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
205
|
+
priorLockdown === undefined ||
|
|
206
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
207
|
+
assert.fail(
|
|
208
|
+
d`Already locked down at ${priorLockdown} (SES_ALREADY_LOCKED_DOWN)`,
|
|
209
|
+
TypeError,
|
|
210
|
+
);
|
|
215
211
|
priorLockdown = new TypeError('Prior lockdown (SES_ALREADY_LOCKED_DOWN)');
|
|
216
212
|
// Tease V8 to generate the stack string and release the closures the stack
|
|
217
213
|
// trace retained:
|
|
@@ -253,7 +249,6 @@ export const repairIntrinsics = (options = {}) => {
|
|
|
253
249
|
};
|
|
254
250
|
|
|
255
251
|
if (seemsToBeLockedDown()) {
|
|
256
|
-
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
257
252
|
throw new TypeError(
|
|
258
253
|
`Already locked down but not by this SES instance (SES_MULTIPLE_INSTANCES)`,
|
|
259
254
|
);
|
|
@@ -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.moduleLexicals
|
|
30
|
+
* @param {object} context.globalObject
|
|
31
|
+
* @param {object} context.scopeTerminator
|
|
32
|
+
*/
|
|
33
|
+
export const makeEvaluate = context => {
|
|
34
|
+
const { globalObjectConstants, moduleLexicalConstants } = getScopeConstants(
|
|
35
|
+
context.globalObject,
|
|
36
|
+
context.moduleLexicals,
|
|
37
|
+
);
|
|
38
|
+
const globalObjectOptimizer = buildOptimizer(
|
|
39
|
+
globalObjectConstants,
|
|
40
|
+
'globalObject',
|
|
41
|
+
);
|
|
42
|
+
const moduleLexicalOptimizer = buildOptimizer(
|
|
43
|
+
moduleLexicalConstants,
|
|
44
|
+
'moduleLexicals',
|
|
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) `moduleLexicals` 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
|
+
// `moduleLexicals` 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.moduleLexicals) {
|
|
96
|
+
with (this.evalScope) {
|
|
97
|
+
${globalObjectOptimizer}
|
|
98
|
+
${moduleLexicalOptimizer}
|
|
99
|
+
return function() {
|
|
100
|
+
'use strict';
|
|
101
|
+
return eval(arguments[0]);
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
`);
|
|
108
|
+
|
|
109
|
+
return apply(evaluateFactory, context, []);
|
|
110
|
+
};
|
|
@@ -7,6 +7,8 @@ import {
|
|
|
7
7
|
} from './commons.js';
|
|
8
8
|
import { assert } from './error/assert.js';
|
|
9
9
|
|
|
10
|
+
const { Fail } = assert;
|
|
11
|
+
|
|
10
12
|
/*
|
|
11
13
|
* makeFunctionConstructor()
|
|
12
14
|
* A safe version of the native Function which relies on
|
|
@@ -67,14 +69,10 @@ export const makeFunctionConstructor = safeEvaluate => {
|
|
|
67
69
|
});
|
|
68
70
|
|
|
69
71
|
// Assert identity of Function.__proto__ accross all compartments
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
assert(
|
|
75
|
-
getPrototypeOf(newFunction) === FERAL_FUNCTION.prototype,
|
|
76
|
-
'Function constructor prototype is the same accross compartments',
|
|
77
|
-
);
|
|
72
|
+
getPrototypeOf(FERAL_FUNCTION) === FERAL_FUNCTION.prototype ||
|
|
73
|
+
Fail`Function prototype is the same accross compartments`;
|
|
74
|
+
getPrototypeOf(newFunction) === FERAL_FUNCTION.prototype ||
|
|
75
|
+
Fail`Function constructor prototype is the same accross compartments`;
|
|
78
76
|
|
|
79
77
|
return newFunction;
|
|
80
78
|
};
|