ses 0.14.3 → 0.15.2
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 +60 -0
- package/NEWS.md +51 -1
- package/dist/lockdown.cjs +3090 -3792
- package/dist/lockdown.mjs +3090 -3792
- package/dist/lockdown.umd.js +3090 -3792
- package/dist/lockdown.umd.min.js +1 -1
- package/dist/ses.cjs +3090 -3792
- package/dist/ses.mjs +3090 -3792
- package/dist/ses.umd.js +3090 -3792
- package/dist/ses.umd.min.js +1 -1
- package/index.d.ts +1 -1
- package/index.js +3 -11
- package/package.json +8 -8
- package/src/compartment-evaluate.js +58 -28
- package/src/compartment-shim.js +44 -27
- package/src/environment-options.js +125 -0
- package/src/error/stringify-utils.js +24 -1
- package/src/error/tame-console.js +1 -3
- package/src/global-object.js +33 -26
- package/src/lockdown-shim.js +91 -109
- package/src/make-eval-function.js +3 -5
- package/src/make-function-constructor.js +3 -4
- package/src/make-hardener.js +2 -2
- package/src/make-safe-evaluator.js +117 -0
- package/src/scope-handler.js +13 -2
- package/src/whitelist-intrinsics.js +3 -3
- package/src/whitelist.js +13 -0
- package/src/evaluate.js +0 -102
package/src/lockdown-shim.js
CHANGED
|
@@ -14,7 +14,16 @@
|
|
|
14
14
|
|
|
15
15
|
// @ts-check
|
|
16
16
|
|
|
17
|
-
import {
|
|
17
|
+
import {
|
|
18
|
+
TypeError,
|
|
19
|
+
arrayFilter,
|
|
20
|
+
arrayMap,
|
|
21
|
+
globalThis,
|
|
22
|
+
is,
|
|
23
|
+
ownKeys,
|
|
24
|
+
stringSplit,
|
|
25
|
+
} from './commons.js';
|
|
26
|
+
import { enJoin } from './error/stringify-utils.js';
|
|
18
27
|
import { makeHardener } from './make-hardener.js';
|
|
19
28
|
import { makeIntrinsicsCollector } from './intrinsics.js';
|
|
20
29
|
import whitelistIntrinsics from './whitelist-intrinsics.js';
|
|
@@ -24,7 +33,11 @@ import tameMathObject from './tame-math-object.js';
|
|
|
24
33
|
import tameRegExpConstructor from './tame-regexp-constructor.js';
|
|
25
34
|
import enablePropertyOverrides from './enable-property-overrides.js';
|
|
26
35
|
import tameLocaleMethods from './tame-locale-methods.js';
|
|
27
|
-
import {
|
|
36
|
+
import {
|
|
37
|
+
setGlobalObjectConstantProperties,
|
|
38
|
+
setGlobalObjectMutableProperties,
|
|
39
|
+
} from './global-object.js';
|
|
40
|
+
import { makeSafeEvaluator } from './make-safe-evaluator.js';
|
|
28
41
|
import { initialGlobalPropertyNames } from './whitelist.js';
|
|
29
42
|
import { tameFunctionToString } from './tame-function-tostring.js';
|
|
30
43
|
import { tameDomains } from './tame-domains.js';
|
|
@@ -32,12 +45,16 @@ import { tameDomains } from './tame-domains.js';
|
|
|
32
45
|
import { tameConsole } from './error/tame-console.js';
|
|
33
46
|
import tameErrorConstructor from './error/tame-error-constructor.js';
|
|
34
47
|
import { assert, makeAssert } from './error/assert.js';
|
|
48
|
+
import { makeEnvironmentCaptor } from './environment-options.js';
|
|
49
|
+
import { getAnonymousIntrinsics } from './get-anonymous-intrinsics.js';
|
|
50
|
+
import { makeCompartmentConstructor } from './compartment-shim.js';
|
|
35
51
|
|
|
36
52
|
/** @typedef {import('../index.js').LockdownOptions} LockdownOptions */
|
|
37
53
|
|
|
38
54
|
const { details: d, quote: q } = assert;
|
|
39
55
|
|
|
40
|
-
|
|
56
|
+
/** @type {Error=} */
|
|
57
|
+
let priorLockdown;
|
|
41
58
|
|
|
42
59
|
// Build a harden() with an empty fringe.
|
|
43
60
|
// Gate it on lockdown.
|
|
@@ -48,8 +65,6 @@ let firstOptions;
|
|
|
48
65
|
*/
|
|
49
66
|
const harden = makeHardener();
|
|
50
67
|
|
|
51
|
-
const alreadyHardenedIntrinsics = () => false;
|
|
52
|
-
|
|
53
68
|
/**
|
|
54
69
|
* @callback Transform
|
|
55
70
|
* @param {string} source
|
|
@@ -66,44 +81,17 @@ const alreadyHardenedIntrinsics = () => false;
|
|
|
66
81
|
* @param {Object} [options.globalLexicals]
|
|
67
82
|
*/
|
|
68
83
|
|
|
69
|
-
/**
|
|
70
|
-
* @callback CompartmentConstructorMaker
|
|
71
|
-
* @param {CompartmentConstructorMaker} targetMakeCompartmentConstructor
|
|
72
|
-
* @param {Object} intrinsics
|
|
73
|
-
* @param {(func: Function) => void} markVirtualizedNativeFunction
|
|
74
|
-
* @returns {CompartmentConstructor}
|
|
75
|
-
*/
|
|
76
|
-
|
|
77
84
|
// TODO https://github.com/endojs/endo/issues/814
|
|
78
85
|
// Lockdown currently allows multiple calls provided that the specified options
|
|
79
86
|
// of every call agree. With experience, we have observed that lockdown should
|
|
80
87
|
// only ever need to be called once and that simplifying lockdown will improve
|
|
81
88
|
// the quality of audits.
|
|
82
89
|
|
|
83
|
-
// TODO https://github.com/endojs/endo/issues/815
|
|
84
|
-
// Lockdown receives makeCompartmentConstructor and compartmentPrototype.
|
|
85
|
-
// This is a vestige of an earlier version of SES where makeLockdown was called
|
|
86
|
-
// from two different entry points: one for a layer of SES that had a
|
|
87
|
-
// Compartment that only supported evaluating programs, and a second layer that
|
|
88
|
-
// extended Compartment to support modules, but at the expense of entraining a
|
|
89
|
-
// dependency on Babel.
|
|
90
|
-
// SES currently externalizes the dependency on Babel and one version of
|
|
91
|
-
// makeCompartmentConstructor is sufficient for all uses, so this can be
|
|
92
|
-
// simplified.
|
|
93
|
-
|
|
94
90
|
/**
|
|
95
|
-
* @param {CompartmentConstructorMaker} makeCompartmentConstructor
|
|
96
|
-
* @param {Object} compartmentPrototype
|
|
97
|
-
* @param {() => Object} getAnonymousIntrinsics
|
|
98
91
|
* @param {LockdownOptions} [options]
|
|
99
|
-
* @returns {() =>
|
|
92
|
+
* @returns {() => void} repairIntrinsics
|
|
100
93
|
*/
|
|
101
|
-
export const repairIntrinsics = (
|
|
102
|
-
makeCompartmentConstructor,
|
|
103
|
-
compartmentPrototype,
|
|
104
|
-
getAnonymousIntrinsics,
|
|
105
|
-
options = {},
|
|
106
|
-
) => {
|
|
94
|
+
export const repairIntrinsics = (options = {}) => {
|
|
107
95
|
// First time, absent options default to 'safe'.
|
|
108
96
|
// Subsequent times, absent options default to first options.
|
|
109
97
|
// Thus, all present options must agree with first options.
|
|
@@ -128,24 +116,46 @@ export const repairIntrinsics = (
|
|
|
128
116
|
// is useful. See
|
|
129
117
|
// [`stackFiltering` options](https://github.com/Agoric/SES-shim/blob/master/packages/ses/lockdown-options.md#stackfiltering-options)
|
|
130
118
|
// for an explanation.
|
|
131
|
-
|
|
119
|
+
|
|
120
|
+
const {
|
|
121
|
+
getEnvironmentOption: getenv,
|
|
122
|
+
getCapturedEnvironmentOptionNames,
|
|
123
|
+
} = makeEnvironmentCaptor(globalThis);
|
|
124
|
+
|
|
132
125
|
const {
|
|
126
|
+
errorTaming = getenv('LOCKDOWN_ERROR_TAMING', 'safe'),
|
|
127
|
+
errorTrapping = getenv('LOCKDOWN_ERROR_TRAPPING', 'platform'),
|
|
128
|
+
regExpTaming = getenv('LOCKDOWN_REGEXP_TAMING', 'safe'),
|
|
129
|
+
localeTaming = getenv('LOCKDOWN_LOCALE_TAMING', 'safe'),
|
|
130
|
+
consoleTaming = getenv('LOCKDOWN_CONSOLE_TAMING', 'safe'),
|
|
131
|
+
overrideTaming = getenv('LOCKDOWN_OVERRIDE_TAMING', 'moderate'),
|
|
132
|
+
stackFiltering = getenv('LOCKDOWN_STACK_FILTERING', 'concise'),
|
|
133
|
+
domainTaming = getenv('LOCKDOWN_DOMAIN_TAMING', 'safe'),
|
|
134
|
+
overrideDebug = arrayFilter(
|
|
135
|
+
stringSplit(getenv('LOCKDOWN_OVERRIDE_DEBUG', ''), ','),
|
|
136
|
+
/** @param {string} debugName */
|
|
137
|
+
debugName => debugName !== '',
|
|
138
|
+
),
|
|
139
|
+
__allowUnsafeMonkeyPatching__ = getenv(
|
|
140
|
+
'__LOCKDOWN_ALLOW_UNSAFE_MONKEY_PATCHING__',
|
|
141
|
+
'safe',
|
|
142
|
+
),
|
|
133
143
|
dateTaming = 'safe', // deprecated
|
|
134
|
-
errorTaming = 'safe',
|
|
135
144
|
mathTaming = 'safe', // deprecated
|
|
136
|
-
errorTrapping = 'platform',
|
|
137
|
-
regExpTaming = 'safe',
|
|
138
|
-
localeTaming = 'safe',
|
|
139
|
-
consoleTaming = 'safe',
|
|
140
|
-
overrideTaming = 'moderate',
|
|
141
|
-
overrideDebug = [],
|
|
142
|
-
stackFiltering = 'concise',
|
|
143
|
-
domainTaming = 'unsafe', // TODO become 'safe' by default in next-breaking-release.
|
|
144
|
-
__allowUnsafeMonkeyPatching__ = 'safe',
|
|
145
|
-
|
|
146
145
|
...extraOptions
|
|
147
146
|
} = options;
|
|
148
147
|
|
|
148
|
+
const capturedEnvironmentOptionNames = getCapturedEnvironmentOptionNames();
|
|
149
|
+
if (capturedEnvironmentOptionNames.length > 0) {
|
|
150
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
151
|
+
console.warn(
|
|
152
|
+
`SES Lockdown using options from environment variables ${enJoin(
|
|
153
|
+
arrayMap(capturedEnvironmentOptionNames, q),
|
|
154
|
+
'and',
|
|
155
|
+
)}`,
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
149
159
|
// Assert that only supported options were passed.
|
|
150
160
|
// Use Reflect.ownKeys to reject symbol-named properties as well.
|
|
151
161
|
const extraOptionsNames = ownKeys(extraOptions);
|
|
@@ -154,36 +164,21 @@ export const repairIntrinsics = (
|
|
|
154
164
|
d`lockdown(): non supported option ${q(extraOptionsNames)}`,
|
|
155
165
|
);
|
|
156
166
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
firstOptions = {
|
|
169
|
-
dateTaming, // deprecated
|
|
170
|
-
errorTaming,
|
|
171
|
-
mathTaming, // deprecated
|
|
172
|
-
regExpTaming,
|
|
173
|
-
localeTaming,
|
|
174
|
-
consoleTaming,
|
|
175
|
-
overrideTaming,
|
|
176
|
-
overrideDebug,
|
|
177
|
-
stackFiltering,
|
|
178
|
-
domainTaming,
|
|
179
|
-
__allowUnsafeMonkeyPatching__,
|
|
180
|
-
};
|
|
167
|
+
assert(
|
|
168
|
+
priorLockdown === undefined,
|
|
169
|
+
`Already locked down at ${priorLockdown} (SES_ALREADY_LOCKED_DOWN)`,
|
|
170
|
+
TypeError,
|
|
171
|
+
);
|
|
172
|
+
priorLockdown = new TypeError('Prior lockdown (SES_ALREADY_LOCKED_DOWN)');
|
|
173
|
+
// Tease V8 to generate the stack string and release the closures the stack
|
|
174
|
+
// trace retained:
|
|
175
|
+
priorLockdown.stack;
|
|
181
176
|
|
|
182
177
|
/**
|
|
183
178
|
* Because of packagers and bundlers, etc, multiple invocations of lockdown
|
|
184
179
|
* might happen in separate instantiations of the source of this module.
|
|
185
180
|
* In that case, each one sees its own `firstOptions` variable, so the test
|
|
186
|
-
* above will not detect that lockdown has already happened.
|
|
181
|
+
* above will not detect that lockdown has already happened. We
|
|
187
182
|
* unreliably test some telltale signs that lockdown has run, to avoid
|
|
188
183
|
* trying to lock down a locked down environment. Although the test is
|
|
189
184
|
* unreliable, this is consistent with the SES threat model. SES provides
|
|
@@ -194,12 +189,7 @@ export const repairIntrinsics = (
|
|
|
194
189
|
* tests without actually locking down counts as corrupting code.
|
|
195
190
|
*
|
|
196
191
|
* The specifics of what this tests for may change over time, but it
|
|
197
|
-
* should be consistent with any setting of the lockdown options.
|
|
198
|
-
* do no checking that the state is consistent with current lockdown
|
|
199
|
-
* options. So a call to lockdown with one set of options may silently
|
|
200
|
-
* succeed with a state not reflecting those options, but only
|
|
201
|
-
* if a previous lockdown happened from something other than this
|
|
202
|
-
* instance of this module.
|
|
192
|
+
* should be consistent with any setting of the lockdown options.
|
|
203
193
|
*/
|
|
204
194
|
const seemsToBeLockedDown = () => {
|
|
205
195
|
return (
|
|
@@ -216,8 +206,9 @@ export const repairIntrinsics = (
|
|
|
216
206
|
|
|
217
207
|
if (seemsToBeLockedDown()) {
|
|
218
208
|
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
219
|
-
|
|
220
|
-
|
|
209
|
+
throw new TypeError(
|
|
210
|
+
`Already locked down but not by this SES instance (SES_MULTIPLE_INSTANCES)`,
|
|
211
|
+
);
|
|
221
212
|
}
|
|
222
213
|
|
|
223
214
|
/**
|
|
@@ -256,6 +247,7 @@ export const repairIntrinsics = (
|
|
|
256
247
|
optGetStackString = intrinsics['%InitialGetStackString%'];
|
|
257
248
|
}
|
|
258
249
|
const consoleRecord = tameConsole(
|
|
250
|
+
// @ts-ignore tameConsole does its own input validation
|
|
259
251
|
consoleTaming,
|
|
260
252
|
errorTrapping,
|
|
261
253
|
optGetStackString,
|
|
@@ -289,19 +281,26 @@ export const repairIntrinsics = (
|
|
|
289
281
|
|
|
290
282
|
// Initialize the powerful initial global, i.e., the global of the
|
|
291
283
|
// start compartment, from the intrinsics.
|
|
292
|
-
|
|
293
|
-
|
|
284
|
+
|
|
285
|
+
setGlobalObjectConstantProperties(globalThis);
|
|
286
|
+
|
|
287
|
+
const { safeEvaluate } = makeSafeEvaluator({ globalObject: globalThis });
|
|
288
|
+
|
|
289
|
+
setGlobalObjectMutableProperties(globalThis, {
|
|
294
290
|
intrinsics,
|
|
295
|
-
initialGlobalPropertyNames,
|
|
291
|
+
newGlobalPropertyNames: initialGlobalPropertyNames,
|
|
296
292
|
makeCompartmentConstructor,
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
},
|
|
301
|
-
);
|
|
293
|
+
safeEvaluate,
|
|
294
|
+
markVirtualizedNativeFunction,
|
|
295
|
+
});
|
|
302
296
|
|
|
303
297
|
/**
|
|
304
298
|
* 3. HARDEN to share the intrinsics.
|
|
299
|
+
*
|
|
300
|
+
* We define hardenIntrinsics here so that options are in scope, but return
|
|
301
|
+
* it to the caller because we intend to eventually allow vetted shims to run
|
|
302
|
+
* between repairs and the hardening of intrinsics and so we can benchmark
|
|
303
|
+
* repair separately from hardening.
|
|
305
304
|
*/
|
|
306
305
|
|
|
307
306
|
function hardenIntrinsics() {
|
|
@@ -309,6 +308,7 @@ export const repairIntrinsics = (
|
|
|
309
308
|
// TODO consider moving this to the end of the repair phase, and
|
|
310
309
|
// therefore before vetted shims rather than afterwards. It is not
|
|
311
310
|
// clear yet which is better.
|
|
311
|
+
// @ts-ignore enablePropertyOverrides does its own input validation
|
|
312
312
|
enablePropertyOverrides(intrinsics, overrideTaming, overrideDebug);
|
|
313
313
|
|
|
314
314
|
if (__allowUnsafeMonkeyPatching__ !== 'unsafe') {
|
|
@@ -334,27 +334,9 @@ export const repairIntrinsics = (
|
|
|
334
334
|
};
|
|
335
335
|
|
|
336
336
|
/**
|
|
337
|
-
* @param {
|
|
338
|
-
* @param {Object} compartmentPrototype
|
|
339
|
-
* @param {() => Object} getAnonymousIntrinsics
|
|
340
|
-
* @returns {import('../index.js').Lockdown}
|
|
337
|
+
* @param {LockdownOptions} [options]
|
|
341
338
|
*/
|
|
342
|
-
export const
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
getAnonymousIntrinsics,
|
|
346
|
-
) => {
|
|
347
|
-
/**
|
|
348
|
-
* @param {LockdownOptions} [options]
|
|
349
|
-
*/
|
|
350
|
-
const lockdown = (options = {}) => {
|
|
351
|
-
const maybeHardenIntrinsics = repairIntrinsics(
|
|
352
|
-
makeCompartmentConstructor,
|
|
353
|
-
compartmentPrototype,
|
|
354
|
-
getAnonymousIntrinsics,
|
|
355
|
-
options,
|
|
356
|
-
);
|
|
357
|
-
return maybeHardenIntrinsics();
|
|
358
|
-
};
|
|
359
|
-
return lockdown;
|
|
339
|
+
export const lockdown = (options = {}) => {
|
|
340
|
+
const hardenIntrinsics = repairIntrinsics(options);
|
|
341
|
+
hardenIntrinsics();
|
|
360
342
|
};
|
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import { performEval } from './evaluate.js';
|
|
2
|
-
|
|
3
1
|
/*
|
|
4
2
|
* makeEvalFunction()
|
|
5
3
|
* A safe version of the native eval function which relies on
|
|
6
|
-
* the safety of
|
|
4
|
+
* the safety of safeEvaluate for confinement.
|
|
7
5
|
*/
|
|
8
|
-
export const makeEvalFunction =
|
|
6
|
+
export const makeEvalFunction = safeEvaluate => {
|
|
9
7
|
// We use the the concise method syntax to create an eval without a
|
|
10
8
|
// [[Construct]] behavior (such that the invocation "new eval()" throws
|
|
11
9
|
// TypeError: eval is not a constructor"), but which still accepts a
|
|
@@ -19,7 +17,7 @@ export const makeEvalFunction = (globalObject, options = {}) => {
|
|
|
19
17
|
// rule. Track.
|
|
20
18
|
return source;
|
|
21
19
|
}
|
|
22
|
-
return
|
|
20
|
+
return safeEvaluate(source);
|
|
23
21
|
},
|
|
24
22
|
}.eval;
|
|
25
23
|
|
|
@@ -5,15 +5,14 @@ import {
|
|
|
5
5
|
defineProperties,
|
|
6
6
|
getPrototypeOf,
|
|
7
7
|
} from './commons.js';
|
|
8
|
-
import { performEval } from './evaluate.js';
|
|
9
8
|
import { assert } from './error/assert.js';
|
|
10
9
|
|
|
11
10
|
/*
|
|
12
11
|
* makeFunctionConstructor()
|
|
13
12
|
* A safe version of the native Function which relies on
|
|
14
|
-
* the safety of
|
|
13
|
+
* the safety of safeEvaluate for confinement.
|
|
15
14
|
*/
|
|
16
|
-
export const makeFunctionConstructor =
|
|
15
|
+
export const makeFunctionConstructor = safeEvaluate => {
|
|
17
16
|
// Define an unused parameter to ensure Function.length === 1
|
|
18
17
|
const newFunction = function Function(_body) {
|
|
19
18
|
// Sanitize all parameters at the entry point.
|
|
@@ -53,7 +52,7 @@ export const makeFunctionConstructor = (globaObject, options = {}) => {
|
|
|
53
52
|
// TODO: since we create an anonymous function, the 'this' value
|
|
54
53
|
// isn't bound to the global object as per specs, but set as undefined.
|
|
55
54
|
const src = `(function anonymous(${parameters}\n) {\n${bodyText}\n})`;
|
|
56
|
-
return
|
|
55
|
+
return safeEvaluate(src);
|
|
57
56
|
};
|
|
58
57
|
|
|
59
58
|
defineProperties(newFunction, {
|
package/src/make-hardener.js
CHANGED
|
@@ -85,7 +85,7 @@ export const makeHardener = () => {
|
|
|
85
85
|
// Ignore if this is an exit, or we've already visited it
|
|
86
86
|
return;
|
|
87
87
|
}
|
|
88
|
-
// console.
|
|
88
|
+
// console.warn(`adding ${val} to toFreeze`, val);
|
|
89
89
|
setAdd(toFreeze, val);
|
|
90
90
|
weakmapSet(paths, val, path);
|
|
91
91
|
}
|
|
@@ -150,7 +150,7 @@ export const makeHardener = () => {
|
|
|
150
150
|
|
|
151
151
|
enqueue(root);
|
|
152
152
|
dequeue();
|
|
153
|
-
// console.
|
|
153
|
+
// console.warn("toFreeze set:", toFreeze);
|
|
154
154
|
commit();
|
|
155
155
|
|
|
156
156
|
return root;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// Portions adapted from V8 - Copyright 2016 the V8 project authors.
|
|
2
|
+
// https://github.com/v8/v8/blob/master/src/builtins/builtins-function.cc
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
WeakSet,
|
|
6
|
+
apply,
|
|
7
|
+
immutableObject,
|
|
8
|
+
proxyRevocable,
|
|
9
|
+
weaksetAdd,
|
|
10
|
+
} from './commons.js';
|
|
11
|
+
import { getScopeConstants } from './scope-constants.js';
|
|
12
|
+
import { createScopeHandler } from './scope-handler.js';
|
|
13
|
+
import { applyTransforms, mandatoryTransforms } from './transforms.js';
|
|
14
|
+
import { makeEvaluateFactory } from './make-evaluate-factory.js';
|
|
15
|
+
import { assert } from './error/assert.js';
|
|
16
|
+
|
|
17
|
+
const { details: d } = assert;
|
|
18
|
+
|
|
19
|
+
// TODO: rename localObject to scopeObject
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* makeSafeEvaluator()
|
|
23
|
+
* Build the low-level operation used by all evaluators:
|
|
24
|
+
* eval(), Function(), Compartment.prototype.evaluate().
|
|
25
|
+
*
|
|
26
|
+
* @param {Object} options
|
|
27
|
+
* @param {Object} options.globalObject
|
|
28
|
+
* @param {Object} [options.localObject]
|
|
29
|
+
* @param {Array<Transform>} [options.globalTransforms]
|
|
30
|
+
* @param {bool} [options.sloppyGlobalsMode]
|
|
31
|
+
* @param {WeakSet} [options.knownScopeProxies]
|
|
32
|
+
*/
|
|
33
|
+
export const makeSafeEvaluator = ({
|
|
34
|
+
globalObject,
|
|
35
|
+
localObject = {},
|
|
36
|
+
globalTransforms = [],
|
|
37
|
+
sloppyGlobalsMode = false,
|
|
38
|
+
knownScopeProxies = new WeakSet(),
|
|
39
|
+
} = {}) => {
|
|
40
|
+
const {
|
|
41
|
+
scopeHandler,
|
|
42
|
+
admitOneUnsafeEvalNext,
|
|
43
|
+
resetOneUnsafeEvalNext,
|
|
44
|
+
} = createScopeHandler(globalObject, localObject, {
|
|
45
|
+
sloppyGlobalsMode,
|
|
46
|
+
});
|
|
47
|
+
const { proxy: scopeProxy, revoke: revokeScopeProxy } = proxyRevocable(
|
|
48
|
+
immutableObject,
|
|
49
|
+
scopeHandler,
|
|
50
|
+
);
|
|
51
|
+
weaksetAdd(knownScopeProxies, scopeProxy);
|
|
52
|
+
|
|
53
|
+
// Defer creating the actual evaluator to first use.
|
|
54
|
+
// Creating a compartment should be possible in no-eval environments
|
|
55
|
+
// It also allows more global constants to be captured by the optimizer
|
|
56
|
+
let evaluate;
|
|
57
|
+
const makeEvaluate = () => {
|
|
58
|
+
if (!evaluate) {
|
|
59
|
+
const constants = getScopeConstants(globalObject, localObject);
|
|
60
|
+
const evaluateFactory = makeEvaluateFactory(constants);
|
|
61
|
+
evaluate = apply(evaluateFactory, scopeProxy, []);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @param {string} source
|
|
67
|
+
* @param {Object} [options]
|
|
68
|
+
* @param {Array<Transform>} [options.localTransforms]
|
|
69
|
+
*/
|
|
70
|
+
const safeEvaluate = (source, { localTransforms = [] } = {}) => {
|
|
71
|
+
makeEvaluate();
|
|
72
|
+
|
|
73
|
+
// Execute the mandatory transforms last to ensure that any rewritten code
|
|
74
|
+
// meets those mandatory requirements.
|
|
75
|
+
source = applyTransforms(source, [
|
|
76
|
+
...localTransforms,
|
|
77
|
+
...globalTransforms,
|
|
78
|
+
mandatoryTransforms,
|
|
79
|
+
]);
|
|
80
|
+
|
|
81
|
+
admitOneUnsafeEvalNext();
|
|
82
|
+
let err;
|
|
83
|
+
try {
|
|
84
|
+
// Ensure that "this" resolves to the safe global.
|
|
85
|
+
return apply(evaluate, globalObject, [source]);
|
|
86
|
+
} catch (e) {
|
|
87
|
+
// stash the child-code error in hopes of debugging the internal failure
|
|
88
|
+
err = e;
|
|
89
|
+
throw e;
|
|
90
|
+
} finally {
|
|
91
|
+
if (resetOneUnsafeEvalNext()) {
|
|
92
|
+
// Barring a defect in the SES shim, the scope proxy should allow the
|
|
93
|
+
// powerful, unsafe `eval` to be used by `evaluate` exactly once, as the
|
|
94
|
+
// very first name that it attempts to access from the lexical scope.
|
|
95
|
+
// A defect in the SES shim could throw an exception after our call to
|
|
96
|
+
// `admitOneUnsafeEvalNext()` and before `evaluate` calls `eval`
|
|
97
|
+
// internally.
|
|
98
|
+
// If we get here, SES is very broken.
|
|
99
|
+
// This condition is one where this vat is now hopelessly confused, and
|
|
100
|
+
// the vat as a whole should be aborted.
|
|
101
|
+
// No further code should run.
|
|
102
|
+
// All immediately reachable state should be abandoned.
|
|
103
|
+
// However, that is not yet possible, so we at least prevent further
|
|
104
|
+
// variable resolution via the scopeHandler, and throw an error with
|
|
105
|
+
// diagnostic info including the thrown error if any from evaluating the
|
|
106
|
+
// source code.
|
|
107
|
+
revokeScopeProxy();
|
|
108
|
+
// TODO A GOOD PLACE TO PANIC(), i.e., kill the vat incarnation.
|
|
109
|
+
// See https://github.com/Agoric/SES-shim/issues/490
|
|
110
|
+
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
111
|
+
assert.fail(d`handler did not reset allowNextEvalToBeUnsafe ${err}`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
return { safeEvaluate };
|
|
117
|
+
};
|
package/src/scope-handler.js
CHANGED
|
@@ -39,7 +39,7 @@ const alwaysThrowHandler = new Proxy(
|
|
|
39
39
|
/*
|
|
40
40
|
* createScopeHandler()
|
|
41
41
|
* ScopeHandler manages a Proxy which serves as the global scope for the
|
|
42
|
-
*
|
|
42
|
+
* safeEvaluate operation (the Proxy is the argument of a 'with' binding).
|
|
43
43
|
* As described in createSafeEvaluator(), it has several functions:
|
|
44
44
|
* - allow the very first (and only the very first) use of 'eval' to map to
|
|
45
45
|
* the real (unsafe) eval function, so it acts as a 'direct eval' and can
|
|
@@ -152,9 +152,20 @@ export const createScopeHandler = (
|
|
|
152
152
|
// at the expense of 'typeof' being wrong for those properties. For
|
|
153
153
|
// example, in the browser, evaluating 'document = 3', will add
|
|
154
154
|
// a property to globalObject instead of throwing a ReferenceError.
|
|
155
|
+
|
|
156
|
+
// !!!!! WARNING: DANGER ZONE !!!!!!
|
|
157
|
+
// The order of the conditions in the `||` expression below is of the
|
|
158
|
+
// utmost importance. Under no circumstances should `eval` be checked
|
|
159
|
+
// after `globalObject`. The prototype of the global object is under
|
|
160
|
+
// full control of user code and may be replaced by a proxy with a
|
|
161
|
+
// `has` trap. If we allow that trap to trigger while the
|
|
162
|
+
// `allowNextEvalToBeUnsafe` flag is down, it could allow user code
|
|
163
|
+
// to get a hold of `FERAL_EVAL`, resulting in a complete escape of
|
|
164
|
+
// the compartment.
|
|
165
|
+
// !!!!! WARNING: DANGER ZONE !!!!!!
|
|
155
166
|
return (
|
|
156
167
|
sloppyGlobalsMode ||
|
|
157
|
-
prop === 'eval' ||
|
|
168
|
+
(allowNextEvalToBeUnsafe && prop === 'eval') ||
|
|
158
169
|
prop in localObject ||
|
|
159
170
|
prop in globalObject ||
|
|
160
171
|
prop in globalThis
|
|
@@ -244,11 +244,11 @@ export default function whitelistIntrinsics(
|
|
|
244
244
|
// that we are removing it so we know to look into it, as happens when
|
|
245
245
|
// the language evolves new features to existing intrinsics.
|
|
246
246
|
if (subPermit !== false) {
|
|
247
|
-
// This call to `console.
|
|
248
|
-
//
|
|
247
|
+
// This call to `console.warn` is intentional. It is not a vestige of
|
|
248
|
+
// a debugging attempt. See the comment at top of file for an
|
|
249
249
|
// explanation.
|
|
250
250
|
// eslint-disable-next-line @endo/no-polymorphic-call
|
|
251
|
-
console.
|
|
251
|
+
console.warn(`Removing ${subPath}`);
|
|
252
252
|
}
|
|
253
253
|
try {
|
|
254
254
|
delete obj[prop];
|
package/src/whitelist.js
CHANGED
|
@@ -425,6 +425,8 @@ export const whitelist = {
|
|
|
425
425
|
seal: fn,
|
|
426
426
|
setPrototypeOf: fn,
|
|
427
427
|
values: fn,
|
|
428
|
+
// See https://github.com/tc39/proposal-accessible-object-hasownproperty
|
|
429
|
+
hasOwn: fn,
|
|
428
430
|
},
|
|
429
431
|
|
|
430
432
|
'%ObjectPrototype%': {
|
|
@@ -922,6 +924,9 @@ export const whitelist = {
|
|
|
922
924
|
// Failed tc39 proposal
|
|
923
925
|
// Seen on FF Nightly 88.0a1
|
|
924
926
|
at: false,
|
|
927
|
+
// See https://github.com/tc39/proposal-array-find-from-last
|
|
928
|
+
findLast: fn,
|
|
929
|
+
findLastIndex: fn,
|
|
925
930
|
},
|
|
926
931
|
|
|
927
932
|
'%ArrayIteratorPrototype%': {
|
|
@@ -978,6 +983,9 @@ export const whitelist = {
|
|
|
978
983
|
// Failed tc39 proposal
|
|
979
984
|
// Seen on FF Nightly 88.0a1
|
|
980
985
|
at: false,
|
|
986
|
+
// See https://github.com/tc39/proposal-array-find-from-last
|
|
987
|
+
findLast: fn,
|
|
988
|
+
findLastIndex: fn,
|
|
981
989
|
},
|
|
982
990
|
|
|
983
991
|
// The TypedArray Constructors
|
|
@@ -1117,6 +1125,11 @@ export const whitelist = {
|
|
|
1117
1125
|
'@@toStringTag': 'string',
|
|
1118
1126
|
// See https://github.com/Moddable-OpenSource/moddable/issues/523
|
|
1119
1127
|
concat: false,
|
|
1128
|
+
// See https://github.com/tc39/proposal-resizablearraybuffer
|
|
1129
|
+
transfer: fn,
|
|
1130
|
+
resize: fn,
|
|
1131
|
+
resizable: getter,
|
|
1132
|
+
maxByteLength: getter,
|
|
1120
1133
|
},
|
|
1121
1134
|
|
|
1122
1135
|
// SharedArrayBuffer Objects
|