ses 1.14.0 → 2.0.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
@@ -26,11 +26,14 @@ export const {
26
26
  ArrayBuffer,
27
27
  Date,
28
28
  FinalizationRegistry,
29
- Float32Array,
29
+ // Renamed to FERAL_* because it enables the NaN side-channel
30
+ Float64Array: FERAL_FLOAT64_ARRAY,
31
+ DataView,
30
32
  JSON,
31
33
  Map,
32
34
  Math,
33
35
  Number,
36
+ BigInt,
34
37
  Object,
35
38
  Promise,
36
39
  Proxy,
@@ -77,7 +80,6 @@ export const {
77
80
  setPrototypeOf,
78
81
  values,
79
82
  fromEntries,
80
- hasOwn,
81
83
  } = Object;
82
84
 
83
85
  export const {
@@ -85,6 +87,7 @@ export const {
85
87
  toStringTag: toStringTagSymbol,
86
88
  iterator: iteratorSymbol,
87
89
  matchAll: matchAllSymbol,
90
+ replace: replaceSymbol,
88
91
  unscopables: unscopablesSymbol,
89
92
  keyFor: symbolKeyFor,
90
93
  for: symbolFor,
@@ -167,10 +170,27 @@ const { bind } = functionPrototype;
167
170
  */
168
171
  export const uncurryThis = bind.bind(bind.call); // eslint-disable-line @endo/no-polymorphic-call
169
172
 
170
- /**
171
- * @deprecated Use `hasOwn` instead
172
- */
173
- export const objectHasOwnProperty = hasOwn;
173
+ // See https://github.com/endojs/endo/issues/2930
174
+ if (!('hasOwn' in Object)) {
175
+ const ObjectPrototypeHasOwnProperty = objectPrototype.hasOwnProperty;
176
+ const hasOwnShim = (obj, key) => {
177
+ if (obj === undefined || obj === null) {
178
+ // We need to add this extra test because of differences in
179
+ // the order in which `hasOwn` vs `hasOwnProperty` validates
180
+ // arguments.
181
+ throw TypeError('Cannot convert undefined or null to object');
182
+ }
183
+ return apply(ObjectPrototypeHasOwnProperty, obj, [key]);
184
+ };
185
+ defineProperty(Object, 'hasOwn', {
186
+ value: hasOwnShim,
187
+ writable: true,
188
+ enumerable: false,
189
+ configurable: true,
190
+ });
191
+ }
192
+
193
+ export const { hasOwn } = Object;
174
194
  //
175
195
  export const arrayFilter = uncurryThis(arrayPrototype.filter);
176
196
  export const arrayForEach = uncurryThis(arrayPrototype.forEach);
@@ -211,32 +231,53 @@ export const setForEach = uncurryThis(setPrototype.forEach);
211
231
  export const setHas = uncurryThis(setPrototype.has);
212
232
  export const iterateSet = uncurryThis(setPrototype[iteratorSymbol]);
213
233
  //
214
- export const regexpTest = uncurryThis(regexpPrototype.test);
234
+ /**
235
+ * `regexpExec` is provided in exclusion of `regexpTest`, which would be
236
+ * vulnerable to RegExp.prototype poisoning.
237
+ */
215
238
  export const regexpExec = uncurryThis(regexpPrototype.exec);
239
+ /**
240
+ * @type { &
241
+ * ((thisArg: RegExp, string: string, replaceValue: string) => string) &
242
+ * ((thisArg: RegExp, string: string, replacer: (substring: string, ...args: any[]) => string) => string)
243
+ * }
244
+ */
245
+ export const regexpReplace = /** @type {any} */ (
246
+ uncurryThis(regexpPrototype[replaceSymbol])
247
+ );
216
248
  export const matchAllRegExp = uncurryThis(regexpPrototype[matchAllSymbol]);
249
+ const { _regexpConstructor, ...regexpDescriptors } =
250
+ getOwnPropertyDescriptors(regexpPrototype);
251
+ arrayForEach(ownKeys(regexpDescriptors), key => {
252
+ const desc = regexpDescriptors[/** @type {any} */ (key)];
253
+ desc.configurable = false;
254
+ if (desc.writable) desc.writable = false;
255
+ });
256
+ /**
257
+ * Protect a RegExp instance against RegExp.prototype poisoning ("exec",
258
+ * "flags", Symbol.replace, etc.).
259
+ * @type {<T extends RegExp>(regexp: T) => T}
260
+ */
261
+ export const sealRegexp = regexp =>
262
+ seal(defineProperties(regexp, regexpDescriptors));
217
263
  //
218
264
  export const stringEndsWith = uncurryThis(stringPrototype.endsWith);
219
265
  export const stringIncludes = uncurryThis(stringPrototype.includes);
220
266
  export const stringIndexOf = uncurryThis(stringPrototype.indexOf);
221
267
  export const stringMatch = uncurryThis(stringPrototype.match);
222
- export const generatorNext = uncurryThis(generatorPrototype.next);
223
- export const generatorThrow = uncurryThis(generatorPrototype.throw);
224
-
268
+ // `stringReplace` is intentionally omitted because it would be vulnerable to
269
+ // RegExp.prototype poisoning; use `regexpReplace(re, str, replacer)` instead
270
+ // (and `sealRegexp` on its regular expressions).
271
+ export const stringSearch = uncurryThis(stringPrototype.search);
272
+ export const stringSlice = uncurryThis(stringPrototype.slice);
225
273
  /**
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
- * }
274
+ * `stringSplit` should only be used with a string separator; regular
275
+ * expressions are vulnerable to RegExp.prototype poisoning.
276
+ * @type {(thisArg: string, separator: string, limit?: number) => string[]}
230
277
  */
231
- export const stringReplace = /** @type {any} */ (
232
- uncurryThis(stringPrototype.replace)
278
+ export const stringSplit = /** @type {any} */ (
279
+ uncurryThis(stringPrototype.split)
233
280
  );
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
281
  export const stringStartsWith = uncurryThis(stringPrototype.startsWith);
241
282
  export const iterateString = uncurryThis(stringPrototype[iteratorSymbol]);
242
283
  //
@@ -252,6 +293,9 @@ export const weaksetHas = uncurryThis(weaksetPrototype.has);
252
293
  export const functionToString = uncurryThis(functionPrototype.toString);
253
294
  export const functionBind = uncurryThis(bind);
254
295
  //
296
+ export const generatorNext = uncurryThis(generatorPrototype.next);
297
+ export const generatorThrow = uncurryThis(generatorPrototype.throw);
298
+ //
255
299
  const { all } = Promise;
256
300
  export const promiseAll = promises => apply(all, Promise, [promises]);
257
301
  export const promiseCatch = uncurryThis(promisePrototype.catch);
@@ -266,15 +310,6 @@ export const finalizationRegistryUnregister =
266
310
  FinalizationRegistry &&
267
311
  uncurryThis(FinalizationRegistry.prototype.unregister);
268
312
 
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
313
  /**
279
314
  * TODO Consolidate with `isPrimitive` that's currently in `@endo/pass-style`.
280
315
  * Layering constraints make this tricky, which is why we haven't yet figured
@@ -328,12 +363,39 @@ export const noEvalEvaluate = () => {
328
363
 
329
364
  // ////////////////// FERAL_STACK_GETTER FERAL_STACK_SETTER ////////////////////
330
365
 
331
- const er1StackDesc = getOwnPropertyDescriptor(Error('er1'), 'stack');
332
- const er2StackDesc = getOwnPropertyDescriptor(TypeError('er2'), 'stack');
366
+ // The error repair mechanism is very similar to code in
367
+ // pass-style/src/error.js and these implementations should be kept in sync.
368
+
369
+ /**
370
+ * We gratuitiously construct a TypeError instance using syntax in order to
371
+ * obviate the possibility that code that ran before SES (for which we are
372
+ * irreducable vulnerable) may have replaced the global TypeError constructor.
373
+ * We treat the nature of this error instance as the source of truth for the
374
+ * nature of runtime constructed errors on the platform, particularly whether
375
+ * such errors will have an own "stack" property with getters and setters.
376
+ * At time of writing (2025) we know of no comparable mechanism for obtaining a
377
+ * host-generated base Error instance, but we corroborate the nature of the
378
+ * global Error constructor's instances and refuse to initialize SES in an
379
+ * environment where the syntactic TypeError and global Error produce
380
+ * inconsistent "stack" properties.
381
+ * @returns {TypeError}
382
+ */
383
+ const makeTypeError = () => {
384
+ try {
385
+ // @ts-expect-error deliberate TypeError
386
+ null.null;
387
+ throw TypeError('obligatory'); // To convince the type flow inferrence.
388
+ } catch (error) {
389
+ return error;
390
+ }
391
+ };
392
+
393
+ const errorStackDesc = getOwnPropertyDescriptor(Error('obligatory'), 'stack');
394
+ const typeErrorStackDesc = getOwnPropertyDescriptor(makeTypeError(), 'stack');
333
395
 
334
396
  let feralStackGetter;
335
397
  let feralStackSetter;
336
- if (er1StackDesc && er2StackDesc && er1StackDesc.get) {
398
+ if (typeErrorStackDesc && typeErrorStackDesc.get) {
337
399
  // We should only encounter this case on v8 because of its problematic
338
400
  // error own stack accessor behavior.
339
401
  // Note that FF/SpiderMonkey, Moddable/XS, and the error stack proposal
@@ -344,16 +406,17 @@ if (er1StackDesc && er2StackDesc && er1StackDesc.get) {
344
406
  // accessor property, but within the same realm, all these accessor
345
407
  // properties have the same getter and have the same setter.
346
408
  // 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
409
+ errorStackDesc &&
410
+ typeof typeErrorStackDesc.get === 'function' &&
411
+ typeErrorStackDesc.get === errorStackDesc.get &&
412
+ typeof typeErrorStackDesc.set === 'function' &&
413
+ typeErrorStackDesc.set === errorStackDesc.set
351
414
  ) {
352
415
  // Otherwise, we have own stack accessor properties that are outside
353
416
  // our expectations, that therefore need to be understood better
354
417
  // before we know how to repair them.
355
- feralStackGetter = freeze(er1StackDesc.get);
356
- feralStackSetter = freeze(er1StackDesc.set);
418
+ feralStackGetter = freeze(typeErrorStackDesc.get);
419
+ feralStackSetter = freeze(typeErrorStackDesc.set);
357
420
  } else {
358
421
  // See https://github.com/endojs/endo/blob/master/packages/ses/error-codes/SES_UNEXPECTED_ERROR_OWN_STACK_ACCESSOR.md
359
422
  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,35 +92,42 @@ 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
- toString: true,
98
+ ...minEnablements['%ObjectPrototype%'],
96
99
  valueOf: true,
97
100
  },
98
101
 
99
- '%ArrayPrototype%': {
100
- toString: true,
101
- push: true, // set by "Google Analytics"
102
- concat: true, // set by mobx generated code (old TS compiler?)
103
- [iteratorSymbol]: true, // set by mobx generated code (old TS compiler?)
104
- },
105
-
106
102
  // Function.prototype has no 'prototype' property to enable.
107
103
  // Function instances have their own 'name' and 'length' properties
108
104
  // which are configurable and non-writable. Thus, they are already
109
105
  // non-assignable anyway.
110
106
  '%FunctionPrototype%': {
107
+ ...minEnablements['%FunctionPrototype%'],
111
108
  constructor: true, // set by "regenerator-runtime"
112
109
  bind: true, // set by "underscore", "express"
113
- toString: true, // set by "rollup"
114
110
  },
115
111
 
116
112
  '%ErrorPrototype%': {
113
+ ...minEnablements['%ErrorPrototype%'],
117
114
  constructor: true, // set by "fast-json-patch", "node-fetch"
118
115
  message: true,
119
- name: true, // set by "precond", "ava", "node-fetch", "node 14"
120
116
  toString: true, // set by "bluebird"
121
117
  },
122
118
 
119
+ '%IteratorPrototype%': {
120
+ ...minEnablements['%IteratorPrototype%'],
121
+ [iteratorSymbol]: true, // is sometimes used in custom iterators and generators implementations eg. @rive-app/canvas
122
+ },
123
+
124
+ '%ArrayPrototype%': {
125
+ toString: true,
126
+ push: true, // set by "Google Analytics"
127
+ concat: true, // set by mobx generated code (old TS compiler?)
128
+ [iteratorSymbol]: true, // set by mobx generated code (old TS compiler?)
129
+ },
130
+
123
131
  '%TypeErrorPrototype%': {
124
132
  constructor: true, // set by "readable-stream"
125
133
  message: true, // set by "tape"
@@ -168,14 +176,6 @@ export const moderateEnablements = {
168
176
  name: true,
169
177
  toString: true,
170
178
  },
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
179
  };
180
180
 
181
181
  /**