ses 1.4.0 → 1.5.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.
@@ -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
@@ -7,8 +7,10 @@ import './assert-sloppy-mode.js';
7
7
  import { globalThis } from './commons.js';
8
8
  import { repairIntrinsics } from './lockdown.js';
9
9
 
10
+ /** @import {LockdownOptions} from '../types.js' */
11
+
10
12
  /**
11
- * @param {import('./lockdown.js').LockdownOptions} options
13
+ * @param {LockdownOptions} options
12
14
  */
13
15
  globalThis.lockdown = options => {
14
16
  const hardenIntrinsics = repairIntrinsics(options);
@@ -16,7 +18,7 @@ globalThis.lockdown = options => {
16
18
  };
17
19
 
18
20
  /**
19
- * @param {import('./lockdown.js').LockdownOptions} options
21
+ * @param {LockdownOptions} options
20
22
  */
21
23
  globalThis.repairIntrinsics = options => {
22
24
  const hardenIntrinsics = repairIntrinsics(options);
package/src/lockdown.js CHANGED
@@ -56,7 +56,7 @@ import { tameHarden } from './tame-harden.js';
56
56
  import { tameSymbolConstructor } from './tame-symbol-constructor.js';
57
57
  import { tameFauxDataProperties } from './tame-faux-data-properties.js';
58
58
 
59
- /** @typedef {import('../types.js').LockdownOptions} LockdownOptions */
59
+ /** @import {LockdownOptions} from '../types.js' */
60
60
 
61
61
  const { Fail, details: d, quote: q } = assert;
62
62
 
@@ -25,7 +25,6 @@ import {
25
25
  Set,
26
26
  String,
27
27
  TypeError,
28
- WeakMap,
29
28
  WeakSet,
30
29
  globalThis,
31
30
  apply,
@@ -45,15 +44,16 @@ import {
45
44
  setHas,
46
45
  toStringTagSymbol,
47
46
  typedArrayPrototype,
48
- weakmapGet,
49
- weakmapSet,
50
47
  weaksetAdd,
51
48
  weaksetHas,
49
+ FERAL_STACK_GETTER,
50
+ FERAL_STACK_SETTER,
51
+ isError,
52
52
  } from './commons.js';
53
53
  import { assert } from './error/assert.js';
54
54
 
55
55
  /**
56
- * @typedef {import('../types.js').Harden} Harden
56
+ * @import {Harden} from '../types.js'
57
57
  */
58
58
 
59
59
  // Obtain the string tag accessor of of TypedArray so we can indirectly use the
@@ -144,15 +144,13 @@ export const makeHardener = () => {
144
144
  */
145
145
  harden(root) {
146
146
  const toFreeze = new Set();
147
- const paths = new WeakMap();
148
147
 
149
148
  // If val is something we should be freezing but aren't yet,
150
149
  // add it to toFreeze.
151
150
  /**
152
151
  * @param {any} val
153
- * @param {string} [path]
154
152
  */
155
- function enqueue(val, path = undefined) {
153
+ function enqueue(val) {
156
154
  if (!isObject(val)) {
157
155
  // ignore primitives
158
156
  return;
@@ -168,13 +166,12 @@ export const makeHardener = () => {
168
166
  }
169
167
  // console.warn(`adding ${val} to toFreeze`, val);
170
168
  setAdd(toFreeze, val);
171
- weakmapSet(paths, val, path);
172
169
  }
173
170
 
174
171
  /**
175
172
  * @param {any} obj
176
173
  */
177
- function freezeAndTraverse(obj) {
174
+ const baseFreezeAndTraverse = obj => {
178
175
  // Now freeze the object to ensure reactive
179
176
  // objects such as proxies won't add properties
180
177
  // during traversal, before they get frozen.
@@ -193,13 +190,11 @@ export const makeHardener = () => {
193
190
 
194
191
  // get stable/immutable outbound links before a Proxy has a chance to do
195
192
  // something sneaky.
196
- const path = weakmapGet(paths, obj) || 'unknown';
197
193
  const descs = getOwnPropertyDescriptors(obj);
198
194
  const proto = getPrototypeOf(obj);
199
- enqueue(proto, `${path}.__proto__`);
195
+ enqueue(proto);
200
196
 
201
197
  arrayForEach(ownKeys(descs), (/** @type {string | symbol} */ name) => {
202
- const pathname = `${path}.${String(name)}`;
203
198
  // The 'name' may be a symbol, and TypeScript doesn't like us to
204
199
  // index arbitrary symbols on objects, so we pretend they're just
205
200
  // strings.
@@ -212,27 +207,60 @@ export const makeHardener = () => {
212
207
  // whether 'value' is present or not, which tells us for sure that
213
208
  // this is a data property.
214
209
  if (objectHasOwnProperty(desc, 'value')) {
215
- enqueue(desc.value, `${pathname}`);
210
+ enqueue(desc.value);
216
211
  } else {
217
- enqueue(desc.get, `${pathname}(get)`);
218
- enqueue(desc.set, `${pathname}(set)`);
212
+ enqueue(desc.get);
213
+ enqueue(desc.set);
219
214
  }
220
215
  });
221
- }
216
+ };
217
+
218
+ const freezeAndTraverse =
219
+ FERAL_STACK_GETTER === undefined && FERAL_STACK_SETTER === undefined
220
+ ? // On platforms without v8's error own stack accessor problem,
221
+ // don't pay for any extra overhead.
222
+ baseFreezeAndTraverse
223
+ : obj => {
224
+ if (isError(obj)) {
225
+ // Only pay the overhead if it first passes this cheap isError
226
+ // check. Otherwise, it will be unrepaired, but won't be judged
227
+ // to be a passable error anyway, so will not be unsafe.
228
+ const stackDesc = getOwnPropertyDescriptor(obj, 'stack');
229
+ if (
230
+ stackDesc &&
231
+ stackDesc.get === FERAL_STACK_GETTER &&
232
+ stackDesc.configurable
233
+ ) {
234
+ // Can only repair if it is configurable. Otherwise, leave
235
+ // unrepaired, in which case it will not be judged passable,
236
+ // avoiding a safety problem.
237
+ defineProperty(obj, 'stack', {
238
+ // NOTE: Calls getter during harden, which seems dangerous.
239
+ // But we're only calling the problematic getter whose
240
+ // hazards we think we understand.
241
+ // @ts-expect-error TS should know FERAL_STACK_GETTER
242
+ // cannot be `undefined` here.
243
+ // See https://github.com/endojs/endo/pull/2232#discussion_r1575179471
244
+ value: apply(FERAL_STACK_GETTER, obj, []),
245
+ });
246
+ }
247
+ }
248
+ return baseFreezeAndTraverse(obj);
249
+ };
222
250
 
223
- function dequeue() {
251
+ const dequeue = () => {
224
252
  // New values added before forEach() has finished will be visited.
225
253
  setForEach(toFreeze, freezeAndTraverse);
226
- }
254
+ };
227
255
 
228
256
  /** @param {any} value */
229
- function markHardened(value) {
257
+ const markHardened = value => {
230
258
  weaksetAdd(hardened, value);
231
- }
259
+ };
232
260
 
233
- function commit() {
261
+ const commit = () => {
234
262
  setForEach(toFreeze, markHardened);
235
- }
263
+ };
236
264
 
237
265
  enqueue(root);
238
266
  dequeue();