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