-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathintrinsics.js
174 lines (158 loc) · 5.23 KB
/
intrinsics.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import {
TypeError,
WeakSet,
arrayFilter,
create,
defineProperty,
entries,
freeze,
getOwnPropertyDescriptor,
getOwnPropertyDescriptors,
globalThis,
is,
isObject,
objectHasOwnProperty,
values,
weaksetHas,
} from './commons.js';
import {
constantProperties,
sharedGlobalPropertyNames,
universalPropertyNames,
permitted,
} from './permits.js';
const isFunction = obj => typeof obj === 'function';
// Like defineProperty, but throws if it would modify an existing property.
// We use this to ensure that two conflicting attempts to define the same
// property throws, causing SES initialization to fail. Otherwise, a
// conflict between, for example, two of SES's internal whitelists might
// get masked as one overwrites the other. Accordingly, the thrown error
// complains of a "Conflicting definition".
function initProperty(obj, name, desc) {
if (objectHasOwnProperty(obj, name)) {
const preDesc = getOwnPropertyDescriptor(obj, name);
if (
!preDesc ||
!is(preDesc.value, desc.value) ||
preDesc.get !== desc.get ||
preDesc.set !== desc.set ||
preDesc.writable !== desc.writable ||
preDesc.enumerable !== desc.enumerable ||
preDesc.configurable !== desc.configurable
) {
throw TypeError(`Conflicting definitions of ${name}`);
}
}
defineProperty(obj, name, desc);
}
// Like defineProperties, but throws if it would modify an existing property.
// This ensures that the intrinsics added to the intrinsics collector object
// graph do not overlap.
function initProperties(obj, descs) {
for (const [name, desc] of entries(descs)) {
initProperty(obj, name, desc);
}
}
// sampleGlobals creates an intrinsics object, suitable for
// interinsicsCollector.addIntrinsics, from the named properties of a global
// object.
function sampleGlobals(globalObject, newPropertyNames) {
const newIntrinsics = { __proto__: null };
for (const [globalName, intrinsicName] of entries(newPropertyNames)) {
if (objectHasOwnProperty(globalObject, globalName)) {
newIntrinsics[intrinsicName] = globalObject[globalName];
}
}
return newIntrinsics;
}
export const makeIntrinsicsCollector = () => {
/** @type {Record<any, any>} */
const intrinsics = create(null);
let pseudoNatives;
const addIntrinsics = newIntrinsics => {
initProperties(intrinsics, getOwnPropertyDescriptors(newIntrinsics));
};
freeze(addIntrinsics);
// For each intrinsic, if it has a `.prototype` property, use the
// whitelist to find out the intrinsic name for that prototype and add it
// to the intrinsics.
const completePrototypes = () => {
for (const [name, intrinsic] of entries(intrinsics)) {
if (!isObject(intrinsic)) {
// eslint-disable-next-line no-continue
continue;
}
if (!objectHasOwnProperty(intrinsic, 'prototype')) {
// eslint-disable-next-line no-continue
continue;
}
const permit = permitted[name];
if (typeof permit !== 'object') {
throw TypeError(`Expected permit object at whitelist.${name}`);
}
const namePrototype = permit.prototype;
if (!namePrototype) {
throw TypeError(`${name}.prototype property not whitelisted`);
}
if (
typeof namePrototype !== 'string' ||
!objectHasOwnProperty(permitted, namePrototype)
) {
throw TypeError(`Unrecognized ${name}.prototype whitelist entry`);
}
const intrinsicPrototype = intrinsic.prototype;
if (objectHasOwnProperty(intrinsics, namePrototype)) {
if (intrinsics[namePrototype] !== intrinsicPrototype) {
throw TypeError(`Conflicting bindings of ${namePrototype}`);
}
// eslint-disable-next-line no-continue
continue;
}
intrinsics[namePrototype] = intrinsicPrototype;
}
};
freeze(completePrototypes);
const finalIntrinsics = () => {
freeze(intrinsics);
pseudoNatives = new WeakSet(arrayFilter(values(intrinsics), isFunction));
return intrinsics;
};
freeze(finalIntrinsics);
const isPseudoNative = obj => {
if (!pseudoNatives) {
throw TypeError(
'isPseudoNative can only be called after finalIntrinsics',
);
}
return weaksetHas(pseudoNatives, obj);
};
freeze(isPseudoNative);
const intrinsicsCollector = {
addIntrinsics,
completePrototypes,
finalIntrinsics,
isPseudoNative,
};
freeze(intrinsicsCollector);
addIntrinsics(constantProperties);
addIntrinsics(sampleGlobals(globalThis, universalPropertyNames));
return intrinsicsCollector;
};
/**
* getGlobalIntrinsics()
* Doesn't tame, delete, or modify anything. Samples globalObject to create an
* intrinsics record containing only the whitelisted global variables, listed
* by the intrinsic names appropriate for new globals, i.e., the globals of
* newly constructed compartments.
*
* WARNING:
* If run before lockdown, the returned intrinsics record will carry the
* *original* unsafe (feral, untamed) bindings of these global variables.
*
* @param {object} globalObject
*/
export const getGlobalIntrinsics = globalObject => {
const { addIntrinsics, finalIntrinsics } = makeIntrinsicsCollector();
addIntrinsics(sampleGlobals(globalObject, sharedGlobalPropertyNames));
return finalIntrinsics();
};