ses 0.14.4 → 0.15.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/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.4",
3
+ "version": "0.15.0",
4
4
  "description": "Hardened JavaScript for Fearless Cooperation",
5
5
  "keywords": [
6
6
  "lockdown",
@@ -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.4",
63
- "@endo/eslint-config": "^0.3.16",
64
- "@endo/static-module-record": "^0.6.4",
65
- "@endo/test262-runner": "^0.1.10",
62
+ "@endo/compartment-mapper": "^0.5.5",
63
+ "@endo/eslint-config": "^0.3.17",
64
+ "@endo/static-module-record": "^0.6.5",
65
+ "@endo/test262-runner": "^0.1.11",
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": "e92151ee140769c4de6d8e8533cf9abaac6781e9"
182
+ "gitHead": "bf06e1432ab8fa377e19610c883e4c40c4002089"
183
183
  }
@@ -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"
@@ -14,7 +14,16 @@
14
14
 
15
15
  // @ts-check
16
16
 
17
- import { globalThis, is, keys, ownKeys } from './commons.js';
17
+ import {
18
+ TypeError,
19
+ arrayFilter,
20
+ arrayMap,
21
+ globalThis,
22
+ is,
23
+ ownKeys,
24
+ stringSplit,
25
+ } from './commons.js';
26
+ import { enJoin } from './error/stringify-utils.js';
18
27
  import { makeHardener } from './make-hardener.js';
19
28
  import { makeIntrinsicsCollector } from './intrinsics.js';
20
29
  import whitelistIntrinsics from './whitelist-intrinsics.js';
@@ -36,12 +45,16 @@ import { tameDomains } from './tame-domains.js';
36
45
  import { tameConsole } from './error/tame-console.js';
37
46
  import tameErrorConstructor from './error/tame-error-constructor.js';
38
47
  import { assert, makeAssert } from './error/assert.js';
48
+ import { makeEnvironmentCaptor } from './environment-options.js';
49
+ import { getAnonymousIntrinsics } from './get-anonymous-intrinsics.js';
50
+ import { makeCompartmentConstructor } from './compartment-shim.js';
39
51
 
40
52
  /** @typedef {import('../index.js').LockdownOptions} LockdownOptions */
41
53
 
42
54
  const { details: d, quote: q } = assert;
43
55
 
44
- let firstOptions;
56
+ /** @type {Error=} */
57
+ let priorLockdown;
45
58
 
46
59
  // Build a harden() with an empty fringe.
47
60
  // Gate it on lockdown.
@@ -52,8 +65,6 @@ let firstOptions;
52
65
  */
53
66
  const harden = makeHardener();
54
67
 
55
- const alreadyHardenedIntrinsics = () => false;
56
-
57
68
  /**
58
69
  * @callback Transform
59
70
  * @param {string} source
@@ -70,44 +81,17 @@ const alreadyHardenedIntrinsics = () => false;
70
81
  * @param {Object} [options.globalLexicals]
71
82
  */
72
83
 
73
- /**
74
- * @callback CompartmentConstructorMaker
75
- * @param {CompartmentConstructorMaker} targetMakeCompartmentConstructor
76
- * @param {Object} intrinsics
77
- * @param {(func: Function) => void} markVirtualizedNativeFunction
78
- * @returns {CompartmentConstructor}
79
- */
80
-
81
84
  // TODO https://github.com/endojs/endo/issues/814
82
85
  // Lockdown currently allows multiple calls provided that the specified options
83
86
  // of every call agree. With experience, we have observed that lockdown should
84
87
  // only ever need to be called once and that simplifying lockdown will improve
85
88
  // the quality of audits.
86
89
 
87
- // TODO https://github.com/endojs/endo/issues/815
88
- // Lockdown receives makeCompartmentConstructor and compartmentPrototype.
89
- // This is a vestige of an earlier version of SES where makeLockdown was called
90
- // from two different entry points: one for a layer of SES that had a
91
- // Compartment that only supported evaluating programs, and a second layer that
92
- // extended Compartment to support modules, but at the expense of entraining a
93
- // dependency on Babel.
94
- // SES currently externalizes the dependency on Babel and one version of
95
- // makeCompartmentConstructor is sufficient for all uses, so this can be
96
- // simplified.
97
-
98
90
  /**
99
- * @param {CompartmentConstructorMaker} makeCompartmentConstructor
100
- * @param {Object} compartmentPrototype
101
- * @param {() => Object} getAnonymousIntrinsics
102
91
  * @param {LockdownOptions} [options]
103
- * @returns {() => {}} repairIntrinsics
92
+ * @returns {() => void} repairIntrinsics
104
93
  */
105
- export const repairIntrinsics = (
106
- makeCompartmentConstructor,
107
- compartmentPrototype,
108
- getAnonymousIntrinsics,
109
- options = {},
110
- ) => {
94
+ export const repairIntrinsics = (options = {}) => {
111
95
  // First time, absent options default to 'safe'.
112
96
  // Subsequent times, absent options default to first options.
113
97
  // Thus, all present options must agree with first options.
@@ -132,24 +116,46 @@ export const repairIntrinsics = (
132
116
  // is useful. See
133
117
  // [`stackFiltering` options](https://github.com/Agoric/SES-shim/blob/master/packages/ses/lockdown-options.md#stackfiltering-options)
134
118
  // for an explanation.
135
- options = /** @type {LockdownOptions} */ ({ ...firstOptions, ...options });
119
+
136
120
  const {
121
+ getEnvironmentOption: getenv,
122
+ getCapturedEnvironmentOptionNames,
123
+ } = makeEnvironmentCaptor(globalThis);
124
+
125
+ const {
126
+ errorTaming = getenv('LOCKDOWN_ERROR_TAMING', 'safe'),
127
+ errorTrapping = getenv('LOCKDOWN_ERROR_TRAPPING', 'platform'),
128
+ regExpTaming = getenv('LOCKDOWN_REGEXP_TAMING', 'safe'),
129
+ localeTaming = getenv('LOCKDOWN_LOCALE_TAMING', 'safe'),
130
+ consoleTaming = getenv('LOCKDOWN_CONSOLE_TAMING', 'safe'),
131
+ overrideTaming = getenv('LOCKDOWN_OVERRIDE_TAMING', 'moderate'),
132
+ stackFiltering = getenv('LOCKDOWN_STACK_FILTERING', 'concise'),
133
+ domainTaming = getenv('LOCKDOWN_DOMAIN_TAMING', 'safe'),
134
+ overrideDebug = arrayFilter(
135
+ stringSplit(getenv('LOCKDOWN_OVERRIDE_DEBUG', ''), ','),
136
+ /** @param {string} debugName */
137
+ debugName => debugName !== '',
138
+ ),
139
+ __allowUnsafeMonkeyPatching__ = getenv(
140
+ '__LOCKDOWN_ALLOW_UNSAFE_MONKEY_PATCHING__',
141
+ 'safe',
142
+ ),
137
143
  dateTaming = 'safe', // deprecated
138
- errorTaming = 'safe',
139
144
  mathTaming = 'safe', // deprecated
140
- errorTrapping = 'platform',
141
- regExpTaming = 'safe',
142
- localeTaming = 'safe',
143
- consoleTaming = 'safe',
144
- overrideTaming = 'moderate',
145
- overrideDebug = [],
146
- stackFiltering = 'concise',
147
- domainTaming = 'unsafe', // TODO become 'safe' by default in next-breaking-release.
148
- __allowUnsafeMonkeyPatching__ = 'safe',
149
-
150
145
  ...extraOptions
151
146
  } = options;
152
147
 
148
+ const capturedEnvironmentOptionNames = getCapturedEnvironmentOptionNames();
149
+ if (capturedEnvironmentOptionNames.length > 0) {
150
+ // eslint-disable-next-line @endo/no-polymorphic-call
151
+ console.warn(
152
+ `SES Lockdown using options from environment variables ${enJoin(
153
+ arrayMap(capturedEnvironmentOptionNames, q),
154
+ 'and',
155
+ )}`,
156
+ );
157
+ }
158
+
153
159
  // Assert that only supported options were passed.
154
160
  // Use Reflect.ownKeys to reject symbol-named properties as well.
155
161
  const extraOptionsNames = ownKeys(extraOptions);
@@ -158,36 +164,21 @@ export const repairIntrinsics = (
158
164
  d`lockdown(): non supported option ${q(extraOptionsNames)}`,
159
165
  );
160
166
 
161
- // Asserts for multiple invocation of lockdown().
162
- if (firstOptions) {
163
- for (const name of keys(firstOptions)) {
164
- assert(
165
- options[name] === firstOptions[name],
166
- d`lockdown(): cannot re-invoke with different option ${q(name)}`,
167
- );
168
- }
169
- return alreadyHardenedIntrinsics;
170
- }
171
-
172
- firstOptions = {
173
- dateTaming, // deprecated
174
- errorTaming,
175
- mathTaming, // deprecated
176
- regExpTaming,
177
- localeTaming,
178
- consoleTaming,
179
- overrideTaming,
180
- overrideDebug,
181
- stackFiltering,
182
- domainTaming,
183
- __allowUnsafeMonkeyPatching__,
184
- };
167
+ assert(
168
+ priorLockdown === undefined,
169
+ `Already locked down at ${priorLockdown} (SES_ALREADY_LOCKED_DOWN)`,
170
+ TypeError,
171
+ );
172
+ priorLockdown = new TypeError('Prior lockdown (SES_ALREADY_LOCKED_DOWN)');
173
+ // Tease V8 to generate the stack string and release the closures the stack
174
+ // trace retained:
175
+ priorLockdown.stack;
185
176
 
186
177
  /**
187
178
  * Because of packagers and bundlers, etc, multiple invocations of lockdown
188
179
  * might happen in separate instantiations of the source of this module.
189
180
  * In that case, each one sees its own `firstOptions` variable, so the test
190
- * above will not detect that lockdown has already happened. Instead, we
181
+ * above will not detect that lockdown has already happened. We
191
182
  * unreliably test some telltale signs that lockdown has run, to avoid
192
183
  * trying to lock down a locked down environment. Although the test is
193
184
  * unreliable, this is consistent with the SES threat model. SES provides
@@ -198,12 +189,7 @@ export const repairIntrinsics = (
198
189
  * tests without actually locking down counts as corrupting code.
199
190
  *
200
191
  * The specifics of what this tests for may change over time, but it
201
- * should be consistent with any setting of the lockdown options. We
202
- * do no checking that the state is consistent with current lockdown
203
- * options. So a call to lockdown with one set of options may silently
204
- * succeed with a state not reflecting those options, but only
205
- * if a previous lockdown happened from something other than this
206
- * instance of this module.
192
+ * should be consistent with any setting of the lockdown options.
207
193
  */
208
194
  const seemsToBeLockedDown = () => {
209
195
  return (
@@ -220,8 +206,9 @@ export const repairIntrinsics = (
220
206
 
221
207
  if (seemsToBeLockedDown()) {
222
208
  // eslint-disable-next-line @endo/no-polymorphic-call
223
- console.log('Seems to already be locked down. Skipping second lockdown');
224
- return alreadyHardenedIntrinsics;
209
+ throw new TypeError(
210
+ `Already locked down but not by this SES instance (SES_MULTIPLE_INSTANCES)`,
211
+ );
225
212
  }
226
213
 
227
214
  /**
@@ -260,6 +247,7 @@ export const repairIntrinsics = (
260
247
  optGetStackString = intrinsics['%InitialGetStackString%'];
261
248
  }
262
249
  const consoleRecord = tameConsole(
250
+ // @ts-ignore tameConsole does its own input validation
263
251
  consoleTaming,
264
252
  errorTrapping,
265
253
  optGetStackString,
@@ -308,6 +296,11 @@ export const repairIntrinsics = (
308
296
 
309
297
  /**
310
298
  * 3. HARDEN to share the intrinsics.
299
+ *
300
+ * We define hardenIntrinsics here so that options are in scope, but return
301
+ * it to the caller because we intend to eventually allow vetted shims to run
302
+ * between repairs and the hardening of intrinsics and so we can benchmark
303
+ * repair separately from hardening.
311
304
  */
312
305
 
313
306
  function hardenIntrinsics() {
@@ -315,6 +308,7 @@ export const repairIntrinsics = (
315
308
  // TODO consider moving this to the end of the repair phase, and
316
309
  // therefore before vetted shims rather than afterwards. It is not
317
310
  // clear yet which is better.
311
+ // @ts-ignore enablePropertyOverrides does its own input validation
318
312
  enablePropertyOverrides(intrinsics, overrideTaming, overrideDebug);
319
313
 
320
314
  if (__allowUnsafeMonkeyPatching__ !== 'unsafe') {
@@ -340,27 +334,9 @@ export const repairIntrinsics = (
340
334
  };
341
335
 
342
336
  /**
343
- * @param {CompartmentConstructorMaker} makeCompartmentConstructor
344
- * @param {Object} compartmentPrototype
345
- * @param {() => Object} getAnonymousIntrinsics
346
- * @returns {import('../index.js').Lockdown}
337
+ * @param {LockdownOptions} [options]
347
338
  */
348
- export const makeLockdown = (
349
- makeCompartmentConstructor,
350
- compartmentPrototype,
351
- getAnonymousIntrinsics,
352
- ) => {
353
- /**
354
- * @param {LockdownOptions} [options]
355
- */
356
- const lockdown = (options = {}) => {
357
- const maybeHardenIntrinsics = repairIntrinsics(
358
- makeCompartmentConstructor,
359
- compartmentPrototype,
360
- getAnonymousIntrinsics,
361
- options,
362
- );
363
- return maybeHardenIntrinsics();
364
- };
365
- return lockdown;
339
+ export const lockdown = (options = {}) => {
340
+ const hardenIntrinsics = repairIntrinsics(options);
341
+ hardenIntrinsics();
366
342
  };