ses 1.0.0 → 1.1.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.
@@ -25,6 +25,7 @@ import {
25
25
  reflectHas,
26
26
  reflectIsExtensible,
27
27
  reflectPreventExtensions,
28
+ toStringTagSymbol,
28
29
  weakmapSet,
29
30
  } from './commons.js';
30
31
  import { assert } from './error/assert.js';
@@ -51,13 +52,21 @@ const { quote: q } = assert;
51
52
  //
52
53
  export const deferExports = () => {
53
54
  let active = false;
54
- const proxiedExports = create(null);
55
+ const exportsTarget = create(null, {
56
+ // Make this appear like an ESM module namespace object.
57
+ [toStringTagSymbol]: {
58
+ value: 'Module',
59
+ writable: false,
60
+ enumerable: false,
61
+ configurable: false,
62
+ },
63
+ });
55
64
  return freeze({
56
65
  activate() {
57
66
  active = true;
58
67
  },
59
- proxiedExports,
60
- exportsProxy: new Proxy(proxiedExports, {
68
+ exportsTarget,
69
+ exportsProxy: new Proxy(exportsTarget, {
61
70
  get(_target, name, receiver) {
62
71
  if (!active) {
63
72
  throw TypeError(
@@ -66,7 +75,7 @@ export const deferExports = () => {
66
75
  )} of module exports namespace, the module has not yet begun to execute`,
67
76
  );
68
77
  }
69
- return reflectGet(proxiedExports, name, receiver);
78
+ return reflectGet(exportsTarget, name, receiver);
70
79
  },
71
80
  set(_target, name, _value) {
72
81
  throw TypeError(
@@ -81,7 +90,7 @@ export const deferExports = () => {
81
90
  )}, the module has not yet begun to execute`,
82
91
  );
83
92
  }
84
- return reflectHas(proxiedExports, name);
93
+ return reflectHas(exportsTarget, name);
85
94
  },
86
95
  deleteProperty(_target, name) {
87
96
  throw TypeError(
@@ -94,7 +103,7 @@ export const deferExports = () => {
94
103
  'Cannot enumerate keys, the module has not yet begun to execute',
95
104
  );
96
105
  }
97
- return ownKeys(proxiedExports);
106
+ return ownKeys(exportsTarget);
98
107
  },
99
108
  getOwnPropertyDescriptor(_target, name) {
100
109
  if (!active) {
@@ -104,7 +113,7 @@ export const deferExports = () => {
104
113
  )}, the module has not yet begun to execute`,
105
114
  );
106
115
  }
107
- return reflectGetOwnPropertyDescriptor(proxiedExports, name);
116
+ return reflectGetOwnPropertyDescriptor(exportsTarget, name);
108
117
  },
109
118
  preventExtensions(_target) {
110
119
  if (!active) {
@@ -112,7 +121,7 @@ export const deferExports = () => {
112
121
  'Cannot prevent extensions of module exports namespace, the module has not yet begun to execute',
113
122
  );
114
123
  }
115
- return reflectPreventExtensions(proxiedExports);
124
+ return reflectPreventExtensions(exportsTarget);
116
125
  },
117
126
  isExtensible() {
118
127
  if (!active) {
@@ -120,7 +129,7 @@ export const deferExports = () => {
120
129
  'Cannot check extensibility of module exports namespace, the module has not yet begun to execute',
121
130
  );
122
131
  }
123
- return reflectIsExtensible(proxiedExports);
132
+ return reflectIsExtensible(exportsTarget);
124
133
  },
125
134
  getPrototypeOf(_target) {
126
135
  return null;
@@ -147,11 +156,30 @@ export const deferExports = () => {
147
156
  });
148
157
  };
149
158
 
150
- // `getDeferredExports` memoizes the creation of a deferred module exports
151
- // namespace proxy for any abritrary full specifier in a compartment.
152
- // It also records the compartment and specifier affiliated with that module
153
- // exports namespace proxy so it can be used as an alias into another
154
- // compartment when threaded through a compartment's `moduleMap` argument.
159
+ /**
160
+ * @typedef {object} DeferredExports
161
+ * @property {Record<string, any>} exportsTarget - The object to which a
162
+ * module's exports will be added.
163
+ * @property {Record<string, any>} exportsProxy - A proxy over the `exportsTarget`,
164
+ * used to expose its "exports" to other compartments.
165
+ * @property {() => void} activate - Activate the `exportsProxy` such that it can
166
+ * be used as a module namespace object.
167
+ */
168
+
169
+ /**
170
+ * Memoizes the creation of a deferred module exports namespace proxy for any
171
+ * arbitrary full specifier in a compartment. It also records the compartment
172
+ * and specifier affiliated with that module exports namespace proxy so it
173
+ * can be used as an alias into another compartment when threaded through
174
+ * a compartment's `moduleMap` argument.
175
+ *
176
+ * @param {*} compartment - The compartment to retrieve deferred exports from.
177
+ * @param {*} compartmentPrivateFields - The private fields of the compartment.
178
+ * @param {*} moduleAliases - The module aliases of the compartment.
179
+ * @param {string} specifier - The module specifier to retrieve deferred exports for.
180
+ * @returns {DeferredExports} - The deferred exports for the module specifier of
181
+ * the compartment.
182
+ */
155
183
  export const getDeferredExports = (
156
184
  compartment,
157
185
  compartmentPrivateFields,
@@ -74,7 +74,18 @@ export default function whitelistIntrinsics(
74
74
  intrinsics,
75
75
  markVirtualizedNativeFunction,
76
76
  ) {
77
- // These primitives are allowed allowed for permits.
77
+ let groupStarted = false;
78
+ const inConsoleGroup = (level, ...args) => {
79
+ if (!groupStarted) {
80
+ // eslint-disable-next-line @endo/no-polymorphic-call
81
+ console.groupCollapsed('Removing unpermitted intrinsics');
82
+ groupStarted = true;
83
+ }
84
+ // eslint-disable-next-line @endo/no-polymorphic-call
85
+ return console[level](...args);
86
+ };
87
+
88
+ // These primitives are allowed for permits.
78
89
  const primitives = ['undefined', 'boolean', 'number', 'string', 'symbol'];
79
90
 
80
91
  // These symbols are allowed as well-known symbols
@@ -279,11 +290,7 @@ export default function whitelistIntrinsics(
279
290
  // that we are removing it so we know to look into it, as happens when
280
291
  // the language evolves new features to existing intrinsics.
281
292
  if (subPermit !== false) {
282
- // This call to `console.warn` is intentional. It is not a vestige of
283
- // a debugging attempt. See the comment at top of file for an
284
- // explanation.
285
- // eslint-disable-next-line @endo/no-polymorphic-call
286
- console.warn(`Removing ${subPath}`);
293
+ inConsoleGroup('warn', `Removing ${subPath}`);
287
294
  }
288
295
  try {
289
296
  delete obj[prop];
@@ -292,17 +299,17 @@ export default function whitelistIntrinsics(
292
299
  if (typeof obj === 'function' && prop === 'prototype') {
293
300
  obj.prototype = undefined;
294
301
  if (obj.prototype === undefined) {
295
- // eslint-disable-next-line @endo/no-polymorphic-call
296
- console.warn(`Tolerating undeletable ${subPath} === undefined`);
302
+ inConsoleGroup(
303
+ 'warn',
304
+ `Tolerating undeletable ${subPath} === undefined`,
305
+ );
297
306
  // eslint-disable-next-line no-continue
298
307
  continue;
299
308
  }
300
309
  }
301
- // eslint-disable-next-line @endo/no-polymorphic-call
302
- console.error(`failed to delete ${subPath}`, err);
310
+ inConsoleGroup('error', `failed to delete ${subPath}`, err);
303
311
  } else {
304
- // eslint-disable-next-line @endo/no-polymorphic-call
305
- console.error(`deleting ${subPath} threw`, err);
312
+ inConsoleGroup('error', `deleting ${subPath} threw`, err);
306
313
  }
307
314
  throw err;
308
315
  }
@@ -310,7 +317,14 @@ export default function whitelistIntrinsics(
310
317
  }
311
318
  }
312
319
 
313
- // Start path with 'intrinsics' to clarify that properties are not
314
- // removed from the global object by the whitelisting operation.
315
- visitProperties('intrinsics', intrinsics, permitted);
320
+ try {
321
+ // Start path with 'intrinsics' to clarify that properties are not
322
+ // removed from the global object by the whitelisting operation.
323
+ visitProperties('intrinsics', intrinsics, permitted);
324
+ } finally {
325
+ if (groupStarted) {
326
+ // eslint-disable-next-line @endo/no-polymorphic-call
327
+ console.groupEnd();
328
+ }
329
+ }
316
330
  }
package/src/permits.js CHANGED
@@ -1121,6 +1121,20 @@ export const permitted = {
1121
1121
  values: fn,
1122
1122
  '@@iterator': fn,
1123
1123
  '@@toStringTag': 'string',
1124
+ // See https://github.com/tc39/proposal-set-methods
1125
+ intersection: fn,
1126
+ // See https://github.com/tc39/proposal-set-methods
1127
+ union: fn,
1128
+ // See https://github.com/tc39/proposal-set-methods
1129
+ difference: fn,
1130
+ // See https://github.com/tc39/proposal-set-methods
1131
+ symmetricDifference: fn,
1132
+ // See https://github.com/tc39/proposal-set-methods
1133
+ isSubsetOf: fn,
1134
+ // See https://github.com/tc39/proposal-set-methods
1135
+ isSupersetOf: fn,
1136
+ // See https://github.com/tc39/proposal-set-methods
1137
+ isDisjointFrom: fn,
1124
1138
  },
1125
1139
 
1126
1140
  '%SetIteratorPrototype%': {
@@ -17,12 +17,10 @@ export function tameDomains(domainTaming = 'safe') {
17
17
  }
18
18
 
19
19
  // Protect against the hazard presented by Node.js domains.
20
- if (typeof globalThis.process === 'object' && globalThis.process !== null) {
20
+ const globalProcess = globalThis.process || undefined;
21
+ if (typeof globalProcess === 'object') {
21
22
  // Check whether domains were initialized.
22
- const domainDescriptor = getOwnPropertyDescriptor(
23
- globalThis.process,
24
- 'domain',
25
- );
23
+ const domainDescriptor = getOwnPropertyDescriptor(globalProcess, 'domain');
26
24
  if (domainDescriptor !== undefined && domainDescriptor.get !== undefined) {
27
25
  // The domain descriptor on Node.js initially has value: null, which
28
26
  // becomes a get, set pair after domains initialize.
@@ -37,7 +35,7 @@ export function tameDomains(domainTaming = 'safe') {
37
35
  // The domain module merely throws an exception when it attempts to define
38
36
  // the domain property of the process global during its initialization.
39
37
  // We have no better recourse because Node.js uses defineProperty too.
40
- defineProperty(globalThis.process, 'domain', {
38
+ defineProperty(globalProcess, 'domain', {
41
39
  value: null,
42
40
  configurable: false,
43
41
  writable: false,
@@ -0,0 +1,210 @@
1
+ import {
2
+ getOwnPropertyDescriptor,
3
+ apply,
4
+ defineProperty,
5
+ toStringTagSymbol,
6
+ } from './commons.js';
7
+
8
+ const throws = thunk => {
9
+ try {
10
+ thunk();
11
+ return false;
12
+ } catch (er) {
13
+ return true;
14
+ }
15
+ };
16
+
17
+ /**
18
+ * Exported for convenience of unit testing. Harmless, but not expected
19
+ * to be useful by itself.
20
+ *
21
+ * @param {any} obj
22
+ * @param {string|symbol} prop
23
+ * @param {any} expectedValue
24
+ * @returns {boolean}
25
+ * Returns whether `tameFauxDataProperty` turned the property in question
26
+ * from an apparent faux data property into the actual data property it
27
+ * seemed to emulate.
28
+ * If this function returns `false`, then we hope no effects happened.
29
+ * However, sniffing out if an accessor property seems to be a faux data
30
+ * property requires invoking the getter and setter functions that might
31
+ * possibly have side effects.
32
+ * `tameFauxDataProperty` is not in a position to tell.
33
+ */
34
+ export const tameFauxDataProperty = (obj, prop, expectedValue) => {
35
+ if (obj === undefined) {
36
+ // The object does not exist in this version of the platform
37
+ return false;
38
+ }
39
+ const desc = getOwnPropertyDescriptor(obj, prop);
40
+ if (!desc || 'value' in desc) {
41
+ // The property either doesn't exist, or is already an actual data property.
42
+ return false;
43
+ }
44
+ const { get, set } = desc;
45
+ if (typeof get !== 'function' || typeof set !== 'function') {
46
+ // A faux data property has both a getter and a setter
47
+ return false;
48
+ }
49
+ if (get() !== expectedValue) {
50
+ // The getter called by itself should produce the expectedValue
51
+ return false;
52
+ }
53
+ if (apply(get, obj, []) !== expectedValue) {
54
+ // The getter called with `this === obj` should also return the
55
+ // expectedValue.
56
+ return false;
57
+ }
58
+ const testValue = 'Seems to be a setter';
59
+ const subject1 = { __proto__: null };
60
+ apply(set, subject1, [testValue]);
61
+ if (subject1[prop] !== testValue) {
62
+ // The setter called with an unrelated object as `this` should
63
+ // set the property on the object.
64
+ return false;
65
+ }
66
+ const subject2 = { __proto__: obj };
67
+ apply(set, subject2, [testValue]);
68
+ if (subject2[prop] !== testValue) {
69
+ // The setter called on an object that inherits from `obj` should
70
+ // override the property from `obj` as if by assignment.
71
+ return false;
72
+ }
73
+ if (!throws(() => apply(set, obj, [expectedValue]))) {
74
+ // The setter called with `this === obj` should throw without having
75
+ // caused any effect.
76
+ // This is the test that has the greatest danger of leaving behind some
77
+ // persistent side effect. The most obvious one is to emulate a
78
+ // successful assignment to the property. That's why this test
79
+ // uses `expectedValue`, so that case is likely not to actually
80
+ // change anything.
81
+ return false;
82
+ }
83
+ if ('originalValue' in get) {
84
+ // The ses-shim uniquely, as far as we know, puts an `originalValue`
85
+ // property on the getter, so that reflect property tranversal algorithms,
86
+ // like `harden`, will traverse into the enulated value without
87
+ // calling the getter. That does not happen until `permits-intrinsics.js`
88
+ // which is much later. So if we see one this early, we should
89
+ // not assume we understand what's going on.
90
+ return false;
91
+ }
92
+
93
+ // We assume that this code runs before any untrusted code runs, so
94
+ // we do not need to worry about the above conditions passing because of
95
+ // malicious intent. In fact, it runs even before vetted shims are supposed
96
+ // to run, between repair and hardening. Given that, after all these tests
97
+ // pass, we have adequately validated that the property in question is
98
+ // an accessor function whose purpose is suppressing the override mistake,
99
+ // i.e., enabling a non-writable property to be overridden by assignment.
100
+ // In that case, here we *temporarily* turn it into the data property
101
+ // it seems to emulate, but writable so that it does not trigger the
102
+ // override mistake while in this temporary state.
103
+
104
+ // For those properties that are also listed in `enablements.js`,
105
+ // that phase will re-enable override for these properties, but
106
+ // via accessor functions that SES controls, so we know what they are
107
+ // doing. In addition, the getter functions installed by
108
+ // `enable-property-overrides.js` have an `originalValue` field
109
+ // enabling meta-traversal code like harden to visit the original value
110
+ // without calling the getter.
111
+
112
+ if (desc.configurable === false) {
113
+ // Even though it seems to be a faux data property, we're unable to fix it.
114
+ return false;
115
+ }
116
+
117
+ // Many of the `return false;` cases above plausibly should be turned into
118
+ // errors, or an least generate warnings. However, for those, the checks
119
+ // following this phase are likely to signal an error anyway.
120
+
121
+ // At this point, we have passed all our sniff checks for validating that
122
+ // it seems to be a faux data property with the expected value. Turn
123
+ // it into the actual data property it emulates, but writable so there is
124
+ // not yet an override mistake problem.
125
+
126
+ defineProperty(obj, prop, {
127
+ value: expectedValue,
128
+ writable: true,
129
+ enumerable: desc.enumerable,
130
+ configurable: true,
131
+ });
132
+
133
+ return true;
134
+ };
135
+
136
+ /**
137
+ * In JavaScript, the so-called "override mistake" is the inability to
138
+ * override an inherited non-writable data property by assignment. A common
139
+ * workaround is to instead define an accessor property that acts like
140
+ * a non-writable data property, except that it allows an object that
141
+ * inherits this property to override it by assignment. Let's call
142
+ * an access property that acts this way a "faux data property". In this
143
+ * ses-shim, `enable-property-overrides.js` makes the properties listed in
144
+ * `enablements.js` into faux data properties.
145
+ *
146
+ * But the ses-shim is not alone in use of this trick. Starting with the
147
+ * [Iterator Helpers proposal](https://github.com/tc39/proposal-iterator-helpers),
148
+ * some properties are defined as (what we call) faux data properties.
149
+ * Some of these are new properties (`Interator.prototype.constructor`) and
150
+ * some are old data properties converted to accessor properties
151
+ * (`Iterator.prototype[String.toStringTag]`). So the ses-shim needs to be
152
+ * prepared for some enumerated set of properties to already be faux data
153
+ * properties in the platform prior to our initialization.
154
+ *
155
+ * For these possible faux data properties, it is important that
156
+ * `permits.js` describe each as a data property, so that it can further
157
+ * constrain the apparent value (that allegedly would be returned by the
158
+ * getter) according to its own permits.
159
+ *
160
+ * However, at the time of this writing, the precise behavior specified
161
+ * by the iterator-helpers proposal for these faux data properties is
162
+ * novel. We should not be too confident that all further such platform
163
+ * additions do what we would now expect. So, for each of these possible
164
+ * faux data properties, we do some sniffing to see if it behaves as we
165
+ * currently expect a faux data property to act. If not, then
166
+ * `tameFauxDataProperties` tries not to modify it, leaving it to later
167
+ * checks, especially `permits-intrinsics.js`, to error when it sees an
168
+ * unexpected accessor.
169
+ *
170
+ * If one of these enumerated accessor properties does seem to be
171
+ * a faithful faux data property, then `tameFauxDataProperties` itself
172
+ * *tempoarily* turns it into the actual data property that it seems to emulate.
173
+ * This data property starts as writable, so that in this state it will
174
+ * not trigger the override mistake, i.e., assignment to an object inheriting
175
+ * this property is allowed to succeed at overriding this property.
176
+ *
177
+ * For those properties that should be a faux data property rather than an
178
+ * actual one, such as those from the iterator-helpers proposal,
179
+ * they should be listed as such in `enablements.js`, so
180
+ * `enable-property-overrides.js` will turn it back into a faux data property.
181
+ * But one controlled by the ses-shim, whose behavior we understand.
182
+ *
183
+ * `tameFauxDataProperties`, which turns these into actual data properties,
184
+ * happens during the `repairIntrinsics` phase
185
+ * of `lockdown`, before even vetted shim are supposed to run.
186
+ * `enable-property-overrides.js` runs after vetted shims, turning the
187
+ * appropriate ones back into faux data properties. Thus vetted shims
188
+ * can observe the possibly non-conforming state where these are temporarily
189
+ * actual data properties, rather than faux data properties.
190
+ *
191
+ * Coordinate the property enumeration here
192
+ * with `enablements.js`, so the appropriate properties are
193
+ * turned back to faux data properties.
194
+ *
195
+ * @param {Record<any,any>} intrinsics
196
+ */
197
+ export const tameFauxDataProperties = intrinsics => {
198
+ // https://github.com/tc39/proposal-iterator-helpers
199
+ tameFauxDataProperty(
200
+ intrinsics['%IteratorPrototype%'],
201
+ 'constructor',
202
+ intrinsics.Iterator,
203
+ );
204
+ // https://github.com/tc39/proposal-iterator-helpers
205
+ tameFauxDataProperty(
206
+ intrinsics['%IteratorPrototype%'],
207
+ toStringTagSymbol,
208
+ 'Iterator',
209
+ );
210
+ };
package/types.d.ts CHANGED
@@ -62,7 +62,7 @@ export interface ThirdPartyStaticModuleInterface {
62
62
  * Note that this value does _not_ contain any numeric or symbol property keys, which can theoretically be members of `exports` in a CommonJS module.
63
63
  */
64
64
  execute(
65
- proxiedExports: Record<string, any>,
65
+ exportsTarget: Record<string, any>,
66
66
  compartment: Compartment,
67
67
  resolvedImports: Record<string, string>,
68
68
  ): void;