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
|
@@ -1,20 +1,15 @@
|
|
|
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
|
-
const {
|
|
12
|
+
const { Fail } = assert;
|
|
18
13
|
|
|
19
14
|
/**
|
|
20
15
|
* makeSafeEvaluator()
|
|
@@ -23,40 +18,36 @@ const { details: d } = assert;
|
|
|
23
18
|
*
|
|
24
19
|
* @param {Object} options
|
|
25
20
|
* @param {Object} options.globalObject
|
|
26
|
-
* @param {Object} [options.
|
|
21
|
+
* @param {Object} [options.moduleLexicals]
|
|
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
|
+
moduleLexicals = {},
|
|
34
28
|
globalTransforms = [],
|
|
35
29
|
sloppyGlobalsMode = false,
|
|
36
|
-
knownScopeProxies = new WeakSet(),
|
|
37
30
|
} = {}) => {
|
|
38
|
-
const
|
|
31
|
+
const scopeTerminator = sloppyGlobalsMode
|
|
32
|
+
? createSloppyGlobalsScopeTerminator(globalObject)
|
|
33
|
+
: strictScopeTerminator;
|
|
34
|
+
const evalScopeKit = makeEvalScopeKit();
|
|
35
|
+
const { evalScope } = evalScopeKit;
|
|
36
|
+
|
|
37
|
+
const evaluateContext = freeze({
|
|
38
|
+
evalScope,
|
|
39
|
+
moduleLexicals,
|
|
39
40
|
globalObject,
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
sloppyGlobalsMode,
|
|
43
|
-
},
|
|
44
|
-
);
|
|
45
|
-
const { proxy: scopeProxy, revoke: revokeScopeProxy } = proxyRevocable(
|
|
46
|
-
immutableObject,
|
|
47
|
-
scopeHandler,
|
|
48
|
-
);
|
|
49
|
-
weaksetAdd(knownScopeProxies, scopeProxy);
|
|
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,11 +97,10 @@ 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
|
-
|
|
111
|
-
assert.fail(d`handler did not reset allowNextEvalToBeUnsafe ${err}`);
|
|
103
|
+
Fail`handler did not reset allowNextEvalToBeUnsafe ${err}`;
|
|
112
104
|
}
|
|
113
105
|
}
|
|
114
106
|
};
|
package/src/module-instance.js
CHANGED
|
@@ -152,10 +152,10 @@ export const makeModuleInstance = (
|
|
|
152
152
|
// object (eventually proxied).
|
|
153
153
|
const exportsProps = create(null);
|
|
154
154
|
|
|
155
|
-
// {_localName_: accessor} proxy traps for
|
|
156
|
-
// The
|
|
157
|
-
//
|
|
158
|
-
const
|
|
155
|
+
// {_localName_: accessor} proxy traps for moduleLexicals and live bindings.
|
|
156
|
+
// The moduleLexicals object is frozen and the corresponding properties of
|
|
157
|
+
// moduleLexicals must be immutable, so we copy the descriptors.
|
|
158
|
+
const moduleLexicals = create(null);
|
|
159
159
|
|
|
160
160
|
// {_localName_: init(initValue) -> initValue} used by the
|
|
161
161
|
// rewritten code to initialize exported fixed bindings.
|
|
@@ -321,7 +321,7 @@ export const makeModuleInstance = (
|
|
|
321
321
|
|
|
322
322
|
localGetNotify[localName] = liveGetNotify;
|
|
323
323
|
if (setProxyTrap) {
|
|
324
|
-
defineProperty(
|
|
324
|
+
defineProperty(moduleLexicals, localName, {
|
|
325
325
|
get,
|
|
326
326
|
set,
|
|
327
327
|
enumerable: true,
|
|
@@ -437,7 +437,7 @@ export const makeModuleInstance = (
|
|
|
437
437
|
let optFunctor = compartmentEvaluate(compartmentFields, functorSource, {
|
|
438
438
|
globalObject: compartment.globalThis,
|
|
439
439
|
transforms: __shimTransforms__,
|
|
440
|
-
__moduleShimLexicals__:
|
|
440
|
+
__moduleShimLexicals__: moduleLexicals,
|
|
441
441
|
});
|
|
442
442
|
let didThrow = false;
|
|
443
443
|
let thrownError;
|
package/src/module-link.js
CHANGED
|
@@ -26,7 +26,7 @@ import {
|
|
|
26
26
|
weakmapGet,
|
|
27
27
|
} from './commons.js';
|
|
28
28
|
|
|
29
|
-
const { quote: q } = assert;
|
|
29
|
+
const { Fail, quote: q } = assert;
|
|
30
30
|
|
|
31
31
|
// `link` creates `ModuleInstances` and `ModuleNamespaces` for a module and its
|
|
32
32
|
// transitive dependencies and connects their imports and exports.
|
|
@@ -70,18 +70,14 @@ function validatePrecompiledStaticModuleRecord(
|
|
|
70
70
|
moduleSpecifier,
|
|
71
71
|
) {
|
|
72
72
|
const { __fixedExportMap__, __liveExportMap__ } = staticModuleRecord;
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
`Property '__fixedExportMap__' of a precompiled module record must be an object, got ${q(
|
|
73
|
+
isObject(__fixedExportMap__) ||
|
|
74
|
+
Fail`Property '__fixedExportMap__' of a precompiled module record must be an object, got ${q(
|
|
76
75
|
__fixedExportMap__,
|
|
77
|
-
)}, for module ${q(moduleSpecifier)}
|
|
78
|
-
)
|
|
79
|
-
|
|
80
|
-
isObject(__liveExportMap__),
|
|
81
|
-
`Property '__liveExportMap__' of a precompiled module record must be an object, got ${q(
|
|
76
|
+
)}, for module ${q(moduleSpecifier)}`;
|
|
77
|
+
isObject(__liveExportMap__) ||
|
|
78
|
+
Fail`Property '__liveExportMap__' of a precompiled module record must be an object, got ${q(
|
|
82
79
|
__liveExportMap__,
|
|
83
|
-
)}, for module ${q(moduleSpecifier)}
|
|
84
|
-
);
|
|
80
|
+
)}, for module ${q(moduleSpecifier)}`;
|
|
85
81
|
}
|
|
86
82
|
|
|
87
83
|
function isThirdParty(staticModuleRecord) {
|
|
@@ -93,40 +89,30 @@ function validateThirdPartyStaticModuleRecord(
|
|
|
93
89
|
moduleSpecifier,
|
|
94
90
|
) {
|
|
95
91
|
const { exports } = staticModuleRecord;
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
`Property 'exports' of a third-party static module record must be an array, got ${q(
|
|
92
|
+
isArray(exports) ||
|
|
93
|
+
Fail`Property 'exports' of a third-party static module record must be an array, got ${q(
|
|
99
94
|
exports,
|
|
100
|
-
)}, for module ${q(moduleSpecifier)}
|
|
101
|
-
);
|
|
95
|
+
)}, for module ${q(moduleSpecifier)}`;
|
|
102
96
|
}
|
|
103
97
|
|
|
104
98
|
function validateStaticModuleRecord(staticModuleRecord, moduleSpecifier) {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
`Static module records must be of type object, got ${q(
|
|
99
|
+
isObject(staticModuleRecord) ||
|
|
100
|
+
Fail`Static module records must be of type object, got ${q(
|
|
108
101
|
staticModuleRecord,
|
|
109
|
-
)}, for module ${q(moduleSpecifier)}
|
|
110
|
-
);
|
|
102
|
+
)}, for module ${q(moduleSpecifier)}`;
|
|
111
103
|
const { imports, exports, reexports = [] } = staticModuleRecord;
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
`Property 'imports' of a static module record must be an array, got ${q(
|
|
104
|
+
isArray(imports) ||
|
|
105
|
+
Fail`Property 'imports' of a static module record must be an array, got ${q(
|
|
115
106
|
imports,
|
|
116
|
-
)}, for module ${q(moduleSpecifier)}
|
|
117
|
-
)
|
|
118
|
-
|
|
119
|
-
isArray(exports),
|
|
120
|
-
`Property 'exports' of a precompiled module record must be an array, got ${q(
|
|
107
|
+
)}, for module ${q(moduleSpecifier)}`;
|
|
108
|
+
isArray(exports) ||
|
|
109
|
+
Fail`Property 'exports' of a precompiled module record must be an array, got ${q(
|
|
121
110
|
exports,
|
|
122
|
-
)}, for module ${q(moduleSpecifier)}
|
|
123
|
-
)
|
|
124
|
-
|
|
125
|
-
isArray(reexports),
|
|
126
|
-
`Property 'reexports' of a precompiled module record must be an array if present, got ${q(
|
|
111
|
+
)}, for module ${q(moduleSpecifier)}`;
|
|
112
|
+
isArray(reexports) ||
|
|
113
|
+
Fail`Property 'reexports' of a precompiled module record must be an array if present, got ${q(
|
|
127
114
|
reexports,
|
|
128
|
-
)}, for module ${q(moduleSpecifier)}
|
|
129
|
-
);
|
|
115
|
+
)}, for module ${q(moduleSpecifier)}`;
|
|
130
116
|
}
|
|
131
117
|
|
|
132
118
|
export const instantiate = (
|
package/src/module-load.js
CHANGED
|
@@ -27,7 +27,7 @@ import {
|
|
|
27
27
|
} from './commons.js';
|
|
28
28
|
import { assert } from './error/assert.js';
|
|
29
29
|
|
|
30
|
-
const { details: d, quote: q } = assert;
|
|
30
|
+
const { Fail, details: d, quote: q } = assert;
|
|
31
31
|
|
|
32
32
|
const noop = () => {};
|
|
33
33
|
|
|
@@ -170,12 +170,9 @@ const loadWithoutErrorAnnotation = async (
|
|
|
170
170
|
const staticModuleRecord = await importHook(moduleSpecifier);
|
|
171
171
|
|
|
172
172
|
if (staticModuleRecord === null || typeof staticModuleRecord !== 'object') {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
moduleSpecifier,
|
|
177
|
-
)} in compartment ${q(compartment.name)}`,
|
|
178
|
-
);
|
|
173
|
+
Fail`importHook must return a promise for an object, for module ${q(
|
|
174
|
+
moduleSpecifier,
|
|
175
|
+
)} in compartment ${q(compartment.name)}`;
|
|
179
176
|
}
|
|
180
177
|
|
|
181
178
|
if (staticModuleRecord.record !== undefined) {
|
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} moduleLexicals
|
|
148
148
|
*/
|
|
149
|
-
export const getScopeConstants = (globalObject,
|
|
149
|
+
export const getScopeConstants = (globalObject, moduleLexicals = {}) => {
|
|
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 moduleLexicalNames = getOwnPropertyNames(moduleLexicals);
|
|
154
154
|
|
|
155
155
|
// Collect all valid & immutable identifiers from the endowments.
|
|
156
|
-
const
|
|
157
|
-
|
|
156
|
+
const moduleLexicalConstants = arrayFilter(
|
|
157
|
+
moduleLexicalNames,
|
|
158
158
|
name =>
|
|
159
|
-
isValidIdentifierName(name) &&
|
|
159
|
+
isValidIdentifierName(name) &&
|
|
160
|
+
isImmutableDataProperty(moduleLexicals, 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(moduleLexicalNames, name) &&
|
|
170
171
|
isValidIdentifierName(name) &&
|
|
171
172
|
isImmutableDataProperty(globalObject, name),
|
|
172
173
|
);
|
|
173
174
|
|
|
174
|
-
return
|
|
175
|
+
return {
|
|
176
|
+
globalObjectConstants,
|
|
177
|
+
moduleLexicalConstants,
|
|
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,88 @@
|
|
|
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 { Fail, 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
|
+
Fail`Please report unexpected scope handler trap: ${q(String(prop))}`;
|
|
28
|
+
},
|
|
29
|
+
}),
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
/*
|
|
33
|
+
* scopeProxyHandlerProperties
|
|
34
|
+
* scopeTerminatorHandler manages a strictScopeTerminator Proxy which serves as
|
|
35
|
+
* the final scope boundary that will always return "undefined" in order
|
|
36
|
+
* to prevent access to "start compartment globals".
|
|
37
|
+
*/
|
|
38
|
+
const scopeProxyHandlerProperties = {
|
|
39
|
+
get(_shadow, _prop) {
|
|
40
|
+
return undefined;
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
set(_shadow, prop, _value) {
|
|
44
|
+
// We should only hit this if the has() hook returned true matches the v8
|
|
45
|
+
// ReferenceError message "Uncaught ReferenceError: xyz is not defined"
|
|
46
|
+
throw new ReferenceError(`${String(prop)} is not defined`);
|
|
47
|
+
},
|
|
48
|
+
|
|
49
|
+
has(_shadow, prop) {
|
|
50
|
+
// we must at least return true for all properties on the realm globalThis
|
|
51
|
+
return prop in globalThis;
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
// note: this is likely a bug of safari
|
|
55
|
+
// https://bugs.webkit.org/show_bug.cgi?id=195534
|
|
56
|
+
getPrototypeOf() {
|
|
57
|
+
return null;
|
|
58
|
+
},
|
|
59
|
+
|
|
60
|
+
// Chip has seen this happen single stepping under the Chrome/v8 debugger.
|
|
61
|
+
// TODO record how to reliably reproduce, and to test if this fix helps.
|
|
62
|
+
// TODO report as bug to v8 or Chrome, and record issue link here.
|
|
63
|
+
getOwnPropertyDescriptor(_target, prop) {
|
|
64
|
+
// Coerce with `String` in case prop is a symbol.
|
|
65
|
+
const quotedProp = q(String(prop));
|
|
66
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
67
|
+
console.warn(
|
|
68
|
+
`getOwnPropertyDescriptor trap on scopeTerminatorHandler for ${quotedProp}`,
|
|
69
|
+
new TypeError().stack,
|
|
70
|
+
);
|
|
71
|
+
return undefined;
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
// The scope handler's prototype is a proxy that throws if any trap other
|
|
76
|
+
// than get/set/has are run (like getOwnPropertyDescriptors, apply,
|
|
77
|
+
// getPrototypeOf).
|
|
78
|
+
export const strictScopeTerminatorHandler = freeze(
|
|
79
|
+
create(
|
|
80
|
+
alwaysThrowHandler,
|
|
81
|
+
getOwnPropertyDescriptors(scopeProxyHandlerProperties),
|
|
82
|
+
),
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
export const strictScopeTerminator = new Proxy(
|
|
86
|
+
immutableObject,
|
|
87
|
+
strictScopeTerminatorHandler,
|
|
88
|
+
);
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
} from './commons.js';
|
|
10
10
|
import { assert } from './error/assert.js';
|
|
11
11
|
|
|
12
|
-
const {
|
|
12
|
+
const { Fail, quote: q } = assert;
|
|
13
13
|
|
|
14
14
|
const localePattern = /^(\w*[a-z])Locale([A-Z]\w*)$/;
|
|
15
15
|
|
|
@@ -31,7 +31,7 @@ const tamedMethods = {
|
|
|
31
31
|
if (s > that) {
|
|
32
32
|
return 1;
|
|
33
33
|
}
|
|
34
|
-
|
|
34
|
+
s === that || Fail`expected ${q(s)} and ${q(that)} to compare`;
|
|
35
35
|
return 0;
|
|
36
36
|
},
|
|
37
37
|
|
|
@@ -61,16 +61,12 @@ export default function tameLocaleMethods(intrinsics, localeTaming = 'safe') {
|
|
|
61
61
|
for (const methodName of getOwnPropertyNames(intrinsic)) {
|
|
62
62
|
const match = regexpExec(localePattern, methodName);
|
|
63
63
|
if (match) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
d`expected ${q(methodName)} to be a function`,
|
|
67
|
-
);
|
|
64
|
+
typeof intrinsic[methodName] === 'function' ||
|
|
65
|
+
Fail`expected ${q(methodName)} to be a function`;
|
|
68
66
|
const nonLocaleMethodName = `${match[1]}${match[2]}`;
|
|
69
67
|
const method = intrinsic[nonLocaleMethodName];
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
d`function ${q(nonLocaleMethodName)} not found`,
|
|
73
|
-
);
|
|
68
|
+
typeof method === 'function' ||
|
|
69
|
+
Fail`function ${q(nonLocaleMethodName)} not found`;
|
|
74
70
|
defineProperty(intrinsic, methodName, { value: method });
|
|
75
71
|
}
|
|
76
72
|
}
|
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
|
-
};
|