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/dist/types.d.cts CHANGED
@@ -3,6 +3,8 @@
3
3
  * @module
4
4
  */
5
5
 
6
+ import '@endo/immutable-arraybuffer/shim.js';
7
+
6
8
  /* eslint-disable no-restricted-globals, vars-on-top, no-var */
7
9
 
8
10
  // It's academically tempting to define a hardened type, but TypeScript doesn't
@@ -187,20 +189,30 @@ export interface EvaluateOptions {
187
189
  __rejectSomeDirectEvalExpressions__?: boolean;
188
190
  }
189
191
 
192
+ interface Stringable {
193
+ toString(): string;
194
+ }
195
+
196
+ /** @deprecated */
197
+ type StringPayload = Stringable;
198
+
190
199
  /**
191
- * A call to the `details` template literal makes and returns a fresh details
192
- * token, which is a frozen empty object associated with the arguments of that
193
- * `details` template literal expression.
200
+ * A call to the {@link details} template literal makes and returns a fresh
201
+ * DetailsToken, which is a frozen empty object associated with the arguments of
202
+ * that expression.
194
203
  */
195
204
  export type DetailsToken = Record<any, never>;
196
- /** Either a plain string, or made by the `details` template literal tag. */
205
+ /**
206
+ * A plain string, or a {@link DetailsToken} from the {@link details} template
207
+ * literal tag.
208
+ */
197
209
  export type Details = string | DetailsToken;
198
210
 
199
211
  export interface AssertMakeErrorOptions {
200
212
  /**
201
213
  * Does not affect the error.name property. That remains determined by
202
214
  * the constructor. Rather, the `errorName` determines how this error is
203
- * identified in the causal console log's output.
215
+ * identified in an associated console's output.
204
216
  */
205
217
  errorName?: string;
206
218
 
@@ -280,10 +292,6 @@ export type AssertTypeof = AssertTypeofBigint &
280
292
  AssertTypeofSymbol &
281
293
  AssertTypeofUndefined;
282
294
 
283
- interface StringablePayload {
284
- toString(): string;
285
- }
286
-
287
295
  /**
288
296
  * TypeScript does not treat `AggregateErrorConstructor` as a subtype of
289
297
  * `ErrorConstructor`, which makes sense because their constructors
@@ -307,48 +315,44 @@ export type GenericErrorConstructor =
307
315
  export type Raise = (reason: Error) => void;
308
316
 
309
317
  /**
310
- * Makes and returns an `assert` function object that shares the bookkeeping
311
- * state defined by this module with other `assert` function objects made by
312
- * `makeAssert`. This state is per-module-instance and is exposed by the
313
- * `loggedErrorHandler` above. We refer to `assert` as a "function object"
314
- * because it can be called directly as a function, but also has methods that
315
- * can be called.
318
+ * Makes and returns an `assert` function object that shares the
319
+ * per-module-instance bookkeeping state defined by this module with other
320
+ * `assert` function objects made by `makeAssert`. We refer to `assert` as a
321
+ * "function object" because it can be called directly as a function, but also
322
+ * has callable methods of its own.
316
323
  *
317
- * If `optRaise` is provided, the returned `assert` function object will call
318
- * `optRaise(reason)` before throwing the error. This enables `optRaise` to
319
- * engage in even more violent termination behavior, like terminating the vat,
320
- * that prevents execution from reaching the following throw. However, if
321
- * `optRaise` returns normally, which would be unusual, the throw following
322
- * `optRaise(reason)` would still happen.
324
+ * If `raise` is provided, the returned `assert` function object will call
325
+ * `raise(reason)` before throwing an assertion-failure error. This enables
326
+ * `raise` to engage in even more violent termination behavior, like process
327
+ * termination, that prevents execution from reaching the following throw.
328
+ * However, if `raise(reason)` returns normally, which would be unusual, that
329
+ * throw still happens.
323
330
  */
324
- // Behold: recursion.
325
331
  // eslint-disable-next-line no-use-before-define
326
332
  export type MakeAssert = (raise?: Raise, unredacted?: boolean) => Assert;
327
333
 
328
334
  export type BaseAssert = (
329
- /** The truthy/falsy value we're testing */
330
- flag: any,
331
- /** The details of what was asserted */
335
+ /** The condition whose truthiness is being asserted */
336
+ condition: any,
337
+ /** The details associated with assertion failure (falsy condition) */
332
338
  details?: Details,
333
339
  /** An optional alternate error constructor to use */
334
340
  errConstructor?: GenericErrorConstructor,
335
341
  options?: AssertMakeErrorOptions,
336
- ) => asserts flag;
342
+ ) => asserts condition;
337
343
 
338
344
  export interface AssertionFunctions extends BaseAssert {
339
345
  typeof: AssertTypeof;
340
346
 
341
347
  /**
342
- * The `assert.equal` method
343
- *
344
- * Assert that two values must be `Object.is`.
348
+ * Assert that two values are the same as observed by `Object.is`.
345
349
  */
346
350
  equal<T>(
347
351
  /** What we received */
348
352
  actual: unknown,
349
353
  /** What we wanted */
350
354
  expected: T,
351
- /** The details of what was asserted */
355
+ /** The details associated with assertion failure (`Object.is` returning false) */
352
356
  details?: Details,
353
357
  /** An optional alternate error constructor to use */
354
358
  errConstructor?: GenericErrorConstructor,
@@ -356,12 +360,9 @@ export interface AssertionFunctions extends BaseAssert {
356
360
  ): asserts actual is T;
357
361
 
358
362
  /**
359
- * The `assert.string` method.
360
- *
363
+ * Assert that a value is a primitive string.
361
364
  * `assert.string(v)` is equivalent to `assert.typeof(v, 'string')`. We
362
365
  * special case this one because it is the most frequently used.
363
- *
364
- * Assert an expected typeof result.
365
366
  */
366
367
  string(
367
368
  specimen: any,
@@ -370,14 +371,9 @@ export interface AssertionFunctions extends BaseAssert {
370
371
  ): asserts specimen is string;
371
372
 
372
373
  /**
373
- * The `assert.fail` method.
374
- *
375
- * Fail an assertion, recording full details to the console and
376
- * raising an exception with a message in which `details` substitution values
377
- * have been redacted.
378
- *
379
- * The optional `optDetails` can be a string for backwards compatibility
380
- * with the nodejs assertion library.
374
+ * Fail an assertion, raising an exception with a `message` in which unquoted
375
+ * `details` substitution values may have been redacted into `typeof` types
376
+ * but are still available for logging to an associated console.
381
377
  */
382
378
  fail(
383
379
  /** The details of what was asserted */
@@ -390,11 +386,11 @@ export interface AssertionFunctions extends BaseAssert {
390
386
 
391
387
  export interface AssertionUtilities {
392
388
  /**
393
- * Aka the `makeError` function as imported from `@endo/errors`
394
- *
395
- * Recording unredacted details for the console.
389
+ * Create an error with a `message` in which unquoted {@link details}
390
+ * substitution values may have been redacted into lossy `typeof` output but
391
+ * are still available for logging to an associated console.
396
392
  */
397
- error(
393
+ makeError(
398
394
  /** The details of what was asserted */
399
395
  details?: Details,
400
396
  /** An optional alternate error constructor to use */
@@ -403,43 +399,27 @@ export interface AssertionUtilities {
403
399
  ): Error;
404
400
 
405
401
  /**
406
- * Aka the `annotateError` function as imported from `@endo/errors`
407
- *
408
- * Annotate an error with details, potentially to be used by an
409
- * augmented console such as the causal console of `console.js`, to
410
- * provide extra information associated with logged errors.
402
+ * Associate `details` with `error`, potentially to be logged by an associated
403
+ * console for providing extra information about the error.
411
404
  */
412
405
  note(error: Error, details: Details): void;
413
406
 
414
407
  /**
415
- * Use the `details` function as a template literal tag to create
416
- * informative error messages. The assertion functions take such messages
417
- * as optional arguments:
418
- * ```js
419
- * assert(sky.isBlue(), details`${sky.color} should be "blue"`);
420
- * ```
421
- * or following the normal convention to locally rename `details` to `X`
422
- * and `quote` to `q` like `const { details: X, quote: q } = assert;`:
423
- * ```js
424
- * assert(sky.isBlue(), X`${sky.color} should be "blue"`);
425
- * ```
408
+ * Use as a template literal tag to create an opaque {@link DetailsToken} for
409
+ * use with other assertion functions that might redact unquoted substitution
410
+ * values (i.e., those that are not output from the {@link quote} function)
411
+ * into lossy `typeof` output but still preserve them for logging to an
412
+ * associated console.
413
+ *
414
+ * The normal convention is to locally rename `details` to `X` like
415
+ * `const { details: X, quote: q, Fail } = assert;`.
426
416
  * However, note that in most cases it is preferable to instead use the `Fail`
427
- * template literal tag (which has the same input signature as `details`
428
- * but automatically creates and throws an error):
417
+ * template literal tag (which has the same input signature but automatically
418
+ * creates and throws an error):
429
419
  * ```js
430
420
  * sky.isBlue() || Fail`${sky.color} should be "blue"`;
431
421
  * ```
432
422
  *
433
- * The details template tag returns a `DetailsToken` object that can print
434
- * itself with the formatted message in two ways.
435
- * It will report full details to the console, but
436
- * mask embedded substitution values with their typeof information in the thrown error
437
- * to prevent revealing secrets up the exceptional path. In the example
438
- * above, the thrown error may reveal only that `sky.color` is a string,
439
- * whereas the same diagnostic printed to the console reveals that the
440
- * sky was green. This masking can be disabled for an individual substitution value
441
- * using `quote`.
442
- *
443
423
  * The `raw` property of an input template array is ignored, so a simple
444
424
  * array of strings may be provided directly.
445
425
  */
@@ -449,70 +429,69 @@ export interface AssertionUtilities {
449
429
  ): DetailsToken;
450
430
 
451
431
  /**
452
- * Use the `Fail` function as a template literal tag to efficiently
453
- * create and throw a `details`-style error only when a condition is not satisfied.
454
- * ```js
455
- * condition || Fail`...complaint...`;
456
- * ```
457
- * This avoids the overhead of creating usually-unnecessary errors like
458
- * ```js
459
- * assert(condition, details`...complaint...`);
460
- * ```
461
- * while improving readability over alternatives like
462
- * ```js
463
- * condition || assert.fail(details`...complaint...`);
464
- * ```
432
+ * Use as a template literal tag to create and throw an error in whose
433
+ * `message` unquoted substitution values (i.e., those that are not output
434
+ * from the {@link quote} function) may have been redacted into lossy `typeof`
435
+ * output but are still available for logging to an associated console.
465
436
  *
466
- * However, due to current weakness in TypeScript, static reasoning
467
- * is less powerful with the `||` patterns than with an `assert` call.
468
- * Until/unless https://github.com/microsoft/TypeScript/issues/51426 is fixed,
469
- * for `||`-style assertions where this loss of static reasoning is a problem,
470
- * instead express the assertion as
437
+ * For example, using the normal convention to locally rename properties like
438
+ * `const { quote: q, Fail } = assert;`:
471
439
  * ```js
472
- * if (!condition) {
473
- * Fail`...complaint...`;
474
- * }
440
+ * sky.isBlue() || Fail`${sky.color} should be "blue"`;
475
441
  * ```
476
- * or, if needed,
442
+ *
443
+ * This `||` pattern saves the cost of creating {@link DetailsToken} and/or
444
+ * error instances when the asserted condition holds, but can weaken
445
+ * TypeScript static reasoning due to
446
+ * https://github.com/microsoft/TypeScript/issues/51426 . Where this is a
447
+ * problem, instead express the assertion as
477
448
  * ```js
478
- * if (!condition) {
479
- * // `throw` is noop since `Fail` throws, but it improves static analysis
480
- * throw Fail`...complaint...`;
481
- * }
449
+ * if (!sky.isBlue()) {
450
+ * // This `throw` does not affect runtime behavior since `Fail` throws, but
451
+ * // might be needed to improve static analysis.
452
+ * throw Fail`${sky.color} should be "blue"`;
453
+ * }
482
454
  * ```
455
+ *
456
+ * The `raw` property of an input template array is ignored, so a simple
457
+ * array of strings may be provided directly.
483
458
  */
484
459
  Fail(template: TemplateStringsArray | string[], ...args: any): never;
485
460
 
486
461
  /**
487
- * To "declassify" and quote a substitution value used in a
488
- * ``` details`...` ``` template literal, enclose that substitution expression
489
- * in a call to `quote`. This makes the value appear quoted
490
- * (as if with `JSON.stringify`) in the message of the thrown error. The
491
- * payload itself is still passed unquoted to the console as it would be
492
- * without `quote`.
462
+ * Wrap a value such that its use as a substitution value in a template
463
+ * literal tagged with {@link details} or {@link Fail} will result in it
464
+ * appearing quoted (in a way similar to but more general than
465
+ * `JSON.stringify`) rather than redacted in the `message` of errors based on
466
+ * the resulting {@link DetailsToken}.
493
467
  *
494
- * For example, the following will reveal the expected sky color, but not the
495
- * actual incorrect sky color, in the thrown error's message:
496
- * ```js
497
- * sky.color === expectedColor || Fail`${sky.color} should be ${quote(expectedColor)}`;
498
- * ```
468
+ * This does not affect representation in output of an associated console,
469
+ * which still logs the value as it would without `quote`, but *does* reveal
470
+ * it to functions in the propagation path of such errors.
499
471
  *
500
- * The normal convention is to locally rename `details` to `X` and `quote` to `q`
501
- * like `const { details: X, quote: q } = assert;`, so the above example would then be
472
+ * For example, the following will reveal the expected value in the thrown
473
+ * error's `message`, but only the _type_ of the actual incorrect value (using
474
+ * the normal convention to locally rename properties like
475
+ * `const { quote: q, Fail } = assert;`):
502
476
  * ```js
503
- * sky.color === expectedColor || Fail`${sky.color} should be ${q(expectedColor)}`;
477
+ * actual === expected || Fail`${actual} should be ${q(expected)}`;
504
478
  * ```
479
+ *
480
+ * The optional `space` parameter matches that of `JSON.stringify`, and is
481
+ * used to request insertion of non-semantic line feeds, indentation, and
482
+ * separating spaces in the output for improving readability of objects and
483
+ * arrays.
505
484
  */
506
- quote(
507
- /** What to declassify */
508
- payload: any,
509
- spaces?: string | number,
510
- ): /** The declassified and quoted payload */ StringablePayload;
485
+ quote(value: any, space?: string | number): Stringable;
511
486
 
512
487
  /**
513
- * Embed a string directly into error details without wrapping punctuation.
488
+ * Wrap a string such that its use as a substitution value in a template
489
+ * literal tagged with {@link details} or {@link Fail} will be treated
490
+ * literally rather than being quoted or redacted.
491
+ *
514
492
  * To avoid injection attacks that exploit quoting confusion, this must NEVER
515
493
  * be used with data that is possibly attacker-controlled.
494
+ *
516
495
  * As a further safeguard, we fall back to quoting any input that is not a
517
496
  * string of sufficiently word-like parts separated by isolated spaces (rather
518
497
  * than throwing an exception, which could hide the original problem for which
@@ -522,35 +501,25 @@ export interface AssertionUtilities {
522
501
  * by inline replacement similar to that of `bestEffortStringify` for producing
523
502
  * rendered messages like `(an object) was tagged "[Unsafe bare string]"`).
524
503
  */
525
- bare(
526
- /** What to declassify */
527
- payload: any,
528
- spaces?: string | number,
529
- ): /** The declassified payload without quotes (beware confusion hazard) */
530
- StringablePayload;
504
+ bare(text: string, spaces?: string | number): Stringable;
531
505
  }
532
506
 
533
507
  export interface DeprecatedAssertionUtilities {
534
508
  makeAssert: MakeAssert;
509
+ error: AssertionUtilities['makeError'];
535
510
  }
536
511
 
537
512
  /**
538
- * assert that expr is truthy, with an optional details to describe
539
- * the assertion. It is a tagged template literal like
540
- * ```js
541
- * assert(expr, details`....`);`
542
- * ```
543
- *
544
- * The literal portions of the template are assumed non-sensitive, as
545
- * are the `typeof` types of the substitution values. These are
546
- * assembled into the thrown error message. The actual contents of the
547
- * substitution values are assumed sensitive, to be revealed to
548
- * the console only. We assume only the virtual platform's owner can read
549
- * what is written to the console, where the owner is in a privileged
550
- * position over computation running on that platform.
513
+ * Assert that `condition` is truthy, with optional details associated with
514
+ * assertion failure (falsy condition).
551
515
  *
552
- * The optional `optDetails` can be a string for backwards compatibility
553
- * with the nodejs assertion library.
516
+ * The literal portions of the template used to make a {@link DetailsToken} are
517
+ * assumed non-sensitive, as are the `typeof` output for substitution values.
518
+ * These are assembled into the error `message`. The actual contents of the
519
+ * substitution values are assumed sensitive and usually redacted, to be
520
+ * revealed only to an associated console. We assume only the virtual platform's
521
+ * owner can read what is written to the console, where the owner is in a
522
+ * privileged position over computation running on that platform.
554
523
  */
555
524
  export type Assert = AssertionFunctions &
556
525
  AssertionUtilities &
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ses",
3
- "version": "1.14.0",
3
+ "version": "2.0.0",
4
4
  "description": "Hardened JavaScript for Fearless Cooperation",
5
5
  "keywords": [
6
6
  "lockdown",
@@ -80,7 +80,7 @@
80
80
  "build:hermes": "node scripts/bundle.js hermes",
81
81
  "build": "yarn build:vanilla && yarn build:hermes",
82
82
  "clean": "rm -rf dist",
83
- "cover": "c8 ava",
83
+ "cover": "c8 ava --config test/_ava-ses.config.js",
84
84
  "demo": "python3 -m http.server",
85
85
  "lint": "yarn lint:types && yarn lint:eslint",
86
86
  "lint-fix": "eslint --fix .",
@@ -91,7 +91,7 @@
91
91
  "test": "tsd && ava",
92
92
  "test:hermes": "./scripts/hermes-test.sh",
93
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 \"*.d.ts*\" \"*.d.cts*\" \"*.d.mts*\" \"*.tsbuildinfo\""
94
+ "postpack": "git clean -fX -e node_modules/"
95
95
  },
96
96
  "dependencies": {
97
97
  "@endo/cache-map": "^1.1.0",
@@ -99,28 +99,21 @@
99
99
  "@endo/immutable-arraybuffer": "^1.1.2"
100
100
  },
101
101
  "devDependencies": {
102
- "@babel/generator": "^7.26.3",
103
- "@babel/parser": "~7.26.2",
104
- "@babel/traverse": "~7.25.9",
105
- "@babel/types": "~7.26.0",
106
- "@endo/compartment-mapper": "^1.6.3",
107
- "@endo/module-source": "^1.3.3",
108
- "@endo/test262-runner": "^0.1.48",
109
- "@types/babel__traverse": "^7.20.5",
110
- "ava": "^6.1.3",
111
- "babel-eslint": "^10.1.0",
112
- "c8": "^7.14.0",
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.1.0",
107
+ "@endo/module-source": "^1.4.1",
108
+ "@endo/test262-runner": "^0.1.50",
109
+ "ava": "catalog:dev",
110
+ "c8": "catalog:dev",
113
111
  "core-js": "^3.31.0",
114
- "eslint": "^8.57.1",
115
- "eslint-config-airbnb-base": "^15.0.0",
116
- "eslint-config-prettier": "^9.1.0",
117
- "eslint-plugin-eslint-comments": "^3.2.0",
118
- "eslint-plugin-import": "^2.31.0",
112
+ "eslint": "catalog:dev",
119
113
  "hermes-engine-cli": "^0.12.0",
120
- "prettier": "^3.5.3",
121
114
  "terser": "^5.16.6",
122
- "tsd": "^0.31.2",
123
- "typescript": "~5.8.3"
115
+ "tsd": "catalog:dev",
116
+ "typescript": "catalog:dev"
124
117
  },
125
118
  "files": [
126
119
  "./*.d.ts",
@@ -150,5 +143,5 @@
150
143
  "typeCoverage": {
151
144
  "atLeast": 81.17
152
145
  },
153
- "gitHead": "9815aea9541f241389d2135c6097a7442bdffa17"
146
+ "gitHead": "c3616c39c35f2d052f7083aba31054910951beb4"
154
147
  }
@@ -1,4 +1,4 @@
1
1
  import { globalThis } from './commons.js';
2
- import { assert } from './error/assert.js';
2
+ import { makeAssert } from './error/assert.js';
3
3
 
4
- globalThis.assert = assert;
4
+ globalThis.assert = makeAssert(undefined, true);