ses 1.4.1 → 1.6.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.
@@ -25,7 +25,6 @@ import {
25
25
  Set,
26
26
  String,
27
27
  TypeError,
28
- WeakMap,
29
28
  WeakSet,
30
29
  globalThis,
31
30
  apply,
@@ -45,10 +44,11 @@ import {
45
44
  setHas,
46
45
  toStringTagSymbol,
47
46
  typedArrayPrototype,
48
- weakmapGet,
49
- weakmapSet,
50
47
  weaksetAdd,
51
48
  weaksetHas,
49
+ FERAL_STACK_GETTER,
50
+ FERAL_STACK_SETTER,
51
+ isError,
52
52
  } from './commons.js';
53
53
  import { assert } from './error/assert.js';
54
54
 
@@ -144,15 +144,13 @@ export const makeHardener = () => {
144
144
  */
145
145
  harden(root) {
146
146
  const toFreeze = new Set();
147
- const paths = new WeakMap();
148
147
 
149
148
  // If val is something we should be freezing but aren't yet,
150
149
  // add it to toFreeze.
151
150
  /**
152
151
  * @param {any} val
153
- * @param {string} [path]
154
152
  */
155
- function enqueue(val, path = undefined) {
153
+ function enqueue(val) {
156
154
  if (!isObject(val)) {
157
155
  // ignore primitives
158
156
  return;
@@ -168,13 +166,12 @@ export const makeHardener = () => {
168
166
  }
169
167
  // console.warn(`adding ${val} to toFreeze`, val);
170
168
  setAdd(toFreeze, val);
171
- weakmapSet(paths, val, path);
172
169
  }
173
170
 
174
171
  /**
175
172
  * @param {any} obj
176
173
  */
177
- function freezeAndTraverse(obj) {
174
+ const baseFreezeAndTraverse = obj => {
178
175
  // Now freeze the object to ensure reactive
179
176
  // objects such as proxies won't add properties
180
177
  // during traversal, before they get frozen.
@@ -193,13 +190,11 @@ export const makeHardener = () => {
193
190
 
194
191
  // get stable/immutable outbound links before a Proxy has a chance to do
195
192
  // something sneaky.
196
- const path = weakmapGet(paths, obj) || 'unknown';
197
193
  const descs = getOwnPropertyDescriptors(obj);
198
194
  const proto = getPrototypeOf(obj);
199
- enqueue(proto, `${path}.__proto__`);
195
+ enqueue(proto);
200
196
 
201
197
  arrayForEach(ownKeys(descs), (/** @type {string | symbol} */ name) => {
202
- const pathname = `${path}.${String(name)}`;
203
198
  // The 'name' may be a symbol, and TypeScript doesn't like us to
204
199
  // index arbitrary symbols on objects, so we pretend they're just
205
200
  // strings.
@@ -212,27 +207,60 @@ export const makeHardener = () => {
212
207
  // whether 'value' is present or not, which tells us for sure that
213
208
  // this is a data property.
214
209
  if (objectHasOwnProperty(desc, 'value')) {
215
- enqueue(desc.value, `${pathname}`);
210
+ enqueue(desc.value);
216
211
  } else {
217
- enqueue(desc.get, `${pathname}(get)`);
218
- enqueue(desc.set, `${pathname}(set)`);
212
+ enqueue(desc.get);
213
+ enqueue(desc.set);
219
214
  }
220
215
  });
221
- }
216
+ };
217
+
218
+ const freezeAndTraverse =
219
+ FERAL_STACK_GETTER === undefined && FERAL_STACK_SETTER === undefined
220
+ ? // On platforms without v8's error own stack accessor problem,
221
+ // don't pay for any extra overhead.
222
+ baseFreezeAndTraverse
223
+ : obj => {
224
+ if (isError(obj)) {
225
+ // Only pay the overhead if it first passes this cheap isError
226
+ // check. Otherwise, it will be unrepaired, but won't be judged
227
+ // to be a passable error anyway, so will not be unsafe.
228
+ const stackDesc = getOwnPropertyDescriptor(obj, 'stack');
229
+ if (
230
+ stackDesc &&
231
+ stackDesc.get === FERAL_STACK_GETTER &&
232
+ stackDesc.configurable
233
+ ) {
234
+ // Can only repair if it is configurable. Otherwise, leave
235
+ // unrepaired, in which case it will not be judged passable,
236
+ // avoiding a safety problem.
237
+ defineProperty(obj, 'stack', {
238
+ // NOTE: Calls getter during harden, which seems dangerous.
239
+ // But we're only calling the problematic getter whose
240
+ // hazards we think we understand.
241
+ // @ts-expect-error TS should know FERAL_STACK_GETTER
242
+ // cannot be `undefined` here.
243
+ // See https://github.com/endojs/endo/pull/2232#discussion_r1575179471
244
+ value: apply(FERAL_STACK_GETTER, obj, []),
245
+ });
246
+ }
247
+ }
248
+ return baseFreezeAndTraverse(obj);
249
+ };
222
250
 
223
- function dequeue() {
251
+ const dequeue = () => {
224
252
  // New values added before forEach() has finished will be visited.
225
253
  setForEach(toFreeze, freezeAndTraverse);
226
- }
254
+ };
227
255
 
228
256
  /** @param {any} value */
229
- function markHardened(value) {
257
+ const markHardened = value => {
230
258
  weaksetAdd(hardened, value);
231
- }
259
+ };
232
260
 
233
- function commit() {
261
+ const commit = () => {
234
262
  setForEach(toFreeze, markHardened);
235
- }
263
+ };
236
264
 
237
265
  enqueue(root);
238
266
  dequeue();
@@ -24,9 +24,9 @@ import { compartmentEvaluate } from './compartment-evaluate.js';
24
24
 
25
25
  const { quote: q } = assert;
26
26
 
27
- export const makeThirdPartyModuleInstance = (
27
+ export const makeVirtualModuleInstance = (
28
28
  compartmentPrivateFields,
29
- staticModuleRecord,
29
+ moduleSource,
30
30
  compartment,
31
31
  moduleAliases,
32
32
  moduleSpecifier,
@@ -41,16 +41,16 @@ export const makeThirdPartyModuleInstance = (
41
41
 
42
42
  const notifiers = create(null);
43
43
 
44
- if (staticModuleRecord.exports) {
44
+ if (moduleSource.exports) {
45
45
  if (
46
- !isArray(staticModuleRecord.exports) ||
47
- arraySome(staticModuleRecord.exports, name => typeof name !== 'string')
46
+ !isArray(moduleSource.exports) ||
47
+ arraySome(moduleSource.exports, name => typeof name !== 'string')
48
48
  ) {
49
49
  throw TypeError(
50
- `SES third-party static module record "exports" property must be an array of strings for module ${moduleSpecifier}`,
50
+ `SES virtual module source "exports" property must be an array of strings for module ${moduleSpecifier}`,
51
51
  );
52
52
  }
53
- arrayForEach(staticModuleRecord.exports, name => {
53
+ arrayForEach(moduleSource.exports, name => {
54
54
  let value = exportsTarget[name];
55
55
  const updaters = [];
56
56
 
@@ -96,11 +96,7 @@ export const makeThirdPartyModuleInstance = (
96
96
  localState.activated = true;
97
97
  try {
98
98
  // eslint-disable-next-line @endo/no-polymorphic-call
99
- staticModuleRecord.execute(
100
- exportsTarget,
101
- compartment,
102
- resolvedImports,
103
- );
99
+ moduleSource.execute(exportsTarget, compartment, resolvedImports);
104
100
  } catch (err) {
105
101
  localState.errorFromExecute = err;
106
102
  throw err;
@@ -126,7 +122,7 @@ export const makeModuleInstance = (
126
122
  const {
127
123
  compartment,
128
124
  moduleSpecifier,
129
- staticModuleRecord,
125
+ moduleSource,
130
126
  importMeta: moduleRecordMeta,
131
127
  } = moduleRecord;
132
128
  const {
@@ -137,7 +133,7 @@ export const makeModuleInstance = (
137
133
  __reexportMap__: reexportMap = {},
138
134
  __needsImportMeta__: needsImportMeta = false,
139
135
  __syncModuleFunctor__,
140
- } = staticModuleRecord;
136
+ } = moduleSource;
141
137
 
142
138
  const compartmentFields = weakmapGet(privateFields, compartment);
143
139
 
@@ -1,17 +1,9 @@
1
1
  /* eslint-disable no-underscore-dangle */
2
2
 
3
- // For brevity, in this file, as in module-load.js, the term "moduleRecord"
4
- // without qualification means "module compartment record".
5
- // This is a super-set of the "static module record", that is reusable between
6
- // compartments with different hooks.
7
- // The "module compartment record" captures the compartment and overlays the
8
- // module's "imports" with the more specific "resolvedImports" as inferred from
9
- // the particular compartment's "resolveHook".
10
-
11
3
  import { assert } from './error/assert.js';
12
4
  import {
13
5
  makeModuleInstance,
14
- makeThirdPartyModuleInstance,
6
+ makeVirtualModuleInstance,
15
7
  } from './module-instance.js';
16
8
  import {
17
9
  Map,
@@ -61,56 +53,50 @@ export const link = (
61
53
  return instantiate(compartmentPrivateFields, moduleAliases, moduleRecord);
62
54
  };
63
55
 
64
- function isPrecompiled(staticModuleRecord) {
65
- return typeof staticModuleRecord.__syncModuleProgram__ === 'string';
56
+ function mayBePrecompiledModuleSource(moduleSource) {
57
+ return typeof moduleSource.__syncModuleProgram__ === 'string';
66
58
  }
67
59
 
68
- function validatePrecompiledStaticModuleRecord(
69
- staticModuleRecord,
70
- moduleSpecifier,
71
- ) {
72
- const { __fixedExportMap__, __liveExportMap__ } = staticModuleRecord;
60
+ function validatePrecompiledModuleSource(moduleSource, moduleSpecifier) {
61
+ const { __fixedExportMap__, __liveExportMap__ } = moduleSource;
73
62
  isObject(__fixedExportMap__) ||
74
- Fail`Property '__fixedExportMap__' of a precompiled module record must be an object, got ${q(
63
+ Fail`Property '__fixedExportMap__' of a precompiled module source must be an object, got ${q(
75
64
  __fixedExportMap__,
76
65
  )}, for module ${q(moduleSpecifier)}`;
77
66
  isObject(__liveExportMap__) ||
78
- Fail`Property '__liveExportMap__' of a precompiled module record must be an object, got ${q(
67
+ Fail`Property '__liveExportMap__' of a precompiled module source must be an object, got ${q(
79
68
  __liveExportMap__,
80
69
  )}, for module ${q(moduleSpecifier)}`;
81
70
  }
82
71
 
83
- function isThirdParty(staticModuleRecord) {
84
- return typeof staticModuleRecord.execute === 'function';
72
+ function mayBeVirtualModuleSource(moduleSource) {
73
+ return typeof moduleSource.execute === 'function';
85
74
  }
86
75
 
87
- function validateThirdPartyStaticModuleRecord(
88
- staticModuleRecord,
89
- moduleSpecifier,
90
- ) {
91
- const { exports } = staticModuleRecord;
76
+ function validateVirtualModuleSource(moduleSource, moduleSpecifier) {
77
+ const { exports } = moduleSource;
92
78
  isArray(exports) ||
93
- Fail`Property 'exports' of a third-party static module record must be an array, got ${q(
79
+ Fail`Property 'exports' of a third-party module source must be an array, got ${q(
94
80
  exports,
95
81
  )}, for module ${q(moduleSpecifier)}`;
96
82
  }
97
83
 
98
- function validateStaticModuleRecord(staticModuleRecord, moduleSpecifier) {
99
- isObject(staticModuleRecord) ||
100
- Fail`Static module records must be of type object, got ${q(
101
- staticModuleRecord,
84
+ function validateModuleSource(moduleSource, moduleSpecifier) {
85
+ isObject(moduleSource) ||
86
+ Fail`Module sources must be of type object, got ${q(
87
+ moduleSource,
102
88
  )}, for module ${q(moduleSpecifier)}`;
103
- const { imports, exports, reexports = [] } = staticModuleRecord;
89
+ const { imports, exports, reexports = [] } = moduleSource;
104
90
  isArray(imports) ||
105
- Fail`Property 'imports' of a static module record must be an array, got ${q(
91
+ Fail`Property 'imports' of a module source must be an array, got ${q(
106
92
  imports,
107
93
  )}, for module ${q(moduleSpecifier)}`;
108
94
  isArray(exports) ||
109
- Fail`Property 'exports' of a precompiled module record must be an array, got ${q(
95
+ Fail`Property 'exports' of a precompiled module source must be an array, got ${q(
110
96
  exports,
111
97
  )}, for module ${q(moduleSpecifier)}`;
112
98
  isArray(reexports) ||
113
- Fail`Property 'reexports' of a precompiled module record must be an array if present, got ${q(
99
+ Fail`Property 'reexports' of a precompiled module source must be an array if present, got ${q(
114
100
  reexports,
115
101
  )}, for module ${q(moduleSpecifier)}`;
116
102
  }
@@ -120,7 +106,7 @@ export const instantiate = (
120
106
  moduleAliases,
121
107
  moduleRecord,
122
108
  ) => {
123
- const { compartment, moduleSpecifier, resolvedImports, staticModuleRecord } =
109
+ const { compartment, moduleSpecifier, resolvedImports, moduleSource } =
124
110
  moduleRecord;
125
111
  const { instances } = weakmapGet(compartmentPrivateFields, compartment);
126
112
 
@@ -129,23 +115,23 @@ export const instantiate = (
129
115
  return mapGet(instances, moduleSpecifier);
130
116
  }
131
117
 
132
- validateStaticModuleRecord(staticModuleRecord, moduleSpecifier);
118
+ validateModuleSource(moduleSource, moduleSpecifier);
133
119
 
134
120
  const importedInstances = new Map();
135
121
  let moduleInstance;
136
- if (isPrecompiled(staticModuleRecord)) {
137
- validatePrecompiledStaticModuleRecord(staticModuleRecord, moduleSpecifier);
122
+ if (mayBePrecompiledModuleSource(moduleSource)) {
123
+ validatePrecompiledModuleSource(moduleSource, moduleSpecifier);
138
124
  moduleInstance = makeModuleInstance(
139
125
  compartmentPrivateFields,
140
126
  moduleAliases,
141
127
  moduleRecord,
142
128
  importedInstances,
143
129
  );
144
- } else if (isThirdParty(staticModuleRecord)) {
145
- validateThirdPartyStaticModuleRecord(staticModuleRecord, moduleSpecifier);
146
- moduleInstance = makeThirdPartyModuleInstance(
130
+ } else if (mayBeVirtualModuleSource(moduleSource)) {
131
+ validateVirtualModuleSource(moduleSource, moduleSpecifier);
132
+ moduleInstance = makeVirtualModuleInstance(
147
133
  compartmentPrivateFields,
148
- staticModuleRecord,
134
+ moduleSource,
149
135
  compartment,
150
136
  moduleAliases,
151
137
  moduleSpecifier,
@@ -153,9 +139,7 @@ export const instantiate = (
153
139
  );
154
140
  } else {
155
141
  throw TypeError(
156
- `importHook must return a static module record, got ${q(
157
- staticModuleRecord,
158
- )}`,
142
+ `importHook must provide a module source, got ${q(moduleSource)}`,
159
143
  );
160
144
  }
161
145