ses 0.18.7 → 1.0.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/NEWS.md +26 -1
- package/README.md +4 -4
- package/dist/lockdown.cjs +3516 -3332
- package/dist/lockdown.mjs +3516 -3332
- package/dist/lockdown.umd.js +3516 -3332
- package/dist/lockdown.umd.min.js +1 -1
- package/dist/ses.cjs +3516 -3332
- package/dist/ses.mjs +3516 -3332
- package/dist/ses.umd.js +3516 -3332
- package/dist/ses.umd.min.js +1 -1
- package/index.js +3 -31
- package/package.json +12 -7
- package/src/assert-shim.js +4 -0
- package/src/assert-sloppy-mode.js +11 -0
- package/src/compartment-shim.js +11 -288
- package/src/compartment.js +292 -0
- package/src/enablements.js +1 -1
- package/src/error/console.js +4 -0
- package/src/error/internal-types.js +2 -2
- package/src/error/note-log-args.js +2 -3
- package/src/error/tame-console.js +38 -4
- package/src/error/types.js +4 -4
- package/src/get-anonymous-intrinsics.js +1 -1
- package/src/global-object.js +8 -6
- package/src/lockdown-shim.js +19 -379
- package/src/lockdown.js +409 -0
- package/src/make-safe-evaluator.js +2 -2
- package/src/permits.js +14 -5
- package/src/tame-date-constructor.js +37 -21
- package/src/tame-math-object.js +20 -1
- package/types.d.ts +29 -9
package/index.js
CHANGED
|
@@ -12,34 +12,6 @@
|
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
|
|
15
|
-
import
|
|
16
|
-
|
|
17
|
-
import
|
|
18
|
-
import { getGlobalIntrinsics } from './src/intrinsics.js';
|
|
19
|
-
import { lockdown } from './src/lockdown-shim.js';
|
|
20
|
-
import { makeCompartmentConstructor } from './src/compartment-shim.js';
|
|
21
|
-
import { assert } from './src/error/assert.js';
|
|
22
|
-
|
|
23
|
-
/** getThis returns globalThis in sloppy mode or undefined in strict mode. */
|
|
24
|
-
function getThis() {
|
|
25
|
-
return this;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
if (getThis()) {
|
|
29
|
-
// See https://github.com/endojs/endo/blob/master/packages/ses/error-codes/SES_NO_SLOPPY.md
|
|
30
|
-
throw TypeError(`SES failed to initialize, sloppy mode (SES_NO_SLOPPY)`);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const markVirtualizedNativeFunction = tameFunctionToString();
|
|
34
|
-
|
|
35
|
-
const Compartment = makeCompartmentConstructor(
|
|
36
|
-
makeCompartmentConstructor,
|
|
37
|
-
getGlobalIntrinsics(globalThis),
|
|
38
|
-
markVirtualizedNativeFunction,
|
|
39
|
-
);
|
|
40
|
-
|
|
41
|
-
assign(globalThis, {
|
|
42
|
-
lockdown,
|
|
43
|
-
Compartment,
|
|
44
|
-
assert,
|
|
45
|
-
});
|
|
15
|
+
import './src/lockdown-shim.js';
|
|
16
|
+
import './src/compartment-shim.js';
|
|
17
|
+
import './src/assert-shim.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ses",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Hardened JavaScript for Fearless Cooperation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lockdown",
|
|
@@ -35,10 +35,12 @@
|
|
|
35
35
|
"types": "./types.d.ts",
|
|
36
36
|
"exports": {
|
|
37
37
|
".": {
|
|
38
|
+
"types": "./types.d.ts",
|
|
38
39
|
"import": "./index.js",
|
|
39
40
|
"require": "./dist/ses.cjs"
|
|
40
41
|
},
|
|
41
42
|
"./lockdown": {
|
|
43
|
+
"types": "./types.d.ts",
|
|
42
44
|
"import": "./index.js",
|
|
43
45
|
"require": "./dist/ses.cjs"
|
|
44
46
|
},
|
|
@@ -60,11 +62,11 @@
|
|
|
60
62
|
"test:platform-compatibility": "node test/package/test.cjs"
|
|
61
63
|
},
|
|
62
64
|
"dependencies": {
|
|
63
|
-
"@endo/env-options": "^0.
|
|
65
|
+
"@endo/env-options": "^1.0.0"
|
|
64
66
|
},
|
|
65
67
|
"devDependencies": {
|
|
66
|
-
"@endo/compartment-mapper": "^0.
|
|
67
|
-
"@endo/static-module-record": "^0.
|
|
68
|
+
"@endo/compartment-mapper": "^1.0.0",
|
|
69
|
+
"@endo/static-module-record": "^1.0.0",
|
|
68
70
|
"@endo/test262-runner": "^0.1.32",
|
|
69
71
|
"ava": "^5.3.0",
|
|
70
72
|
"babel-eslint": "^10.0.3",
|
|
@@ -74,12 +76,12 @@
|
|
|
74
76
|
"eslint-config-airbnb-base": "^15.0.0",
|
|
75
77
|
"eslint-config-prettier": "^8.8.0",
|
|
76
78
|
"eslint-plugin-eslint-comments": "^3.1.2",
|
|
77
|
-
"eslint-plugin-import": "^2.
|
|
79
|
+
"eslint-plugin-import": "^2.29.0",
|
|
78
80
|
"prettier": "^3.0.0",
|
|
79
81
|
"sinon": "^15.1.0",
|
|
80
82
|
"terser": "^5.16.6",
|
|
81
83
|
"tsd": "^0.28.1",
|
|
82
|
-
"typescript": "~5.
|
|
84
|
+
"typescript": "~5.2.2"
|
|
83
85
|
},
|
|
84
86
|
"files": [
|
|
85
87
|
"LICENSE*",
|
|
@@ -179,5 +181,8 @@
|
|
|
179
181
|
],
|
|
180
182
|
"timeout": "2m"
|
|
181
183
|
},
|
|
182
|
-
"
|
|
184
|
+
"typeCoverage": {
|
|
185
|
+
"atLeast": 81.13
|
|
186
|
+
},
|
|
187
|
+
"gitHead": "6aa22009bf8128575c446aebceb0f9a01459d165"
|
|
183
188
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { TypeError } from './commons.js';
|
|
2
|
+
|
|
3
|
+
/** getThis returns globalThis in sloppy mode or undefined in strict mode. */
|
|
4
|
+
function getThis() {
|
|
5
|
+
return this;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (getThis()) {
|
|
9
|
+
// See https://github.com/endojs/endo/blob/master/packages/ses/error-codes/SES_NO_SLOPPY.md
|
|
10
|
+
throw TypeError(`SES failed to initialize, sloppy mode (SES_NO_SLOPPY)`);
|
|
11
|
+
}
|
package/src/compartment-shim.js
CHANGED
|
@@ -1,292 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
/* eslint-disable no-underscore-dangle */
|
|
3
|
-
/// <reference types="ses">
|
|
1
|
+
/// <reference types="ses"/>
|
|
4
2
|
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
WeakMap,
|
|
10
|
-
assign,
|
|
11
|
-
defineProperties,
|
|
12
|
-
entries,
|
|
13
|
-
promiseThen,
|
|
14
|
-
weakmapGet,
|
|
15
|
-
weakmapSet,
|
|
16
|
-
} from './commons.js';
|
|
17
|
-
import {
|
|
18
|
-
setGlobalObjectSymbolUnscopables,
|
|
19
|
-
setGlobalObjectConstantProperties,
|
|
20
|
-
setGlobalObjectMutableProperties,
|
|
21
|
-
setGlobalObjectEvaluators,
|
|
22
|
-
} from './global-object.js';
|
|
23
|
-
import { sharedGlobalPropertyNames } from './permits.js';
|
|
24
|
-
import { load } from './module-load.js';
|
|
25
|
-
import { link } from './module-link.js';
|
|
26
|
-
import { getDeferredExports } from './module-proxy.js';
|
|
27
|
-
import { assert } from './error/assert.js';
|
|
28
|
-
import { compartmentEvaluate } from './compartment-evaluate.js';
|
|
29
|
-
import { makeSafeEvaluator } from './make-safe-evaluator.js';
|
|
3
|
+
import { globalThis } from './commons.js';
|
|
4
|
+
import { makeCompartmentConstructor } from './compartment.js';
|
|
5
|
+
import { tameFunctionToString } from './tame-function-tostring.js';
|
|
6
|
+
import { getGlobalIntrinsics } from './intrinsics.js';
|
|
30
7
|
|
|
31
|
-
const
|
|
8
|
+
const markVirtualizedNativeFunction = tameFunctionToString();
|
|
32
9
|
|
|
33
|
-
//
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
// to obtain the exports namespace of a foreign module and pass it into another
|
|
38
|
-
// compartment's moduleMap constructor option.
|
|
39
|
-
const moduleAliases = new WeakMap();
|
|
40
|
-
|
|
41
|
-
// privateFields captures the private state for each compartment.
|
|
42
|
-
const privateFields = new WeakMap();
|
|
43
|
-
|
|
44
|
-
// Compartments do not need an importHook or resolveHook to be useful
|
|
45
|
-
// as a vessel for evaluating programs.
|
|
46
|
-
// However, any method that operates the module system will throw an exception
|
|
47
|
-
// if these hooks are not available.
|
|
48
|
-
const assertModuleHooks = compartment => {
|
|
49
|
-
const { importHook, resolveHook } = weakmapGet(privateFields, compartment);
|
|
50
|
-
if (typeof importHook !== 'function' || typeof resolveHook !== 'function') {
|
|
51
|
-
throw TypeError(
|
|
52
|
-
'Compartment must be constructed with an importHook and a resolveHook for it to be able to load modules',
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
export const InertCompartment = function Compartment(
|
|
58
|
-
_endowments = {},
|
|
59
|
-
_modules = {},
|
|
60
|
-
_options = {},
|
|
61
|
-
) {
|
|
62
|
-
throw TypeError(
|
|
63
|
-
'Compartment.prototype.constructor is not a valid constructor.',
|
|
64
|
-
);
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* @param {Compartment} compartment
|
|
69
|
-
* @param {string} specifier
|
|
70
|
-
*/
|
|
71
|
-
const compartmentImportNow = (compartment, specifier) => {
|
|
72
|
-
const { execute, exportsProxy } = link(
|
|
73
|
-
privateFields,
|
|
74
|
-
moduleAliases,
|
|
75
|
-
compartment,
|
|
76
|
-
specifier,
|
|
77
|
-
);
|
|
78
|
-
execute();
|
|
79
|
-
return exportsProxy;
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
export const CompartmentPrototype = {
|
|
83
|
-
constructor: InertCompartment,
|
|
84
|
-
|
|
85
|
-
get globalThis() {
|
|
86
|
-
return weakmapGet(privateFields, this).globalObject;
|
|
87
|
-
},
|
|
88
|
-
|
|
89
|
-
get name() {
|
|
90
|
-
return weakmapGet(privateFields, this).name;
|
|
91
|
-
},
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* @param {string} source is a JavaScript program grammar construction.
|
|
95
|
-
* @param {object} [options]
|
|
96
|
-
* @param {Array<import('./lockdown-shim').Transform>} [options.transforms]
|
|
97
|
-
* @param {boolean} [options.sloppyGlobalsMode]
|
|
98
|
-
* @param {object} [options.__moduleShimLexicals__]
|
|
99
|
-
* @param {boolean} [options.__evadeHtmlCommentTest__]
|
|
100
|
-
* @param {boolean} [options.__evadeImportExpressionTest__]
|
|
101
|
-
* @param {boolean} [options.__rejectSomeDirectEvalExpressions__]
|
|
102
|
-
*/
|
|
103
|
-
evaluate(source, options = {}) {
|
|
104
|
-
const compartmentFields = weakmapGet(privateFields, this);
|
|
105
|
-
return compartmentEvaluate(compartmentFields, source, options);
|
|
106
|
-
},
|
|
107
|
-
|
|
108
|
-
toString() {
|
|
109
|
-
return '[object Compartment]';
|
|
110
|
-
},
|
|
111
|
-
|
|
112
|
-
module(specifier) {
|
|
113
|
-
if (typeof specifier !== 'string') {
|
|
114
|
-
throw TypeError('first argument of module() must be a string');
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
assertModuleHooks(this);
|
|
118
|
-
|
|
119
|
-
const { exportsProxy } = getDeferredExports(
|
|
120
|
-
this,
|
|
121
|
-
weakmapGet(privateFields, this),
|
|
122
|
-
moduleAliases,
|
|
123
|
-
specifier,
|
|
124
|
-
);
|
|
125
|
-
|
|
126
|
-
return exportsProxy;
|
|
127
|
-
},
|
|
128
|
-
|
|
129
|
-
async import(specifier) {
|
|
130
|
-
if (typeof specifier !== 'string') {
|
|
131
|
-
throw TypeError('first argument of import() must be a string');
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
assertModuleHooks(this);
|
|
135
|
-
|
|
136
|
-
return promiseThen(
|
|
137
|
-
load(privateFields, moduleAliases, this, specifier),
|
|
138
|
-
() => {
|
|
139
|
-
// The namespace box is a contentious design and likely to be a breaking
|
|
140
|
-
// change in an appropriately numbered future version.
|
|
141
|
-
const namespace = compartmentImportNow(
|
|
142
|
-
/** @type {Compartment} */ (this),
|
|
143
|
-
specifier,
|
|
144
|
-
);
|
|
145
|
-
return { namespace };
|
|
146
|
-
},
|
|
147
|
-
);
|
|
148
|
-
},
|
|
149
|
-
|
|
150
|
-
async load(specifier) {
|
|
151
|
-
if (typeof specifier !== 'string') {
|
|
152
|
-
throw TypeError('first argument of load() must be a string');
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
assertModuleHooks(this);
|
|
156
|
-
|
|
157
|
-
return load(privateFields, moduleAliases, this, specifier);
|
|
158
|
-
},
|
|
159
|
-
|
|
160
|
-
importNow(specifier) {
|
|
161
|
-
if (typeof specifier !== 'string') {
|
|
162
|
-
throw TypeError('first argument of importNow() must be a string');
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
assertModuleHooks(this);
|
|
166
|
-
|
|
167
|
-
return compartmentImportNow(/** @type {Compartment} */ (this), specifier);
|
|
168
|
-
},
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
defineProperties(InertCompartment, {
|
|
172
|
-
prototype: { value: CompartmentPrototype },
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* @callback MakeCompartmentConstructor
|
|
177
|
-
* @param {MakeCompartmentConstructor} targetMakeCompartmentConstructor
|
|
178
|
-
* @param {Record<string, any>} intrinsics
|
|
179
|
-
* @param {(object: object) => void} markVirtualizedNativeFunction
|
|
180
|
-
* @returns {Compartment['constructor']}
|
|
181
|
-
*/
|
|
182
|
-
|
|
183
|
-
/** @type {MakeCompartmentConstructor} */
|
|
184
|
-
export const makeCompartmentConstructor = (
|
|
185
|
-
targetMakeCompartmentConstructor,
|
|
186
|
-
intrinsics,
|
|
10
|
+
// @ts-ignore Compartment is definitely on globalThis.
|
|
11
|
+
globalThis.Compartment = makeCompartmentConstructor(
|
|
12
|
+
makeCompartmentConstructor,
|
|
13
|
+
getGlobalIntrinsics(globalThis),
|
|
187
14
|
markVirtualizedNativeFunction,
|
|
188
|
-
)
|
|
189
|
-
function Compartment(endowments = {}, moduleMap = {}, options = {}) {
|
|
190
|
-
if (new.target === undefined) {
|
|
191
|
-
throw TypeError(
|
|
192
|
-
"Class constructor Compartment cannot be invoked without 'new'",
|
|
193
|
-
);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// Extract options, and shallow-clone transforms.
|
|
197
|
-
const {
|
|
198
|
-
name = '<unknown>',
|
|
199
|
-
transforms = [],
|
|
200
|
-
__shimTransforms__ = [],
|
|
201
|
-
resolveHook,
|
|
202
|
-
importHook,
|
|
203
|
-
moduleMapHook,
|
|
204
|
-
importMetaHook,
|
|
205
|
-
} = options;
|
|
206
|
-
const globalTransforms = [...transforms, ...__shimTransforms__];
|
|
207
|
-
|
|
208
|
-
// Map<FullSpecifier, ModuleCompartmentRecord>
|
|
209
|
-
const moduleRecords = new Map();
|
|
210
|
-
// Map<FullSpecifier, ModuleInstance>
|
|
211
|
-
const instances = new Map();
|
|
212
|
-
// Map<FullSpecifier, {ExportsProxy, ProxiedExports, activate()}>
|
|
213
|
-
const deferredExports = new Map();
|
|
214
|
-
|
|
215
|
-
// Validate given moduleMap.
|
|
216
|
-
// The module map gets translated on-demand in module-load.js and the
|
|
217
|
-
// moduleMap can be invalid in ways that cannot be detected in the
|
|
218
|
-
// constructor, but these checks allow us to throw early for a better
|
|
219
|
-
// developer experience.
|
|
220
|
-
for (const [specifier, aliasNamespace] of entries(moduleMap || {})) {
|
|
221
|
-
if (typeof aliasNamespace === 'string') {
|
|
222
|
-
// TODO implement parent module record retrieval.
|
|
223
|
-
throw TypeError(
|
|
224
|
-
`Cannot map module ${q(specifier)} to ${q(
|
|
225
|
-
aliasNamespace,
|
|
226
|
-
)} in parent compartment`,
|
|
227
|
-
);
|
|
228
|
-
} else if (weakmapGet(moduleAliases, aliasNamespace) === undefined) {
|
|
229
|
-
// TODO create and link a synthetic module instance from the given
|
|
230
|
-
// namespace object.
|
|
231
|
-
throw ReferenceError(
|
|
232
|
-
`Cannot map module ${q(
|
|
233
|
-
specifier,
|
|
234
|
-
)} because it has no known compartment in this realm`,
|
|
235
|
-
);
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
const globalObject = {};
|
|
240
|
-
|
|
241
|
-
setGlobalObjectSymbolUnscopables(globalObject);
|
|
242
|
-
|
|
243
|
-
// We must initialize all constant properties first because
|
|
244
|
-
// `makeSafeEvaluator` may use them to create optimized bindings
|
|
245
|
-
// in the evaluator.
|
|
246
|
-
// TODO: consider merging into a single initialization if internal
|
|
247
|
-
// evaluator is no longer eagerly created
|
|
248
|
-
setGlobalObjectConstantProperties(globalObject);
|
|
249
|
-
|
|
250
|
-
const { safeEvaluate } = makeSafeEvaluator({
|
|
251
|
-
globalObject,
|
|
252
|
-
globalTransforms,
|
|
253
|
-
sloppyGlobalsMode: false,
|
|
254
|
-
});
|
|
255
|
-
|
|
256
|
-
setGlobalObjectMutableProperties(globalObject, {
|
|
257
|
-
intrinsics,
|
|
258
|
-
newGlobalPropertyNames: sharedGlobalPropertyNames,
|
|
259
|
-
makeCompartmentConstructor: targetMakeCompartmentConstructor,
|
|
260
|
-
markVirtualizedNativeFunction,
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
// TODO: maybe add evalTaming to the Compartment constructor 3rd options?
|
|
264
|
-
setGlobalObjectEvaluators(
|
|
265
|
-
globalObject,
|
|
266
|
-
safeEvaluate,
|
|
267
|
-
markVirtualizedNativeFunction,
|
|
268
|
-
);
|
|
269
|
-
|
|
270
|
-
assign(globalObject, endowments);
|
|
271
|
-
|
|
272
|
-
weakmapSet(privateFields, this, {
|
|
273
|
-
name: `${name}`,
|
|
274
|
-
globalTransforms,
|
|
275
|
-
globalObject,
|
|
276
|
-
safeEvaluate,
|
|
277
|
-
resolveHook,
|
|
278
|
-
importHook,
|
|
279
|
-
moduleMap,
|
|
280
|
-
moduleMapHook,
|
|
281
|
-
importMetaHook,
|
|
282
|
-
moduleRecords,
|
|
283
|
-
__shimTransforms__,
|
|
284
|
-
deferredExports,
|
|
285
|
-
instances,
|
|
286
|
-
});
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
Compartment.prototype = CompartmentPrototype;
|
|
290
|
-
|
|
291
|
-
return Compartment;
|
|
292
|
-
};
|
|
15
|
+
);
|