ses 1.4.1 → 1.6.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.
@@ -7,6 +7,7 @@ import {
7
7
  setPrototypeOf,
8
8
  getOwnPropertyDescriptor,
9
9
  defineProperty,
10
+ getOwnPropertyDescriptors,
10
11
  } from '../commons.js';
11
12
  import { NativeErrors } from '../permits.js';
12
13
  import { tameV8ErrorConstructor } from './tame-v8-error-constructor.js';
@@ -29,12 +30,17 @@ const tamedMethods = {
29
30
  return '';
30
31
  },
31
32
  };
33
+ let initialGetStackString = tamedMethods.getStackString;
32
34
 
33
35
  export default function tameErrorConstructor(
34
36
  errorTaming = 'safe',
35
37
  stackFiltering = 'concise',
36
38
  ) {
37
- if (errorTaming !== 'safe' && errorTaming !== 'unsafe') {
39
+ if (
40
+ errorTaming !== 'safe' &&
41
+ errorTaming !== 'unsafe' &&
42
+ errorTaming !== 'unsafe-debug'
43
+ ) {
38
44
  throw TypeError(`unrecognized errorTaming ${errorTaming}`);
39
45
  }
40
46
  if (stackFiltering !== 'concise' && stackFiltering !== 'verbose') {
@@ -42,9 +48,9 @@ export default function tameErrorConstructor(
42
48
  }
43
49
  const ErrorPrototype = FERAL_ERROR.prototype;
44
50
 
45
- const platform =
46
- typeof FERAL_ERROR.captureStackTrace === 'function' ? 'v8' : 'unknown';
47
51
  const { captureStackTrace: originalCaptureStackTrace } = FERAL_ERROR;
52
+ const platform =
53
+ typeof originalCaptureStackTrace === 'function' ? 'v8' : 'unknown';
48
54
 
49
55
  const makeErrorConstructor = (_ = {}) => {
50
56
  // eslint-disable-next-line no-shadow
@@ -122,6 +128,45 @@ export default function tameErrorConstructor(
122
128
  },
123
129
  });
124
130
 
131
+ if (errorTaming === 'unsafe-debug' && platform === 'v8') {
132
+ // This case is a kludge to work around
133
+ // https://github.com/endojs/endo/issues/1798
134
+ // https://github.com/endojs/endo/issues/2348
135
+ // https://github.com/Agoric/agoric-sdk/issues/8662
136
+
137
+ defineProperties(InitialError, {
138
+ prepareStackTrace: {
139
+ get() {
140
+ return FERAL_ERROR.prepareStackTrace;
141
+ },
142
+ set(newPrepareStackTrace) {
143
+ FERAL_ERROR.prepareStackTrace = newPrepareStackTrace;
144
+ },
145
+ enumerable: false,
146
+ configurable: true,
147
+ },
148
+ captureStackTrace: {
149
+ value: FERAL_ERROR.captureStackTrace,
150
+ writable: true,
151
+ enumerable: false,
152
+ configurable: true,
153
+ },
154
+ });
155
+
156
+ const descs = getOwnPropertyDescriptors(InitialError);
157
+ defineProperties(SharedError, {
158
+ stackTraceLimit: descs.stackTraceLimit,
159
+ prepareStackTrace: descs.prepareStackTrace,
160
+ captureStackTrace: descs.captureStackTrace,
161
+ });
162
+
163
+ return {
164
+ '%InitialGetStackString%': initialGetStackString,
165
+ '%InitialError%': InitialError,
166
+ '%SharedError%': SharedError,
167
+ };
168
+ }
169
+
125
170
  // The default SharedError much be completely powerless even on v8,
126
171
  // so the lenient `stackTraceLimit` accessor does nothing on all
127
172
  // platforms.
@@ -171,7 +216,6 @@ export default function tameErrorConstructor(
171
216
  });
172
217
  }
173
218
 
174
- let initialGetStackString = tamedMethods.getStackString;
175
219
  if (platform === 'v8') {
176
220
  initialGetStackString = tameV8ErrorConstructor(
177
221
  FERAL_ERROR,
@@ -179,7 +223,7 @@ export default function tameErrorConstructor(
179
223
  errorTaming,
180
224
  stackFiltering,
181
225
  );
182
- } else if (errorTaming === 'unsafe') {
226
+ } else if (errorTaming === 'unsafe' || errorTaming === 'unsafe-debug') {
183
227
  // v8 has too much magic around their 'stack' own property for it to
184
228
  // coexist cleanly with this accessor. So only install it on non-v8
185
229
 
@@ -16,6 +16,7 @@ import {
16
16
  weakmapSet,
17
17
  weaksetAdd,
18
18
  weaksetHas,
19
+ TypeError,
19
20
  } from '../commons.js';
20
21
 
21
22
  // Whitelist names from https://v8.dev/docs/stack-trace-api
@@ -164,6 +165,11 @@ export const tameV8ErrorConstructor = (
164
165
  errorTaming,
165
166
  stackFiltering,
166
167
  ) => {
168
+ if (errorTaming === 'unsafe-debug') {
169
+ throw TypeError(
170
+ 'internal: v8+unsafe-debug special case should already be done',
171
+ );
172
+ }
167
173
  // TODO: Proper CallSite types
168
174
  /** @typedef {{}} CallSite */
169
175
 
@@ -1,370 +1,6 @@
1
1
  // @ts-check
2
2
 
3
- /**
4
- * TypeScript does not treat `AggregateErrorConstructor` as a subtype of
5
- * `ErrorConstructor`, which makes sense because their constructors
6
- * have incompatible signatures. However, we want to parameterize some
7
- * operations by any error constructor, including possible `AggregateError`.
8
- * So we introduce `GenericErrorConstructor` as a common supertype. Any call
9
- * to it to make an instance must therefore first case split on whether the
10
- * constructor is an AggregateErrorConstructor or a normal ErrorConstructor.
11
- *
12
- * @typedef {ErrorConstructor | AggregateErrorConstructor} GenericErrorConstructor
13
- */
14
-
15
- /**
16
- * @callback BaseAssert
17
- * The `assert` function itself.
18
- *
19
- * @param {any} flag The truthy/falsy value
20
- * @param {Details} [optDetails] The details to throw
21
- * @param {GenericErrorConstructor} [errConstructor]
22
- * An optional alternate error constructor to use.
23
- * @param {AssertMakeErrorOptions} [options]
24
- * @returns {asserts flag}
25
- */
26
-
27
- /**
28
- * @typedef {object} AssertMakeErrorOptions
29
- * @property {string} [errorName]
30
- * @property {Error} [cause]
31
- * @property {Error[]} [errors]
32
- * Normally only used when the ErrorConstuctor is `AggregateError`
33
- */
34
-
35
- /**
36
- * @callback AssertMakeError
37
- *
38
- * The `assert.error` method, recording details for the console.
39
- *
40
- * The optional `optDetails` can be a string.
41
- * @param {Details} [optDetails] The details of what was asserted
42
- * @param {GenericErrorConstructor} [errConstructor]
43
- * An optional alternate error constructor to use.
44
- * @param {AssertMakeErrorOptions} [options]
45
- * @returns {Error}
46
- */
47
-
48
- /**
49
- * @callback AssertFail
50
- *
51
- * The `assert.fail` method.
52
- *
53
- * Fail an assertion, recording full details to the console and
54
- * raising an exception with a message in which `details` substitution values
55
- * have been redacted.
56
- *
57
- * The optional `optDetails` can be a string for backwards compatibility
58
- * with the nodejs assertion library.
59
- * @param {Details} [optDetails] The details of what was asserted
60
- * @param {GenericErrorConstructor} [errConstructor]
61
- * An optional alternate error constructor to use.
62
- * @param {AssertMakeErrorOptions} [options]
63
- * @returns {never}
64
- */
65
-
66
- /**
67
- * @callback AssertEqual
68
- * The `assert.equal` method
69
- *
70
- * Assert that two values must be `Object.is`.
71
- * @param {any} actual The value we received
72
- * @param {any} expected What we wanted
73
- * @param {Details} [optDetails] The details to throw
74
- * @param {GenericErrorConstructor} [errConstructor]
75
- * An optional alternate error constructor to use.
76
- * @param {AssertMakeErrorOptions} [options]
77
- * @returns {void}
78
- */
79
-
80
- // Type all the overloads of the assertTypeof function.
81
- // There may eventually be a better way to do this, but
82
- // thems the breaks with Typescript 4.0.
83
- /**
84
- * @callback AssertTypeofBigint
85
- * @param {any} specimen
86
- * @param {'bigint'} typename
87
- * @param {Details} [optDetails]
88
- * @returns {asserts specimen is bigint}
89
- */
90
-
91
- /**
92
- * @callback AssertTypeofBoolean
93
- * @param {any} specimen
94
- * @param {'boolean'} typename
95
- * @param {Details} [optDetails]
96
- * @returns {asserts specimen is boolean}
97
- */
98
-
99
- /**
100
- * @callback AssertTypeofFunction
101
- * @param {any} specimen
102
- * @param {'function'} typename
103
- * @param {Details} [optDetails]
104
- * @returns {asserts specimen is Function}
105
- */
106
-
107
- /**
108
- * @callback AssertTypeofNumber
109
- * @param {any} specimen
110
- * @param {'number'} typename
111
- * @param {Details} [optDetails]
112
- * @returns {asserts specimen is number}
113
- */
114
-
115
- /**
116
- * @callback AssertTypeofObject
117
- * @param {any} specimen
118
- * @param {'object'} typename
119
- * @param {Details} [optDetails]
120
- * @returns {asserts specimen is Record<any, any> | null}
121
- */
122
-
123
- /**
124
- * @callback AssertTypeofString
125
- * @param {any} specimen
126
- * @param {'string'} typename
127
- * @param {Details} [optDetails]
128
- * @returns {asserts specimen is string}
129
- */
130
-
131
- /**
132
- * @callback AssertTypeofSymbol
133
- * @param {any} specimen
134
- * @param {'symbol'} typename
135
- * @param {Details} [optDetails]
136
- * @returns {asserts specimen is symbol}
137
- */
138
-
139
- /**
140
- * @callback AssertTypeofUndefined
141
- * @param {any} specimen
142
- * @param {'undefined'} typename
143
- * @param {Details} [optDetails]
144
- * @returns {asserts specimen is undefined}
145
- */
146
-
147
- /**
148
- * The `assert.typeof` method
149
- *
150
- * @typedef {AssertTypeofBigint & AssertTypeofBoolean & AssertTypeofFunction & AssertTypeofNumber & AssertTypeofObject & AssertTypeofString & AssertTypeofSymbol & AssertTypeofUndefined} AssertTypeof
151
- */
152
-
153
- /**
154
- * @callback AssertString
155
- * The `assert.string` method.
156
- *
157
- * `assert.string(v)` is equivalent to `assert.typeof(v, 'string')`. We
158
- * special case this one because it is the most frequently used.
159
- *
160
- * Assert an expected typeof result.
161
- * @param {any} specimen The value to get the typeof
162
- * @param {Details} [optDetails] The details to throw
163
- * @returns {asserts specimen is string}
164
- */
165
-
166
- /**
167
- * @callback AssertNote
168
- * The `assert.note` method.
169
- *
170
- * Annotate an error with details, potentially to be used by an
171
- * augmented console such as the causal console of `console.js`, to
172
- * provide extra information associated with logged errors.
173
- *
174
- * @param {Error} error
175
- * @param {Details} detailsNote
176
- * @returns {void}
177
- */
178
-
179
- // /////////////////////////////////////////////////////////////////////////////
180
-
181
- /**
182
- * @typedef {{}} DetailsToken
183
- * A call to the `details` template literal makes and returns a fresh details
184
- * token, which is a frozen empty object associated with the arguments of that
185
- * `details` template literal expression.
186
- */
187
-
188
- /**
189
- * @typedef {string | DetailsToken} Details
190
- * Either a plain string, or made by the `details` template literal tag.
191
- */
192
-
193
- /**
194
- * @typedef {object} StringablePayload
195
- * Holds the payload passed to quote so that its printed form is visible.
196
- * @property {() => string} toString How to print the payload
197
- */
198
-
199
- /**
200
- * To "declassify" and quote a substitution value used in a
201
- * ``` details`...` ``` template literal, enclose that substitution expression
202
- * in a call to `quote`. This makes the value appear quoted
203
- * (as if with `JSON.stringify`) in the message of the thrown error. The
204
- * payload itself is still passed unquoted to the console as it would be
205
- * without `quote`.
206
- *
207
- * For example, the following will reveal the expected sky color, but not the
208
- * actual incorrect sky color, in the thrown error's message:
209
- * ```js
210
- * sky.color === expectedColor || Fail`${sky.color} should be ${quote(expectedColor)}`;
211
- * ```
212
- *
213
- * // TODO Update SES-shim to new convention, where `details` is
214
- * // renamed to `X` rather than `d`.
215
- * The normal convention is to locally rename `details` to `d` and `quote` to `q`
216
- * like `const { details: d, quote: q } = assert;`, so the above example would then be
217
- * ```js
218
- * sky.color === expectedColor || Fail`${sky.color} should be ${q(expectedColor)}`;
219
- * ```
220
- *
221
- * @callback AssertQuote
222
- * @param {any} payload What to declassify
223
- * @param {(string|number)} [spaces]
224
- * @returns {StringablePayload} The declassified payload
225
- */
226
-
227
- /**
228
- * @callback Raise
229
- *
230
- * To make an `assert` which terminates some larger unit of computation
231
- * like a transaction, vat, or process, call `makeAssert` with a `Raise`
232
- * callback, where that callback actually performs that larger termination.
233
- * If possible, the callback should also report its `reason` parameter as
234
- * the alleged reason for the termination.
235
- *
236
- * @param {Error} reason
237
- */
238
-
239
- /**
240
- * @callback MakeAssert
241
- *
242
- * Makes and returns an `assert` function object that shares the bookkeeping
243
- * state defined by this module with other `assert` function objects made by
244
- * `makeAssert`. This state is per-module-instance and is exposed by the
245
- * `loggedErrorHandler` above. We refer to `assert` as a "function object"
246
- * because it can be called directly as a function, but also has methods that
247
- * can be called.
248
- *
249
- * If `optRaise` is provided, the returned `assert` function object will call
250
- * `optRaise(reason)` before throwing the error. This enables `optRaise` to
251
- * engage in even more violent termination behavior, like terminating the vat,
252
- * that prevents execution from reaching the following throw. However, if
253
- * `optRaise` returns normally, which would be unusual, the throw following
254
- * `optRaise(reason)` would still happen.
255
- *
256
- * @param {Raise} [optRaise]
257
- * @param {boolean} [unredacted]
258
- * @returns {Assert}
259
- */
260
-
261
- /**
262
- * @typedef {(template: TemplateStringsArray | string[], ...args: any) => DetailsToken} DetailsTag
263
- *
264
- * Use the `details` function as a template literal tag to create
265
- * informative error messages. The assertion functions take such messages
266
- * as optional arguments:
267
- * ```js
268
- * assert(sky.isBlue(), details`${sky.color} should be "blue"`);
269
- * ```
270
- * // TODO Update SES-shim to new convention, where `details` is
271
- * // renamed to `X` rather than `d`.
272
- * or following the normal convention to locally rename `details` to `d`
273
- * and `quote` to `q` like `const { details: d, quote: q } = assert;`:
274
- * ```js
275
- * assert(sky.isBlue(), d`${sky.color} should be "blue"`);
276
- * ```
277
- * However, note that in most cases it is preferable to instead use the `Fail`
278
- * template literal tag (which has the same input signature as `details`
279
- * but automatically creates and throws an error):
280
- * ```js
281
- * sky.isBlue() || Fail`${sky.color} should be "blue"`;
282
- * ```
283
- *
284
- * The details template tag returns a `DetailsToken` object that can print
285
- * itself with the formatted message in two ways.
286
- * It will report full details to the console, but
287
- * mask embedded substitution values with their typeof information in the thrown error
288
- * to prevent revealing secrets up the exceptional path. In the example
289
- * above, the thrown error may reveal only that `sky.color` is a string,
290
- * whereas the same diagnostic printed to the console reveals that the
291
- * sky was green. This masking can be disabled for an individual substitution value
292
- * using `quote`.
293
- *
294
- * The `raw` property of an input template array is ignored, so a simple
295
- * array of strings may be provided directly.
296
- */
297
-
298
- /**
299
- * @typedef {(template: TemplateStringsArray | string[], ...args: any) => never} FailTag
300
- *
301
- * Use the `Fail` function as a template literal tag to efficiently
302
- * create and throw a `details`-style error only when a condition is not satisfied.
303
- * ```js
304
- * condition || Fail`...complaint...`;
305
- * ```
306
- * This avoids the overhead of creating usually-unnecessary errors like
307
- * ```js
308
- * assert(condition, details`...complaint...`);
309
- * ```
310
- * while improving readability over alternatives like
311
- * ```js
312
- * condition || assert.fail(details`...complaint...`);
313
- * ```
314
- *
315
- * However, due to current weakness in TypeScript, static reasoning
316
- * is less powerful with the `||` patterns than with an `assert` call.
317
- * Until/unless https://github.com/microsoft/TypeScript/issues/51426 is fixed,
318
- * for `||`-style assertions where this loss of static reasoning is a problem,
319
- * instead express the assertion as
320
- * ```js
321
- * if (!condition) {
322
- * Fail`...complaint...`;
323
- * }
324
- * ```
325
- * or, if needed,
326
- * ```js
327
- * if (!condition) {
328
- * // `throw` is noop since `Fail` throws, but it improves static analysis
329
- * throw Fail`...complaint...`;
330
- * }
331
- * ```
332
- */
333
-
334
- /**
335
- * assert that expr is truthy, with an optional details to describe
336
- * the assertion. It is a tagged template literal like
337
- * ```js
338
- * assert(expr, details`....`);`
339
- * ```
340
- *
341
- * The literal portions of the template are assumed non-sensitive, as
342
- * are the `typeof` types of the substitution values. These are
343
- * assembled into the thrown error message. The actual contents of the
344
- * substitution values are assumed sensitive, to be revealed to
345
- * the console only. We assume only the virtual platform's owner can read
346
- * what is written to the console, where the owner is in a privileged
347
- * position over computation running on that platform.
348
- *
349
- * The optional `optDetails` can be a string for backwards compatibility
350
- * with the nodejs assertion library.
351
- *
352
- * @typedef { BaseAssert & {
353
- * typeof: AssertTypeof,
354
- * error: AssertMakeError,
355
- * fail: AssertFail,
356
- * equal: AssertEqual,
357
- * string: AssertString,
358
- * note: AssertNote,
359
- * details: DetailsTag,
360
- * Fail: FailTag,
361
- * quote: AssertQuote,
362
- * bare: AssertQuote,
363
- * makeAssert: MakeAssert,
364
- * } } Assert
365
- */
366
-
367
- // /////////////////////////////////////////////////////////////////////////////
3
+ /** @import {GenericErrorConstructor, AssertMakeErrorOptions, DetailsToken, StringablePayload} from '../../types.js' */
368
4
 
369
5
  /**
370
6
  * @typedef {object} VirtualConsole
@@ -69,11 +69,12 @@ export const setGlobalObjectConstantProperties = globalObject => {
69
69
  * `sharedGlobalPropertyNames`.
70
70
  *
71
71
  * @param {object} globalObject
72
- * @param {object} param1
73
- * @param {object} param1.intrinsics
74
- * @param {object} param1.newGlobalPropertyNames
75
- * @param {Function} param1.makeCompartmentConstructor
76
- * @param {(object) => void} param1.markVirtualizedNativeFunction
72
+ * @param {object} args
73
+ * @param {object} args.intrinsics
74
+ * @param {object} args.newGlobalPropertyNames
75
+ * @param {Function} args.makeCompartmentConstructor
76
+ * @param {(object) => void} args.markVirtualizedNativeFunction
77
+ * @param {Compartment} [args.parentCompartment]
77
78
  */
78
79
  export const setGlobalObjectMutableProperties = (
79
80
  globalObject,
@@ -82,6 +83,7 @@ export const setGlobalObjectMutableProperties = (
82
83
  newGlobalPropertyNames,
83
84
  makeCompartmentConstructor,
84
85
  markVirtualizedNativeFunction,
86
+ parentCompartment,
85
87
  },
86
88
  ) => {
87
89
  for (const [name, intrinsicName] of entries(universalPropertyNames)) {
@@ -115,6 +117,7 @@ export const setGlobalObjectMutableProperties = (
115
117
  makeCompartmentConstructor,
116
118
  intrinsics,
117
119
  markVirtualizedNativeFunction,
120
+ parentCompartment,
118
121
  ),
119
122
  );
120
123
 
package/src/lockdown.js CHANGED
@@ -58,7 +58,7 @@ import { tameFauxDataProperties } from './tame-faux-data-properties.js';
58
58
 
59
59
  /** @import {LockdownOptions} from '../types.js' */
60
60
 
61
- const { Fail, details: d, quote: q } = assert;
61
+ const { Fail, details: X, quote: q } = assert;
62
62
 
63
63
  /** @type {Error=} */
64
64
  let priorRepairIntrinsics;
@@ -200,7 +200,7 @@ export const repairIntrinsics = (options = {}) => {
200
200
  priorRepairIntrinsics === undefined ||
201
201
  // eslint-disable-next-line @endo/no-polymorphic-call
202
202
  assert.fail(
203
- d`Already locked down at ${priorRepairIntrinsics} (SES_ALREADY_LOCKED_DOWN)`,
203
+ X`Already locked down at ${priorRepairIntrinsics} (SES_ALREADY_LOCKED_DOWN)`,
204
204
  TypeError,
205
205
  );
206
206
  // See https://github.com/endojs/endo/blob/master/packages/ses/error-codes/SES_ALREADY_LOCKED_DOWN.md
@@ -298,7 +298,7 @@ export const repairIntrinsics = (options = {}) => {
298
298
  * @type {((error: any) => string | undefined) | undefined}
299
299
  */
300
300
  let optGetStackString;
301
- if (errorTaming !== 'unsafe') {
301
+ if (errorTaming === 'safe') {
302
302
  optGetStackString = intrinsics['%InitialGetStackString%'];
303
303
  }
304
304
  const consoleRecord = tameConsole(
@@ -323,8 +323,12 @@ export const repairIntrinsics = (options = {}) => {
323
323
  }
324
324
 
325
325
  // @ts-ignore assert is absent on globalThis type def.
326
- if (errorTaming === 'unsafe' && globalThis.assert === assert) {
327
- // If errorTaming is 'unsafe' we replace the global assert with
326
+ if (
327
+ (errorTaming === 'unsafe' || errorTaming === 'unsafe-debug') &&
328
+ globalThis.assert === assert
329
+ ) {
330
+ // If errorTaming is 'unsafe' or 'unsafe-debug' we replace the
331
+ // global assert with
328
332
  // one whose `details` template literal tag does not redact
329
333
  // unmarked substitution values. IOW, it blabs information that
330
334
  // was supposed to be secret from callers, as an aid to debugging
@@ -391,7 +395,7 @@ export const repairIntrinsics = (options = {}) => {
391
395
  priorHardenIntrinsics === undefined ||
392
396
  // eslint-disable-next-line @endo/no-polymorphic-call
393
397
  assert.fail(
394
- d`Already locked down at ${priorHardenIntrinsics} (SES_ALREADY_LOCKED_DOWN)`,
398
+ X`Already locked down at ${priorHardenIntrinsics} (SES_ALREADY_LOCKED_DOWN)`,
395
399
  TypeError,
396
400
  );
397
401
  // See https://github.com/endojs/endo/blob/master/packages/ses/error-codes/SES_ALREADY_LOCKED_DOWN.md