ses 2.2.0 → 2.3.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/dist/types.d.cts CHANGED
@@ -242,6 +242,11 @@ export interface AssertMakeErrorOptions {
242
242
  * {@link sanitizeError}.
243
243
  */
244
244
  sanitize?: boolean;
245
+
246
+ /**
247
+ * The error code to assign to the error.
248
+ */
249
+ code?: string;
245
250
  }
246
251
 
247
252
  // TODO inline overloading
@@ -393,11 +398,33 @@ export interface AssertionUtilities {
393
398
  * Create an error with a `message` in which unquoted {@link details}
394
399
  * substitution values may have been redacted into lossy `typeof` output but
395
400
  * are still available for logging to an associated console.
401
+ *
402
+ * Narrows the return type to the constructor type.
403
+ *
404
+ * @template T The constructor to use
405
+ * @param details The details of what was asserted
406
+ * @param errConstructor The constructor to use
407
+ * @param options Options
408
+ * @returns Instance of `T`
409
+ */
410
+ makeError<T extends GenericErrorConstructor>(
411
+ details: Details,
412
+ errConstructor: T,
413
+ options?: AssertMakeErrorOptions,
414
+ ): InstanceType<T>;
415
+
416
+ /**
417
+ * Create an error with a `message` in which unquoted {@link details}
418
+ * substitution values may have been redacted into lossy `typeof` output but
419
+ * are still available for logging to an associated console.
420
+ *
421
+ * @param details The details of what was asserted
422
+ * @param errConstructor The constructor to use
423
+ * @param options Options
424
+ * @returns Instance of `Error`
396
425
  */
397
426
  makeError(
398
- /** The details of what was asserted */
399
427
  details?: Details,
400
- /** An optional alternate error constructor to use */
401
428
  errConstructor?: GenericErrorConstructor,
402
429
  options?: AssertMakeErrorOptions,
403
430
  ): Error;
@@ -564,6 +591,8 @@ declare global {
564
591
 
565
592
  get name(): string;
566
593
 
594
+ get __noNamespaceBox__(): boolean;
595
+
567
596
  evaluate(code: string, options?: CompartmentEvaluateOptions): any;
568
597
 
569
598
  import(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ses",
3
- "version": "2.2.0",
3
+ "version": "2.3.0",
4
4
  "description": "Hardened JavaScript for Fearless Cooperation",
5
5
  "keywords": [
6
6
  "lockdown",
@@ -75,13 +75,47 @@
75
75
  "./console-shim.js": "./console-shim.js",
76
76
  "./package.json": "./package.json"
77
77
  },
78
+ "scripts": {
79
+ "build:vanilla": "node scripts/bundle.js",
80
+ "build:hermes": "node scripts/bundle.js hermes",
81
+ "build": "yarn build:vanilla && yarn build:hermes",
82
+ "clean": "rm -rf dist",
83
+ "cover": "c8 ava --config test/_ava-ses.config.js",
84
+ "demo": "python3 -m http.server",
85
+ "lint": "yarn lint:types && yarn lint:eslint",
86
+ "lint-fix": "eslint --fix .",
87
+ "lint:eslint": "eslint .",
88
+ "lint:types": "tsc",
89
+ "prepare": "npm run clean && npm run build",
90
+ "qt": "ava",
91
+ "test": "tsd && ava",
92
+ "test:hermes": "./scripts/hermes-test.sh",
93
+ "test:xs": "xst dist/ses.umd.js test/_lockdown-safe.js && node scripts/generate-test-xs.js && xst tmp/test-xs.js && rm -rf tmp",
94
+ "postpack": "git clean -fX -e node_modules/"
95
+ },
78
96
  "dependencies": {
79
97
  "@endo/cache-map": "^1.1.0",
80
98
  "@endo/env-options": "^1.1.11",
81
- "@endo/immutable-arraybuffer": "^1.1.2"
99
+ "@endo/immutable-arraybuffer": "^2.0.0"
100
+ },
101
+ "devDependencies": {
102
+ "@babel/generator": "^7.28.3",
103
+ "@babel/parser": "~7.28.3",
104
+ "@babel/traverse": "~7.28.3",
105
+ "@babel/types": "~7.28.2",
106
+ "@endo/compartment-mapper": "^2.4.0",
107
+ "@endo/module-source": "^1.5.0",
108
+ "@endo/test262-runner": "^0.1.50",
109
+ "ava": "^8.0.1",
110
+ "c8": "^11.0.0",
111
+ "core-js": "^3.31.0",
112
+ "eslint": "^10.4.1",
113
+ "hermes-engine-cli": "^0.12.0",
114
+ "terser": "^5.16.6",
115
+ "tsd": "^0.33.0",
116
+ "typescript": "~6.0.3"
82
117
  },
83
118
  "files": [
84
- "./*.d.js",
85
119
  "./*.d.ts",
86
120
  "./*.js",
87
121
  "./*.map",
@@ -96,11 +130,6 @@
96
130
  "publishConfig": {
97
131
  "access": "public"
98
132
  },
99
- "eslintConfig": {
100
- "extends": [
101
- "plugin:@endo/ses"
102
- ]
103
- },
104
133
  "ava": {
105
134
  "files": [
106
135
  "test/**/*.test.*"
@@ -110,4 +139,4 @@
110
139
  "typeCoverage": {
111
140
  "atLeast": 81.17
112
141
  }
113
- }
142
+ }
package/src/commons.js CHANGED
@@ -13,7 +13,6 @@
13
13
  * @module
14
14
  */
15
15
 
16
- /* global globalThis */
17
16
  /* eslint-disable no-restricted-globals */
18
17
 
19
18
  // We cannot use globalThis as the local name since it would capture the
@@ -38,7 +38,6 @@
38
38
  */
39
39
 
40
40
  // @ts-check
41
- /* eslint-disable no-underscore-dangle */
42
41
 
43
42
  import {
44
43
  Map,
@@ -153,6 +152,11 @@ export const CompartmentPrototype = {
153
152
  .name;
154
153
  },
155
154
 
155
+ get __noNamespaceBox__() {
156
+ return /** @type {CompartmentFields} */ (weakmapGet(privateFields, this))
157
+ .noNamespaceBox;
158
+ },
159
+
156
160
  evaluate(source, options = {}) {
157
161
  const compartmentFields = weakmapGet(privateFields, this);
158
162
  return compartmentEvaluate(compartmentFields, source, options);
@@ -1,10 +1,49 @@
1
- import { toStringTagSymbol, iteratorSymbol } from './commons.js';
2
-
3
1
  /**
4
- * Exports {@code enablements}, a recursively defined
5
- * JSON record defining the optimum set of intrinsics properties
6
- * that need to be "repaired" before hardening is applied on
7
- * enviromments subject to the override mistake.
2
+ * Exports `enablements`, a recursively defined JSON record defining the optimum
3
+ * set of intrinsics properties that need to be "repaired" before hardening is
4
+ * applied on enviromments subject to the override mistake.
5
+ *
6
+ * Because "repairing" replaces data properties with accessors, every time a
7
+ * repaired property is accessed, the associated getter is invoked, which
8
+ * degrades the runtime performance of all code executing in the repaired
9
+ * enviromment, compared to the non-repaired case. In order to maintain
10
+ * performance, we only repair the properties of objects for which hardening
11
+ * causes a breakage of their normal intended usage.
12
+ *
13
+ * There are three unwanted cases:
14
+ *
15
+ * - Overriding properties on objects typically used as records, namely `Object`
16
+ * and `Array`. In the case of arrays, the situation is unintentional, a given
17
+ * program might not be aware that non-numerical properties are stored on the
18
+ * underlying object instance, not on the array. When an object is typically
19
+ * used as a map, we repair all of its prototype properties.
20
+ * - Overriding properties on objects that provide defaults on their prototype
21
+ * and that programs typically set using an assignment, such as
22
+ * `Error.prototype.message` and `Function.prototype.name` (both default to
23
+ * `""`).
24
+ * - Setting-up a prototype chain, where a constructor is set to extend another
25
+ * one. This is typically set by assignment, for example
26
+ * `Child.prototype.constructor = Child`, instead of invoking
27
+ * `Object.defineProperty()`.
28
+ *
29
+ * Each JSON record enumerates the disposition of the properties on some
30
+ * corresponding intrinsic object.
31
+ *
32
+ * For each such record, the values associated with its property names can be:
33
+ *
34
+ * - `true`, in which case this property is simply repaired. The value associated
35
+ * with that property is not traversed. For example, `Function.prototype.name`
36
+ * leads to true, meaning that the `name` property of `Function.prototype`
37
+ * should be repaired (which is needed when inheriting from `Function` and
38
+ * setting the subclass's `prototype.name` property). If the property is
39
+ * already an accessor property, it is not repaired (because accessors are not
40
+ * subject to the override mistake).
41
+ * - `*`, in which case this property is not repaired but the value associated
42
+ * with that property are traversed and repaired.
43
+ * - Another record, in which case this property is not repaired and that next
44
+ * record represents the disposition of the object which is its value. For
45
+ * example,`FunctionPrototype` leads to another record explaining which
46
+ * properties `Function.prototype` need to be repaired.
8
47
  *
9
48
  * @author JF Paradis
10
49
  * @author Mark S. Miller
@@ -12,59 +51,11 @@ import { toStringTagSymbol, iteratorSymbol } from './commons.js';
12
51
  * @module
13
52
  */
14
53
 
15
- /**
16
- * <p>Because "repairing" replaces data properties with accessors, every
17
- * time a repaired property is accessed, the associated getter is invoked,
18
- * which degrades the runtime performance of all code executing in the
19
- * repaired enviromment, compared to the non-repaired case. In order
20
- * to maintain performance, we only repair the properties of objects
21
- * for which hardening causes a breakage of their normal intended usage.
22
- *
23
- * There are three unwanted cases:
24
- * <ul>
25
- * <li>Overriding properties on objects typically used as records,
26
- * namely {@code "Object"} and {@code "Array"}. In the case of arrays,
27
- * the situation is unintentional, a given program might not be aware
28
- * that non-numerical properties are stored on the underlying object
29
- * instance, not on the array. When an object is typically used as a
30
- * map, we repair all of its prototype properties.
31
- * <li>Overriding properties on objects that provide defaults on their
32
- * prototype and that programs typically set using an assignment, such as
33
- * {@code "Error.prototype.message"} and {@code "Function.prototype.name"}
34
- * (both default to "").
35
- * <li>Setting-up a prototype chain, where a constructor is set to extend
36
- * another one. This is typically set by assignment, for example
37
- * {@code "Child.prototype.constructor = Child"}, instead of invoking
38
- * Object.defineProperty();
39
- *
40
- * <p>Each JSON record enumerates the disposition of the properties on
41
- * some corresponding intrinsic object.
42
- *
43
- * <p>For each such record, the values associated with its property
44
- * names can be:
45
- * <ul>
46
- * <li>true, in which case this property is simply repaired. The
47
- * value associated with that property is not traversed. For
48
- * example, {@code "Function.prototype.name"} leads to true,
49
- * meaning that the {@code "name"} property of {@code
50
- * "Function.prototype"} should be repaired (which is needed
51
- * when inheriting from @code{Function} and setting the subclass's
52
- * {@code "prototype.name"} property). If the property is
53
- * already an accessor property, it is not repaired (because
54
- * accessors are not subject to the override mistake).
55
- * <li>"*", in which case this property is not repaired but the
56
- * value associated with that property are traversed and repaired.
57
- * <li>Another record, in which case this property is not repaired
58
- * and that next record represents the disposition of the object
59
- * which is its value. For example,{@code "FunctionPrototype"}
60
- * leads to another record explaining which properties {@code
61
- * Function.prototype} need to be repaired.
62
- */
54
+ import { toStringTagSymbol, iteratorSymbol } from './commons.js';
63
55
 
64
56
  /**
65
- * Minimal enablements when all the code is modern and known not to
66
- * step into the override mistake, except for the following pervasive
67
- * cases.
57
+ * Minimal enablements when all the code is modern and known not to step into
58
+ * the override mistake, except for the following pervasive cases.
68
59
  */
69
60
  export const minEnablements = {
70
61
  '%ObjectPrototype%': {
@@ -48,7 +48,17 @@ import './internal-types.js';
48
48
  import { makeNoteLogArgsArrayKit } from './note-log-args.js';
49
49
 
50
50
  /**
51
- * @import {BaseAssert, Assert, AssertionFunctions, AssertionUtilities, DeprecatedAssertionUtilities, Stringable, DetailsToken, MakeAssert} from '../../types.js';
51
+ * @import {BaseAssert,
52
+ * Assert,
53
+ * AssertionFunctions,
54
+ * AssertionUtilities,
55
+ * DeprecatedAssertionUtilities,
56
+ * Stringable,
57
+ * DetailsToken,
58
+ * MakeAssert,
59
+ * Details,
60
+ * GenericErrorConstructor,
61
+ * AssertMakeErrorOptions} from '../../types.js';
52
62
  * @import {LogArgs, NoteCallback, LoggedErrorHandler} from './internal-types.js';
53
63
  */
54
64
 
@@ -285,7 +295,7 @@ const tagError = (err, optErrorName = err.name) => {
285
295
  * such as `stack` on v8 (Chrome, Brave, Edge?)
286
296
  * - `sanitizeError` will freeze the error, preventing any correct engine from
287
297
  * adding or
288
- * altering any of the error's own properties `sanitizeError` is done.
298
+ * altering any of the error's own properties once `sanitizeError` is done.
289
299
  *
290
300
  * However, `sanitizeError` will not, for example, `harden`
291
301
  * (i.e., deeply freeze)
@@ -305,10 +315,16 @@ export const sanitizeError = error => {
305
315
  errors: _errorsDesc = undefined,
306
316
  cause: _causeDesc = undefined,
307
317
  stack: _stackDesc = undefined,
318
+ code: codeDesc = undefined,
308
319
  ...restDescs
309
320
  } = descs;
310
321
 
311
322
  const restNames = ownKeys(restDescs);
323
+
324
+ // the spec allows any value, but we drop 'code' if it's not a string
325
+ if (codeDesc?.value !== undefined && typeof codeDesc.value !== 'string') {
326
+ arrayPush(restNames, 'code');
327
+ }
312
328
  if (restNames.length >= 1) {
313
329
  for (const name of restNames) {
314
330
  delete error[name];
@@ -333,34 +349,51 @@ export const sanitizeError = error => {
333
349
  * @type {AssertionUtilities['makeError']}
334
350
  */
335
351
  const makeError = (
336
- optDetails = redactedDetails`Assert failed`,
337
- errConstructor = globalThis.Error,
352
+ optDetails,
353
+ errConstructor,
338
354
  {
339
355
  errorName = undefined,
340
356
  cause = undefined,
341
357
  errors = undefined,
342
358
  sanitize = true,
359
+ code = undefined,
343
360
  } = {},
344
361
  ) => {
362
+ // The first two parameters above cannot be inferred unless this is rewritten
363
+ // as a function declaration using an @overload tag. This is a workaround so
364
+ // that we at least have type-safety within the function body.
365
+ //
366
+ // Note that due to the overload of AssertionUtilities['makeError'], strict
367
+ // Note that due to the overload of AssertionUtilities['makeError'], Typescript's so-called "strict
368
+ // mode" will complain if default parameters are provided in the method
369
+ // signature. The below workaround (optDetails -> details; errConstructor ->
370
+ // errCtor) is functionally equivalent but allows us to use type assertions to
371
+ // workaround the issue with TypeScript's so-called "strict mode".
372
+ let details = /** @type {Details} */ (
373
+ optDetails ?? redactedDetails`Assert failed`
374
+ );
375
+
376
+ // Internally, this is a GenericErrorConstructor, but externally it can be
377
+ // some T which extends GenericErrorConstructor.
378
+ const errCtor = /** @type {GenericErrorConstructor} */ (
379
+ errConstructor ?? globalThis.Error
380
+ );
345
381
  // Promote string-valued `optDetails` into a minimal DetailsParts
346
382
  // consisting of that string as the sole literal part with no substitutions.
347
- if (typeof optDetails === 'string') {
348
- optDetails = redactedDetails([optDetails]);
383
+ if (typeof details === 'string') {
384
+ details = redactedDetails([details]);
349
385
  }
350
- const hiddenDetails = weakmapGet(hiddenDetailsMap, optDetails);
386
+ const hiddenDetails = weakmapGet(hiddenDetailsMap, details);
351
387
  if (hiddenDetails === undefined) {
352
- throw TypeError(`unrecognized details ${quote(optDetails)}`);
388
+ throw TypeError(`unrecognized details ${quote(details)}`);
353
389
  }
354
390
  const messageString = getMessageString(hiddenDetails);
355
391
  const opts = cause && { cause };
356
392
  let error;
357
- if (
358
- typeof AggregateError !== 'undefined' &&
359
- errConstructor === AggregateError
360
- ) {
393
+ if (typeof AggregateError !== 'undefined' && errCtor === AggregateError) {
361
394
  error = AggregateError(errors || [], messageString, opts);
362
395
  } else {
363
- const ErrorCtor = /** @type {ErrorConstructor} */ (errConstructor);
396
+ const ErrorCtor = /** @type {ErrorConstructor} */ (errCtor);
364
397
  error = ErrorCtor(messageString, opts);
365
398
  // Since we need to tolerate `errors` on an AggregateError, we may as well
366
399
  // tolerate it on all errors.
@@ -373,6 +406,14 @@ const makeError = (
373
406
  });
374
407
  }
375
408
  }
409
+ if (code !== undefined) {
410
+ defineProperty(error, 'code', {
411
+ value: code,
412
+ writable: true,
413
+ enumerable: false,
414
+ configurable: true,
415
+ });
416
+ }
376
417
  weakmapSet(hiddenMessageLogArgs, error, getLogArgs(hiddenDetails));
377
418
  if (errorName !== undefined) {
378
419
  tagError(error, errorName);
@@ -380,7 +421,11 @@ const makeError = (
380
421
  if (sanitize) {
381
422
  sanitizeError(error);
382
423
  }
383
- // The next line is a particularly fruitful place to put a breakpoint.
424
+ // Externally, the return type below is InstanceType<T> where T extends
425
+ // GenericErrorConstructor. Internally, it's
426
+ // InstanceType<GenericErrorConstructor> for simplicity of implementation.
427
+
428
+ // The next line is a particularly fruitful place to put a breakpoint.
384
429
  return error;
385
430
  };
386
431
  freeze(makeError);