ses 0.18.7 → 0.18.8

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.
@@ -0,0 +1,392 @@
1
+ // Copyright (C) 2018 Agoric
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ // @ts-check
16
+
17
+ import { makeEnvironmentCaptor } from '@endo/env-options';
18
+ import {
19
+ FERAL_FUNCTION,
20
+ FERAL_EVAL,
21
+ TypeError,
22
+ arrayFilter,
23
+ globalThis,
24
+ is,
25
+ ownKeys,
26
+ stringSplit,
27
+ noEvalEvaluate,
28
+ } from './commons.js';
29
+ import { makeHardener } from './make-hardener.js';
30
+ import { makeIntrinsicsCollector } from './intrinsics.js';
31
+ import whitelistIntrinsics from './permits-intrinsics.js';
32
+ import tameFunctionConstructors from './tame-function-constructors.js';
33
+ import tameDateConstructor from './tame-date-constructor.js';
34
+ import tameMathObject from './tame-math-object.js';
35
+ import tameRegExpConstructor from './tame-regexp-constructor.js';
36
+ import enablePropertyOverrides from './enable-property-overrides.js';
37
+ import tameLocaleMethods from './tame-locale-methods.js';
38
+ import {
39
+ setGlobalObjectConstantProperties,
40
+ setGlobalObjectMutableProperties,
41
+ setGlobalObjectEvaluators,
42
+ } from './global-object.js';
43
+ import { makeSafeEvaluator } from './make-safe-evaluator.js';
44
+ import { initialGlobalPropertyNames } from './permits.js';
45
+ import { tameFunctionToString } from './tame-function-tostring.js';
46
+ import { tameDomains } from './tame-domains.js';
47
+
48
+ import { tameConsole } from './error/tame-console.js';
49
+ import tameErrorConstructor from './error/tame-error-constructor.js';
50
+ import { assert, makeAssert } from './error/assert.js';
51
+ import { getAnonymousIntrinsics } from './get-anonymous-intrinsics.js';
52
+ import { makeCompartmentConstructor } from './compartment.js';
53
+ import { tameHarden } from './tame-harden.js';
54
+ import { tameSymbolConstructor } from './tame-symbol-constructor.js';
55
+
56
+ /** @typedef {import('../types.js').LockdownOptions} LockdownOptions */
57
+
58
+ const { Fail, details: d, quote: q } = assert;
59
+
60
+ /** @type {Error=} */
61
+ let priorRepairIntrinsics;
62
+
63
+ /** @type {Error=} */
64
+ let priorHardenIntrinsics;
65
+
66
+ // Build a harden() with an empty fringe.
67
+ // Gate it on lockdown.
68
+ /**
69
+ * @template T
70
+ * @param {T} ref
71
+ * @returns {T}
72
+ */
73
+ const safeHarden = makeHardener();
74
+
75
+ /**
76
+ * @callback Transform
77
+ * @param {string} source
78
+ * @returns {string}
79
+ */
80
+
81
+ /**
82
+ * @callback CompartmentConstructor
83
+ * @param {object} endowments
84
+ * @param {object} moduleMap
85
+ * @param {object} [options]
86
+ * @param {Array<Transform>} [options.transforms]
87
+ * @param {Array<Transform>} [options.__shimTransforms__]
88
+ */
89
+
90
+ // TODO https://github.com/endojs/endo/issues/814
91
+ // Lockdown currently allows multiple calls provided that the specified options
92
+ // of every call agree. With experience, we have observed that lockdown should
93
+ // only ever need to be called once and that simplifying lockdown will improve
94
+ // the quality of audits.
95
+
96
+ const assertDirectEvalAvailable = () => {
97
+ let allowed = false;
98
+ try {
99
+ allowed = FERAL_FUNCTION(
100
+ 'eval',
101
+ 'SES_changed',
102
+ `\
103
+ eval("SES_changed = true");
104
+ return SES_changed;
105
+ `,
106
+ )(FERAL_EVAL, false);
107
+ // If we get here and SES_changed stayed false, that means the eval was sloppy
108
+ // and indirect, which generally creates a new global.
109
+ // We are going to throw an exception for failing to initialize SES, but
110
+ // good neighbors clean up.
111
+ if (!allowed) {
112
+ delete globalThis.SES_changed;
113
+ }
114
+ } catch (_error) {
115
+ // We reach here if eval is outright forbidden by a Content Security Policy.
116
+ // We allow this for SES usage that delegates the responsibility to isolate
117
+ // guest code to production code generation.
118
+ allowed = true;
119
+ }
120
+ if (!allowed) {
121
+ // See https://github.com/endojs/endo/blob/master/packages/ses/error-codes/SES_DIRECT_EVAL.md
122
+ throw TypeError(
123
+ `SES cannot initialize unless 'eval' is the original intrinsic 'eval', suitable for direct-eval (dynamically scoped eval) (SES_DIRECT_EVAL)`,
124
+ );
125
+ }
126
+ };
127
+
128
+ /**
129
+ * @param {LockdownOptions} [options]
130
+ */
131
+ export const repairIntrinsics = (options = {}) => {
132
+ // First time, absent options default to 'safe'.
133
+ // Subsequent times, absent options default to first options.
134
+ // Thus, all present options must agree with first options.
135
+ // Reconstructing `option` here also ensures that it is a well
136
+ // behaved record, with only own data properties.
137
+ //
138
+ // The `overrideTaming` is not a safety issue. Rather it is a tradeoff
139
+ // between code compatibility, which is better with the `'moderate'`
140
+ // setting, and tool compatibility, which is better with the `'min'`
141
+ // setting. See
142
+ // https://github.com/Agoric/SES-shim/blob/master/packages/ses/README.md#enabling-override-by-assignment)
143
+ // for an explanation of when to use which.
144
+ //
145
+ // The `stackFiltering` is not a safety issue. Rather it is a tradeoff
146
+ // between relevance and completeness of the stack frames shown on the
147
+ // console. Setting`stackFiltering` to `'verbose'` applies no filters, providing
148
+ // the raw stack frames that can be quite versbose. Setting
149
+ // `stackFrameFiltering` to`'concise'` limits the display to the stack frame
150
+ // information most likely to be relevant, eliminating distracting frames
151
+ // such as those from the infrastructure. However, the bug you're trying to
152
+ // track down might be in the infrastrure, in which case the `'verbose'` setting
153
+ // is useful. See
154
+ // [`stackFiltering` options](https://github.com/Agoric/SES-shim/blob/master/packages/ses/lockdown-options.md#stackfiltering-options)
155
+ // for an explanation.
156
+
157
+ const { getEnvironmentOption: getenv } = makeEnvironmentCaptor(globalThis);
158
+
159
+ const {
160
+ errorTaming = getenv('LOCKDOWN_ERROR_TAMING', 'safe'),
161
+ errorTrapping = getenv('LOCKDOWN_ERROR_TRAPPING', 'platform'),
162
+ unhandledRejectionTrapping = getenv(
163
+ 'LOCKDOWN_UNHANDLED_REJECTION_TRAPPING',
164
+ 'report',
165
+ ),
166
+ regExpTaming = getenv('LOCKDOWN_REGEXP_TAMING', 'safe'),
167
+ localeTaming = getenv('LOCKDOWN_LOCALE_TAMING', 'safe'),
168
+ consoleTaming = getenv('LOCKDOWN_CONSOLE_TAMING', 'safe'),
169
+ overrideTaming = getenv('LOCKDOWN_OVERRIDE_TAMING', 'moderate'),
170
+ stackFiltering = getenv('LOCKDOWN_STACK_FILTERING', 'concise'),
171
+ domainTaming = getenv('LOCKDOWN_DOMAIN_TAMING', 'safe'),
172
+ evalTaming = getenv('LOCKDOWN_EVAL_TAMING', 'safeEval'),
173
+ overrideDebug = arrayFilter(
174
+ stringSplit(getenv('LOCKDOWN_OVERRIDE_DEBUG', ''), ','),
175
+ /** @param {string} debugName */
176
+ debugName => debugName !== '',
177
+ ),
178
+ __hardenTaming__ = getenv('LOCKDOWN_HARDEN_TAMING', 'safe'),
179
+ dateTaming = 'safe', // deprecated
180
+ mathTaming = 'safe', // deprecated
181
+ ...extraOptions
182
+ } = options;
183
+
184
+ evalTaming === 'unsafeEval' ||
185
+ evalTaming === 'safeEval' ||
186
+ evalTaming === 'noEval' ||
187
+ Fail`lockdown(): non supported option evalTaming: ${q(evalTaming)}`;
188
+
189
+ // Assert that only supported options were passed.
190
+ // Use Reflect.ownKeys to reject symbol-named properties as well.
191
+ const extraOptionsNames = ownKeys(extraOptions);
192
+ extraOptionsNames.length === 0 ||
193
+ Fail`lockdown(): non supported option ${q(extraOptionsNames)}`;
194
+
195
+ priorRepairIntrinsics === undefined ||
196
+ // eslint-disable-next-line @endo/no-polymorphic-call
197
+ assert.fail(
198
+ d`Already locked down at ${priorRepairIntrinsics} (SES_ALREADY_LOCKED_DOWN)`,
199
+ TypeError,
200
+ );
201
+ // See https://github.com/endojs/endo/blob/master/packages/ses/error-codes/SES_ALREADY_LOCKED_DOWN.md
202
+ priorRepairIntrinsics = TypeError('Prior lockdown (SES_ALREADY_LOCKED_DOWN)');
203
+ // Tease V8 to generate the stack string and release the closures the stack
204
+ // trace retained:
205
+ priorRepairIntrinsics.stack;
206
+
207
+ assertDirectEvalAvailable();
208
+
209
+ /**
210
+ * Because of packagers and bundlers, etc, multiple invocations of lockdown
211
+ * might happen in separate instantiations of the source of this module.
212
+ * In that case, each one sees its own `firstOptions` variable, so the test
213
+ * above will not detect that lockdown has already happened. We
214
+ * unreliably test some telltale signs that lockdown has run, to avoid
215
+ * trying to lock down a locked down environment. Although the test is
216
+ * unreliable, this is consistent with the SES threat model. SES provides
217
+ * security only if it runs first in a given realm, or if everything that
218
+ * runs before it is SES-aware and cooperative. Neither SES nor anything
219
+ * can protect itself from corrupting code that runs first. For these
220
+ * purposes, code that turns a realm into something that passes these
221
+ * tests without actually locking down counts as corrupting code.
222
+ *
223
+ * The specifics of what this tests for may change over time, but it
224
+ * should be consistent with any setting of the lockdown options.
225
+ */
226
+ const seemsToBeLockedDown = () => {
227
+ return (
228
+ globalThis.Function.prototype.constructor !== globalThis.Function &&
229
+ // @ts-ignore harden is absent on globalThis type def.
230
+ typeof globalThis.harden === 'function' &&
231
+ // @ts-ignore lockdown is absent on globalThis type def.
232
+ typeof globalThis.lockdown === 'function' &&
233
+ globalThis.Date.prototype.constructor !== globalThis.Date &&
234
+ typeof globalThis.Date.now === 'function' &&
235
+ // @ts-ignore does not recognize that Date constructor is a special
236
+ // Function.
237
+ // eslint-disable-next-line @endo/no-polymorphic-call
238
+ is(globalThis.Date.prototype.constructor.now(), NaN)
239
+ );
240
+ };
241
+
242
+ if (seemsToBeLockedDown()) {
243
+ // See https://github.com/endojs/endo/blob/master/packages/ses/error-codes/SES_MULTIPLE_INSTANCES.md
244
+ throw TypeError(
245
+ `Already locked down but not by this SES instance (SES_MULTIPLE_INSTANCES)`,
246
+ );
247
+ }
248
+
249
+ /**
250
+ * 1. TAME powers & gather intrinsics first.
251
+ */
252
+
253
+ tameDomains(domainTaming);
254
+
255
+ // Replace Function.prototype.toString with one that recognizes
256
+ // shimmed functions as honorary native functions.
257
+ const markVirtualizedNativeFunction = tameFunctionToString();
258
+
259
+ const { addIntrinsics, completePrototypes, finalIntrinsics } =
260
+ makeIntrinsicsCollector();
261
+
262
+ const tamedHarden = tameHarden(safeHarden, __hardenTaming__);
263
+ addIntrinsics({ harden: tamedHarden });
264
+
265
+ addIntrinsics(tameFunctionConstructors());
266
+
267
+ addIntrinsics(tameDateConstructor(dateTaming));
268
+ addIntrinsics(tameErrorConstructor(errorTaming, stackFiltering));
269
+ addIntrinsics(tameMathObject(mathTaming));
270
+ addIntrinsics(tameRegExpConstructor(regExpTaming));
271
+ addIntrinsics(tameSymbolConstructor());
272
+
273
+ addIntrinsics(getAnonymousIntrinsics());
274
+
275
+ completePrototypes();
276
+
277
+ const intrinsics = finalIntrinsics();
278
+
279
+ /**
280
+ * Wrap console unless suppressed.
281
+ * At the moment, the console is considered a host power in the start
282
+ * compartment, and not a primordial. Hence it is absent from the whilelist
283
+ * and bypasses the intrinsicsCollector.
284
+ *
285
+ * @type {((error: any) => string | undefined) | undefined}
286
+ */
287
+ let optGetStackString;
288
+ if (errorTaming !== 'unsafe') {
289
+ optGetStackString = intrinsics['%InitialGetStackString%'];
290
+ }
291
+ const consoleRecord = tameConsole(
292
+ consoleTaming,
293
+ errorTrapping,
294
+ unhandledRejectionTrapping,
295
+ optGetStackString,
296
+ );
297
+ globalThis.console = /** @type {Console} */ (consoleRecord.console);
298
+
299
+ // @ts-ignore assert is absent on globalThis type def.
300
+ if (errorTaming === 'unsafe' && globalThis.assert === assert) {
301
+ // If errorTaming is 'unsafe' we replace the global assert with
302
+ // one whose `details` template literal tag does not redact
303
+ // unmarked substitution values. IOW, it blabs information that
304
+ // was supposed to be secret from callers, as an aid to debugging
305
+ // at a further cost in safety.
306
+ // @ts-ignore assert is absent on globalThis type def.
307
+ globalThis.assert = makeAssert(undefined, true);
308
+ }
309
+
310
+ // Replace *Locale* methods with their non-locale equivalents
311
+ tameLocaleMethods(intrinsics, localeTaming);
312
+
313
+ /**
314
+ * 2. WHITELIST to standardize the environment.
315
+ */
316
+
317
+ // Remove non-standard properties.
318
+ // All remaining function encountered during whitelisting are
319
+ // branded as honorary native functions.
320
+ whitelistIntrinsics(intrinsics, markVirtualizedNativeFunction);
321
+
322
+ // Initialize the powerful initial global, i.e., the global of the
323
+ // start compartment, from the intrinsics.
324
+
325
+ setGlobalObjectConstantProperties(globalThis);
326
+
327
+ setGlobalObjectMutableProperties(globalThis, {
328
+ intrinsics,
329
+ newGlobalPropertyNames: initialGlobalPropertyNames,
330
+ makeCompartmentConstructor,
331
+ markVirtualizedNativeFunction,
332
+ });
333
+
334
+ if (evalTaming === 'noEval') {
335
+ setGlobalObjectEvaluators(
336
+ globalThis,
337
+ noEvalEvaluate,
338
+ markVirtualizedNativeFunction,
339
+ );
340
+ } else if (evalTaming === 'safeEval') {
341
+ const { safeEvaluate } = makeSafeEvaluator({ globalObject: globalThis });
342
+ setGlobalObjectEvaluators(
343
+ globalThis,
344
+ safeEvaluate,
345
+ markVirtualizedNativeFunction,
346
+ );
347
+ } else if (evalTaming === 'unsafeEval') {
348
+ // Leave eval function and Function constructor of the initial compartment in-tact.
349
+ // Other compartments will not have access to these evaluators unless a guest program
350
+ // escapes containment.
351
+ }
352
+
353
+ /**
354
+ * 3. HARDEN to share the intrinsics.
355
+ *
356
+ * We define hardenIntrinsics here so that options are in scope, but return
357
+ * it to the caller because we intend to eventually allow vetted shims to run
358
+ * between repairs and the hardening of intrinsics and so we can benchmark
359
+ * repair separately from hardening.
360
+ */
361
+
362
+ const hardenIntrinsics = () => {
363
+ priorHardenIntrinsics === undefined ||
364
+ // eslint-disable-next-line @endo/no-polymorphic-call
365
+ assert.fail(
366
+ d`Already locked down at ${priorHardenIntrinsics} (SES_ALREADY_LOCKED_DOWN)`,
367
+ TypeError,
368
+ );
369
+ // See https://github.com/endojs/endo/blob/master/packages/ses/error-codes/SES_ALREADY_LOCKED_DOWN.md
370
+ priorHardenIntrinsics = TypeError(
371
+ 'Prior lockdown (SES_ALREADY_LOCKED_DOWN)',
372
+ );
373
+ // Tease V8 to generate the stack string and release the closures the stack
374
+ // trace retained:
375
+ priorHardenIntrinsics.stack;
376
+
377
+ // Circumvent the override mistake.
378
+ // TODO consider moving this to the end of the repair phase, and
379
+ // therefore before vetted shims rather than afterwards. It is not
380
+ // clear yet which is better.
381
+ // @ts-ignore enablePropertyOverrides does its own input validation
382
+ enablePropertyOverrides(intrinsics, overrideTaming, overrideDebug);
383
+
384
+ // Finally register and optionally freeze all the intrinsics. This
385
+ // must be the operation that modifies the intrinsics.
386
+ tamedHarden(intrinsics);
387
+
388
+ return tamedHarden;
389
+ };
390
+
391
+ return hardenIntrinsics;
392
+ };
@@ -19,7 +19,7 @@ const { Fail } = assert;
19
19
  * @param {object} options
20
20
  * @param {object} options.globalObject
21
21
  * @param {object} [options.moduleLexicals]
22
- * @param {Array<import('./lockdown-shim.js').Transform>} [options.globalTransforms]
22
+ * @param {Array<import('./lockdown.js').Transform>} [options.globalTransforms]
23
23
  * @param {boolean} [options.sloppyGlobalsMode]
24
24
  */
25
25
  export const makeSafeEvaluator = ({
@@ -54,7 +54,7 @@ export const makeSafeEvaluator = ({
54
54
  /**
55
55
  * @param {string} source
56
56
  * @param {object} [options]
57
- * @param {Array<import('./lockdown-shim.js').Transform>} [options.localTransforms]
57
+ * @param {Array<import('./lockdown.js').Transform>} [options.localTransforms]
58
58
  */
59
59
  const safeEvaluate = (source, options) => {
60
60
  const { localTransforms = [] } = options || {};
package/src/permits.js CHANGED
@@ -326,7 +326,7 @@ function TypedArrayPrototype(constructor) {
326
326
  }
327
327
 
328
328
  // Without Math.random
329
- const SharedMath = {
329
+ const CommonMath = {
330
330
  E: 'number',
331
331
  LN10: 'number',
332
332
  LN2: 'number',
@@ -442,7 +442,7 @@ export const permitted = {
442
442
  setPrototypeOf: fn,
443
443
  values: fn,
444
444
  // https://github.com/tc39/proposal-array-grouping
445
- groupBy: 'boolean',
445
+ groupBy: fn,
446
446
  },
447
447
 
448
448
  '%ObjectPrototype%': {
@@ -649,12 +649,16 @@ export const permitted = {
649
649
  },
650
650
 
651
651
  '%InitialMath%': {
652
- ...SharedMath,
653
- // random is standard but omitted from SharedMath
652
+ ...CommonMath,
653
+ // `%InitialMath%.random()` has the standard unsafe behavior
654
654
  random: fn,
655
655
  },
656
656
 
657
- '%SharedMath%': SharedMath,
657
+ '%SharedMath%': {
658
+ ...CommonMath,
659
+ // `%SharedMath%.random()` is tamed to always throw
660
+ random: fn,
661
+ },
658
662
 
659
663
  '%InitialDate%': {
660
664
  // Properties of the Date Constructor
@@ -668,6 +672,7 @@ export const permitted = {
668
672
  '%SharedDate%': {
669
673
  // Properties of the Date Constructor
670
674
  '[[Proto]]': '%FunctionPrototype%',
675
+ // `%SharedDate%.now()` is tamed to always throw
671
676
  now: fn,
672
677
  parse: fn,
673
678
  prototype: '%DatePrototype%',
@@ -1069,6 +1074,8 @@ export const permitted = {
1069
1074
  '[[Proto]]': '%FunctionPrototype%',
1070
1075
  '@@species': getter,
1071
1076
  prototype: '%MapPrototype%',
1077
+ // https://github.com/tc39/proposal-array-grouping
1078
+ groupBy: fn,
1072
1079
  },
1073
1080
 
1074
1081
  '%MapPrototype%': {
@@ -17,26 +17,38 @@ export default function tameDateConstructor(dateTaming = 'safe') {
17
17
 
18
18
  // Use concise methods to obtain named functions without constructors.
19
19
  const tamedMethods = {
20
+ /**
21
+ * `%SharedDate%.now()` throw a `TypeError` starting with "secure mode".
22
+ * See https://github.com/endojs/endo/issues/910#issuecomment-1581855420
23
+ */
20
24
  now() {
21
- return NaN;
25
+ throw TypeError('secure mode Calling %SharedDate%.now() throws');
22
26
  },
23
27
  };
24
28
 
25
- // Tame the Date constructor.
26
- // Common behavior
27
- // * new Date(x) coerces x into a number and then returns a Date
28
- // for that number of millis since the epoch
29
- // * new Date(NaN) returns a Date object which stringifies to
30
- // 'Invalid Date'
31
- // * new Date(undefined) returns a Date object which stringifies to
32
- // 'Invalid Date'
33
- // OriginalDate (normal standard) behavior
34
- // * Date(anything) gives a string with the current time
35
- // * new Date() returns the current time, as a Date object
36
- // SharedDate behavior
37
- // * Date(anything) returned 'Invalid Date'
38
- // * new Date() returns a Date object which stringifies to
39
- // 'Invalid Date'
29
+ /**
30
+ * Tame the Date constructor.
31
+ * See https://github.com/endojs/endo/issues/910#issuecomment-1581855420
32
+ *
33
+ * Common behavior
34
+ * * `new Date(x)` coerces x into a number and then returns a Date
35
+ * for that number of millis since the epoch
36
+ * * `new Date(NaN)` returns a Date object which stringifies to
37
+ * 'Invalid Date'
38
+ * * `new Date(undefined)` returns a Date object which stringifies to
39
+ * 'Invalid Date'
40
+ *
41
+ * OriginalDate (normal standard) behavior preserved by
42
+ * `%InitialDate%`.
43
+ * * `Date(anything)` gives a string with the current time
44
+ * * `new Date()` returns the current time, as a Date object
45
+ *
46
+ * `%SharedDate%` behavior
47
+ * * `Date(anything)` throws a TypeError starting with "secure mode"
48
+ * * `new Date()` throws a TypeError starting with "secure mode"
49
+ *
50
+ * @param {{powers?: string}} [opts]
51
+ */
40
52
  const makeDateConstructor = ({ powers = 'none' } = {}) => {
41
53
  let ResultDate;
42
54
  if (powers === 'original') {
@@ -51,10 +63,14 @@ export default function tameDateConstructor(dateTaming = 'safe') {
51
63
  // eslint-disable-next-line no-shadow
52
64
  ResultDate = function Date(...rest) {
53
65
  if (new.target === undefined) {
54
- return 'Invalid Date';
66
+ throw TypeError(
67
+ 'secure mode Calling %SharedDate% constructor as a function throws',
68
+ );
55
69
  }
56
70
  if (rest.length === 0) {
57
- rest = [NaN];
71
+ throw TypeError(
72
+ 'secure mode Calling new %SharedDate%() with no arguments throws',
73
+ );
58
74
  }
59
75
  return construct(OriginalDate, rest, new.target);
60
76
  };
@@ -69,13 +85,13 @@ export default function tameDateConstructor(dateTaming = 'safe') {
69
85
  configurable: false,
70
86
  },
71
87
  parse: {
72
- value: Date.parse,
88
+ value: OriginalDate.parse,
73
89
  writable: true,
74
90
  enumerable: false,
75
91
  configurable: true,
76
92
  },
77
93
  UTC: {
78
- value: Date.UTC,
94
+ value: OriginalDate.UTC,
79
95
  writable: true,
80
96
  enumerable: false,
81
97
  configurable: true,
@@ -88,7 +104,7 @@ export default function tameDateConstructor(dateTaming = 'safe') {
88
104
 
89
105
  defineProperties(InitialDate, {
90
106
  now: {
91
- value: Date.now,
107
+ value: OriginalDate.now,
92
108
  writable: true,
93
109
  enumerable: false,
94
110
  configurable: true,
@@ -16,7 +16,26 @@ export default function tameMathObject(mathTaming = 'safe') {
16
16
  const { random: _, ...otherDescriptors } =
17
17
  getOwnPropertyDescriptors(originalMath);
18
18
 
19
- const sharedMath = create(objectPrototype, otherDescriptors);
19
+ // Use concise methods to obtain named functions without constructors.
20
+ const tamedMethods = {
21
+ /**
22
+ * `%SharedMath%.random()` throws a TypeError starting with "secure mode".
23
+ * See https://github.com/endojs/endo/issues/910#issuecomment-1581855420
24
+ */
25
+ random() {
26
+ throw TypeError('secure mode %SharedMath%.random() throws');
27
+ },
28
+ };
29
+
30
+ const sharedMath = create(objectPrototype, {
31
+ ...otherDescriptors,
32
+ random: {
33
+ value: tamedMethods.random,
34
+ writable: true,
35
+ enumerable: false,
36
+ configurable: true,
37
+ },
38
+ });
20
39
 
21
40
  return {
22
41
  '%InitialMath%': initialMath,
package/types.d.ts CHANGED
@@ -19,7 +19,7 @@
19
19
  // version of harden.
20
20
  export type Harden = <T>(value: T) => T; // not Hardened<T>;
21
21
 
22
- export interface LockdownOptions {
22
+ export interface RepairOptions {
23
23
  regExpTaming?: 'safe' | 'unsafe';
24
24
  localeTaming?: 'safe' | 'unsafe';
25
25
  consoleTaming?: 'safe' | 'unsafe';
@@ -36,6 +36,11 @@ export interface LockdownOptions {
36
36
  __hardenTaming__?: 'safe' | 'unsafe';
37
37
  }
38
38
 
39
+ // Deprecated in favor of the more specific RepairOptions
40
+ export type LockdownOptions = RepairOptions;
41
+
42
+ export type RepairIntrinsics = (options?: LockdownOptions) => void;
43
+ export type HardenIntrinsics = () => void;
39
44
  export type Lockdown = (options?: LockdownOptions) => void;
40
45
 
41
46
  export type __LiveExportMap__ = Record<string, [string, boolean]>;
@@ -172,19 +177,13 @@ export type Raise = (reason: Error) => void;
172
177
  // eslint-disable-next-line no-use-before-define
173
178
  export type MakeAssert = (raise?: Raise, unredacted?: boolean) => Assert;
174
179
 
175
- export interface Assert {
180
+ export interface AssertionFunctions {
176
181
  (
177
182
  value: any,
178
183
  details?: Details,
179
184
  errorConstructor?: ErrorConstructor,
180
185
  ): asserts value;
181
186
  typeof: AssertTypeof;
182
- error(
183
- details?: Details,
184
- errorConstructor?: ErrorConstructor,
185
- options?: AssertMakeErrorOptions,
186
- ): Error;
187
- fail(details?: Details, errorConstructor?: ErrorConstructor): never;
188
187
  equal(
189
188
  left: any,
190
189
  right: any,
@@ -192,6 +191,15 @@ export interface Assert {
192
191
  errorConstructor?: ErrorConstructor,
193
192
  ): void;
194
193
  string(specimen: any, details?: Details): asserts specimen is string;
194
+ fail(details?: Details, errorConstructor?: ErrorConstructor): never;
195
+ }
196
+
197
+ export interface AssertionUtilities {
198
+ error(
199
+ details?: Details,
200
+ errorConstructor?: ErrorConstructor,
201
+ options?: AssertMakeErrorOptions,
202
+ ): Error;
195
203
  note(error: Error, details: Details): void;
196
204
  details(
197
205
  template: TemplateStringsArray | string[],
@@ -200,9 +208,16 @@ export interface Assert {
200
208
  Fail(template: TemplateStringsArray | string[], ...args: any): never;
201
209
  quote(payload: any, spaces?: string | number): ToStringable;
202
210
  bare(payload: any, spaces?: string | number): ToStringable;
211
+ }
212
+
213
+ export interface DeprecatedAssertionUtilities {
203
214
  makeAssert: MakeAssert;
204
215
  }
205
216
 
217
+ export type Assert = AssertionFunctions &
218
+ AssertionUtilities &
219
+ DeprecatedAssertionUtilities;
220
+
206
221
  interface CompartmentEvaluateOptions {
207
222
  sloppyGlobalsMode?: boolean;
208
223
  __moduleShimLexicals__?: Object;
@@ -214,6 +229,8 @@ interface CompartmentEvaluateOptions {
214
229
  declare global {
215
230
  var harden: Harden;
216
231
 
232
+ var repairIntrinsics: RepairIntrinsics;
233
+ var hardenIntrinsics: HardenIntrinsics;
217
234
  var lockdown: Lockdown;
218
235
 
219
236
  var assert: Assert;