ses 1.8.0 → 1.9.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.
@@ -0,0 +1 @@
1
+ import './src/lockdown-shim.js';
package/lockdown.js ADDED
@@ -0,0 +1 @@
1
+ import './index.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ses",
3
- "version": "1.8.0",
3
+ "version": "1.9.0",
4
4
  "description": "Hardened JavaScript for Fearless Cooperation",
5
5
  "keywords": [
6
6
  "lockdown",
@@ -59,6 +59,7 @@
59
59
  "./assert-shim.js": "./assert-shim.js",
60
60
  "./lockdown-shim.js": "./lockdown-shim.js",
61
61
  "./compartment-shim.js": "./compartment-shim.js",
62
+ "./console-shim.js": "./console-shim.js",
62
63
  "./package.json": "./package.json"
63
64
  },
64
65
  "scripts": {
@@ -72,15 +73,17 @@
72
73
  "lint:types": "tsc",
73
74
  "prepare": "npm run clean && npm run build",
74
75
  "qt": "ava",
75
- "test": "tsd && ava"
76
+ "test": "tsd && ava",
77
+ "test:xs": "xst dist/ses.umd.js test/_lockdown-safe.js",
78
+ "postpack": "git clean -f '*.d.ts*' '*.tsbuildinfo'"
76
79
  },
77
80
  "dependencies": {
78
- "@endo/env-options": "^1.1.6"
81
+ "@endo/env-options": "^1.1.7"
79
82
  },
80
83
  "devDependencies": {
81
- "@endo/compartment-mapper": "^1.2.2",
82
- "@endo/module-source": "^1.0.2",
83
- "@endo/test262-runner": "^0.1.40",
84
+ "@endo/compartment-mapper": "^1.3.0",
85
+ "@endo/module-source": "^1.1.0",
86
+ "@endo/test262-runner": "^0.1.41",
84
87
  "ava": "^6.1.3",
85
88
  "babel-eslint": "^10.1.0",
86
89
  "c8": "^7.14.0",
@@ -91,20 +94,20 @@
91
94
  "eslint-plugin-eslint-comments": "^3.2.0",
92
95
  "eslint-plugin-import": "^2.29.1",
93
96
  "prettier": "^3.2.5",
94
- "sinon": "^15.1.0",
95
97
  "terser": "^5.16.6",
96
- "tsd": "^0.30.7",
97
- "typescript": "~5.6.1-rc"
98
+ "tsd": "^0.31.2",
99
+ "typescript": "~5.6.2"
98
100
  },
99
101
  "files": [
102
+ "./*.d.ts",
103
+ "./*.js",
104
+ "./*.map",
100
105
  "LICENSE*",
101
- "NEWS*",
102
106
  "SECURITY*",
103
107
  "dist",
104
- "types.d.ts",
105
- "index.js",
106
- "tools.js",
107
- "src"
108
+ "lib",
109
+ "src",
110
+ "tools"
108
111
  ],
109
112
  "publishConfig": {
110
113
  "access": "public"
@@ -116,6 +119,7 @@
116
119
  "rules": {
117
120
  "no-restricted-globals": [
118
121
  "error",
122
+ "AggregateError",
119
123
  "Array",
120
124
  "ArrayBuffer",
121
125
  "Atomics",
@@ -123,6 +127,7 @@
123
127
  "BigInt64Array",
124
128
  "BigUint64Array",
125
129
  "Boolean",
130
+ "Compartment",
126
131
  "DataView",
127
132
  "Date",
128
133
  "Error",
@@ -158,6 +163,7 @@
158
163
  "Uint8ClampedArray",
159
164
  "WeakMap",
160
165
  "WeakSet",
166
+ "assert",
161
167
  "decodeURI",
162
168
  "decodeURIComponent",
163
169
  "encodeURI",
@@ -167,10 +173,10 @@
167
173
  "globalThis",
168
174
  "isFinite",
169
175
  "isNaN",
176
+ "lockdown",
170
177
  "parseFloat",
171
178
  "parseInt",
172
- "unescape",
173
- "AggregateError"
179
+ "unescape"
174
180
  ],
175
181
  "@endo/no-polymorphic-call": "error"
176
182
  },
@@ -190,7 +196,6 @@
190
196
  },
191
197
  "ava": {
192
198
  "files": [
193
- "test/**/test-*.*",
194
199
  "test/**/*.test.*"
195
200
  ],
196
201
  "timeout": "2m"
@@ -198,5 +203,5 @@
198
203
  "typeCoverage": {
199
204
  "atLeast": 81.17
200
205
  },
201
- "gitHead": "cb060b7e22c92cc951fb03d86cdbceeae82fec34"
206
+ "gitHead": "bbf20fae6726f7d26456714b8852572787d34e05"
202
207
  }
package/src/commons.js CHANGED
@@ -19,6 +19,7 @@ export { universalThis as globalThis };
19
19
 
20
20
  export const {
21
21
  Array,
22
+ ArrayBuffer,
22
23
  Date,
23
24
  FinalizationRegistry,
24
25
  Float32Array,
@@ -34,6 +35,7 @@ export const {
34
35
  Set,
35
36
  String,
36
37
  Symbol,
38
+ Uint8Array,
37
39
  WeakMap,
38
40
  WeakSet,
39
41
  } = globalThis;
@@ -124,6 +126,7 @@ export const {
124
126
  } = Reflect;
125
127
 
126
128
  export const { isArray, prototype: arrayPrototype } = Array;
129
+ export const { prototype: arrayBufferPrototype } = ArrayBuffer;
127
130
  export const { prototype: mapPrototype } = Map;
128
131
  export const { revocable: proxyRevocable } = Proxy;
129
132
  export const { prototype: regexpPrototype } = RegExp;
@@ -178,6 +181,15 @@ export const arraySome = uncurryThis(arrayPrototype.some);
178
181
  export const arraySort = uncurryThis(arrayPrototype.sort);
179
182
  export const iterateArray = uncurryThis(arrayPrototype[iteratorSymbol]);
180
183
  //
184
+ export const arrayBufferSlice = uncurryThis(arrayBufferPrototype.slice);
185
+ /** @type {(b: ArrayBuffer) => number} */
186
+ export const arrayBufferGetByteLength = uncurryThis(
187
+ // @ts-expect-error we know it is there on all conforming platforms
188
+ getOwnPropertyDescriptor(arrayBufferPrototype, 'byteLength').get,
189
+ );
190
+ //
191
+ export const typedArraySet = uncurryThis(typedArrayPrototype.set);
192
+ //
181
193
  export const mapSet = uncurryThis(mapPrototype.set);
182
194
  export const mapGet = uncurryThis(mapPrototype.get);
183
195
  export const mapHas = uncurryThis(mapPrototype.has);
@@ -19,7 +19,7 @@ import {
19
19
  setGlobalObjectMutableProperties,
20
20
  setGlobalObjectEvaluators,
21
21
  } from './global-object.js';
22
- import { assertEqual } from './error/assert.js';
22
+ import { assert, assertEqual } from './error/assert.js';
23
23
  import { sharedGlobalPropertyNames } from './permits.js';
24
24
  import { load, loadNow } from './module-load.js';
25
25
  import { link } from './module-link.js';
@@ -368,8 +368,10 @@ export const makeCausalConsole = (baseConsole, loggedErrorHandler) => {
368
368
  const levelMethod = (...logArgs) => {
369
369
  const subErrors = [];
370
370
  const argTags = extractErrorArgs(logArgs, subErrors);
371
- // eslint-disable-next-line @endo/no-polymorphic-call
372
- baseConsole[level](...argTags);
371
+ if (baseConsole[level]) {
372
+ // eslint-disable-next-line @endo/no-polymorphic-call
373
+ baseConsole[level](...argTags);
374
+ }
373
375
  // @ts-expect-error ConsoleProp vs LogSeverity mismatch
374
376
  logSubErrors(level, subErrors);
375
377
  };
@@ -476,12 +478,16 @@ export const defineCausalConsoleFromLogger = loggedErrorHandler => {
476
478
  // the host console will separate them with additional spaces.
477
479
  arrayPush(indents, ' ');
478
480
  });
481
+ } else {
482
+ baseConsole[name] = () => {};
479
483
  }
480
484
  }
481
485
  if (baseConsole.groupEnd) {
482
486
  baseConsole.groupEnd = makeNamed('groupEnd', (...args) => {
483
487
  arrayPop(indents);
484
488
  });
489
+ } else {
490
+ baseConsole.groupEnd = () => {};
485
491
  }
486
492
  harden(baseConsole);
487
493
  const causalConsole = makeCausalConsole(
package/src/lockdown.js CHANGED
@@ -46,6 +46,7 @@ import { makeSafeEvaluator } from './make-safe-evaluator.js';
46
46
  import { initialGlobalPropertyNames } from './permits.js';
47
47
  import { tameFunctionToString } from './tame-function-tostring.js';
48
48
  import { tameDomains } from './tame-domains.js';
49
+ import { tameModuleSource } from './tame-module-source.js';
49
50
 
50
51
  import { tameConsole } from './error/tame-console.js';
51
52
  import tameErrorConstructor from './error/tame-error-constructor.js';
@@ -56,6 +57,7 @@ import { tameHarden } from './tame-harden.js';
56
57
  import { tameSymbolConstructor } from './tame-symbol-constructor.js';
57
58
  import { tameFauxDataProperties } from './tame-faux-data-properties.js';
58
59
  import { tameRegeneratorRuntime } from './tame-regenerator-runtime.js';
60
+ import { shimArrayBufferTransfer } from './shim-arraybuffer-transfer.js';
59
61
 
60
62
  /** @import {LockdownOptions} from '../types.js' */
61
63
 
@@ -284,6 +286,8 @@ export const repairIntrinsics = (options = {}) => {
284
286
  addIntrinsics(tameMathObject(mathTaming));
285
287
  addIntrinsics(tameRegExpConstructor(regExpTaming));
286
288
  addIntrinsics(tameSymbolConstructor());
289
+ addIntrinsics(shimArrayBufferTransfer());
290
+ addIntrinsics(tameModuleSource());
287
291
 
288
292
  addIntrinsics(getAnonymousIntrinsics());
289
293
 
@@ -156,8 +156,10 @@ export default function whitelistIntrinsics(
156
156
  return;
157
157
  }
158
158
 
159
- // We can't clean [[prototype]], therefore abort.
160
- throw TypeError(`Unexpected intrinsic ${path}.__proto__ at ${protoName}`);
159
+ // We can't clean [[Prototype]], therefore abort.
160
+ throw TypeError(
161
+ `Unexpected [[Prototype]] at ${path}.__proto__ (expected ${protoName || '%ObjectPrototype%'})`,
162
+ );
161
163
  }
162
164
 
163
165
  /*
@@ -212,7 +214,9 @@ export default function whitelistIntrinsics(
212
214
  }
213
215
  }
214
216
 
215
- throw TypeError(`Unexpected whitelist permit ${permit} at ${path}`);
217
+ throw TypeError(
218
+ `Unexpected property ${prop} with permit ${permit} at ${path}`,
219
+ );
216
220
  }
217
221
 
218
222
  /*
package/src/permits.js CHANGED
@@ -101,8 +101,12 @@ export const universalPropertyNames = {
101
101
 
102
102
  // ESNext
103
103
 
104
+ // https://github.com/tc39/proposal-source-phase-imports?tab=readme-ov-file#js-module-source
105
+ ModuleSource: 'ModuleSource',
106
+
104
107
  lockdown: 'lockdown',
105
108
  harden: 'harden',
109
+
106
110
  HandledPromise: 'HandledPromise', // TODO: Until Promise.delegate (see below).
107
111
  };
108
112
 
@@ -1505,6 +1509,32 @@ export const permitted = {
1505
1509
  resolve: fn,
1506
1510
  },
1507
1511
 
1512
+ // https://github.com/tc39/proposal-source-phase-imports?tab=readme-ov-file#js-module-source
1513
+
1514
+ ModuleSource: {
1515
+ '[[Proto]]': '%AbstractModuleSource%',
1516
+ prototype: '%ModuleSourcePrototype%',
1517
+ },
1518
+
1519
+ '%ModuleSourcePrototype%': {
1520
+ '[[Proto]]': '%AbstractModuleSourcePrototype%',
1521
+ constructor: 'ModuleSource',
1522
+ '@@toStringTag': 'string',
1523
+ // https://github.com/tc39/proposal-compartments
1524
+ bindings: getter,
1525
+ needsImport: getter,
1526
+ needsImportMeta: getter,
1527
+ },
1528
+
1529
+ '%AbstractModuleSource%': {
1530
+ '[[Proto]]': '%FunctionPrototype%',
1531
+ prototype: '%AbstractModuleSourcePrototype%',
1532
+ },
1533
+
1534
+ '%AbstractModuleSourcePrototype%': {
1535
+ constructor: '%AbstractModuleSource%',
1536
+ },
1537
+
1508
1538
  Promise: {
1509
1539
  // Properties of the Promise Constructor
1510
1540
  '[[Proto]]': '%FunctionPrototype%',
@@ -0,0 +1,86 @@
1
+ import {
2
+ ArrayBuffer,
3
+ arrayBufferPrototype,
4
+ arrayBufferSlice,
5
+ arrayBufferGetByteLength,
6
+ Uint8Array,
7
+ typedArraySet,
8
+ globalThis,
9
+ TypeError,
10
+ defineProperty,
11
+ } from './commons.js';
12
+
13
+ export const shimArrayBufferTransfer = () => {
14
+ // @ts-expect-error TODO extend ArrayBuffer type to include transfer, etc.
15
+ if (typeof arrayBufferPrototype.transfer === 'function') {
16
+ // Assume already exists so does not need to be shimmed.
17
+ // Such conditional shimming is ok in this case since ArrayBuffer.p.transfer
18
+ // is already officially part of JS.
19
+ //
20
+ // Empty object because this shim has nothing for `addIntrinsics` to add.
21
+ return {};
22
+ }
23
+ const clone = globalThis.structuredClone;
24
+ if (typeof clone !== 'function') {
25
+ // On a platform with neither `Array.prototype.transfer`
26
+ // nor `structuredClone`, this shim does nothing.
27
+ // For example, Node <= 16 has neither.
28
+ //
29
+ // Empty object because this shim has nothing for `addIntrinsics` to add.
30
+ return {};
31
+ // TODO Rather than doing nothing, should the endo ses-shim throw
32
+ // in this case?
33
+ // throw TypeError(
34
+ // `Can only shim missing ArrayBuffer.prototype.transfer on a platform with "structuredClone"`,
35
+ // );
36
+ // For example, endo no longer supports Node <= 16. All browsers have
37
+ // `structuredClone`. XS has `Array.prototype.transfer`. Are there still
38
+ // any platforms without both that Endo should still support?
39
+ // What about Hermes?
40
+ }
41
+
42
+ /**
43
+ * @type {ThisType<ArrayBuffer>}
44
+ */
45
+ const methods = {
46
+ /**
47
+ * @param {number} [newLength]
48
+ */
49
+ transfer(newLength = undefined) {
50
+ // Using this builtin getter also ensures that `this` is a genuine
51
+ // ArrayBuffer.
52
+ const oldLength = arrayBufferGetByteLength(this);
53
+ if (newLength === undefined || newLength === oldLength) {
54
+ return clone(this, { transfer: [this] });
55
+ }
56
+ if (typeof newLength !== 'number') {
57
+ throw TypeError(`transfer newLength if provided must be a number`);
58
+ }
59
+ if (newLength > oldLength) {
60
+ const result = new ArrayBuffer(newLength);
61
+ const taOld = new Uint8Array(this);
62
+ const taNew = new Uint8Array(result);
63
+ typedArraySet(taNew, taOld);
64
+ // Using clone only to detach, and only after the copy succeeds
65
+ clone(this, { transfer: [this] });
66
+ return result;
67
+ } else {
68
+ const result = arrayBufferSlice(this, 0, newLength);
69
+ // Using clone only to detach, and only after the slice succeeds
70
+ clone(this, { transfer: [this] });
71
+ return result;
72
+ }
73
+ },
74
+ };
75
+
76
+ defineProperty(arrayBufferPrototype, 'transfer', {
77
+ // @ts-expect-error
78
+ value: methods.transfer,
79
+ writable: true,
80
+ enumerable: false,
81
+ configurable: true,
82
+ });
83
+
84
+ // Empty object because this shim has nothing for `addIntrinsics` to add.
85
+ return {};
86
+ };
@@ -0,0 +1,51 @@
1
+ import {
2
+ functionPrototype,
3
+ getPrototypeOf,
4
+ globalThis,
5
+ objectPrototype,
6
+ setPrototypeOf,
7
+ } from './commons.js';
8
+
9
+ export const tameModuleSource = () => {
10
+ const newIntrinsics = {};
11
+
12
+ const ModuleSource = globalThis.ModuleSource;
13
+ if (ModuleSource !== undefined) {
14
+ newIntrinsics.ModuleSource = ModuleSource;
15
+
16
+ // We introduce ModuleSource.[[Proto]] === AbstractModuleSource
17
+ // and ModuleSource.prototype.[[Proto]] === AbstractModuleSource.prototype
18
+ // if that layer is absent because the permitting system can more
19
+ // gracefully tolerate the absence of an expected prototype than the
20
+ // presence of an unexpected prototype,.
21
+ function AbstractModuleSource() {
22
+ // no-op safe to super()
23
+ }
24
+
25
+ const ModuleSourceProto = getPrototypeOf(ModuleSource);
26
+ if (ModuleSourceProto === functionPrototype) {
27
+ setPrototypeOf(ModuleSource, AbstractModuleSource);
28
+ newIntrinsics['%AbstractModuleSource%'] = AbstractModuleSource;
29
+ newIntrinsics['%AbstractModuleSourcePrototype%'] =
30
+ AbstractModuleSource.prototype;
31
+ } else {
32
+ newIntrinsics['%AbstractModuleSource%'] = ModuleSourceProto;
33
+ newIntrinsics['%AbstractModuleSourcePrototype%'] =
34
+ ModuleSourceProto.prototype;
35
+ }
36
+
37
+ const ModuleSourcePrototype = ModuleSource.prototype;
38
+ if (ModuleSourcePrototype !== undefined) {
39
+ newIntrinsics['%ModuleSourcePrototype%'] = ModuleSourcePrototype;
40
+
41
+ // ModuleSource.prototype.__proto__ should be the
42
+ // AbstractModuleSource.prototype.
43
+ const ModuleSourcePrototypeProto = getPrototypeOf(ModuleSourcePrototype);
44
+ if (ModuleSourcePrototypeProto === objectPrototype) {
45
+ setPrototypeOf(ModuleSource.prototype, AbstractModuleSource.prototype);
46
+ }
47
+ }
48
+ }
49
+
50
+ return newIntrinsics;
51
+ };