ses 1.15.0 → 2.1.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/dist/lockdown.cjs +683 -290
- package/dist/lockdown.mjs +683 -290
- package/dist/lockdown.umd.js +683 -290
- package/dist/lockdown.umd.min.js +1 -1
- package/dist/ses-hermes.cjs +683 -290
- package/dist/ses.cjs +683 -290
- package/dist/ses.mjs +683 -290
- package/dist/ses.umd.js +683 -290
- package/dist/ses.umd.min.js +1 -1
- package/package.json +5 -39
- package/src/assert-sloppy-mode.js +2 -1
- package/src/cauterize-property.js +3 -2
- package/src/commons.js +65 -10
- package/src/compartment.js +8 -6
- package/src/enablements.js +15 -14
- package/src/error/assert.js +16 -21
- package/src/error/tame-error-constructor.js +6 -0
- package/src/error/tame-v8-error-constructor.js +24 -10
- package/src/get-anonymous-intrinsics.js +3 -3
- package/src/get-source-url.js +11 -4
- package/src/lockdown.js +2 -0
- package/src/module-load.js +4 -4
- package/src/permits.js +20 -5
- package/src/scope-constants.js +4 -3
- package/src/tame-locale-methods.js +2 -1
- package/src/tame-nan-sidechannel.js +253 -0
- package/src/transforms.js +5 -5
- package/src-xs/commons.js +29 -0
- package/src-xs/compartment-shim.js +134 -0
- package/src-xs/compartment.js +318 -0
- package/src-xs/index.js +27 -0
- package/src-xs/lockdown-shim.js +29 -0
package/src/module-load.js
CHANGED
|
@@ -510,7 +510,7 @@ const memoizedLoadWithErrorAnnotation = (
|
|
|
510
510
|
/**
|
|
511
511
|
* If `aggregateErrors` is `false`, the `errors` property of the fulfilled object
|
|
512
512
|
* will always be empty.
|
|
513
|
-
* @param {{errors?: Error[], noAggregateErrors?: boolean}} [options]
|
|
513
|
+
* @param {{errors?: Error[], noAggregateErrors?: boolean | undefined}} [options]
|
|
514
514
|
*/
|
|
515
515
|
const asyncJobQueue = ({ errors = [], noAggregateErrors = false } = {}) => {
|
|
516
516
|
/** @type {Set<Promise<undefined>>} */
|
|
@@ -552,7 +552,7 @@ const asyncJobQueue = ({ errors = [], noAggregateErrors = false } = {}) => {
|
|
|
552
552
|
/**
|
|
553
553
|
* If `aggregateErrors` is `false`, the `errors` property of the returned object
|
|
554
554
|
* will always be empty.
|
|
555
|
-
* @param {{errors?: Error[], noAggregateErrors?: boolean}} [options]
|
|
555
|
+
* @param {{errors?: Error[], noAggregateErrors?: boolean | undefined}} [options]
|
|
556
556
|
*/
|
|
557
557
|
const syncJobQueue = ({ errors = [], noAggregateErrors = false } = {}) => {
|
|
558
558
|
let current = [];
|
|
@@ -625,7 +625,7 @@ const preferAsync = (asyncImpl, _syncImpl) => asyncImpl;
|
|
|
625
625
|
* @param {WeakMap<object, object>} moduleAliases
|
|
626
626
|
* @param {Compartment} compartment
|
|
627
627
|
* @param {string} moduleSpecifier - The module specifier to load.
|
|
628
|
-
* @param {{ noAggregateErrors?: boolean}} options
|
|
628
|
+
* @param {{ noAggregateErrors?: boolean | undefined}} options
|
|
629
629
|
*/
|
|
630
630
|
export const load = async (
|
|
631
631
|
compartmentPrivateFields,
|
|
@@ -676,7 +676,7 @@ export const load = async (
|
|
|
676
676
|
* @param {WeakMap<object, object>} moduleAliases
|
|
677
677
|
* @param {Compartment} compartment
|
|
678
678
|
* @param {string} moduleSpecifier - The module specifier to load.
|
|
679
|
-
* @param {{ noAggregateErrors?: boolean}} options
|
|
679
|
+
* @param {{ noAggregateErrors?: boolean | undefined}} options
|
|
680
680
|
*/
|
|
681
681
|
|
|
682
682
|
export const loadNow = (
|
package/src/permits.js
CHANGED
|
@@ -59,10 +59,7 @@ export const universalPropertyNames = {
|
|
|
59
59
|
Boolean: 'Boolean',
|
|
60
60
|
DataView: 'DataView',
|
|
61
61
|
EvalError: 'EvalError',
|
|
62
|
-
|
|
63
|
-
Float16Array: 'Float16Array',
|
|
64
|
-
Float32Array: 'Float32Array',
|
|
65
|
-
Float64Array: 'Float64Array',
|
|
62
|
+
|
|
66
63
|
Int8Array: 'Int8Array',
|
|
67
64
|
Int16Array: 'Int16Array',
|
|
68
65
|
Int32Array: 'Int32Array',
|
|
@@ -144,6 +141,18 @@ export const initialGlobalPropertyNames = {
|
|
|
144
141
|
|
|
145
142
|
Math: '%InitialMath%',
|
|
146
143
|
|
|
144
|
+
// We move these from universalPropertyNames because the NaN side channel
|
|
145
|
+
// means that they are not quite harmless.
|
|
146
|
+
// We move them to initialGlobalPropertyNames so that they'll still be
|
|
147
|
+
// included in the primordials, repaired, and hardened. Thus, they
|
|
148
|
+
// can be endowed into compartments without hazard beyond the
|
|
149
|
+
// NaN side channel.
|
|
150
|
+
//
|
|
151
|
+
// // https://github.com/tc39/proposal-float16array
|
|
152
|
+
Float16Array: 'Float16Array',
|
|
153
|
+
Float32Array: 'Float32Array',
|
|
154
|
+
Float64Array: 'Float64Array',
|
|
155
|
+
|
|
147
156
|
// ESNext
|
|
148
157
|
|
|
149
158
|
// From Error-stack proposal
|
|
@@ -329,7 +338,7 @@ arrayForEach(['caller', 'arguments'], prop => {
|
|
|
329
338
|
strict[prop];
|
|
330
339
|
} catch (e) {
|
|
331
340
|
// https://github.com/facebook/hermes/blob/main/test/hermes/function-non-strict.js
|
|
332
|
-
if (e.message === 'Restricted in strict mode') {
|
|
341
|
+
if (/** @type {Error} */ (e).message === 'Restricted in strict mode') {
|
|
333
342
|
// Fixed in Static Hermes: https://github.com/facebook/hermes/issues/1582
|
|
334
343
|
FunctionInstance[prop] = accessor;
|
|
335
344
|
}
|
|
@@ -856,6 +865,8 @@ export const permitted = {
|
|
|
856
865
|
getYear: fn,
|
|
857
866
|
setYear: fn,
|
|
858
867
|
toGMTString: fn,
|
|
868
|
+
|
|
869
|
+
toTemporalInstant: false,
|
|
859
870
|
},
|
|
860
871
|
|
|
861
872
|
// Text Processing
|
|
@@ -1244,6 +1255,8 @@ export const permitted = {
|
|
|
1244
1255
|
entries: fn,
|
|
1245
1256
|
forEach: fn,
|
|
1246
1257
|
get: fn,
|
|
1258
|
+
getOrInsert: fn,
|
|
1259
|
+
getOrInsertComputed: fn,
|
|
1247
1260
|
has: fn,
|
|
1248
1261
|
keys: fn,
|
|
1249
1262
|
set: fn,
|
|
@@ -1315,6 +1328,8 @@ export const permitted = {
|
|
|
1315
1328
|
constructor: 'WeakMap',
|
|
1316
1329
|
delete: fn,
|
|
1317
1330
|
get: fn,
|
|
1331
|
+
getOrInsert: fn,
|
|
1332
|
+
getOrInsertComputed: fn,
|
|
1318
1333
|
has: fn,
|
|
1319
1334
|
set: fn,
|
|
1320
1335
|
'@@toStringTag': 'string',
|
package/src/scope-constants.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
2
|
arrayFilter,
|
|
3
3
|
arrayIncludes,
|
|
4
|
+
freezeRegexp,
|
|
4
5
|
getOwnPropertyDescriptor,
|
|
5
6
|
getOwnPropertyNames,
|
|
6
7
|
hasOwn,
|
|
7
|
-
|
|
8
|
+
regexpSearch,
|
|
8
9
|
Set,
|
|
9
10
|
setHas,
|
|
10
11
|
} from './commons.js';
|
|
@@ -91,7 +92,7 @@ const reservedNames = new Set([
|
|
|
91
92
|
* Note: \w is equivalent [a-zA-Z_0-9]
|
|
92
93
|
* See 11.6.1 Identifier Names
|
|
93
94
|
*/
|
|
94
|
-
const identifierPattern = /^[a-zA-Z_$][\w$]
|
|
95
|
+
const identifierPattern = freezeRegexp(/^[a-zA-Z_$][\w$]*$/);
|
|
95
96
|
|
|
96
97
|
/**
|
|
97
98
|
* isValidIdentifierName()
|
|
@@ -100,7 +101,7 @@ const identifierPattern = /^[a-zA-Z_$][\w$]*$/;
|
|
|
100
101
|
* @param {string} name
|
|
101
102
|
*/
|
|
102
103
|
export const isValidIdentifierName = name =>
|
|
103
|
-
!setHas(reservedNames, name) &&
|
|
104
|
+
!setHas(reservedNames, name) && regexpSearch(identifierPattern, name) !== -1;
|
|
104
105
|
|
|
105
106
|
/*
|
|
106
107
|
* isImmutableDataProperty
|
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
String,
|
|
4
4
|
TypeError,
|
|
5
5
|
defineProperty,
|
|
6
|
+
freezeRegexp,
|
|
6
7
|
getOwnPropertyNames,
|
|
7
8
|
isPrimitive,
|
|
8
9
|
regexpExec,
|
|
@@ -11,7 +12,7 @@ import { assert } from './error/assert.js';
|
|
|
11
12
|
|
|
12
13
|
const { Fail, quote: q } = assert;
|
|
13
14
|
|
|
14
|
-
const localePattern = /^(\w*[a-z])Locale([A-Z]\w*)
|
|
15
|
+
const localePattern = freezeRegexp(/^(\w*[a-z])Locale([A-Z]\w*)$/);
|
|
15
16
|
|
|
16
17
|
// Use concise methods to obtain named functions without constructor
|
|
17
18
|
// behavior or `.prototype` property.
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import {
|
|
2
|
+
RangeError,
|
|
3
|
+
dataViewPrototype,
|
|
4
|
+
MAX_SAFE_INTEGER,
|
|
5
|
+
apply,
|
|
6
|
+
defineProperty,
|
|
7
|
+
entries,
|
|
8
|
+
getOwnPropertyDescriptor,
|
|
9
|
+
hasOwn,
|
|
10
|
+
is,
|
|
11
|
+
max,
|
|
12
|
+
trunc,
|
|
13
|
+
uncurryThis,
|
|
14
|
+
} from './commons.js';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* These `FERAL*` methods open up the NaN side-channel on some platforms,
|
|
18
|
+
* like v8. Thus, we need to encapsulate them and replace them with wrappers
|
|
19
|
+
* that canonicalize NaNs.
|
|
20
|
+
*/
|
|
21
|
+
const {
|
|
22
|
+
setFloat16: FERAL_SET_FLOAT16,
|
|
23
|
+
setFloat32: FERAL_SET_FLOAT32,
|
|
24
|
+
setFloat64: FERAL_SET_FLOAT64,
|
|
25
|
+
|
|
26
|
+
setUint16,
|
|
27
|
+
setUint32,
|
|
28
|
+
setBigUint64,
|
|
29
|
+
} = dataViewPrototype;
|
|
30
|
+
|
|
31
|
+
const dataViewGetBuffer = uncurryThis(
|
|
32
|
+
// @ts-expect-error we know it is there on all conforming platforms
|
|
33
|
+
getOwnPropertyDescriptor(dataViewPrototype, 'buffer').get,
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* See https://webidl.spec.whatwg.org/#js-unrestricted-double which implies
|
|
38
|
+
* that this is the canonical NaN for web standards.
|
|
39
|
+
* Casual googling strongly suggests that this is also the cosmWasm
|
|
40
|
+
* canonical NaN. But I have not yet found an authoritative page stating this.
|
|
41
|
+
*
|
|
42
|
+
* As noted there, WebIDL choses this value because
|
|
43
|
+
* > The `NaN` value ... is chosen simply because it is the quiet NaN
|
|
44
|
+
* > with the lowest value when its bit pattern is interpreted as an 64-bit
|
|
45
|
+
* > unsigned integer.
|
|
46
|
+
*
|
|
47
|
+
* See https://github.com/endojs/endo/pull/3214#discussion_r3155852021
|
|
48
|
+
*/
|
|
49
|
+
const canonicalNaN64Encoding = 0x7ff8000000000000n;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* As of this writing (April 29, 2026) all implementations agree that a
|
|
53
|
+
* ***literal*** `NaN` seems to encode in 32 bits as `0x7fc00000`.
|
|
54
|
+
* See https://github.com/endojs/endo/pull/3214#discussion_r3162974396
|
|
55
|
+
*
|
|
56
|
+
* Applying WebIDL's rationale for the 64 bit float
|
|
57
|
+
* > The `NaN` value ... is chosen simply because it is the quiet NaN
|
|
58
|
+
* > with the lowest value when its bit pattern is interpreted as an 64-bit
|
|
59
|
+
* > unsigned integer.
|
|
60
|
+
*
|
|
61
|
+
* to the 32 bit case agrees with the choice the implementations make
|
|
62
|
+
* for the 32 bit encoding of a ***literal*** `NaN`.
|
|
63
|
+
*
|
|
64
|
+
* See https://github.com/endojs/endo/pull/3214#discussion_r3155852021
|
|
65
|
+
*/
|
|
66
|
+
const canonicalNaN32Encoding = 0x7fc00000;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* As of this writing (April 29, 2026) all implementations that implement
|
|
70
|
+
* float16 (with the almost surely unintentional exception of QuickJS),
|
|
71
|
+
* agree that a ***literal*** `NaN` seems to encode in 16 bits as `0x7e00`.
|
|
72
|
+
* See https://github.com/endojs/endo/pull/3214#discussion_r3162974396
|
|
73
|
+
*
|
|
74
|
+
* Applying WebIDL's rationale for the 64 bit float
|
|
75
|
+
* > The `NaN` value ... is chosen simply because it is the quiet NaN
|
|
76
|
+
* > with the lowest value when its bit pattern is interpreted as an 64-bit
|
|
77
|
+
* > unsigned integer.
|
|
78
|
+
*
|
|
79
|
+
* to the 16 bit case agrees with the choice those implementations make
|
|
80
|
+
* for the 16 bit encoding of a ***literal*** `NaN`.
|
|
81
|
+
*
|
|
82
|
+
* See https://github.com/endojs/endo/pull/3214#discussion_r3155852021
|
|
83
|
+
*/
|
|
84
|
+
const canonicalNaN16Encoding = 0x7e00;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Perform `RequireInternalSlot(obj, [[DataView]])` as needed in
|
|
88
|
+
* SetViewValue.
|
|
89
|
+
* https://tc39.es/ecma262/multipage/structured-data.html#sec-setviewvalue
|
|
90
|
+
* https://tc39.es/ecma262/#sec-get-dataview.prototype.buffer
|
|
91
|
+
*
|
|
92
|
+
* @param {unknown} obj
|
|
93
|
+
* @returns {void}
|
|
94
|
+
*/
|
|
95
|
+
const requireDataView = obj => {
|
|
96
|
+
dataViewGetBuffer(obj);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Emulate the internal `ToIndex` from
|
|
101
|
+
* https://tc39.es/ecma262/multipage/abstract-operations.html#sec-toindex
|
|
102
|
+
*
|
|
103
|
+
* @param {unknown} v
|
|
104
|
+
* @returns {number}
|
|
105
|
+
*/
|
|
106
|
+
const toIndex = v => {
|
|
107
|
+
// @ts-expect-error Math.trunc uses the internal `ToNumber` to coerce its
|
|
108
|
+
// argument, whatever it is, to an integer number.
|
|
109
|
+
const n = trunc(v);
|
|
110
|
+
if (n === 0 || is(n, NaN)) {
|
|
111
|
+
return 0;
|
|
112
|
+
}
|
|
113
|
+
if (n < 0 || n > MAX_SAFE_INTEGER) {
|
|
114
|
+
throw RangeError('Invalid offset');
|
|
115
|
+
}
|
|
116
|
+
return n;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Expose the internal `ToNumber` from
|
|
121
|
+
* https://tc39.es/ecma262/multipage/abstract-operations.html#sec-tonumber
|
|
122
|
+
* We can't just use `Number(value)` because that is backed by `ToNumeric`,
|
|
123
|
+
* which coerces bigint output from `ToPrimitive` rather than throwing a
|
|
124
|
+
* TypeError as required of `ToNumber` (e.g., `Number({ valueOf: () => 42n })`
|
|
125
|
+
* returns 42).
|
|
126
|
+
*
|
|
127
|
+
* @param {unknown} v
|
|
128
|
+
* @returns {number}
|
|
129
|
+
*/
|
|
130
|
+
const toNumber = v =>
|
|
131
|
+
// @ts-expect-error Math.max uses the internal `ToNumber` to coerce its
|
|
132
|
+
// argument, whatever it is, to a number.
|
|
133
|
+
max(v);
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Correctly guarding the `setFloat*` built-ins requires performing
|
|
137
|
+
* `ToNumber(value)` ourselves, because a non-NaN value such as
|
|
138
|
+
* `{ valueOf: () => {} }` gets coerced **to** NaN. But, according to
|
|
139
|
+
* [SetViewValue](https://tc39.es/ecma262/multipage/structured-data.html#sec-setviewvalue),
|
|
140
|
+
* we must first perform the observable
|
|
141
|
+
* `RequireInternalSlot(view, [[DataView]])` and `ToIndex(requestIndex)` steps
|
|
142
|
+
* (in that order). If those steps complete successfully, we have number-coerced
|
|
143
|
+
* `byteOffset` and `value` values that will pass those same steps internal to
|
|
144
|
+
* the implementation without incurring further observable interactions.
|
|
145
|
+
*
|
|
146
|
+
* We added these coercions to defend against an attack noticed by
|
|
147
|
+
* https://github.com/deepview-autofix .
|
|
148
|
+
* We were vulnerable to `value` being an
|
|
149
|
+
* object with a `valueOf` method that returns a (bad) NaN, since it
|
|
150
|
+
* would bypass the is NaN check. See where the
|
|
151
|
+
* `tame-nan*-sidechannel.test.js test cases mention "coercion attack".
|
|
152
|
+
*
|
|
153
|
+
* Uses method shorthand syntax to be `this`-sensitive but not be constructable
|
|
154
|
+
* nor have a `prototype` property.
|
|
155
|
+
*/
|
|
156
|
+
const methods = {
|
|
157
|
+
/**
|
|
158
|
+
* @param {number} byteOffset
|
|
159
|
+
* @param {number} value
|
|
160
|
+
* @param {boolean} [isLittleEndian]
|
|
161
|
+
*/
|
|
162
|
+
setFloat16(byteOffset, value, isLittleEndian = undefined) {
|
|
163
|
+
requireDataView(this);
|
|
164
|
+
byteOffset = toIndex(byteOffset);
|
|
165
|
+
value = toNumber(value);
|
|
166
|
+
if (is(value, NaN)) {
|
|
167
|
+
return apply(setUint16, this, [
|
|
168
|
+
byteOffset,
|
|
169
|
+
canonicalNaN16Encoding,
|
|
170
|
+
isLittleEndian,
|
|
171
|
+
]);
|
|
172
|
+
} else {
|
|
173
|
+
return apply(FERAL_SET_FLOAT16, this, [
|
|
174
|
+
byteOffset,
|
|
175
|
+
value,
|
|
176
|
+
isLittleEndian,
|
|
177
|
+
]);
|
|
178
|
+
}
|
|
179
|
+
},
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* @param {number} byteOffset
|
|
183
|
+
* @param {number} value
|
|
184
|
+
* @param {boolean} [isLittleEndian]
|
|
185
|
+
*/
|
|
186
|
+
setFloat32(byteOffset, value, isLittleEndian = undefined) {
|
|
187
|
+
requireDataView(this);
|
|
188
|
+
byteOffset = toIndex(byteOffset);
|
|
189
|
+
value = toNumber(value);
|
|
190
|
+
if (is(value, NaN)) {
|
|
191
|
+
return apply(setUint32, this, [
|
|
192
|
+
byteOffset,
|
|
193
|
+
canonicalNaN32Encoding,
|
|
194
|
+
isLittleEndian,
|
|
195
|
+
]);
|
|
196
|
+
} else {
|
|
197
|
+
return apply(FERAL_SET_FLOAT32, this, [
|
|
198
|
+
byteOffset,
|
|
199
|
+
value,
|
|
200
|
+
isLittleEndian,
|
|
201
|
+
]);
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* @param {number} byteOffset
|
|
207
|
+
* @param {number} value
|
|
208
|
+
* @param {boolean} [isLittleEndian]
|
|
209
|
+
*/
|
|
210
|
+
setFloat64(byteOffset, value, isLittleEndian = undefined) {
|
|
211
|
+
requireDataView(this);
|
|
212
|
+
byteOffset = toIndex(byteOffset);
|
|
213
|
+
value = toNumber(value);
|
|
214
|
+
if (is(value, NaN)) {
|
|
215
|
+
return apply(setBigUint64, this, [
|
|
216
|
+
byteOffset,
|
|
217
|
+
canonicalNaN64Encoding,
|
|
218
|
+
isLittleEndian,
|
|
219
|
+
]);
|
|
220
|
+
} else {
|
|
221
|
+
return apply(FERAL_SET_FLOAT64, this, [
|
|
222
|
+
byteOffset,
|
|
223
|
+
value,
|
|
224
|
+
isLittleEndian,
|
|
225
|
+
]);
|
|
226
|
+
}
|
|
227
|
+
},
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Replaces the dangerous `setFloat*` methods on `DataView.prototype`
|
|
232
|
+
* with safe wrappers that first canonicalize NaNs before calling the
|
|
233
|
+
* original hidden methods.
|
|
234
|
+
* By itself, this does not make us safe against the NaN side channel.
|
|
235
|
+
* Separately, we do not include the `Float*Array` constructors on the
|
|
236
|
+
* list of universal safe globals. Thus, constructed compartments do not
|
|
237
|
+
* get these by default.
|
|
238
|
+
*
|
|
239
|
+
* These replacement `setFloat*` methods canonicalize NaN, but they
|
|
240
|
+
* do not canonicalize `-0` to `0`. If callers wish to do so, they should do
|
|
241
|
+
* it themselves before calling these
|
|
242
|
+
*/
|
|
243
|
+
export const tameNaNSideChannel = () => {
|
|
244
|
+
for (const [name, method] of entries(methods)) {
|
|
245
|
+
if (hasOwn(dataViewPrototype, name)) {
|
|
246
|
+
defineProperty(dataViewPrototype, name, {
|
|
247
|
+
// Since we're redefining properties that already exist, by omitting the
|
|
248
|
+
// other descriptor attributes here, they are unchanged.
|
|
249
|
+
value: method,
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
};
|
package/src/transforms.js
CHANGED
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
import {
|
|
4
4
|
FERAL_REG_EXP,
|
|
5
5
|
SyntaxError,
|
|
6
|
+
freezeRegexp,
|
|
6
7
|
regexpReplace,
|
|
8
|
+
regexpSearch,
|
|
7
9
|
sealRegexp,
|
|
8
|
-
stringSearch,
|
|
9
10
|
stringSlice,
|
|
10
11
|
stringSplit,
|
|
11
12
|
freeze,
|
|
@@ -21,7 +22,7 @@ import { getSourceURL } from './get-source-url.js';
|
|
|
21
22
|
* @returns {number}
|
|
22
23
|
*/
|
|
23
24
|
function getLineNumber(src, pattern) {
|
|
24
|
-
const index =
|
|
25
|
+
const index = regexpSearch(pattern, src);
|
|
25
26
|
if (index < 0) {
|
|
26
27
|
return -1;
|
|
27
28
|
}
|
|
@@ -177,9 +178,8 @@ export const evadeImportExpressionTest = src => {
|
|
|
177
178
|
|
|
178
179
|
// /////////////////////////////////////////////////////////////////////////////
|
|
179
180
|
|
|
180
|
-
const someDirectEvalPattern =
|
|
181
|
-
'(^|[^.])\\beval(\\s*\\()',
|
|
182
|
-
'g',
|
|
181
|
+
const someDirectEvalPattern = freezeRegexp(
|
|
182
|
+
new FERAL_REG_EXP('(^|[^.])\\beval(\\s*\\()'),
|
|
183
183
|
);
|
|
184
184
|
|
|
185
185
|
/**
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module In the spirit of ../src/commons.js, this module captures native
|
|
3
|
+
* functions specific to the XS engine during initialization, so vetted shims
|
|
4
|
+
* are free to modify any intrinsic without risking the integrity of SES.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/// <reference types="ses"/>
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
getOwnPropertyDescriptor,
|
|
11
|
+
globalThis,
|
|
12
|
+
uncurryThis,
|
|
13
|
+
} from '../src/commons.js';
|
|
14
|
+
|
|
15
|
+
/** @type {typeof Compartment} */
|
|
16
|
+
export const NativeStartCompartment = /** @type {any} */ (globalThis)
|
|
17
|
+
.Compartment;
|
|
18
|
+
export const nativeCompartmentPrototype = NativeStartCompartment.prototype;
|
|
19
|
+
export const nativeImport = uncurryThis(nativeCompartmentPrototype.import);
|
|
20
|
+
export const nativeImportNow = uncurryThis(
|
|
21
|
+
nativeCompartmentPrototype.importNow,
|
|
22
|
+
);
|
|
23
|
+
/** @type {(compartment: any, source: string) => unknown} */
|
|
24
|
+
export const nativeEvaluate = uncurryThis(nativeCompartmentPrototype.evaluate);
|
|
25
|
+
/** @type {(compartment: typeof Compartment) => typeof globalThis} */
|
|
26
|
+
export const nativeGetGlobalThis = uncurryThis(
|
|
27
|
+
// @ts-expect-error we know it is there on XS
|
|
28
|
+
getOwnPropertyDescriptor(nativeCompartmentPrototype, 'globalThis').get,
|
|
29
|
+
);
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module Provides a XS-specific variation on the behavior of
|
|
3
|
+
* ../compartment-shim.js, completing the story that begins in
|
|
4
|
+
* ./compartment.js, adding a Compartment constructor adapter to the global
|
|
5
|
+
* scope and transforming all of the methods of a native compartment into
|
|
6
|
+
* thunks that will alternately delegate to its native or shim behaviors
|
|
7
|
+
* depending on the __native__ Compartment constructor option.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/// <reference types="ses"/>
|
|
11
|
+
|
|
12
|
+
import { defineProperty, globalThis, weakmapGet } from '../src/commons.js';
|
|
13
|
+
import {
|
|
14
|
+
NativeStartCompartment,
|
|
15
|
+
nativeCompartmentPrototype,
|
|
16
|
+
nativeImport,
|
|
17
|
+
nativeImportNow,
|
|
18
|
+
nativeEvaluate,
|
|
19
|
+
nativeGetGlobalThis,
|
|
20
|
+
} from './commons.js';
|
|
21
|
+
import {
|
|
22
|
+
ShimStartCompartment,
|
|
23
|
+
adaptCompartmentConstructors,
|
|
24
|
+
privateFields,
|
|
25
|
+
shimEvaluate,
|
|
26
|
+
shimGetGlobalThis,
|
|
27
|
+
shimImport,
|
|
28
|
+
shimImportNow,
|
|
29
|
+
} from './compartment.js';
|
|
30
|
+
|
|
31
|
+
const adapterFunctions = {
|
|
32
|
+
evaluate(source, options) {
|
|
33
|
+
const fields = weakmapGet(privateFields, this);
|
|
34
|
+
if (fields === undefined) {
|
|
35
|
+
return nativeEvaluate(this, source);
|
|
36
|
+
}
|
|
37
|
+
const { delegateNative } = fields;
|
|
38
|
+
if (delegateNative) {
|
|
39
|
+
const { transforms, nativeEval } = fields;
|
|
40
|
+
for (let i = 0; i < transforms.length; i += 1) {
|
|
41
|
+
const transform = transforms[i];
|
|
42
|
+
source = transform(source);
|
|
43
|
+
}
|
|
44
|
+
return nativeEval(source);
|
|
45
|
+
} else {
|
|
46
|
+
return shimEvaluate(this, source, options);
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
async import(specifier) {
|
|
51
|
+
await null;
|
|
52
|
+
const fields = weakmapGet(privateFields, this);
|
|
53
|
+
if (fields === undefined) {
|
|
54
|
+
return nativeImport(this, specifier);
|
|
55
|
+
}
|
|
56
|
+
const { noNamespaceBox, delegateNative } = fields;
|
|
57
|
+
const delegateImport = delegateNative ? nativeImport : shimImport;
|
|
58
|
+
const namespace = delegateImport(this, specifier);
|
|
59
|
+
return noNamespaceBox ? namespace : { namespace: await namespace };
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
importNow(specifier) {
|
|
63
|
+
const fields = weakmapGet(privateFields, this);
|
|
64
|
+
if (fields === undefined) {
|
|
65
|
+
return nativeImportNow(this, specifier);
|
|
66
|
+
}
|
|
67
|
+
const { delegateNative } = fields;
|
|
68
|
+
const delegateImportNow = delegateNative ? nativeImportNow : shimImportNow;
|
|
69
|
+
return delegateImportNow(this, specifier);
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
defineProperty(nativeCompartmentPrototype, 'evaluate', {
|
|
74
|
+
value: adapterFunctions.evaluate,
|
|
75
|
+
writable: true,
|
|
76
|
+
configurable: true,
|
|
77
|
+
enumerable: false,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
defineProperty(nativeCompartmentPrototype, 'import', {
|
|
81
|
+
value: adapterFunctions.import,
|
|
82
|
+
writable: true,
|
|
83
|
+
configurable: true,
|
|
84
|
+
enumerable: false,
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
defineProperty(nativeCompartmentPrototype, 'importNow', {
|
|
88
|
+
value: adapterFunctions.importNow,
|
|
89
|
+
writable: true,
|
|
90
|
+
configurable: true,
|
|
91
|
+
enumerable: false,
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
defineProperty(nativeCompartmentPrototype, 'globalThis', {
|
|
95
|
+
get() {
|
|
96
|
+
const fields = weakmapGet(privateFields, this);
|
|
97
|
+
if (fields === undefined) {
|
|
98
|
+
return nativeGetGlobalThis(this);
|
|
99
|
+
}
|
|
100
|
+
const { delegateNative } = fields;
|
|
101
|
+
const delegateGetGlobalThis = delegateNative
|
|
102
|
+
? nativeGetGlobalThis
|
|
103
|
+
: shimGetGlobalThis;
|
|
104
|
+
return delegateGetGlobalThis(this);
|
|
105
|
+
},
|
|
106
|
+
configurable: true,
|
|
107
|
+
enumerable: false,
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
defineProperty(nativeCompartmentPrototype, 'name', {
|
|
111
|
+
get() {
|
|
112
|
+
const fields = weakmapGet(privateFields, this);
|
|
113
|
+
if (fields === undefined) {
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
const { name } = fields;
|
|
117
|
+
return name;
|
|
118
|
+
},
|
|
119
|
+
configurable: true,
|
|
120
|
+
enumerable: false,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
// Adapt the start compartment's native Compartment to the SES-compatibility
|
|
124
|
+
// adapter.
|
|
125
|
+
// Before Lockdown, the Compartment constructor in transitive child
|
|
126
|
+
// Compartments is not (and cannot be) hardened.
|
|
127
|
+
const noHarden = object => object;
|
|
128
|
+
// @ts-expect-error TypeScript is not inferring from the types above that
|
|
129
|
+
// Compartment is on globalThis.
|
|
130
|
+
globalThis.Compartment = adaptCompartmentConstructors(
|
|
131
|
+
NativeStartCompartment,
|
|
132
|
+
ShimStartCompartment,
|
|
133
|
+
noHarden,
|
|
134
|
+
);
|