ses 0.18.2 → 0.18.4
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 +14 -0
- package/dist/lockdown.cjs +648 -275
- package/dist/lockdown.mjs +648 -275
- package/dist/lockdown.umd.js +648 -275
- package/dist/lockdown.umd.min.js +1 -1
- package/dist/ses.cjs +648 -275
- package/dist/ses.mjs +648 -275
- package/dist/ses.umd.js +648 -275
- package/dist/ses.umd.min.js +1 -1
- package/package.json +9 -16
- package/src/commons.js +14 -8
- package/src/compartment-shim.js +1 -5
- package/src/enablements.js +1 -1
- package/src/error/assert.js +4 -19
- package/src/error/note-log-args.js +280 -0
- package/src/error/tame-v8-error-constructor.js +1 -1
- package/src/eval-scope.js +4 -2
- package/src/get-anonymous-intrinsics.js +2 -4
- package/src/intrinsics.js +4 -1
- package/src/lockdown-shim.js +7 -4
- package/src/make-hardener.js +7 -0
- package/src/make-safe-evaluator.js +6 -5
- package/src/module-instance.js +3 -2
- package/src/module-load.js +1 -1
- package/src/scope-constants.js +1 -0
- package/src/strict-scope-terminator.js +2 -3
- package/src/tame-function-tostring.js +1 -1
- package/src/tame-harden.js +29 -0
- package/src/tame-regexp-constructor.js +10 -1
- package/src/whitelist-intrinsics.js +3 -0
- package/src/whitelist.js +9 -1
- package/types.d.ts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ses",
|
|
3
|
-
"version": "0.18.
|
|
3
|
+
"version": "0.18.4",
|
|
4
4
|
"description": "Hardened JavaScript for Fearless Cooperation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lockdown",
|
|
@@ -60,22 +60,20 @@
|
|
|
60
60
|
"test:platform-compatibility": "node test/package/test.cjs"
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
|
-
"@endo/compartment-mapper": "^0.8.
|
|
64
|
-
"@endo/
|
|
65
|
-
"@endo/
|
|
66
|
-
"@endo/test262-runner": "^0.1.30",
|
|
63
|
+
"@endo/compartment-mapper": "^0.8.4",
|
|
64
|
+
"@endo/static-module-record": "^0.7.19",
|
|
65
|
+
"@endo/test262-runner": "^0.1.31",
|
|
67
66
|
"ava": "^5.2.0",
|
|
68
67
|
"babel-eslint": "^10.0.3",
|
|
69
68
|
"c8": "^7.7.3",
|
|
70
69
|
"eslint": "^7.32.0",
|
|
71
70
|
"eslint-config-airbnb-base": "^14.0.0",
|
|
72
|
-
"eslint-config-prettier": "^
|
|
71
|
+
"eslint-config-prettier": "^8.8.0",
|
|
73
72
|
"eslint-plugin-eslint-comments": "^3.1.2",
|
|
74
73
|
"eslint-plugin-import": "^2.27.5",
|
|
75
|
-
"
|
|
76
|
-
"prettier": "^2.8.0",
|
|
74
|
+
"prettier": "^2.8.5",
|
|
77
75
|
"sinon": "8.0.4",
|
|
78
|
-
"terser": "^
|
|
76
|
+
"terser": "^5.16.6",
|
|
79
77
|
"tsd": "^0.24.1",
|
|
80
78
|
"typescript": "~4.9.5"
|
|
81
79
|
},
|
|
@@ -95,7 +93,7 @@
|
|
|
95
93
|
},
|
|
96
94
|
"eslintConfig": {
|
|
97
95
|
"extends": [
|
|
98
|
-
"
|
|
96
|
+
"plugin:@endo/internal"
|
|
99
97
|
],
|
|
100
98
|
"rules": {
|
|
101
99
|
"no-restricted-globals": [
|
|
@@ -171,16 +169,11 @@
|
|
|
171
169
|
}
|
|
172
170
|
]
|
|
173
171
|
},
|
|
174
|
-
"prettier": {
|
|
175
|
-
"arrowParens": "avoid",
|
|
176
|
-
"trailingComma": "all",
|
|
177
|
-
"singleQuote": true
|
|
178
|
-
},
|
|
179
172
|
"ava": {
|
|
180
173
|
"files": [
|
|
181
174
|
"test/**/test-*.js"
|
|
182
175
|
],
|
|
183
176
|
"timeout": "2m"
|
|
184
177
|
},
|
|
185
|
-
"gitHead": "
|
|
178
|
+
"gitHead": "38c2c59d6ae8c53f84cd333e6c7828e2d37604e2"
|
|
186
179
|
}
|
package/src/commons.js
CHANGED
|
@@ -59,6 +59,8 @@ export const {
|
|
|
59
59
|
getOwnPropertyNames,
|
|
60
60
|
getPrototypeOf,
|
|
61
61
|
is,
|
|
62
|
+
isFrozen,
|
|
63
|
+
isSealed,
|
|
62
64
|
isExtensible,
|
|
63
65
|
keys,
|
|
64
66
|
prototype: objectPrototype,
|
|
@@ -133,6 +135,7 @@ export const { prototype: promisePrototype } = Promise;
|
|
|
133
135
|
export const typedArrayPrototype = getPrototypeOf(Uint8Array.prototype);
|
|
134
136
|
|
|
135
137
|
const { bind } = functionPrototype;
|
|
138
|
+
|
|
136
139
|
/**
|
|
137
140
|
* uncurryThis()
|
|
138
141
|
* Equivalent of: fn => (thisArg, ...args) => apply(fn, thisArg, args)
|
|
@@ -152,8 +155,8 @@ export const arrayFilter = uncurryThis(arrayPrototype.filter);
|
|
|
152
155
|
export const arrayForEach = uncurryThis(arrayPrototype.forEach);
|
|
153
156
|
export const arrayIncludes = uncurryThis(arrayPrototype.includes);
|
|
154
157
|
export const arrayJoin = uncurryThis(arrayPrototype.join);
|
|
155
|
-
/** @type {<T, U>(thisArg: readonly T[], callbackfn: (value: T, index: number, array: T[]) => U,
|
|
156
|
-
export const arrayMap = uncurryThis(arrayPrototype.map);
|
|
158
|
+
/** @type {<T, U>(thisArg: readonly T[], callbackfn: (value: T, index: number, array: T[]) => U, cbThisArg?: any) => U[]} */
|
|
159
|
+
export const arrayMap = /** @type {any} */ (uncurryThis(arrayPrototype.map));
|
|
157
160
|
export const arrayPop = uncurryThis(arrayPrototype.pop);
|
|
158
161
|
/** @type {<T>(thisArg: T[], ...items: T[]) => number} */
|
|
159
162
|
export const arrayPush = uncurryThis(arrayPrototype.push);
|
|
@@ -184,12 +187,14 @@ export const stringIncludes = uncurryThis(stringPrototype.includes);
|
|
|
184
187
|
export const stringIndexOf = uncurryThis(stringPrototype.indexOf);
|
|
185
188
|
export const stringMatch = uncurryThis(stringPrototype.match);
|
|
186
189
|
/**
|
|
187
|
-
* @type {
|
|
188
|
-
* ((thisArg: string, searchValue: { [Symbol.replace](string: string, replaceValue: string): string; }, replaceValue: string) => string)
|
|
190
|
+
* @type { &
|
|
191
|
+
* ((thisArg: string, searchValue: { [Symbol.replace](string: string, replaceValue: string): string; }, replaceValue: string) => string) &
|
|
189
192
|
* ((thisArg: string, searchValue: { [Symbol.replace](string: string, replacer: (substring: string, ...args: any[]) => string): string; }, replacer: (substring: string, ...args: any[]) => string) => string)
|
|
190
193
|
* }
|
|
191
194
|
*/
|
|
192
|
-
export const stringReplace =
|
|
195
|
+
export const stringReplace = /** @type {any} */ (
|
|
196
|
+
uncurryThis(stringPrototype.replace)
|
|
197
|
+
);
|
|
193
198
|
export const stringSearch = uncurryThis(stringPrototype.search);
|
|
194
199
|
export const stringSlice = uncurryThis(stringPrototype.slice);
|
|
195
200
|
/** @type {(thisArg: string, splitter: string | RegExp | { [Symbol.split](string: string, limit?: number): string[]; }, limit?: number) => string[]} */
|
|
@@ -198,13 +203,12 @@ export const stringStartsWith = uncurryThis(stringPrototype.startsWith);
|
|
|
198
203
|
export const iterateString = uncurryThis(stringPrototype[iteratorSymbol]);
|
|
199
204
|
//
|
|
200
205
|
export const weakmapDelete = uncurryThis(weakmapPrototype.delete);
|
|
201
|
-
/** @type {<K, V>(thisArg: WeakMap<K, V>, ...args: Parameters<WeakMap<K,V>['get']>) => ReturnType<WeakMap<K,V>['get']>} */
|
|
206
|
+
/** @type {<K extends {}, V>(thisArg: WeakMap<K, V>, ...args: Parameters<WeakMap<K,V>['get']>) => ReturnType<WeakMap<K,V>['get']>} */
|
|
202
207
|
export const weakmapGet = uncurryThis(weakmapPrototype.get);
|
|
203
208
|
export const weakmapHas = uncurryThis(weakmapPrototype.has);
|
|
204
209
|
export const weakmapSet = uncurryThis(weakmapPrototype.set);
|
|
205
210
|
//
|
|
206
211
|
export const weaksetAdd = uncurryThis(weaksetPrototype.add);
|
|
207
|
-
export const weaksetGet = uncurryThis(weaksetPrototype.get);
|
|
208
212
|
export const weaksetHas = uncurryThis(weaksetPrototype.has);
|
|
209
213
|
//
|
|
210
214
|
export const functionToString = uncurryThis(functionPrototype.toString);
|
|
@@ -213,7 +217,9 @@ const { all } = Promise;
|
|
|
213
217
|
export const promiseAll = promises => apply(all, Promise, [promises]);
|
|
214
218
|
export const promiseCatch = uncurryThis(promisePrototype.catch);
|
|
215
219
|
/** @type {<T, TResult1 = T, TResult2 = never>(thisArg: T, onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null) => Promise<TResult1 | TResult2>} */
|
|
216
|
-
export const promiseThen =
|
|
220
|
+
export const promiseThen = /** @type {any} */ (
|
|
221
|
+
uncurryThis(promisePrototype.then)
|
|
222
|
+
);
|
|
217
223
|
//
|
|
218
224
|
export const finalizationRegistryRegister =
|
|
219
225
|
FinalizationRegistry && uncurryThis(FinalizationRegistry.prototype.register);
|
package/src/compartment-shim.js
CHANGED
|
@@ -41,10 +41,6 @@ const moduleAliases = new WeakMap();
|
|
|
41
41
|
// privateFields captures the private state for each compartment.
|
|
42
42
|
const privateFields = new WeakMap();
|
|
43
43
|
|
|
44
|
-
/**
|
|
45
|
-
* @typedef {(source: string) => string} Transform
|
|
46
|
-
*/
|
|
47
|
-
|
|
48
44
|
// Compartments do not need an importHook or resolveHook to be useful
|
|
49
45
|
// as a vessel for evaluating programs.
|
|
50
46
|
// However, any method that operates the module system will throw an exception
|
|
@@ -97,7 +93,7 @@ export const CompartmentPrototype = {
|
|
|
97
93
|
/**
|
|
98
94
|
* @param {string} source is a JavaScript program grammar construction.
|
|
99
95
|
* @param {object} [options]
|
|
100
|
-
* @param {Array<Transform>} [options.transforms]
|
|
96
|
+
* @param {Array<import('./lockdown-shim').Transform>} [options.transforms]
|
|
101
97
|
* @param {boolean} [options.sloppyGlobalsMode]
|
|
102
98
|
* @param {object} [options.__moduleShimLexicals__]
|
|
103
99
|
* @param {boolean} [options.__evadeHtmlCommentTest__]
|
package/src/enablements.js
CHANGED
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
* <ul>
|
|
42
42
|
* <li>true, in which case this property is simply repaired. The
|
|
43
43
|
* value associated with that property is not traversed. For
|
|
44
|
-
*
|
|
44
|
+
* example, {@code "Function.prototype.name"} leads to true,
|
|
45
45
|
* meaning that the {@code "name"} property of {@code
|
|
46
46
|
* "Function.prototype"} should be repaired (which is needed
|
|
47
47
|
* when inheriting from @code{Function} and setting the subclass's
|
package/src/error/assert.js
CHANGED
|
@@ -36,6 +36,7 @@ import {
|
|
|
36
36
|
import { an, bestEffortStringify } from './stringify-utils.js';
|
|
37
37
|
import './types.js';
|
|
38
38
|
import './internal-types.js';
|
|
39
|
+
import { makeNoteLogArgsArrayKit } from './note-log-args.js';
|
|
39
40
|
|
|
40
41
|
// For our internal debugging purposes, uncomment
|
|
41
42
|
// const internalDebugConsole = console;
|
|
@@ -251,17 +252,7 @@ freeze(makeError);
|
|
|
251
252
|
|
|
252
253
|
// /////////////////////////////////////////////////////////////////////////////
|
|
253
254
|
|
|
254
|
-
|
|
255
|
-
* @type {WeakMap<Error, LogArgs[]>}
|
|
256
|
-
*
|
|
257
|
-
* Maps from an error to an array of log args, where each log args is
|
|
258
|
-
* remembered as an annotation on that error. This can be used, for example,
|
|
259
|
-
* to keep track of additional causes of the error. The elements of any
|
|
260
|
-
* log args may include errors which are associated with further annotations.
|
|
261
|
-
* An augmented console, like the causal console of `console.js`, could
|
|
262
|
-
* then retrieve the graph of such annotations.
|
|
263
|
-
*/
|
|
264
|
-
const hiddenNoteLogArgsArrays = new WeakMap();
|
|
255
|
+
const { addLogArgs, takeLogArgsArray } = makeNoteLogArgsArrayKit();
|
|
265
256
|
|
|
266
257
|
/**
|
|
267
258
|
* @type {WeakMap<Error, NoteCallback[]>}
|
|
@@ -295,12 +286,7 @@ const note = (error, detailsNote) => {
|
|
|
295
286
|
callback(error, logArgs);
|
|
296
287
|
}
|
|
297
288
|
} else {
|
|
298
|
-
|
|
299
|
-
if (logArgsArray !== undefined) {
|
|
300
|
-
arrayPush(logArgsArray, logArgs);
|
|
301
|
-
} else {
|
|
302
|
-
weakmapSet(hiddenNoteLogArgsArrays, error, [logArgs]);
|
|
303
|
-
}
|
|
289
|
+
addLogArgs(error, logArgs);
|
|
304
290
|
}
|
|
305
291
|
};
|
|
306
292
|
freeze(note);
|
|
@@ -339,8 +325,7 @@ const loggedErrorHandler = {
|
|
|
339
325
|
return result;
|
|
340
326
|
},
|
|
341
327
|
takeNoteLogArgsArray: (error, callback) => {
|
|
342
|
-
const result =
|
|
343
|
-
weakmapDelete(hiddenNoteLogArgsArrays, error);
|
|
328
|
+
const result = takeLogArgsArray(error);
|
|
344
329
|
if (callback !== undefined) {
|
|
345
330
|
const callbacks = weakmapGet(hiddenNoteCallbackArrays, error);
|
|
346
331
|
if (callbacks) {
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/* eslint-disable @endo/no-polymorphic-call */
|
|
3
|
+
/* eslint-disable no-restricted-globals */
|
|
4
|
+
|
|
5
|
+
import './internal-types.js';
|
|
6
|
+
|
|
7
|
+
const { freeze } = Object;
|
|
8
|
+
const { isSafeInteger } = Number;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @template Data
|
|
12
|
+
* @typedef {object} DoublyLinkedCell
|
|
13
|
+
* A cell of a doubly-linked ring, i.e., a doubly-linked circular list.
|
|
14
|
+
* DoublyLinkedCells are not frozen, and so should be closely encapsulated by
|
|
15
|
+
* any abstraction that uses them.
|
|
16
|
+
* @property {DoublyLinkedCell<Data>} next
|
|
17
|
+
* @property {DoublyLinkedCell<Data>} prev
|
|
18
|
+
* @property {Data} data
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Makes a new self-linked cell. There are two reasons to do so:
|
|
23
|
+
* * To make the head sigil of a new initially-empty doubly-linked ring.
|
|
24
|
+
* * To make a non-sigil cell to be `spliceAfter`ed.
|
|
25
|
+
*
|
|
26
|
+
* @template Data
|
|
27
|
+
* @param {Data} data
|
|
28
|
+
* @returns {DoublyLinkedCell<Data>}
|
|
29
|
+
*/
|
|
30
|
+
const makeSelfCell = data => {
|
|
31
|
+
/** @type {Partial<DoublyLinkedCell<Data>>} */
|
|
32
|
+
const incompleteCell = {
|
|
33
|
+
next: undefined,
|
|
34
|
+
prev: undefined,
|
|
35
|
+
data,
|
|
36
|
+
};
|
|
37
|
+
const selfCell = /** @type {DoublyLinkedCell<Data>} */ (incompleteCell);
|
|
38
|
+
selfCell.next = selfCell;
|
|
39
|
+
selfCell.prev = selfCell;
|
|
40
|
+
// Not frozen!
|
|
41
|
+
return selfCell;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Splices a self-linked non-sigil cell into a ring after `prev`.
|
|
46
|
+
* `prev` could be the head sigil, or it could be some other non-sigil
|
|
47
|
+
* cell within a ring.
|
|
48
|
+
*
|
|
49
|
+
* @template Data
|
|
50
|
+
* @param {DoublyLinkedCell<Data>} prev
|
|
51
|
+
* @param {DoublyLinkedCell<Data>} selfCell
|
|
52
|
+
*/
|
|
53
|
+
const spliceAfter = (prev, selfCell) => {
|
|
54
|
+
if (prev === selfCell) {
|
|
55
|
+
throw TypeError('Cannot splice a cell into itself');
|
|
56
|
+
}
|
|
57
|
+
if (selfCell.next !== selfCell || selfCell.prev !== selfCell) {
|
|
58
|
+
throw TypeError('Expected self-linked cell');
|
|
59
|
+
}
|
|
60
|
+
const cell = selfCell;
|
|
61
|
+
// rename variable cause it isn't self-linked after this point.
|
|
62
|
+
|
|
63
|
+
const next = prev.next;
|
|
64
|
+
cell.prev = prev;
|
|
65
|
+
cell.next = next;
|
|
66
|
+
prev.next = cell;
|
|
67
|
+
next.prev = cell;
|
|
68
|
+
// Not frozen!
|
|
69
|
+
return cell;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @template Data
|
|
74
|
+
* @param {DoublyLinkedCell<Data>} cell
|
|
75
|
+
* No-op if the cell is self-linked.
|
|
76
|
+
*/
|
|
77
|
+
const spliceOut = cell => {
|
|
78
|
+
const { prev, next } = cell;
|
|
79
|
+
prev.next = next;
|
|
80
|
+
next.prev = prev;
|
|
81
|
+
cell.prev = cell;
|
|
82
|
+
cell.next = cell;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The LRUCacheMap is used within the implementation of `assert` and so
|
|
87
|
+
* at a layer below SES or harden. Thus, we give it a `WeakMap`-like interface
|
|
88
|
+
* rather than a `WeakMapStore`-like interface. To work before `lockdown`,
|
|
89
|
+
* the implementation must use `freeze` manually, but still exhaustively.
|
|
90
|
+
*
|
|
91
|
+
* It implements the WeakMap interface, and holds its keys weakly. Cached
|
|
92
|
+
* values are only held while the key is held by the user and the key/value
|
|
93
|
+
* bookkeeping cell has not been pushed off the end of the cache by `budget`
|
|
94
|
+
* number of more recently referenced cells. If the key is dropped by the user,
|
|
95
|
+
* the value will no longer be held by the cache, but the bookkeeping cell
|
|
96
|
+
* itself will stay in memory.
|
|
97
|
+
*
|
|
98
|
+
* @template {{}} K
|
|
99
|
+
* @template {unknown} V
|
|
100
|
+
* @param {number} keysBudget
|
|
101
|
+
* @returns {WeakMap<K,V>}
|
|
102
|
+
*/
|
|
103
|
+
export const makeLRUCacheMap = keysBudget => {
|
|
104
|
+
if (!isSafeInteger(keysBudget) || keysBudget < 0) {
|
|
105
|
+
throw new TypeError(
|
|
106
|
+
'keysBudget must be a safe non-negative integer number',
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
/** @typedef {DoublyLinkedCell<WeakMap<K, V> | undefined>} LRUCacheCell */
|
|
110
|
+
/** @type {WeakMap<K, LRUCacheCell>} */
|
|
111
|
+
const keyToCell = new WeakMap();
|
|
112
|
+
let size = 0; // `size` must remain <= `keysBudget`
|
|
113
|
+
// As a sigil, `head` uniquely is not in the `keyToCell` map.
|
|
114
|
+
/** @type {LRUCacheCell} */
|
|
115
|
+
const head = makeSelfCell(undefined);
|
|
116
|
+
|
|
117
|
+
const touchCell = key => {
|
|
118
|
+
const cell = keyToCell.get(key);
|
|
119
|
+
if (cell === undefined || cell.data === undefined) {
|
|
120
|
+
// Either the key was GCed, or the cell was condemned.
|
|
121
|
+
return undefined;
|
|
122
|
+
}
|
|
123
|
+
// Becomes most recently used
|
|
124
|
+
spliceOut(cell);
|
|
125
|
+
spliceAfter(head, cell);
|
|
126
|
+
return cell;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* @param {K} key
|
|
131
|
+
*/
|
|
132
|
+
const has = key => touchCell(key) !== undefined;
|
|
133
|
+
freeze(has);
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* @param {K} key
|
|
137
|
+
*/
|
|
138
|
+
// TODO Change to the following line, once our tools don't choke on `?.`.
|
|
139
|
+
// See https://github.com/endojs/endo/issues/1514
|
|
140
|
+
// const get = key => touchCell(key)?.data?.get(key);
|
|
141
|
+
const get = key => {
|
|
142
|
+
const cell = touchCell(key);
|
|
143
|
+
return cell && cell.data && cell.data.get(key);
|
|
144
|
+
};
|
|
145
|
+
freeze(get);
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @param {K} key
|
|
149
|
+
* @param {V} value
|
|
150
|
+
*/
|
|
151
|
+
const set = (key, value) => {
|
|
152
|
+
if (keysBudget < 1) {
|
|
153
|
+
// eslint-disable-next-line no-use-before-define
|
|
154
|
+
return lruCacheMap; // Implements WeakMap.set
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
let cell = touchCell(key);
|
|
158
|
+
if (cell === undefined) {
|
|
159
|
+
cell = makeSelfCell(undefined);
|
|
160
|
+
spliceAfter(head, cell); // start most recently used
|
|
161
|
+
}
|
|
162
|
+
if (!cell.data) {
|
|
163
|
+
// Either a fresh cell or a reused condemned cell.
|
|
164
|
+
size += 1;
|
|
165
|
+
// Add its data.
|
|
166
|
+
cell.data = new WeakMap();
|
|
167
|
+
// Advertise the cell for this key.
|
|
168
|
+
keyToCell.set(key, cell);
|
|
169
|
+
while (size > keysBudget) {
|
|
170
|
+
const condemned = head.prev;
|
|
171
|
+
spliceOut(condemned); // Drop least recently used
|
|
172
|
+
condemned.data = undefined;
|
|
173
|
+
size -= 1;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Update the data.
|
|
178
|
+
cell.data.set(key, value);
|
|
179
|
+
|
|
180
|
+
// eslint-disable-next-line no-use-before-define
|
|
181
|
+
return lruCacheMap; // Implements WeakMap.set
|
|
182
|
+
};
|
|
183
|
+
freeze(set);
|
|
184
|
+
|
|
185
|
+
// "delete" is a keyword.
|
|
186
|
+
/**
|
|
187
|
+
* @param {K} key
|
|
188
|
+
*/
|
|
189
|
+
const deleteIt = key => {
|
|
190
|
+
const cell = keyToCell.get(key);
|
|
191
|
+
if (cell === undefined) {
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
spliceOut(cell);
|
|
195
|
+
keyToCell.delete(key);
|
|
196
|
+
if (cell.data === undefined) {
|
|
197
|
+
// Already condemned.
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
cell.data = undefined;
|
|
202
|
+
size -= 1;
|
|
203
|
+
return true;
|
|
204
|
+
};
|
|
205
|
+
freeze(deleteIt);
|
|
206
|
+
|
|
207
|
+
const lruCacheMap = freeze({
|
|
208
|
+
has,
|
|
209
|
+
get,
|
|
210
|
+
set,
|
|
211
|
+
delete: deleteIt,
|
|
212
|
+
[Symbol.toStringTag]: 'LRUCacheMap',
|
|
213
|
+
});
|
|
214
|
+
return lruCacheMap;
|
|
215
|
+
};
|
|
216
|
+
freeze(makeLRUCacheMap);
|
|
217
|
+
|
|
218
|
+
const defaultLoggedErrorsBudget = 1000;
|
|
219
|
+
const defaultArgsPerErrorBudget = 100;
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* @param {number} [errorsBudget]
|
|
223
|
+
* @param {number} [argsPerErrorBudget]
|
|
224
|
+
*/
|
|
225
|
+
export const makeNoteLogArgsArrayKit = (
|
|
226
|
+
errorsBudget = defaultLoggedErrorsBudget,
|
|
227
|
+
argsPerErrorBudget = defaultArgsPerErrorBudget,
|
|
228
|
+
) => {
|
|
229
|
+
if (!isSafeInteger(argsPerErrorBudget) || argsPerErrorBudget < 1) {
|
|
230
|
+
throw new TypeError(
|
|
231
|
+
'argsPerErrorBudget must be a safe positive integer number',
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* @type {WeakMap<Error, LogArgs[]>}
|
|
237
|
+
*
|
|
238
|
+
* Maps from an error to an array of log args, where each log args is
|
|
239
|
+
* remembered as an annotation on that error. This can be used, for example,
|
|
240
|
+
* to keep track of additional causes of the error. The elements of any
|
|
241
|
+
* log args may include errors which are associated with further annotations.
|
|
242
|
+
* An augmented console, like the causal console of `console.js`, could
|
|
243
|
+
* then retrieve the graph of such annotations.
|
|
244
|
+
*/
|
|
245
|
+
const noteLogArgsArrayMap = makeLRUCacheMap(errorsBudget);
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* @param {Error} error
|
|
249
|
+
* @param {LogArgs} logArgs
|
|
250
|
+
*/
|
|
251
|
+
const addLogArgs = (error, logArgs) => {
|
|
252
|
+
const logArgsArray = noteLogArgsArrayMap.get(error);
|
|
253
|
+
if (logArgsArray !== undefined) {
|
|
254
|
+
if (logArgsArray.length >= argsPerErrorBudget) {
|
|
255
|
+
logArgsArray.shift();
|
|
256
|
+
}
|
|
257
|
+
logArgsArray.push(logArgs);
|
|
258
|
+
} else {
|
|
259
|
+
noteLogArgsArrayMap.set(error, [logArgs]);
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
freeze(addLogArgs);
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* @param {Error} error
|
|
266
|
+
* @returns {LogArgs[] | undefined}
|
|
267
|
+
*/
|
|
268
|
+
const takeLogArgsArray = error => {
|
|
269
|
+
const result = noteLogArgsArrayMap.get(error);
|
|
270
|
+
noteLogArgsArrayMap.delete(error);
|
|
271
|
+
return result;
|
|
272
|
+
};
|
|
273
|
+
freeze(takeLogArgsArray);
|
|
274
|
+
|
|
275
|
+
return freeze({
|
|
276
|
+
addLogArgs,
|
|
277
|
+
takeLogArgsArray,
|
|
278
|
+
});
|
|
279
|
+
};
|
|
280
|
+
freeze(makeNoteLogArgsArrayKit);
|
|
@@ -207,7 +207,7 @@ export const tameV8ErrorConstructor = (
|
|
|
207
207
|
// Mapping from error instance to the stack for that instance.
|
|
208
208
|
// The stack info is either the structured stack trace
|
|
209
209
|
// or the generated tamed stack string
|
|
210
|
-
/** @type {WeakMap<Error, ParsedStackInfo | StructuredStackInfo} */
|
|
210
|
+
/** @type {WeakMap<Error, ParsedStackInfo | StructuredStackInfo>} */
|
|
211
211
|
const stackInfos = new WeakMap();
|
|
212
212
|
|
|
213
213
|
// Use concise methods to obtain named functions without constructors.
|
package/src/eval-scope.js
CHANGED
|
@@ -72,14 +72,16 @@ export const makeEvalScopeKit = () => {
|
|
|
72
72
|
const evalScopeKit = {
|
|
73
73
|
evalScope,
|
|
74
74
|
allowNextEvalToBeUnsafe() {
|
|
75
|
-
|
|
76
|
-
|
|
75
|
+
const { revoked } = evalScopeKit;
|
|
76
|
+
if (revoked !== null) {
|
|
77
|
+
Fail`a handler did not reset allowNextEvalToBeUnsafe ${revoked.err}`;
|
|
77
78
|
}
|
|
78
79
|
// Allow next reference to eval produce the unsafe FERAL_EVAL.
|
|
79
80
|
// We avoid defineProperty because it consumes an extra stack frame taming
|
|
80
81
|
// its return value.
|
|
81
82
|
defineProperties(evalScope, oneTimeEvalProperties);
|
|
82
83
|
},
|
|
84
|
+
/** @type {null | { err: any }} */
|
|
83
85
|
revoked: null,
|
|
84
86
|
};
|
|
85
87
|
|
|
@@ -45,10 +45,8 @@ export const getAnonymousIntrinsics = () => {
|
|
|
45
45
|
|
|
46
46
|
// 9.2.4.1 %ThrowTypeError%
|
|
47
47
|
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
'callee',
|
|
51
|
-
).get;
|
|
48
|
+
const argsCalleeDesc = getOwnPropertyDescriptor(makeArguments(), 'callee');
|
|
49
|
+
const ThrowTypeError = argsCalleeDesc && argsCalleeDesc.get;
|
|
52
50
|
|
|
53
51
|
// 21.1.5.2 The %StringIteratorPrototype% Object
|
|
54
52
|
|
package/src/intrinsics.js
CHANGED
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
TypeError,
|
|
3
3
|
WeakSet,
|
|
4
4
|
arrayFilter,
|
|
5
|
+
create,
|
|
5
6
|
defineProperty,
|
|
6
7
|
entries,
|
|
7
8
|
freeze,
|
|
@@ -34,6 +35,7 @@ function initProperty(obj, name, desc) {
|
|
|
34
35
|
if (objectHasOwnProperty(obj, name)) {
|
|
35
36
|
const preDesc = getOwnPropertyDescriptor(obj, name);
|
|
36
37
|
if (
|
|
38
|
+
!preDesc ||
|
|
37
39
|
!is(preDesc.value, desc.value) ||
|
|
38
40
|
preDesc.get !== desc.get ||
|
|
39
41
|
preDesc.set !== desc.set ||
|
|
@@ -70,7 +72,8 @@ function sampleGlobals(globalObject, newPropertyNames) {
|
|
|
70
72
|
}
|
|
71
73
|
|
|
72
74
|
export const makeIntrinsicsCollector = () => {
|
|
73
|
-
|
|
75
|
+
/** @type {Record<any, any>} */
|
|
76
|
+
const intrinsics = create(null);
|
|
74
77
|
let pseudoNatives;
|
|
75
78
|
|
|
76
79
|
const addIntrinsics = newIntrinsics => {
|
package/src/lockdown-shim.js
CHANGED
|
@@ -52,6 +52,7 @@ import { assert, makeAssert } from './error/assert.js';
|
|
|
52
52
|
import { makeEnvironmentCaptor } from './environment-options.js';
|
|
53
53
|
import { getAnonymousIntrinsics } from './get-anonymous-intrinsics.js';
|
|
54
54
|
import { makeCompartmentConstructor } from './compartment-shim.js';
|
|
55
|
+
import { tameHarden } from './tame-harden.js';
|
|
55
56
|
|
|
56
57
|
/** @typedef {import('../types.js').LockdownOptions} LockdownOptions */
|
|
57
58
|
|
|
@@ -67,7 +68,7 @@ let priorLockdown;
|
|
|
67
68
|
* @param {T} ref
|
|
68
69
|
* @returns {T}
|
|
69
70
|
*/
|
|
70
|
-
const
|
|
71
|
+
const safeHarden = makeHardener();
|
|
71
72
|
|
|
72
73
|
/**
|
|
73
74
|
* @callback Transform
|
|
@@ -174,6 +175,7 @@ export const repairIntrinsics = (options = {}) => {
|
|
|
174
175
|
/** @param {string} debugName */
|
|
175
176
|
debugName => debugName !== '',
|
|
176
177
|
),
|
|
178
|
+
__hardenTaming__ = getenv('LOCKDOWN_HARDEN_TAMING', 'safe'),
|
|
177
179
|
dateTaming = 'safe', // deprecated
|
|
178
180
|
mathTaming = 'safe', // deprecated
|
|
179
181
|
...extraOptions
|
|
@@ -264,7 +266,8 @@ export const repairIntrinsics = (options = {}) => {
|
|
|
264
266
|
const { addIntrinsics, completePrototypes, finalIntrinsics } =
|
|
265
267
|
makeIntrinsicsCollector();
|
|
266
268
|
|
|
267
|
-
|
|
269
|
+
const tamedHarden = tameHarden(safeHarden, __hardenTaming__);
|
|
270
|
+
addIntrinsics({ harden: tamedHarden });
|
|
268
271
|
|
|
269
272
|
addIntrinsics(tameFunctionConstructors());
|
|
270
273
|
|
|
@@ -377,7 +380,7 @@ export const repairIntrinsics = (options = {}) => {
|
|
|
377
380
|
|
|
378
381
|
// Finally register and optionally freeze all the intrinsics. This
|
|
379
382
|
// must be the operation that modifies the intrinsics.
|
|
380
|
-
|
|
383
|
+
tamedHarden(intrinsics);
|
|
381
384
|
|
|
382
385
|
// Reveal harden after lockdown.
|
|
383
386
|
// Harden is dangerous before lockdown because hardening just
|
|
@@ -387,7 +390,7 @@ export const repairIntrinsics = (options = {}) => {
|
|
|
387
390
|
// scope signals whether such code should attempt to use harden in the
|
|
388
391
|
// defense of its own API.
|
|
389
392
|
// @ts-ignore harden not yet recognized on globalThis.
|
|
390
|
-
globalThis.harden =
|
|
393
|
+
globalThis.harden = tamedHarden;
|
|
391
394
|
|
|
392
395
|
// Returning `true` indicates that this is a JS to SES transition.
|
|
393
396
|
return true;
|
package/src/make-hardener.js
CHANGED
|
@@ -27,6 +27,7 @@ import {
|
|
|
27
27
|
TypeError,
|
|
28
28
|
WeakMap,
|
|
29
29
|
WeakSet,
|
|
30
|
+
globalThis,
|
|
30
31
|
apply,
|
|
31
32
|
arrayForEach,
|
|
32
33
|
defineProperty,
|
|
@@ -127,6 +128,12 @@ const freezeTypedArray = array => {
|
|
|
127
128
|
* @returns {Harden}
|
|
128
129
|
*/
|
|
129
130
|
export const makeHardener = () => {
|
|
131
|
+
// Use a native hardener if possible.
|
|
132
|
+
if (typeof globalThis.harden === 'function') {
|
|
133
|
+
const safeHarden = globalThis.harden;
|
|
134
|
+
return safeHarden;
|
|
135
|
+
}
|
|
136
|
+
|
|
130
137
|
const hardened = new WeakSet();
|
|
131
138
|
|
|
132
139
|
const { harden } = {
|
|
@@ -19,15 +19,15 @@ const { Fail } = assert;
|
|
|
19
19
|
* @param {object} options
|
|
20
20
|
* @param {object} options.globalObject
|
|
21
21
|
* @param {object} [options.moduleLexicals]
|
|
22
|
-
* @param {Array<Transform>} [options.globalTransforms]
|
|
23
|
-
* @param {
|
|
22
|
+
* @param {Array<import('./lockdown-shim.js').Transform>} [options.globalTransforms]
|
|
23
|
+
* @param {boolean} [options.sloppyGlobalsMode]
|
|
24
24
|
*/
|
|
25
25
|
export const makeSafeEvaluator = ({
|
|
26
26
|
globalObject,
|
|
27
27
|
moduleLexicals = {},
|
|
28
28
|
globalTransforms = [],
|
|
29
29
|
sloppyGlobalsMode = false,
|
|
30
|
-
}
|
|
30
|
+
}) => {
|
|
31
31
|
const scopeTerminator = sloppyGlobalsMode
|
|
32
32
|
? createSloppyGlobalsScopeTerminator(globalObject)
|
|
33
33
|
: strictScopeTerminator;
|
|
@@ -54,9 +54,10 @@ export const makeSafeEvaluator = ({
|
|
|
54
54
|
/**
|
|
55
55
|
* @param {string} source
|
|
56
56
|
* @param {object} [options]
|
|
57
|
-
* @param {Array<Transform>} [options.localTransforms]
|
|
57
|
+
* @param {Array<import('./lockdown-shim.js').Transform>} [options.localTransforms]
|
|
58
58
|
*/
|
|
59
|
-
const safeEvaluate = (source,
|
|
59
|
+
const safeEvaluate = (source, options) => {
|
|
60
|
+
const { localTransforms = [] } = options || {};
|
|
60
61
|
provideEvaluate();
|
|
61
62
|
|
|
62
63
|
// Execute the mandatory transforms last to ensure that any rewritten code
|