ses 0.15.7 → 0.15.10

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
@@ -23,6 +23,7 @@ export interface LockdownOptions {
23
23
  errorTaming?: 'safe' | 'unsafe';
24
24
  dateTaming?: 'safe' | 'unsafe'; // deprecated
25
25
  mathTaming?: 'safe' | 'unsafe'; // deprecated
26
+ evalTaming?: 'safeEval' | 'unsafeEval' | 'noEval';
26
27
  stackFiltering?: 'concise' | 'verbose';
27
28
  overrideTaming?: 'moderate' | 'min' | 'severe';
28
29
  overrideDebug?: Array<string>;
@@ -194,6 +195,14 @@ export interface Assert {
194
195
  makeAssert: MakeAssert;
195
196
  }
196
197
 
198
+ interface CompartmentEvaluateOptions {
199
+ sloppyGlobalsMode?: boolean;
200
+ __moduleShimLexicals__?: Object;
201
+ __evadeHtmlCommentTest__?: boolean;
202
+ __evadeImportExpressionTest__?: boolean;
203
+ __rejectSomeDirectEvalExpressions__?: boolean;
204
+ }
205
+
197
206
  declare global {
198
207
  var harden: Harden;
199
208
 
@@ -216,7 +225,7 @@ declare global {
216
225
 
217
226
  get name(): string;
218
227
 
219
- evaluate(code: string): any;
228
+ evaluate(code: string, options?: CompartmentEvaluateOptions): any;
220
229
 
221
230
  import(specifier: string): Promise<{ namespace: ModuleExportsNamespace }>;
222
231
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ses",
3
- "version": "0.15.7",
3
+ "version": "0.15.10",
4
4
  "description": "Hardened JavaScript for Fearless Cooperation",
5
5
  "keywords": [
6
6
  "lockdown",
@@ -52,17 +52,17 @@
52
52
  "lint": "yarn lint:types && yarn lint:js",
53
53
  "lint-fix": "eslint --fix .",
54
54
  "lint:js": "eslint .",
55
- "lint:types": "tsc --build jsconfig.json",
55
+ "lint:types": "tsc -p jsconfig.json",
56
56
  "prepublish": "yarn run clean && yarn build",
57
57
  "qt": "ava",
58
58
  "test": "yarn build && ava",
59
59
  "test:platform-compatability": "node test/package/test.cjs"
60
60
  },
61
61
  "devDependencies": {
62
- "@endo/compartment-mapper": "^0.6.5",
63
- "@endo/eslint-config": "^0.4.2",
64
- "@endo/static-module-record": "^0.6.12",
65
- "@endo/test262-runner": "^0.1.18",
62
+ "@endo/compartment-mapper": "^0.7.0",
63
+ "@endo/eslint-config": "^0.4.5",
64
+ "@endo/static-module-record": "^0.6.15",
65
+ "@endo/test262-runner": "^0.1.21",
66
66
  "ava": "^3.12.1",
67
67
  "babel-eslint": "^10.0.3",
68
68
  "c8": "^7.7.3",
@@ -75,7 +75,7 @@
75
75
  "prettier": "^1.19.1",
76
76
  "sinon": "8.0.4",
77
77
  "terser": "^4.8.0",
78
- "typescript": "^4.2.3"
78
+ "typescript": "~4.5.5"
79
79
  },
80
80
  "files": [
81
81
  "LICENSE*",
@@ -178,5 +178,5 @@
178
178
  ],
179
179
  "timeout": "2m"
180
180
  },
181
- "gitHead": "96df4cf238b245cc92c78e4af4667f405b8cf4f0"
181
+ "gitHead": "08973d4fc6358a58d733251b051b2812bb4c651a"
182
182
  }
package/src/commons.js CHANGED
@@ -62,6 +62,7 @@ export const {
62
62
  keys,
63
63
  prototype: objectPrototype,
64
64
  seal,
65
+ preventExtensions,
65
66
  setPrototypeOf,
66
67
  values,
67
68
  } = Object;
@@ -73,6 +74,8 @@ export const {
73
74
  matchAll: matchAllSymbol,
74
75
  } = Symbol;
75
76
 
77
+ export const { isInteger } = Number;
78
+
76
79
  export const { stringify: stringifyJson } = JSON;
77
80
 
78
81
  // At time of this writing, we still support Node 10 which doesn't have
@@ -137,6 +140,8 @@ export const { prototype: weaksetPrototype } = WeakSet;
137
140
  export const { prototype: functionPrototype } = Function;
138
141
  export const { prototype: promisePrototype } = Promise;
139
142
 
143
+ export const typedArrayPrototype = getPrototypeOf(Uint8Array.prototype);
144
+
140
145
  /**
141
146
  * uncurryThis()
142
147
  * This form of uncurry uses Reflect.apply()
@@ -174,6 +179,7 @@ export const mapHas = uncurryThis(mapPrototype.has);
174
179
  export const iterateMap = uncurryThis(mapPrototype[iteratorSymbol]);
175
180
  //
176
181
  export const setAdd = uncurryThis(setPrototype.add);
182
+ export const setDelete = uncurryThis(setPrototype.delete);
177
183
  export const setForEach = uncurryThis(setPrototype.forEach);
178
184
  export const setHas = uncurryThis(setPrototype.has);
179
185
  export const iterateSet = uncurryThis(setPrototype[iteratorSymbol]);
@@ -266,3 +272,9 @@ export const FERAL_EVAL = eval;
266
272
  // Sample at module initialization time, which is before lockdown can
267
273
  // repair it. Use it only to build powerless abstractions.
268
274
  export const FERAL_FUNCTION = Function;
275
+
276
+ export const noEvalEvaluate = () => {
277
+ throw new TypeError(
278
+ 'Cannot eval with evalTaming set to "noEval" (SES_NO_EVAL)',
279
+ );
280
+ };
@@ -23,6 +23,7 @@ import {
23
23
  import {
24
24
  setGlobalObjectConstantProperties,
25
25
  setGlobalObjectMutableProperties,
26
+ setGlobalObjectEvaluators,
26
27
  } from './global-object.js';
27
28
  import { isValidIdentifierName } from './scope-constants.js';
28
29
  import { sharedGlobalPropertyNames } from './whitelist.js';
@@ -292,10 +293,16 @@ export const makeCompartmentConstructor = (
292
293
  intrinsics,
293
294
  newGlobalPropertyNames: sharedGlobalPropertyNames,
294
295
  makeCompartmentConstructor: targetMakeCompartmentConstructor,
295
- safeEvaluate,
296
296
  markVirtualizedNativeFunction,
297
297
  });
298
298
 
299
+ // TODO: maybe add evalTaming to the Compartment constructor 3rd options?
300
+ setGlobalObjectEvaluators(
301
+ globalObject,
302
+ safeEvaluate,
303
+ markVirtualizedNativeFunction,
304
+ );
305
+
299
306
  assign(globalObject, endowments);
300
307
 
301
308
  weakmapSet(privateFields, this, {
@@ -40,8 +40,8 @@ const { details: X, quote: q } = assert;
40
40
  * global `process` object holding an `env` object,
41
41
  * optionally holding a property named for the `optionName` whose value is the
42
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`.
43
+ * `FOO_BAR` it would look in
44
+ * `globalThis.process.env.FOO_BAR`.
45
45
  *
46
46
  * If setting is either absent or `undefined`, that indicates that
47
47
  * this configuration option should have its default behavior, whatever that is.
@@ -34,7 +34,6 @@ export const setGlobalObjectConstantProperties = globalObject => {
34
34
  * @param {Object} param1.intrinsics
35
35
  * @param {Object} param1.newGlobalPropertyNames
36
36
  * @param {Function} param1.makeCompartmentConstructor
37
- * @param {(string, Object?) => any} param1.safeEvaluate
38
37
  * @param {(Object) => void} param1.markVirtualizedNativeFunction
39
38
  */
40
39
  export const setGlobalObjectMutableProperties = (
@@ -43,7 +42,6 @@ export const setGlobalObjectMutableProperties = (
43
42
  intrinsics,
44
43
  newGlobalPropertyNames,
45
44
  makeCompartmentConstructor,
46
- safeEvaluate,
47
45
  markVirtualizedNativeFunction,
48
46
  },
49
47
  ) => {
@@ -71,8 +69,6 @@ export const setGlobalObjectMutableProperties = (
71
69
 
72
70
  const perCompartmentGlobals = {
73
71
  globalThis: globalObject,
74
- eval: makeEvalFunction(safeEvaluate),
75
- Function: makeFunctionConstructor(safeEvaluate),
76
72
  };
77
73
 
78
74
  perCompartmentGlobals.Compartment = makeCompartmentConstructor(
@@ -95,3 +91,38 @@ export const setGlobalObjectMutableProperties = (
95
91
  }
96
92
  }
97
93
  };
94
+
95
+ /**
96
+ * setGlobalObjectEvaluators()
97
+ * Set the eval and the Function evaluator on the global object with given evalTaming policy.
98
+ *
99
+ * @param {Object} globalObject
100
+ * @param {Function} evaluator
101
+ * @param {(Object) => void} markVirtualizedNativeFunction
102
+ */
103
+ export const setGlobalObjectEvaluators = (
104
+ globalObject,
105
+ evaluator,
106
+ markVirtualizedNativeFunction,
107
+ ) => {
108
+ {
109
+ const f = makeEvalFunction(evaluator);
110
+ markVirtualizedNativeFunction(f);
111
+ defineProperty(globalObject, 'eval', {
112
+ value: f,
113
+ writable: true,
114
+ enumerable: false,
115
+ configurable: true,
116
+ });
117
+ }
118
+ {
119
+ const f = makeFunctionConstructor(evaluator);
120
+ markVirtualizedNativeFunction(f);
121
+ defineProperty(globalObject, 'Function', {
122
+ value: f,
123
+ writable: true,
124
+ enumerable: false,
125
+ configurable: true,
126
+ });
127
+ }
128
+ };
@@ -24,6 +24,7 @@ import {
24
24
  is,
25
25
  ownKeys,
26
26
  stringSplit,
27
+ noEvalEvaluate,
27
28
  } from './commons.js';
28
29
  import { enJoin } from './error/stringify-utils.js';
29
30
  import { makeHardener } from './make-hardener.js';
@@ -38,6 +39,7 @@ import tameLocaleMethods from './tame-locale-methods.js';
38
39
  import {
39
40
  setGlobalObjectConstantProperties,
40
41
  setGlobalObjectMutableProperties,
42
+ setGlobalObjectEvaluators,
41
43
  } from './global-object.js';
42
44
  import { makeSafeEvaluator } from './make-safe-evaluator.js';
43
45
  import { initialGlobalPropertyNames } from './whitelist.js';
@@ -164,6 +166,7 @@ export const repairIntrinsics = (options = {}) => {
164
166
  overrideTaming = getenv('LOCKDOWN_OVERRIDE_TAMING', 'moderate'),
165
167
  stackFiltering = getenv('LOCKDOWN_STACK_FILTERING', 'concise'),
166
168
  domainTaming = getenv('LOCKDOWN_DOMAIN_TAMING', 'safe'),
169
+ evalTaming = getenv('LOCKDOWN_EVAL_TAMING', 'safeEval'),
167
170
  overrideDebug = arrayFilter(
168
171
  stringSplit(getenv('LOCKDOWN_OVERRIDE_DEBUG', ''), ','),
169
172
  /** @param {string} debugName */
@@ -189,6 +192,13 @@ export const repairIntrinsics = (options = {}) => {
189
192
  );
190
193
  }
191
194
 
195
+ assert(
196
+ evalTaming === 'unsafeEval' ||
197
+ evalTaming === 'safeEval' ||
198
+ evalTaming === 'noEval',
199
+ d`lockdown(): non supported option evalTaming: ${q(evalTaming)}`,
200
+ );
201
+
192
202
  // Assert that only supported options were passed.
193
203
  // Use Reflect.ownKeys to reject symbol-named properties as well.
194
204
  const extraOptionsNames = ownKeys(extraOptions);
@@ -324,16 +334,32 @@ export const repairIntrinsics = (options = {}) => {
324
334
 
325
335
  setGlobalObjectConstantProperties(globalThis);
326
336
 
327
- const { safeEvaluate } = makeSafeEvaluator({ globalObject: globalThis });
328
-
329
337
  setGlobalObjectMutableProperties(globalThis, {
330
338
  intrinsics,
331
339
  newGlobalPropertyNames: initialGlobalPropertyNames,
332
340
  makeCompartmentConstructor,
333
- safeEvaluate,
334
341
  markVirtualizedNativeFunction,
335
342
  });
336
343
 
344
+ if (evalTaming === 'noEval') {
345
+ setGlobalObjectEvaluators(
346
+ globalThis,
347
+ noEvalEvaluate,
348
+ markVirtualizedNativeFunction,
349
+ );
350
+ } else if (evalTaming === 'safeEval') {
351
+ const { safeEvaluate } = makeSafeEvaluator({ globalObject: globalThis });
352
+ setGlobalObjectEvaluators(
353
+ globalThis,
354
+ safeEvaluate,
355
+ markVirtualizedNativeFunction,
356
+ );
357
+ } else if (evalTaming === 'unsafeEval') {
358
+ // Leave eval function and Function constructor of the initial compartment in-tact.
359
+ // Other compartments will not have access to these evaluators unless a guest program
360
+ // escapes containment.
361
+ }
362
+
337
363
  /**
338
364
  * 3. HARDEN to share the intrinsics.
339
365
  *
@@ -1,7 +1,9 @@
1
- /*
1
+ /**
2
2
  * makeEvalFunction()
3
3
  * A safe version of the native eval function which relies on
4
4
  * the safety of safeEvaluate for confinement.
5
+ *
6
+ * @param {Function} safeEvaluate
5
7
  */
6
8
  export const makeEvalFunction = safeEvaluate => {
7
9
  // We use the the concise method syntax to create an eval without a
@@ -27,26 +27,88 @@ import {
27
27
  TypeError,
28
28
  WeakMap,
29
29
  WeakSet,
30
+ apply,
30
31
  arrayForEach,
32
+ defineProperty,
31
33
  freeze,
34
+ getOwnPropertyDescriptor,
32
35
  getOwnPropertyDescriptors,
33
36
  getPrototypeOf,
37
+ isInteger,
34
38
  isObject,
35
39
  objectHasOwnProperty,
36
40
  ownKeys,
41
+ preventExtensions,
37
42
  setAdd,
38
43
  setForEach,
39
44
  setHas,
45
+ toStringTagSymbol,
46
+ typedArrayPrototype,
40
47
  weakmapGet,
41
48
  weakmapSet,
42
49
  weaksetAdd,
43
50
  weaksetHas,
44
51
  } from './commons.js';
52
+ import { assert } from './error/assert.js';
45
53
 
46
54
  /**
47
55
  * @typedef {import('../index.js').Harden} Harden
48
56
  */
49
57
 
58
+ // Obtain the string tag accessor of of TypedArray so we can indirectly use the
59
+ // TypedArray brand check it employs.
60
+ const typedArrayToStringTag = getOwnPropertyDescriptor(
61
+ typedArrayPrototype,
62
+ toStringTagSymbol,
63
+ );
64
+ assert(typedArrayToStringTag);
65
+ const getTypedArrayToStringTag = typedArrayToStringTag.get;
66
+ assert(getTypedArrayToStringTag);
67
+
68
+ // Exported for tests.
69
+ /** @param {unknown} object */
70
+ export const isTypedArray = object => {
71
+ // The object must pass a brand check or toStringTag will return undefined.
72
+ const tag = apply(getTypedArrayToStringTag, object, []);
73
+ return tag !== undefined;
74
+ };
75
+
76
+ /**
77
+ * @template T
78
+ * @param {ArrayLike<T>} array
79
+ */
80
+ const freezeTypedArray = array => {
81
+ const descs = getOwnPropertyDescriptors(array);
82
+
83
+ preventExtensions(array);
84
+
85
+ // Downgrade writable expandos to readonly, even if non-configurable.
86
+ arrayForEach(ownKeys(descs), (/** @type {string | symbol} */ name) => {
87
+ const desc = descs[/** @type {string} */ (name)];
88
+ // The numbered properties are writable and non-configurable,
89
+ // and cannot be made non-writable by defineProperty.
90
+ // This is a strange behavior intrinsic to TypedArrays, but no more harmful
91
+ // than the mutability of properties of a hardened Map or Set,
92
+ // so we carve out this exceptional behavior.
93
+ //
94
+ // TypedArrays are integer-indexed exotic objects, so indexed properties
95
+ // outside the range of 0 to the typed array's length are disallowed.
96
+ // Assignment to these indexes silently fails and defining an indexed
97
+ // property throws an error.
98
+ // So, we only need to make non-index properties non-writable and
99
+ // non-configurable.
100
+ // https://tc39.es/ecma262/#sec-integer-indexed-exotic-objects
101
+ const number = +String(name);
102
+ if (!isInteger(number)) {
103
+ defineProperty(array, name, {
104
+ ...desc,
105
+ writable: false,
106
+ configurable: false,
107
+ });
108
+ }
109
+ });
110
+ };
111
+
50
112
  /**
51
113
  * Create a `harden` function.
52
114
  *
@@ -101,7 +163,12 @@ export const makeHardener = () => {
101
163
  // Object are verified before being enqueued,
102
164
  // therefore this is a valid candidate.
103
165
  // Throws if this fails (strict mode).
104
- freeze(obj);
166
+ // Also throws if the object is an ArrayBuffer or any TypedArray.
167
+ if (isTypedArray(obj)) {
168
+ freezeTypedArray(obj);
169
+ } else {
170
+ freeze(obj);
171
+ }
105
172
 
106
173
  // we rely upon certain commitments of Object.freeze and proxies here
107
174