ses 1.13.1 → 1.15.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.
Files changed (41) hide show
  1. package/README.md +70 -52
  2. package/dist/lockdown.cjs +1346 -948
  3. package/dist/lockdown.mjs +1346 -948
  4. package/dist/lockdown.umd.js +1346 -948
  5. package/dist/lockdown.umd.min.js +1 -1
  6. package/dist/ses-hermes.cjs +1328 -804
  7. package/dist/ses.cjs +1346 -948
  8. package/dist/ses.mjs +1346 -948
  9. package/dist/ses.umd.js +1346 -948
  10. package/dist/ses.umd.min.js +1 -1
  11. package/dist/types.d.cts +115 -141
  12. package/package.json +19 -102
  13. package/src/assert-shim.js +2 -2
  14. package/src/cauterize-property.js +2 -2
  15. package/src/commons.js +111 -50
  16. package/src/compartment.js +25 -8
  17. package/src/enable-property-overrides.js +4 -4
  18. package/src/enablements.js +7 -8
  19. package/src/error/assert.js +126 -101
  20. package/src/error/note-log-args.js +5 -4
  21. package/src/error/stringify-utils.js +2 -2
  22. package/src/error/tame-v8-error-constructor.js +15 -12
  23. package/src/error/types.js +1 -1
  24. package/src/get-anonymous-intrinsics.js +0 -1
  25. package/src/global-object.js +3 -3
  26. package/src/intrinsics.js +8 -8
  27. package/src/lockdown.js +34 -12
  28. package/src/make-hardener.js +4 -4
  29. package/src/module-instance.js +22 -18
  30. package/src/module-link.js +4 -4
  31. package/src/module-load.js +48 -13
  32. package/src/permits-intrinsics.js +8 -8
  33. package/src/permits.js +5 -0
  34. package/src/reporting.js +48 -12
  35. package/src/scope-constants.js +19 -25
  36. package/src/tame-locale-methods.js +2 -2
  37. package/src/tame-regenerator-runtime.js +2 -2
  38. package/src/transforms.js +9 -7
  39. package/types.d.ts +115 -141
  40. package/src/error/README.md +0 -99
  41. package/src/make-lru-cachemap.js +0 -221
package/src/commons.js CHANGED
@@ -84,6 +84,7 @@ export const {
84
84
  toStringTag: toStringTagSymbol,
85
85
  iterator: iteratorSymbol,
86
86
  matchAll: matchAllSymbol,
87
+ replace: replaceSymbol,
87
88
  unscopables: unscopablesSymbol,
88
89
  keyFor: symbolKeyFor,
89
90
  for: symbolFor,
@@ -166,7 +167,27 @@ const { bind } = functionPrototype;
166
167
  */
167
168
  export const uncurryThis = bind.bind(bind.call); // eslint-disable-line @endo/no-polymorphic-call
168
169
 
169
- export const objectHasOwnProperty = uncurryThis(objectPrototype.hasOwnProperty);
170
+ // See https://github.com/endojs/endo/issues/2930
171
+ if (!('hasOwn' in Object)) {
172
+ const ObjectPrototypeHasOwnProperty = objectPrototype.hasOwnProperty;
173
+ const hasOwnShim = (obj, key) => {
174
+ if (obj === undefined || obj === null) {
175
+ // We need to add this extra test because of differences in
176
+ // the order in which `hasOwn` vs `hasOwnProperty` validates
177
+ // arguments.
178
+ throw TypeError('Cannot convert undefined or null to object');
179
+ }
180
+ return apply(ObjectPrototypeHasOwnProperty, obj, [key]);
181
+ };
182
+ defineProperty(Object, 'hasOwn', {
183
+ value: hasOwnShim,
184
+ writable: true,
185
+ enumerable: false,
186
+ configurable: true,
187
+ });
188
+ }
189
+
190
+ export const { hasOwn } = Object;
170
191
  //
171
192
  export const arrayFilter = uncurryThis(arrayPrototype.filter);
172
193
  export const arrayForEach = uncurryThis(arrayPrototype.forEach);
@@ -207,32 +228,53 @@ export const setForEach = uncurryThis(setPrototype.forEach);
207
228
  export const setHas = uncurryThis(setPrototype.has);
208
229
  export const iterateSet = uncurryThis(setPrototype[iteratorSymbol]);
209
230
  //
210
- export const regexpTest = uncurryThis(regexpPrototype.test);
231
+ /**
232
+ * `regexpExec` is provided in exclusion of `regexpTest`, which would be
233
+ * vulnerable to RegExp.prototype poisoning.
234
+ */
211
235
  export const regexpExec = uncurryThis(regexpPrototype.exec);
236
+ /**
237
+ * @type { &
238
+ * ((thisArg: RegExp, string: string, replaceValue: string) => string) &
239
+ * ((thisArg: RegExp, string: string, replacer: (substring: string, ...args: any[]) => string) => string)
240
+ * }
241
+ */
242
+ export const regexpReplace = /** @type {any} */ (
243
+ uncurryThis(regexpPrototype[replaceSymbol])
244
+ );
212
245
  export const matchAllRegExp = uncurryThis(regexpPrototype[matchAllSymbol]);
246
+ const { _regexpConstructor, ...regexpDescriptors } =
247
+ getOwnPropertyDescriptors(regexpPrototype);
248
+ arrayForEach(ownKeys(regexpDescriptors), key => {
249
+ const desc = regexpDescriptors[/** @type {any} */ (key)];
250
+ desc.configurable = false;
251
+ if (desc.writable) desc.writable = false;
252
+ });
253
+ /**
254
+ * Protect a RegExp instance against RegExp.prototype poisoning ("exec",
255
+ * "flags", Symbol.replace, etc.).
256
+ * @type {<T extends RegExp>(regexp: T) => T}
257
+ */
258
+ export const sealRegexp = regexp =>
259
+ seal(defineProperties(regexp, regexpDescriptors));
213
260
  //
214
261
  export const stringEndsWith = uncurryThis(stringPrototype.endsWith);
215
262
  export const stringIncludes = uncurryThis(stringPrototype.includes);
216
263
  export const stringIndexOf = uncurryThis(stringPrototype.indexOf);
217
264
  export const stringMatch = uncurryThis(stringPrototype.match);
218
- export const generatorNext = uncurryThis(generatorPrototype.next);
219
- export const generatorThrow = uncurryThis(generatorPrototype.throw);
220
-
265
+ // `stringReplace` is intentionally omitted because it would be vulnerable to
266
+ // RegExp.prototype poisoning; use `regexpReplace(re, str, replacer)` instead
267
+ // (and `sealRegexp` on its regular expressions).
268
+ export const stringSearch = uncurryThis(stringPrototype.search);
269
+ export const stringSlice = uncurryThis(stringPrototype.slice);
221
270
  /**
222
- * @type { &
223
- * ((thisArg: string, searchValue: { [Symbol.replace](string: string, replaceValue: string): string; }, replaceValue: string) => string) &
224
- * ((thisArg: string, searchValue: { [Symbol.replace](string: string, replacer: (substring: string, ...args: any[]) => string): string; }, replacer: (substring: string, ...args: any[]) => string) => string)
225
- * }
271
+ * `stringSplit` should only be used with a string separator; regular
272
+ * expressions are vulnerable to RegExp.prototype poisoning.
273
+ * @type {(thisArg: string, separator: string, limit?: number) => string[]}
226
274
  */
227
- export const stringReplace = /** @type {any} */ (
228
- uncurryThis(stringPrototype.replace)
275
+ export const stringSplit = /** @type {any} */ (
276
+ uncurryThis(stringPrototype.split)
229
277
  );
230
- export const stringSearch = uncurryThis(stringPrototype.search);
231
- export const stringSlice = uncurryThis(stringPrototype.slice);
232
- export const stringSplit =
233
- /** @type {(thisArg: string, splitter: string | RegExp | { [Symbol.split](string: string, limit?: number): string[]; }, limit?: number) => string[]} */ (
234
- uncurryThis(stringPrototype.split)
235
- );
236
278
  export const stringStartsWith = uncurryThis(stringPrototype.startsWith);
237
279
  export const iterateString = uncurryThis(stringPrototype[iteratorSymbol]);
238
280
  //
@@ -248,6 +290,9 @@ export const weaksetHas = uncurryThis(weaksetPrototype.has);
248
290
  export const functionToString = uncurryThis(functionPrototype.toString);
249
291
  export const functionBind = uncurryThis(bind);
250
292
  //
293
+ export const generatorNext = uncurryThis(generatorPrototype.next);
294
+ export const generatorThrow = uncurryThis(generatorPrototype.throw);
295
+ //
251
296
  const { all } = Promise;
252
297
  export const promiseAll = promises => apply(all, Promise, [promises]);
253
298
  export const promiseCatch = uncurryThis(promisePrototype.catch);
@@ -263,32 +308,20 @@ export const finalizationRegistryUnregister =
263
308
  uncurryThis(FinalizationRegistry.prototype.unregister);
264
309
 
265
310
  /**
266
- * getConstructorOf()
267
- * Return the constructor from an instance.
311
+ * TODO Consolidate with `isPrimitive` that's currently in `@endo/pass-style`.
312
+ * Layering constraints make this tricky, which is why we haven't yet figured
313
+ * out how to do this.
268
314
  *
269
- * @param {Function} fn
315
+ * @type {(val: unknown) => val is (undefined
316
+ * | null
317
+ * | boolean
318
+ * | number
319
+ * | bigint
320
+ * | string
321
+ * | symbol)}
270
322
  */
271
- export const getConstructorOf = fn =>
272
- reflectGet(getPrototypeOf(fn), 'constructor');
273
-
274
- /**
275
- * isObject tests whether a value is an object.
276
- * Today, this is equivalent to:
277
- *
278
- * const isObject = value => {
279
- * if (value === null) return false;
280
- * const type = typeof value;
281
- * return type === 'object' || type === 'function';
282
- * };
283
- *
284
- * But this is not safe in the face of possible evolution of the language, for
285
- * example new types or semantics of records and tuples.
286
- * We use this implementation despite the unnecessary allocation implied by
287
- * attempting to box a primitive.
288
- *
289
- * @param {any} value
290
- */
291
- export const isObject = value => Object(value) === value;
323
+ export const isPrimitive = val =>
324
+ !val || (typeof val !== 'object' && typeof val !== 'function');
292
325
 
293
326
  /**
294
327
  * isError tests whether an object inherits from the intrinsic
@@ -327,12 +360,39 @@ export const noEvalEvaluate = () => {
327
360
 
328
361
  // ////////////////// FERAL_STACK_GETTER FERAL_STACK_SETTER ////////////////////
329
362
 
330
- const er1StackDesc = getOwnPropertyDescriptor(Error('er1'), 'stack');
331
- const er2StackDesc = getOwnPropertyDescriptor(TypeError('er2'), 'stack');
363
+ // The error repair mechanism is very similar to code in
364
+ // pass-style/src/error.js and these implementations should be kept in sync.
365
+
366
+ /**
367
+ * We gratuitiously construct a TypeError instance using syntax in order to
368
+ * obviate the possibility that code that ran before SES (for which we are
369
+ * irreducable vulnerable) may have replaced the global TypeError constructor.
370
+ * We treat the nature of this error instance as the source of truth for the
371
+ * nature of runtime constructed errors on the platform, particularly whether
372
+ * such errors will have an own "stack" property with getters and setters.
373
+ * At time of writing (2025) we know of no comparable mechanism for obtaining a
374
+ * host-generated base Error instance, but we corroborate the nature of the
375
+ * global Error constructor's instances and refuse to initialize SES in an
376
+ * environment where the syntactic TypeError and global Error produce
377
+ * inconsistent "stack" properties.
378
+ * @returns {TypeError}
379
+ */
380
+ const makeTypeError = () => {
381
+ try {
382
+ // @ts-expect-error deliberate TypeError
383
+ null.null;
384
+ throw TypeError('obligatory'); // To convince the type flow inferrence.
385
+ } catch (error) {
386
+ return error;
387
+ }
388
+ };
389
+
390
+ const errorStackDesc = getOwnPropertyDescriptor(Error('obligatory'), 'stack');
391
+ const typeErrorStackDesc = getOwnPropertyDescriptor(makeTypeError(), 'stack');
332
392
 
333
393
  let feralStackGetter;
334
394
  let feralStackSetter;
335
- if (er1StackDesc && er2StackDesc && er1StackDesc.get) {
395
+ if (typeErrorStackDesc && typeErrorStackDesc.get) {
336
396
  // We should only encounter this case on v8 because of its problematic
337
397
  // error own stack accessor behavior.
338
398
  // Note that FF/SpiderMonkey, Moddable/XS, and the error stack proposal
@@ -343,16 +403,17 @@ if (er1StackDesc && er2StackDesc && er1StackDesc.get) {
343
403
  // accessor property, but within the same realm, all these accessor
344
404
  // properties have the same getter and have the same setter.
345
405
  // This is therefore the case that we repair.
346
- typeof er1StackDesc.get === 'function' &&
347
- er1StackDesc.get === er2StackDesc.get &&
348
- typeof er1StackDesc.set === 'function' &&
349
- er1StackDesc.set === er2StackDesc.set
406
+ errorStackDesc &&
407
+ typeof typeErrorStackDesc.get === 'function' &&
408
+ typeErrorStackDesc.get === errorStackDesc.get &&
409
+ typeof typeErrorStackDesc.set === 'function' &&
410
+ typeErrorStackDesc.set === errorStackDesc.set
350
411
  ) {
351
412
  // Otherwise, we have own stack accessor properties that are outside
352
413
  // our expectations, that therefore need to be understood better
353
414
  // before we know how to repair them.
354
- feralStackGetter = freeze(er1StackDesc.get);
355
- feralStackSetter = freeze(er1StackDesc.set);
415
+ feralStackGetter = freeze(typeErrorStackDesc.get);
416
+ feralStackSetter = freeze(typeErrorStackDesc.set);
356
417
  } else {
357
418
  // See https://github.com/endojs/endo/blob/master/packages/ses/error-codes/SES_UNEXPECTED_ERROR_OWN_STACK_ACCESSOR.md
358
419
  throw TypeError(
@@ -104,6 +104,7 @@ const moduleAliases = new WeakMap();
104
104
  * @property {Compartment} [parentCompartment]
105
105
  * @property {boolean} noNamespaceBox
106
106
  * @property {(fullSpecifier: string) => Promise<ModuleExportsNamespace>} compartmentImport
107
+ * @property {boolean} [noAggregateLoadErrors]
107
108
  */
108
109
 
109
110
  /**
@@ -173,16 +174,17 @@ export const CompartmentPrototype = {
173
174
  },
174
175
 
175
176
  async import(specifier) {
176
- const { noNamespaceBox } = /** @type {CompartmentFields} */ (
177
- weakmapGet(privateFields, this)
178
- );
177
+ const { noNamespaceBox, noAggregateLoadErrors } =
178
+ /** @type {CompartmentFields} */ (weakmapGet(privateFields, this));
179
179
 
180
180
  if (typeof specifier !== 'string') {
181
181
  throw TypeError('first argument of import() must be a string');
182
182
  }
183
183
 
184
184
  return promiseThen(
185
- load(privateFields, moduleAliases, this, specifier),
185
+ load(privateFields, moduleAliases, this, specifier, {
186
+ noAggregateErrors: noAggregateLoadErrors,
187
+ }),
186
188
  () => {
187
189
  // The namespace box is a contentious design and likely to be a breaking
188
190
  // change in an appropriately numbered future version.
@@ -205,16 +207,27 @@ export const CompartmentPrototype = {
205
207
  throw TypeError('first argument of load() must be a string');
206
208
  }
207
209
 
208
- return load(privateFields, moduleAliases, this, specifier);
210
+ const { noAggregateLoadErrors } = /** @type {CompartmentFields} */ (
211
+ weakmapGet(privateFields, this)
212
+ );
213
+
214
+ return load(privateFields, moduleAliases, this, specifier, {
215
+ noAggregateErrors: noAggregateLoadErrors,
216
+ });
209
217
  },
210
218
 
211
219
  importNow(specifier) {
212
220
  if (typeof specifier !== 'string') {
213
221
  throw TypeError('first argument of importNow() must be a string');
214
222
  }
223
+ const { noAggregateLoadErrors } = /** @type {CompartmentFields} */ (
224
+ weakmapGet(privateFields, this)
225
+ );
215
226
 
216
- loadNow(privateFields, moduleAliases, this, specifier);
217
- return compartmentImportNow(this, specifier);
227
+ loadNow(privateFields, moduleAliases, this, specifier, {
228
+ noAggregateErrors: noAggregateLoadErrors,
229
+ });
230
+ return compartmentImportNow(/** @type {Compartment} */ (this), specifier);
218
231
  },
219
232
  };
220
233
 
@@ -345,6 +358,7 @@ export const makeCompartmentConstructor = (
345
358
  moduleMapHook,
346
359
  importMetaHook,
347
360
  __noNamespaceBox__: noNamespaceBox = false,
361
+ noAggregateLoadErrors = false,
348
362
  } = compartmentOptions(...args);
349
363
  const globalTransforms = arrayFlatMap(
350
364
  [transforms, __shimTransforms__],
@@ -422,7 +436,9 @@ export const makeCompartmentConstructor = (
422
436
  `Compartment does not support dynamic import: no configured resolveHook for compartment ${q(name)}`,
423
437
  );
424
438
  }
425
- await load(privateFields, moduleAliases, compartment, fullSpecifier);
439
+ await load(privateFields, moduleAliases, compartment, fullSpecifier, {
440
+ noAggregateErrors: noAggregateLoadErrors,
441
+ });
426
442
  const { execute, exportsProxy } = link(
427
443
  privateFields,
428
444
  moduleAliases,
@@ -451,6 +467,7 @@ export const makeCompartmentConstructor = (
451
467
  parentCompartment,
452
468
  noNamespaceBox,
453
469
  compartmentImport,
470
+ noAggregateLoadErrors,
454
471
  });
455
472
  }
456
473
 
@@ -11,8 +11,8 @@ import {
11
11
  defineProperty,
12
12
  getOwnPropertyDescriptor,
13
13
  getOwnPropertyDescriptors,
14
- isObject,
15
- objectHasOwnProperty,
14
+ isPrimitive,
15
+ hasOwn,
16
16
  ownKeys,
17
17
  setHas,
18
18
  } from './commons.js';
@@ -109,7 +109,7 @@ export default function enablePropertyOverrides(
109
109
  )}' of '${path}'`,
110
110
  );
111
111
  }
112
- if (objectHasOwnProperty(this, prop)) {
112
+ if (hasOwn(this, prop)) {
113
113
  this[prop] = newValue;
114
114
  } else {
115
115
  if (isDebug) {
@@ -179,7 +179,7 @@ export default function enablePropertyOverrides(
179
179
  enableProperty(subPath, obj, prop);
180
180
  } else if (subPlan === '*') {
181
181
  enableAllProperties(subPath, desc.value);
182
- } else if (isObject(subPlan)) {
182
+ } else if (!isPrimitive(subPlan)) {
183
183
  enableProperties(subPath, desc.value, subPlan);
184
184
  } else {
185
185
  throw TypeError(`Unexpected override enablement plan ${subPath}`);
@@ -78,6 +78,7 @@ export const minEnablements = {
78
78
  '%ErrorPrototype%': {
79
79
  name: true, // set by "precond", "ava", "node-fetch"
80
80
  },
81
+
81
82
  '%IteratorPrototype%': {
82
83
  toString: true,
83
84
  // https://github.com/tc39/proposal-iterator-helpers
@@ -91,6 +92,8 @@ export const minEnablements = {
91
92
  * Moderate enablements are usually good enough for legacy compat.
92
93
  */
93
94
  export const moderateEnablements = {
95
+ ...minEnablements,
96
+
94
97
  '%ObjectPrototype%': {
95
98
  toString: true,
96
99
  valueOf: true,
@@ -103,6 +106,10 @@ export const moderateEnablements = {
103
106
  [iteratorSymbol]: true, // set by mobx generated code (old TS compiler?)
104
107
  },
105
108
 
109
+ '%IteratorPrototype%': {
110
+ [iteratorSymbol]: true, // is sometimes used in custom iterators and generators implementations eg. @rive-app/canvas
111
+ },
112
+
106
113
  // Function.prototype has no 'prototype' property to enable.
107
114
  // Function instances have their own 'name' and 'length' properties
108
115
  // which are configurable and non-writable. Thus, they are already
@@ -168,14 +175,6 @@ export const moderateEnablements = {
168
175
  name: true,
169
176
  toString: true,
170
177
  },
171
-
172
- '%IteratorPrototype%': {
173
- toString: true,
174
- // https://github.com/tc39/proposal-iterator-helpers
175
- constructor: true,
176
- // https://github.com/tc39/proposal-iterator-helpers
177
- [toStringTagSymbol]: true,
178
- },
179
178
  };
180
179
 
181
180
  /**