ses 0.14.3 → 0.15.2

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/index.d.ts CHANGED
@@ -18,7 +18,7 @@ export interface LockdownOptions {
18
18
  regExpTaming?: 'safe' | 'unsafe';
19
19
  localeTaming?: 'safe' | 'unsafe';
20
20
  consoleTaming?: 'safe' | 'unsafe';
21
- errorTrapping?: 'platform' | 'exit' | 'abort' | 'report';
21
+ errorTrapping?: 'platform' | 'exit' | 'abort' | 'report' | 'none';
22
22
  errorTaming?: 'safe' | 'unsafe';
23
23
  dateTaming?: 'safe' | 'unsafe'; // deprecated
24
24
  mathTaming?: 'safe' | 'unsafe'; // deprecated
package/index.js CHANGED
@@ -16,12 +16,8 @@ import { globalThis, TypeError, assign } from './src/commons.js';
16
16
 
17
17
  import { tameFunctionToString } from './src/tame-function-tostring.js';
18
18
  import { getGlobalIntrinsics } from './src/intrinsics.js';
19
- import { getAnonymousIntrinsics } from './src/get-anonymous-intrinsics.js';
20
- import { makeLockdown } from './src/lockdown-shim.js';
21
- import {
22
- makeCompartmentConstructor,
23
- CompartmentPrototype,
24
- } from './src/compartment-shim.js';
19
+ import { lockdown } from './src/lockdown-shim.js';
20
+ import { makeCompartmentConstructor } from './src/compartment-shim.js';
25
21
  import { assert } from './src/error/assert.js';
26
22
 
27
23
  /** getThis returns globalThis in sloppy mode or undefined in strict mode. */
@@ -42,11 +38,7 @@ const Compartment = makeCompartmentConstructor(
42
38
  );
43
39
 
44
40
  assign(globalThis, {
45
- lockdown: makeLockdown(
46
- makeCompartmentConstructor,
47
- CompartmentPrototype,
48
- getAnonymousIntrinsics,
49
- ),
41
+ lockdown,
50
42
  Compartment,
51
43
  assert,
52
44
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ses",
3
- "version": "0.14.3",
3
+ "version": "0.15.2",
4
4
  "description": "Hardened JavaScript for Fearless Cooperation",
5
5
  "keywords": [
6
6
  "lockdown",
@@ -50,8 +50,8 @@
50
50
  "cover": "c8 ava",
51
51
  "demo": "http-server -o /demos",
52
52
  "lint": "yarn lint:types && yarn lint:js",
53
- "lint-fix": "eslint --fix '**/*.js'",
54
- "lint:js": "eslint '**/*.js'",
53
+ "lint-fix": "eslint --fix .",
54
+ "lint:js": "eslint .",
55
55
  "lint:types": "tsc --build jsconfig.json",
56
56
  "prepublish": "yarn run clean && yarn build",
57
57
  "qt": "ava",
@@ -59,10 +59,10 @@
59
59
  "test:platform-compatability": "node test/package/test.cjs"
60
60
  },
61
61
  "devDependencies": {
62
- "@endo/compartment-mapper": "^0.5.3",
63
- "@endo/eslint-config": "^0.3.15",
64
- "@endo/static-module-record": "^0.6.3",
65
- "@endo/test262-runner": "^0.1.9",
62
+ "@endo/compartment-mapper": "^0.6.0",
63
+ "@endo/eslint-config": "^0.3.19",
64
+ "@endo/static-module-record": "^0.6.7",
65
+ "@endo/test262-runner": "^0.1.13",
66
66
  "ava": "^3.12.1",
67
67
  "babel-eslint": "^10.0.3",
68
68
  "c8": "^7.7.3",
@@ -179,5 +179,5 @@
179
179
  ],
180
180
  "timeout": "2m"
181
181
  },
182
- "gitHead": "619005298bde41e3013b8d704db543c0888a239f"
182
+ "gitHead": "26d991afb01cf824827db0c958c50970e038112f"
183
183
  }
@@ -11,7 +11,59 @@ import {
11
11
  evadeImportExpressionTest,
12
12
  rejectSomeDirectEvalExpressions,
13
13
  } from './transforms.js';
14
- import { performEval } from './evaluate.js';
14
+ import { makeSafeEvaluator } from './make-safe-evaluator.js';
15
+
16
+ export const provideCompartmentEvaluator = (compartmentFields, options) => {
17
+ const {
18
+ sloppyGlobalsMode = false,
19
+ __moduleShimLexicals__ = undefined,
20
+ } = options;
21
+
22
+ let safeEvaluate;
23
+
24
+ if (__moduleShimLexicals__ === undefined && !sloppyGlobalsMode) {
25
+ ({ safeEvaluate } = compartmentFields);
26
+ } else {
27
+ // The scope proxy or global lexicals are different from the
28
+ // shared evaluator so we need to build a new one
29
+
30
+ let { globalTransforms } = compartmentFields;
31
+ const {
32
+ globalObject,
33
+ globalLexicals,
34
+ knownScopeProxies,
35
+ } = compartmentFields;
36
+
37
+ let localObject = globalLexicals;
38
+ if (__moduleShimLexicals__ !== undefined) {
39
+ // When using `evaluate` for ESM modules, as should only occur from the
40
+ // module-shim's module-instance.js, we do not reveal the SES-shim's
41
+ // module-to-program translation, as this is not standardizable behavior.
42
+ // However, the `localTransforms` will come from the `__shimTransforms__`
43
+ // Compartment option in this case, which is a non-standardizable escape
44
+ // hatch so programs designed specifically for the SES-shim
45
+ // implementation may opt-in to use the same transforms for `evaluate`
46
+ // and `import`, at the expense of being tightly coupled to SES-shim.
47
+ globalTransforms = undefined;
48
+
49
+ localObject = create(null, getOwnPropertyDescriptors(globalLexicals));
50
+ defineProperties(
51
+ localObject,
52
+ getOwnPropertyDescriptors(__moduleShimLexicals__),
53
+ );
54
+ }
55
+
56
+ ({ safeEvaluate } = makeSafeEvaluator({
57
+ globalObject,
58
+ localObject,
59
+ globalTransforms,
60
+ sloppyGlobalsMode,
61
+ knownScopeProxies,
62
+ }));
63
+ }
64
+
65
+ return { safeEvaluate };
66
+ };
15
67
 
16
68
  export const compartmentEvaluate = (compartmentFields, source, options) => {
17
69
  // Perform this check first to avoid unecessary sanitizing.
@@ -24,8 +76,6 @@ export const compartmentEvaluate = (compartmentFields, source, options) => {
24
76
  // Extract options, and shallow-clone transforms.
25
77
  const {
26
78
  transforms = [],
27
- sloppyGlobalsMode = false,
28
- __moduleShimLexicals__ = undefined,
29
79
  __evadeHtmlCommentTest__ = false,
30
80
  __evadeImportExpressionTest__ = false,
31
81
  __rejectSomeDirectEvalExpressions__ = true, // Note default on
@@ -41,32 +91,12 @@ export const compartmentEvaluate = (compartmentFields, source, options) => {
41
91
  arrayPush(localTransforms, rejectSomeDirectEvalExpressions);
42
92
  }
43
93
 
44
- let { globalTransforms } = compartmentFields;
45
- const { globalObject, globalLexicals, knownScopeProxies } = compartmentFields;
46
-
47
- let localObject = globalLexicals;
48
- if (__moduleShimLexicals__ !== undefined) {
49
- // When using `evaluate` for ESM modules, as should only occur from the
50
- // module-shim's module-instance.js, we do not reveal the SES-shim's
51
- // module-to-program translation, as this is not standardizable behavior.
52
- // However, the `localTransforms` will come from the `__shimTransforms__`
53
- // Compartment option in this case, which is a non-standardizable escape
54
- // hatch so programs designed specifically for the SES-shim
55
- // implementation may opt-in to use the same transforms for `evaluate`
56
- // and `import`, at the expense of being tightly coupled to SES-shim.
57
- globalTransforms = undefined;
58
-
59
- localObject = create(null, getOwnPropertyDescriptors(globalLexicals));
60
- defineProperties(
61
- localObject,
62
- getOwnPropertyDescriptors(__moduleShimLexicals__),
63
- );
64
- }
94
+ const { safeEvaluate } = provideCompartmentEvaluator(
95
+ compartmentFields,
96
+ options,
97
+ );
65
98
 
66
- return performEval(source, globalObject, localObject, {
67
- globalTransforms,
99
+ return safeEvaluate(source, {
68
100
  localTransforms,
69
- sloppyGlobalsMode,
70
- knownScopeProxies,
71
101
  });
72
102
  };
@@ -20,7 +20,10 @@ import {
20
20
  weakmapSet,
21
21
  weaksetHas,
22
22
  } from './commons.js';
23
- import { initGlobalObject } from './global-object.js';
23
+ import {
24
+ setGlobalObjectConstantProperties,
25
+ setGlobalObjectMutableProperties,
26
+ } from './global-object.js';
24
27
  import { isValidIdentifierName } from './scope-constants.js';
25
28
  import { sharedGlobalPropertyNames } from './whitelist.js';
26
29
  import { load } from './module-load.js';
@@ -28,6 +31,7 @@ import { link } from './module-link.js';
28
31
  import { getDeferredExports } from './module-proxy.js';
29
32
  import { assert } from './error/assert.js';
30
33
  import { compartmentEvaluate } from './compartment-evaluate.js';
34
+ import { makeSafeEvaluator } from './make-safe-evaluator.js';
31
35
 
32
36
  const { quote: q } = assert;
33
37
 
@@ -206,7 +210,7 @@ export const makeCompartmentConstructor = (
206
210
  name = '<unknown>',
207
211
  transforms = [],
208
212
  __shimTransforms__ = [],
209
- globalLexicals = {},
213
+ globalLexicals: globalLexicalsOption = {},
210
214
  resolveHook,
211
215
  importHook,
212
216
  moduleMapHook,
@@ -244,23 +248,8 @@ export const makeCompartmentConstructor = (
244
248
  }
245
249
  }
246
250
 
247
- const globalObject = {};
248
- initGlobalObject(
249
- globalObject,
250
- intrinsics,
251
- sharedGlobalPropertyNames,
252
- targetMakeCompartmentConstructor,
253
- this.constructor.prototype,
254
- {
255
- globalTransforms,
256
- markVirtualizedNativeFunction,
257
- },
258
- );
259
-
260
- assign(globalObject, endowments);
261
-
262
251
  const invalidNames = arrayFilter(
263
- getOwnPropertyNames(globalLexicals),
252
+ getOwnPropertyNames(globalLexicalsOption),
264
253
  identifier => !isValidIdentifierName(identifier),
265
254
  );
266
255
  if (invalidNames.length) {
@@ -271,23 +260,51 @@ export const makeCompartmentConstructor = (
271
260
  )}; these names would not be lexically mentionable`,
272
261
  );
273
262
  }
263
+ // The caller continues to own the globalLexicals object they passed to
264
+ // the compartment constructor, but the compartment only respects the
265
+ // original values and they are constants in the scope of evaluated
266
+ // programs and executed modules.
267
+ // This shallow copy captures only the values of enumerable own
268
+ // properties, erasing accessors.
269
+ // The snapshot is frozen to ensure that the properties are immutable
270
+ // when transferred-by-property-descriptor onto local scope objects.
271
+ const globalLexicals = freeze({ ...globalLexicalsOption });
272
+
273
+ const globalObject = {};
274
+
275
+ // We must initialize all constant properties first because
276
+ // `makeSafeEvaluator` may use them to create optimized bindings
277
+ // in the evaluator.
278
+ // TODO: consider merging into a single initialization if internal
279
+ // evaluator is no longer eagerly created
280
+ setGlobalObjectConstantProperties(globalObject);
274
281
 
275
282
  const knownScopeProxies = new WeakSet();
283
+ const { safeEvaluate } = makeSafeEvaluator({
284
+ globalObject,
285
+ localObject: globalLexicals,
286
+ globalTransforms,
287
+ sloppyGlobalsMode: false,
288
+ knownScopeProxies,
289
+ });
290
+
291
+ setGlobalObjectMutableProperties(globalObject, {
292
+ intrinsics,
293
+ newGlobalPropertyNames: sharedGlobalPropertyNames,
294
+ makeCompartmentConstructor: targetMakeCompartmentConstructor,
295
+ safeEvaluate,
296
+ markVirtualizedNativeFunction,
297
+ });
298
+
299
+ assign(globalObject, endowments);
276
300
 
277
301
  weakmapSet(privateFields, this, {
278
302
  name,
279
303
  globalTransforms,
280
304
  globalObject,
281
305
  knownScopeProxies,
282
- // The caller continues to own the globalLexicals object they passed to
283
- // the compartment constructor, but the compartment only respects the
284
- // original values and they are constants in the scope of evaluated
285
- // programs and executed modules.
286
- // This shallow copy captures only the values of enumerable own
287
- // properties, erasing accessors.
288
- // The snapshot is frozen to ensure that the properties are immutable
289
- // when transferred-by-property-descriptor onto local scope objects.
290
- globalLexicals: freeze({ ...globalLexicals }),
306
+ globalLexicals,
307
+ safeEvaluate,
291
308
  resolveHook,
292
309
  importHook,
293
310
  moduleMap,
@@ -0,0 +1,125 @@
1
+ // @ts-check
2
+
3
+ // environment-options needs to be imported quite early, and so should
4
+ // avoid importing from anything but commons.js and assert.js
5
+ import { arrayPush, arraySlice, freeze } from './commons.js';
6
+ import { assert } from './error/assert.js';
7
+
8
+ const { details: X, quote: q } = assert;
9
+
10
+ /**
11
+ * JavaScript module semantics resists attempts to parameterize a module's
12
+ * initialization behavior. A module initializes in order according to
13
+ * the path by which it is first imported, and then the initialized module
14
+ * is reused by all the other times it is imported. Compartments give us
15
+ * the opportunity to bind the same import name to different imported
16
+ * modules, depending on the package/compartment doing the import. Compartments
17
+ * also address the difficulty of parameterizing a module's initialization
18
+ * logic, but not in a pleasant manner.
19
+ *
20
+ * A pleasant parameterization would be for a static module to be function-like
21
+ * with explicit parameters, and for the parameterization to be like
22
+ * calling the static module with parameters in order to derive from it a
23
+ * module instance. Compartments instead lets us parameterize the meaning
24
+ * of a module instance derived from a static module according to the
25
+ * three namespaces provided by the JavaScript semantics, affecting the
26
+ * meaning of a module instance.
27
+ * * The global variable namespaces.
28
+ * * The global scope, aliased to properties of the global object.
29
+ * This is necessarily compartment-wide, and therefore in our
30
+ * recommened usage pattern, package-wide.
31
+ * * The global lexical scope. The SES-shim compartments support
32
+ * these both compartment-wide as well as per-module. But it is
33
+ * not yet clear what we will propose in the Compartment proposal.
34
+ * * The import namespace.
35
+ * * The host hooks.
36
+ *
37
+ * This `environment-options.js` module looks for a setting of an
38
+ * `optionName` parameter rooted in the global scope. If follows the Node
39
+ * precedent for finding Unix environment variable settings, looking for a
40
+ * global `process` object holding an `env` object,
41
+ * optionally holding a property named for the `optionName` whose value is the
42
+ * configuration setting of that option. For example, for the optionName
43
+ * `ALLOW_IMPLICIT_REMOTABLES` it would look in
44
+ * `globalThis.process.env.ALLOW_IMPLICIT_REMOTABLES`.
45
+ *
46
+ * If setting is either absent or `undefined`, that indicates that
47
+ * this configuration option should have its default behavior, whatever that is.
48
+ * Otherwise, reflecting Unix environment variables, the setting must be a
49
+ * string. This also helps ensure that this channel is used only to pass data,
50
+ * not authority beyond the ability to read this global state.
51
+ */
52
+
53
+ /**
54
+ * makeEnvironmentCaptor provides a mechanism for getting environment
55
+ * variables, if they are needed, and a way to catalog the names of all
56
+ * the environment variables that were captured.
57
+ *
58
+ * @param {Object} aGlobal
59
+ */
60
+ export const makeEnvironmentCaptor = aGlobal => {
61
+ const capturedEnvironmentOptionNames = [];
62
+
63
+ /**
64
+ * Gets an environment option by name and returns the option value or the
65
+ * given default.
66
+ *
67
+ * @param {string} optionName
68
+ * @param {string} defaultSetting
69
+ * @returns {string}
70
+ */
71
+ const getEnvironmentOption = (optionName, defaultSetting) => {
72
+ // eslint-disable-next-line @endo/no-polymorphic-call
73
+ assert.typeof(
74
+ optionName,
75
+ 'string',
76
+ X`Environment option name ${q(optionName)} must be a string.`,
77
+ );
78
+ // eslint-disable-next-line @endo/no-polymorphic-call
79
+ assert.typeof(
80
+ defaultSetting,
81
+ 'string',
82
+ X`Environment option default setting ${q(
83
+ defaultSetting,
84
+ )} must be a string.`,
85
+ );
86
+
87
+ /** @type {string} */
88
+ let setting = defaultSetting;
89
+ const globalProcess = aGlobal.process;
90
+ if (globalProcess && typeof globalProcess === 'object') {
91
+ const globalEnv = globalProcess.env;
92
+ if (globalEnv && typeof globalEnv === 'object') {
93
+ if (optionName in globalEnv) {
94
+ arrayPush(capturedEnvironmentOptionNames, optionName);
95
+ const optionValue = globalEnv[optionName];
96
+ // eslint-disable-next-line @endo/no-polymorphic-call
97
+ assert.typeof(
98
+ optionValue,
99
+ 'string',
100
+ X`Environment option named ${q(
101
+ optionName,
102
+ )}, if present, must have a corresponding string value, got ${q(
103
+ optionValue,
104
+ )}`,
105
+ );
106
+ setting = optionValue;
107
+ }
108
+ }
109
+ }
110
+ assert(
111
+ setting === undefined || typeof setting === 'string',
112
+ X`Environment option value ${q(setting)}, if present, must be a string.`,
113
+ );
114
+ return setting;
115
+ };
116
+ freeze(getEnvironmentOption);
117
+
118
+ const getCapturedEnvironmentOptionNames = () => {
119
+ return freeze(arraySlice(capturedEnvironmentOptionNames));
120
+ };
121
+ freeze(getCapturedEnvironmentOptionNames);
122
+
123
+ return { getEnvironmentOption, getCapturedEnvironmentOptionNames };
124
+ };
125
+ freeze(makeEnvironmentCaptor);
@@ -3,17 +3,40 @@
3
3
  import {
4
4
  Set,
5
5
  String,
6
+ arrayJoin,
7
+ arraySlice,
6
8
  freeze,
7
9
  is,
8
10
  isError,
9
11
  setAdd,
10
12
  setHas,
11
- stringStartsWith,
12
13
  stringIncludes,
14
+ stringStartsWith,
13
15
  stringifyJson,
14
16
  toStringTagSymbol,
15
17
  } from '../commons.js';
16
18
 
19
+ /**
20
+ * Joins English terms with commas and an optional conjunction.
21
+ *
22
+ * @param {string[]} terms
23
+ * @param {"and" | "or"} conjunction
24
+ */
25
+ export const enJoin = (terms, conjunction) => {
26
+ if (terms.length === 0) {
27
+ return '(none)';
28
+ } else if (terms.length === 1) {
29
+ return terms[0];
30
+ } else if (terms.length === 2) {
31
+ const [first, second] = terms;
32
+ return `${first} ${conjunction} ${second}`;
33
+ } else {
34
+ return `${arrayJoin(arraySlice(terms, 0, -1), ', ')}, ${conjunction} ${
35
+ terms[terms.length - 1]
36
+ }`;
37
+ }
38
+ };
39
+
17
40
  /**
18
41
  * Prepend the correct indefinite article onto a noun, typically a typeof
19
42
  * result, e.g., "an object" vs. "a number"
@@ -77,9 +77,7 @@ export const tameConsole = (
77
77
  // eslint-disable-next-line @endo/no-polymorphic-call
78
78
  event.preventDefault();
79
79
  // eslint-disable-next-line @endo/no-polymorphic-call
80
- const stackString = loggedErrorHandler.getStackString(event.error);
81
- // eslint-disable-next-line @endo/no-polymorphic-call
82
- causalConsole.error(stackString);
80
+ causalConsole.error(event.error);
83
81
  if (errorTrapping === 'exit' || errorTrapping === 'abort') {
84
82
  globalThis.window.location.href = `about:blank`;
85
83
  }
@@ -4,29 +4,14 @@ import { makeFunctionConstructor } from './make-function-constructor.js';
4
4
  import { constantProperties, universalPropertyNames } from './whitelist.js';
5
5
 
6
6
  /**
7
- * initGlobalObject()
8
- * Create new global object using a process similar to ECMA specifications
9
- * (portions of SetRealmGlobalObject and SetDefaultGlobalBindings).
10
- * `newGlobalPropertyNames` should be either `initialGlobalPropertyNames` or
11
- * `sharedGlobalPropertyNames`.
7
+ * setGlobalObjectConstantProperties()
8
+ * Initializes a new global object using a process similar to ECMA specifications
9
+ * (SetDefaultGlobalBindings). This process is split between this function and
10
+ * `setGlobalObjectMutableProperties`.
12
11
  *
13
12
  * @param {Object} globalObject
14
- * @param {Object} intrinsics
15
- * @param {Object} newGlobalPropertyNames
16
- * @param {Function} makeCompartmentConstructor
17
- * @param {Object} compartmentPrototype
18
- * @param {Object} [options]
19
- * @param {Array<Transform>} [options.globalTransforms]
20
- * @param {(Object) => void} [options.markVirtualizedNativeFunction]
21
13
  */
22
- export const initGlobalObject = (
23
- globalObject,
24
- intrinsics,
25
- newGlobalPropertyNames,
26
- makeCompartmentConstructor,
27
- compartmentPrototype,
28
- { globalTransforms, markVirtualizedNativeFunction },
29
- ) => {
14
+ export const setGlobalObjectConstantProperties = globalObject => {
30
15
  for (const [name, constant] of entries(constantProperties)) {
31
16
  defineProperty(globalObject, name, {
32
17
  value: constant,
@@ -35,7 +20,33 @@ export const initGlobalObject = (
35
20
  configurable: false,
36
21
  });
37
22
  }
23
+ };
38
24
 
25
+ /**
26
+ * setGlobalObjectMutableProperties()
27
+ * Create new global object using a process similar to ECMA specifications
28
+ * (portions of SetRealmGlobalObject and SetDefaultGlobalBindings).
29
+ * `newGlobalPropertyNames` should be either `initialGlobalPropertyNames` or
30
+ * `sharedGlobalPropertyNames`.
31
+ *
32
+ * @param {Object} globalObject
33
+ * @param {Object} param1
34
+ * @param {Object} param1.intrinsics
35
+ * @param {Object} param1.newGlobalPropertyNames
36
+ * @param {Function} param1.makeCompartmentConstructor
37
+ * @param {(string, Object?) => any} param1.safeEvaluate
38
+ * @param {(Object) => void} param1.markVirtualizedNativeFunction
39
+ */
40
+ export const setGlobalObjectMutableProperties = (
41
+ globalObject,
42
+ {
43
+ intrinsics,
44
+ newGlobalPropertyNames,
45
+ makeCompartmentConstructor,
46
+ safeEvaluate,
47
+ markVirtualizedNativeFunction,
48
+ },
49
+ ) => {
39
50
  for (const [name, intrinsicName] of entries(universalPropertyNames)) {
40
51
  if (objectHasOwnProperty(intrinsics, intrinsicName)) {
41
52
  defineProperty(globalObject, name, {
@@ -60,12 +71,8 @@ export const initGlobalObject = (
60
71
 
61
72
  const perCompartmentGlobals = {
62
73
  globalThis: globalObject,
63
- eval: makeEvalFunction(globalObject, {
64
- globalTransforms,
65
- }),
66
- Function: makeFunctionConstructor(globalObject, {
67
- globalTransforms,
68
- }),
74
+ eval: makeEvalFunction(safeEvaluate),
75
+ Function: makeFunctionConstructor(safeEvaluate),
69
76
  };
70
77
 
71
78
  perCompartmentGlobals.Compartment = makeCompartmentConstructor(