ses 1.14.0 → 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.
package/src/commons.js CHANGED
@@ -77,7 +77,6 @@ export const {
77
77
  setPrototypeOf,
78
78
  values,
79
79
  fromEntries,
80
- hasOwn,
81
80
  } = Object;
82
81
 
83
82
  export const {
@@ -85,6 +84,7 @@ export const {
85
84
  toStringTag: toStringTagSymbol,
86
85
  iterator: iteratorSymbol,
87
86
  matchAll: matchAllSymbol,
87
+ replace: replaceSymbol,
88
88
  unscopables: unscopablesSymbol,
89
89
  keyFor: symbolKeyFor,
90
90
  for: symbolFor,
@@ -167,10 +167,27 @@ const { bind } = functionPrototype;
167
167
  */
168
168
  export const uncurryThis = bind.bind(bind.call); // eslint-disable-line @endo/no-polymorphic-call
169
169
 
170
- /**
171
- * @deprecated Use `hasOwn` instead
172
- */
173
- export const objectHasOwnProperty = hasOwn;
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;
174
191
  //
175
192
  export const arrayFilter = uncurryThis(arrayPrototype.filter);
176
193
  export const arrayForEach = uncurryThis(arrayPrototype.forEach);
@@ -211,32 +228,53 @@ export const setForEach = uncurryThis(setPrototype.forEach);
211
228
  export const setHas = uncurryThis(setPrototype.has);
212
229
  export const iterateSet = uncurryThis(setPrototype[iteratorSymbol]);
213
230
  //
214
- 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
+ */
215
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
+ );
216
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));
217
260
  //
218
261
  export const stringEndsWith = uncurryThis(stringPrototype.endsWith);
219
262
  export const stringIncludes = uncurryThis(stringPrototype.includes);
220
263
  export const stringIndexOf = uncurryThis(stringPrototype.indexOf);
221
264
  export const stringMatch = uncurryThis(stringPrototype.match);
222
- export const generatorNext = uncurryThis(generatorPrototype.next);
223
- export const generatorThrow = uncurryThis(generatorPrototype.throw);
224
-
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);
225
270
  /**
226
- * @type { &
227
- * ((thisArg: string, searchValue: { [Symbol.replace](string: string, replaceValue: string): string; }, replaceValue: string) => string) &
228
- * ((thisArg: string, searchValue: { [Symbol.replace](string: string, replacer: (substring: string, ...args: any[]) => string): string; }, replacer: (substring: string, ...args: any[]) => string) => string)
229
- * }
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[]}
230
274
  */
231
- export const stringReplace = /** @type {any} */ (
232
- uncurryThis(stringPrototype.replace)
275
+ export const stringSplit = /** @type {any} */ (
276
+ uncurryThis(stringPrototype.split)
233
277
  );
234
- export const stringSearch = uncurryThis(stringPrototype.search);
235
- export const stringSlice = uncurryThis(stringPrototype.slice);
236
- export const stringSplit =
237
- /** @type {(thisArg: string, splitter: string | RegExp | { [Symbol.split](string: string, limit?: number): string[]; }, limit?: number) => string[]} */ (
238
- uncurryThis(stringPrototype.split)
239
- );
240
278
  export const stringStartsWith = uncurryThis(stringPrototype.startsWith);
241
279
  export const iterateString = uncurryThis(stringPrototype[iteratorSymbol]);
242
280
  //
@@ -252,6 +290,9 @@ export const weaksetHas = uncurryThis(weaksetPrototype.has);
252
290
  export const functionToString = uncurryThis(functionPrototype.toString);
253
291
  export const functionBind = uncurryThis(bind);
254
292
  //
293
+ export const generatorNext = uncurryThis(generatorPrototype.next);
294
+ export const generatorThrow = uncurryThis(generatorPrototype.throw);
295
+ //
255
296
  const { all } = Promise;
256
297
  export const promiseAll = promises => apply(all, Promise, [promises]);
257
298
  export const promiseCatch = uncurryThis(promisePrototype.catch);
@@ -266,15 +307,6 @@ export const finalizationRegistryUnregister =
266
307
  FinalizationRegistry &&
267
308
  uncurryThis(FinalizationRegistry.prototype.unregister);
268
309
 
269
- /**
270
- * getConstructorOf()
271
- * Return the constructor from an instance.
272
- *
273
- * @param {Function} fn
274
- */
275
- export const getConstructorOf = fn =>
276
- reflectGet(getPrototypeOf(fn), 'constructor');
277
-
278
310
  /**
279
311
  * TODO Consolidate with `isPrimitive` that's currently in `@endo/pass-style`.
280
312
  * Layering constraints make this tricky, which is why we haven't yet figured
@@ -328,12 +360,39 @@ export const noEvalEvaluate = () => {
328
360
 
329
361
  // ////////////////// FERAL_STACK_GETTER FERAL_STACK_SETTER ////////////////////
330
362
 
331
- const er1StackDesc = getOwnPropertyDescriptor(Error('er1'), 'stack');
332
- 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');
333
392
 
334
393
  let feralStackGetter;
335
394
  let feralStackSetter;
336
- if (er1StackDesc && er2StackDesc && er1StackDesc.get) {
395
+ if (typeErrorStackDesc && typeErrorStackDesc.get) {
337
396
  // We should only encounter this case on v8 because of its problematic
338
397
  // error own stack accessor behavior.
339
398
  // Note that FF/SpiderMonkey, Moddable/XS, and the error stack proposal
@@ -344,16 +403,17 @@ if (er1StackDesc && er2StackDesc && er1StackDesc.get) {
344
403
  // accessor property, but within the same realm, all these accessor
345
404
  // properties have the same getter and have the same setter.
346
405
  // This is therefore the case that we repair.
347
- typeof er1StackDesc.get === 'function' &&
348
- er1StackDesc.get === er2StackDesc.get &&
349
- typeof er1StackDesc.set === 'function' &&
350
- 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
351
411
  ) {
352
412
  // Otherwise, we have own stack accessor properties that are outside
353
413
  // our expectations, that therefore need to be understood better
354
414
  // before we know how to repair them.
355
- feralStackGetter = freeze(er1StackDesc.get);
356
- feralStackSetter = freeze(er1StackDesc.set);
415
+ feralStackGetter = freeze(typeErrorStackDesc.get);
416
+ feralStackSetter = freeze(typeErrorStackDesc.set);
357
417
  } else {
358
418
  // See https://github.com/endojs/endo/blob/master/packages/ses/error-codes/SES_UNEXPECTED_ERROR_OWN_STACK_ACCESSOR.md
359
419
  throw TypeError(
@@ -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
  /**