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.
@@ -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
- };