ses 0.15.10 → 0.15.13

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
@@ -1,3 +1,7 @@
1
+ /**
2
+ * @file Types of the SES environment
3
+ */
4
+
1
5
  /* eslint-disable no-restricted-globals, vars-on-top, no-var */
2
6
 
3
7
  // It's academically tempting to define a hardened type, but TypeScript doesn't
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ses",
3
- "version": "0.15.10",
3
+ "version": "0.15.13",
4
4
  "description": "Hardened JavaScript for Fearless Cooperation",
5
5
  "keywords": [
6
6
  "lockdown",
@@ -35,11 +35,11 @@
35
35
  "types": "./index.d.ts",
36
36
  "exports": {
37
37
  ".": {
38
- "import": "./dist/ses.umd.js",
38
+ "import": "./index.js",
39
39
  "require": "./dist/ses.cjs"
40
40
  },
41
41
  "./lockdown": {
42
- "import": "./dist/ses.umd.js",
42
+ "import": "./index.js",
43
43
  "require": "./dist/ses.cjs"
44
44
  },
45
45
  "./package.json": "./package.json"
@@ -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 -p jsconfig.json",
55
+ "lint:types": "tsc",
56
56
  "prepublish": "yarn run clean && yarn build",
57
57
  "qt": "ava",
58
- "test": "yarn build && ava",
58
+ "test": "tsd && ava",
59
59
  "test:platform-compatability": "node test/package/test.cjs"
60
60
  },
61
61
  "devDependencies": {
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",
62
+ "@endo/compartment-mapper": "^0.7.3",
63
+ "@endo/eslint-config": "^0.4.8",
64
+ "@endo/static-module-record": "^0.7.2",
65
+ "@endo/test262-runner": "^0.1.24",
66
66
  "ava": "^3.12.1",
67
67
  "babel-eslint": "^10.0.3",
68
68
  "c8": "^7.7.3",
@@ -75,7 +75,8 @@
75
75
  "prettier": "^1.19.1",
76
76
  "sinon": "8.0.4",
77
77
  "terser": "^4.8.0",
78
- "typescript": "~4.5.5"
78
+ "tsd": "^0.19.1",
79
+ "typescript": "~4.6.2"
79
80
  },
80
81
  "files": [
81
82
  "LICENSE*",
@@ -178,5 +179,5 @@
178
179
  ],
179
180
  "timeout": "2m"
180
181
  },
181
- "gitHead": "08973d4fc6358a58d733251b051b2812bb4c651a"
182
+ "gitHead": "ada5203fd64e6828dca74895fd74588e08925d97"
182
183
  }
package/src/commons.js CHANGED
@@ -72,6 +72,8 @@ export const {
72
72
  toStringTag: toStringTagSymbol,
73
73
  iterator: iteratorSymbol,
74
74
  matchAll: matchAllSymbol,
75
+ keyFor: symbolKeyFor,
76
+ for: symbolFor,
75
77
  } = Symbol;
76
78
 
77
79
  export const { isInteger } = Number;
@@ -55,7 +55,7 @@ export const provideCompartmentEvaluator = (compartmentFields, options) => {
55
55
 
56
56
  ({ safeEvaluate } = makeSafeEvaluator({
57
57
  globalObject,
58
- localObject,
58
+ globalLexicals: localObject,
59
59
  globalTransforms,
60
60
  sloppyGlobalsMode,
61
61
  knownScopeProxies,
@@ -283,7 +283,7 @@ export const makeCompartmentConstructor = (
283
283
  const knownScopeProxies = new WeakSet();
284
284
  const { safeEvaluate } = makeSafeEvaluator({
285
285
  globalObject,
286
- localObject: globalLexicals,
286
+ globalLexicals,
287
287
  globalTransforms,
288
288
  sloppyGlobalsMode: false,
289
289
  knownScopeProxies,
@@ -16,8 +16,6 @@ import { assert } from './error/assert.js';
16
16
 
17
17
  const { details: d } = assert;
18
18
 
19
- // TODO: rename localObject to scopeObject
20
-
21
19
  /**
22
20
  * makeSafeEvaluator()
23
21
  * Build the low-level operation used by all evaluators:
@@ -25,25 +23,25 @@ const { details: d } = assert;
25
23
  *
26
24
  * @param {Object} options
27
25
  * @param {Object} options.globalObject
28
- * @param {Object} [options.localObject]
26
+ * @param {Object} [options.globalLexicals]
29
27
  * @param {Array<Transform>} [options.globalTransforms]
30
28
  * @param {bool} [options.sloppyGlobalsMode]
31
29
  * @param {WeakSet} [options.knownScopeProxies]
32
30
  */
33
31
  export const makeSafeEvaluator = ({
34
32
  globalObject,
35
- localObject = {},
33
+ globalLexicals = {},
36
34
  globalTransforms = [],
37
35
  sloppyGlobalsMode = false,
38
36
  knownScopeProxies = new WeakSet(),
39
37
  } = {}) => {
40
- const {
41
- scopeHandler,
42
- admitOneUnsafeEvalNext,
43
- resetOneUnsafeEvalNext,
44
- } = createScopeHandler(globalObject, localObject, {
45
- sloppyGlobalsMode,
46
- });
38
+ const { scopeHandler, scopeController } = createScopeHandler(
39
+ globalObject,
40
+ globalLexicals,
41
+ {
42
+ sloppyGlobalsMode,
43
+ },
44
+ );
47
45
  const { proxy: scopeProxy, revoke: revokeScopeProxy } = proxyRevocable(
48
46
  immutableObject,
49
47
  scopeHandler,
@@ -56,7 +54,7 @@ export const makeSafeEvaluator = ({
56
54
  let evaluate;
57
55
  const makeEvaluate = () => {
58
56
  if (!evaluate) {
59
- const constants = getScopeConstants(globalObject, localObject);
57
+ const constants = getScopeConstants(globalObject, globalLexicals);
60
58
  const evaluateFactory = makeEvaluateFactory(constants);
61
59
  evaluate = apply(evaluateFactory, scopeProxy, []);
62
60
  }
@@ -78,7 +76,7 @@ export const makeSafeEvaluator = ({
78
76
  mandatoryTransforms,
79
77
  ]);
80
78
 
81
- admitOneUnsafeEvalNext();
79
+ scopeController.allowNextEvalToBeUnsafe = true;
82
80
  let err;
83
81
  try {
84
82
  // Ensure that "this" resolves to the safe global.
@@ -88,13 +86,15 @@ export const makeSafeEvaluator = ({
88
86
  err = e;
89
87
  throw e;
90
88
  } finally {
91
- if (resetOneUnsafeEvalNext()) {
89
+ const unsafeEvalWasStillExposed = scopeController.allowNextEvalToBeUnsafe;
90
+ scopeController.allowNextEvalToBeUnsafe = false;
91
+ if (unsafeEvalWasStillExposed) {
92
92
  // Barring a defect in the SES shim, the scope proxy should allow the
93
93
  // powerful, unsafe `eval` to be used by `evaluate` exactly once, as the
94
94
  // very first name that it attempts to access from the lexical scope.
95
- // A defect in the SES shim could throw an exception after our call to
96
- // `admitOneUnsafeEvalNext()` and before `evaluate` calls `eval`
97
- // internally.
95
+ // A defect in the SES shim could throw an exception after we set
96
+ // `scopeController.allowNextEvalToBeUnsafe` and before `evaluate`
97
+ // calls `eval` internally.
98
98
  // If we get here, SES is very broken.
99
99
  // This condition is one where this vat is now hopelessly confused, and
100
100
  // the vat as a whole should be aborted.
@@ -17,6 +17,7 @@ import {
17
17
  keys,
18
18
  mapGet,
19
19
  weakmapGet,
20
+ reflectHas,
20
21
  } from './commons.js';
21
22
  import { compartmentEvaluate } from './compartment-evaluate.js';
22
23
 
@@ -73,22 +74,36 @@ export const makeThirdPartyModuleInstance = (
73
74
  update(value);
74
75
  };
75
76
  });
77
+ // This is enough to support import * from cjs - the '*' field doesn't need to be in exports nor proxiedExports because import will only ever access it via notifiers
78
+ notifiers['*'] = update => {
79
+ update(proxiedExports);
80
+ };
76
81
  }
77
82
 
78
- let activated = false;
83
+ const localState = {
84
+ activated: false,
85
+ };
79
86
  return freeze({
80
87
  notifiers,
81
88
  exportsProxy,
82
89
  execute() {
83
- if (!activated) {
90
+ if (reflectHas(localState, 'errorFromExecute')) {
91
+ throw localState.errorFromExecute;
92
+ }
93
+ if (!localState.activated) {
84
94
  activate();
85
- activated = true;
86
- // eslint-disable-next-line @endo/no-polymorphic-call
87
- staticModuleRecord.execute(
88
- proxiedExports,
89
- compartment,
90
- resolvedImports,
91
- );
95
+ localState.activated = true;
96
+ try {
97
+ // eslint-disable-next-line @endo/no-polymorphic-call
98
+ staticModuleRecord.execute(
99
+ proxiedExports,
100
+ compartment,
101
+ resolvedImports,
102
+ );
103
+ } catch (err) {
104
+ localState.errorFromExecute = err;
105
+ throw err;
106
+ }
92
107
  }
93
108
  },
94
109
  });
@@ -12,6 +12,7 @@ import {
12
12
  objectHasOwnProperty,
13
13
  reflectGet,
14
14
  reflectSet,
15
+ seal,
15
16
  } from './commons.js';
16
17
  import { assert } from './error/assert.js';
17
18
 
@@ -53,23 +54,21 @@ const alwaysThrowHandler = new Proxy(
53
54
  */
54
55
  export const createScopeHandler = (
55
56
  globalObject,
56
- localObject = {},
57
+ globalLexicals = {},
57
58
  { sloppyGlobalsMode = false } = {},
58
59
  ) => {
59
60
  // This flag allow us to determine if the eval() call is an done by the
60
61
  // compartment's code or if it is user-land invocation, so we can react
61
62
  // differently.
62
- let allowNextEvalToBeUnsafe = false;
63
-
64
- const admitOneUnsafeEvalNext = () => {
65
- allowNextEvalToBeUnsafe = true;
66
- };
67
-
68
- const resetOneUnsafeEvalNext = () => {
69
- const wasSet = allowNextEvalToBeUnsafe;
70
- allowNextEvalToBeUnsafe = false;
71
- return wasSet;
63
+ // Using a flag on an object with a single mutable property allows a safe
64
+ // evaluator to signal to the scope proxy without consuming a stack frame.
65
+ // Consuming a stack frame could possibly allow an attacker to control the
66
+ // stack depth before calling `evaluate` to cause a RangeError before this
67
+ // flag can be reset, leaving the unsafe evaluator available.
68
+ const scopeController = {
69
+ allowNextEvalToBeUnsafe: false,
72
70
  };
71
+ seal(scopeController);
73
72
 
74
73
  const scopeProxyHandlerProperties = {
75
74
  get(_shadow, prop) {
@@ -82,20 +81,20 @@ export const createScopeHandler = (
82
81
  // the 'with' context.
83
82
  if (prop === 'eval') {
84
83
  // test that it is true rather than merely truthy
85
- if (allowNextEvalToBeUnsafe === true) {
84
+ if (scopeController.allowNextEvalToBeUnsafe === true) {
86
85
  // revoke before use
87
- allowNextEvalToBeUnsafe = false;
86
+ scopeController.allowNextEvalToBeUnsafe = false;
88
87
  return FERAL_EVAL;
89
88
  }
90
89
  // fall through
91
90
  }
92
91
 
93
- // Properties of the localObject.
94
- if (prop in localObject) {
92
+ // Properties of the globalLexicals.
93
+ if (prop in globalLexicals) {
95
94
  // Use reflect to defeat accessors that could be present on the
96
- // localObject object itself as `this`.
95
+ // globalLexicals object itself as `this`.
97
96
  // This is done out of an overabundance of caution, as the SES shim
98
- // only use the localObject carry globalLexicals and live binding
97
+ // only use the globalLexicals carry globalLexicals and live binding
99
98
  // traps.
100
99
  // The globalLexicals are captured as a snapshot of what's passed to
101
100
  // the Compartment constructor, wherein all accessors and setters are
@@ -104,7 +103,7 @@ export const createScopeHandler = (
104
103
  // make use of their receiver.
105
104
  // Live binding traps provide no avenue for user code to observe the
106
105
  // receiver.
107
- return reflectGet(localObject, prop, globalObject);
106
+ return reflectGet(globalLexicals, prop, globalObject);
108
107
  }
109
108
 
110
109
  // Properties of the global.
@@ -112,17 +111,17 @@ export const createScopeHandler = (
112
111
  },
113
112
 
114
113
  set(_shadow, prop, value) {
115
- // Properties of the localObject.
116
- if (prop in localObject) {
117
- const desc = getOwnPropertyDescriptor(localObject, prop);
114
+ // Properties of the globalLexicals.
115
+ if (prop in globalLexicals) {
116
+ const desc = getOwnPropertyDescriptor(globalLexicals, prop);
118
117
  if (objectHasOwnProperty(desc, 'value')) {
119
118
  // Work around a peculiar behavior in the specs, where
120
119
  // value properties are defined on the receiver.
121
- return reflectSet(localObject, prop, value);
120
+ return reflectSet(globalLexicals, prop, value);
122
121
  }
123
122
  // Ensure that the 'this' value on setters resolves
124
- // to the safeGlobal, not to the localObject object.
125
- return reflectSet(localObject, prop, value, globalObject);
123
+ // to the safeGlobal, not to the globalLexicals object.
124
+ return reflectSet(globalLexicals, prop, value, globalObject);
126
125
  }
127
126
 
128
127
  // Properties of the global.
@@ -165,8 +164,8 @@ export const createScopeHandler = (
165
164
  // !!!!! WARNING: DANGER ZONE !!!!!!
166
165
  return (
167
166
  sloppyGlobalsMode ||
168
- (allowNextEvalToBeUnsafe && prop === 'eval') ||
169
- prop in localObject ||
167
+ (scopeController.allowNextEvalToBeUnsafe && prop === 'eval') ||
168
+ prop in globalLexicals ||
170
169
  prop in globalObject ||
171
170
  prop in globalThis
172
171
  );
@@ -206,8 +205,7 @@ export const createScopeHandler = (
206
205
  );
207
206
 
208
207
  return {
209
- admitOneUnsafeEvalNext,
210
- resetOneUnsafeEvalNext,
208
+ scopeController,
211
209
  scopeHandler,
212
210
  };
213
211
  };
@@ -45,35 +45,22 @@
45
45
 
46
46
  import { whitelist, FunctionInstance, isAccessorPermit } from './whitelist.js';
47
47
  import {
48
+ Map,
48
49
  String,
49
50
  TypeError,
51
+ arrayFilter,
50
52
  arrayIncludes,
53
+ arrayMap,
54
+ entries,
51
55
  getOwnPropertyDescriptor,
52
56
  getPrototypeOf,
53
57
  isObject,
58
+ mapGet,
54
59
  objectHasOwnProperty,
55
60
  ownKeys,
56
- stringSlice,
61
+ symbolKeyFor,
57
62
  } from './commons.js';
58
63
 
59
- /**
60
- * asStringPropertyName()
61
- *
62
- * @param {string} path
63
- * @param {string | symbol} prop
64
- */
65
- function asStringPropertyName(path, prop) {
66
- if (typeof prop === 'string') {
67
- return prop;
68
- }
69
-
70
- if (typeof prop === 'symbol') {
71
- return `@@${stringSlice(String(prop), 14, -1)}`;
72
- }
73
-
74
- throw new TypeError(`Unexpected property name type ${path} ${prop}`);
75
- }
76
-
77
64
  /**
78
65
  * whitelistIntrinsics()
79
66
  * Removes all non-allowed properties found by recursively and
@@ -86,9 +73,53 @@ export default function whitelistIntrinsics(
86
73
  intrinsics,
87
74
  markVirtualizedNativeFunction,
88
75
  ) {
89
- // These primities are allowed allowed for permits.
76
+ // These primitives are allowed allowed for permits.
90
77
  const primitives = ['undefined', 'boolean', 'number', 'string', 'symbol'];
91
78
 
79
+ // These symbols are allowed as well-known symbols
80
+ const wellKnownSymbolNames = new Map(
81
+ intrinsics.Symbol
82
+ ? arrayMap(
83
+ arrayFilter(
84
+ entries(whitelist.Symbol),
85
+ ([name, permit]) =>
86
+ permit === 'symbol' &&
87
+ typeof intrinsics.Symbol[name] === 'symbol',
88
+ ),
89
+ ([name]) => [intrinsics.Symbol[name], `@@${name}`],
90
+ )
91
+ : [],
92
+ );
93
+
94
+ /**
95
+ * asStringPropertyName()
96
+ *
97
+ * @param {string} path
98
+ * @param {string | symbol} prop
99
+ */
100
+ function asStringPropertyName(path, prop) {
101
+ if (typeof prop === 'string') {
102
+ return prop;
103
+ }
104
+
105
+ const wellKnownSymbol = mapGet(wellKnownSymbolNames, prop);
106
+
107
+ if (typeof prop === 'symbol') {
108
+ if (wellKnownSymbol) {
109
+ return wellKnownSymbol;
110
+ } else {
111
+ const registeredKey = symbolKeyFor(prop);
112
+ if (registeredKey !== undefined) {
113
+ return `RegisteredSymbol(${registeredKey})`;
114
+ } else {
115
+ return `Unique${String(prop)}`;
116
+ }
117
+ }
118
+ }
119
+
120
+ throw new TypeError(`Unexpected property name type ${path} ${prop}`);
121
+ }
122
+
92
123
  /*
93
124
  * visitPrototype()
94
125
  * Validate the object's [[prototype]] against a permit.
package/src/whitelist.js CHANGED
@@ -224,7 +224,10 @@ export const NativeErrors = [
224
224
  * property definition.
225
225
  * <li>"ObjectPrototype" is the default "[[Proto]]" (when not specified).
226
226
  * <li>Constants "fn" and "getter" are used to keep the structure DRY.
227
- * <li>Symbol properties are listed using the "@@name" form.
227
+ * <li>Symbol properties are listed as follow:
228
+ * <li>Well-known symbols use the "@@name" form.
229
+ * <li>Registered symbols use the "RegisteredSymbol(key)" form.
230
+ * <li>Unique symbols use the "UniqueSymbol(description)" form.
228
231
  */
229
232
 
230
233
  // Function Instances
@@ -418,6 +421,7 @@ export const whitelist = {
418
421
  getOwnPropertyNames: fn,
419
422
  getOwnPropertySymbols: fn,
420
423
  getPrototypeOf: fn,
424
+ hasOwn: fn,
421
425
  is: fn,
422
426
  isExtensible: fn,
423
427
  isFrozen: fn,
@@ -428,8 +432,6 @@ export const whitelist = {
428
432
  seal: fn,
429
433
  setPrototypeOf: fn,
430
434
  values: fn,
431
- // See https://github.com/tc39/proposal-accessible-object-hasownproperty
432
- hasOwn: fn,
433
435
  },
434
436
 
435
437
  '%ObjectPrototype%': {
@@ -724,6 +726,7 @@ export const whitelist = {
724
726
  '%StringPrototype%': {
725
727
  // Properties of the String Prototype Object
726
728
  length: 'number',
729
+ at: fn,
727
730
  charAt: fn,
728
731
  charCodeAt: fn,
729
732
  codePointAt: fn,
@@ -777,10 +780,6 @@ export const whitelist = {
777
780
  trimRight: fn,
778
781
  // See https://github.com/Moddable-OpenSource/moddable/issues/523
779
782
  compare: false,
780
-
781
- // Stage 3:
782
- // https://tc39.es/proposal-relative-indexing-method/
783
- at: fn,
784
783
  },
785
784
 
786
785
  '%StringIteratorPrototype%': {
@@ -877,6 +876,7 @@ export const whitelist = {
877
876
 
878
877
  '%ArrayPrototype%': {
879
878
  // Properties of the Array Prototype Object
879
+ at: fn,
880
880
  length: 'number',
881
881
  concat: fn,
882
882
  constructor: 'Array',
@@ -926,10 +926,10 @@ export const whitelist = {
926
926
  // Failed tc39 proposal
927
927
  // Seen on FF Nightly 88.0a1
928
928
  at: false,
929
+ // See https://github.com/tc39/proposal-array-find-from-last
930
+ findLast: 'boolean',
931
+ findLastIndex: 'boolean',
929
932
  },
930
- // Failed tc39 proposal
931
- // Seen on FF Nightly 88.0a1
932
- at: false,
933
933
  // See https://github.com/tc39/proposal-array-find-from-last
934
934
  findLast: fn,
935
935
  findLastIndex: fn,
@@ -954,6 +954,7 @@ export const whitelist = {
954
954
  },
955
955
 
956
956
  '%TypedArrayPrototype%': {
957
+ at: fn,
957
958
  buffer: getter,
958
959
  byteLength: getter,
959
960
  byteOffset: getter,
@@ -986,9 +987,6 @@ export const whitelist = {
986
987
  values: fn,
987
988
  '@@iterator': fn,
988
989
  '@@toStringTag': getter,
989
- // Failed tc39 proposal
990
- // Seen on FF Nightly 88.0a1
991
- at: false,
992
990
  // See https://github.com/tc39/proposal-array-find-from-last
993
991
  findLast: fn,
994
992
  findLastIndex: fn,
@@ -1289,6 +1287,10 @@ export const whitelist = {
1289
1287
  finally: fn,
1290
1288
  then: fn,
1291
1289
  '@@toStringTag': 'string',
1290
+ // Non-standard, used in node to prevent async_hooks from breaking
1291
+ 'UniqueSymbol(async_id_symbol)': accessor,
1292
+ 'UniqueSymbol(trigger_async_id_symbol)': accessor,
1293
+ 'UniqueSymbol(destroyed)': accessor,
1292
1294
  },
1293
1295
 
1294
1296
  '%InertAsyncFunction%': {