-
Notifications
You must be signed in to change notification settings - Fork 27
/
secure-proxy-handler.ts
235 lines (228 loc) · 9.33 KB
/
secure-proxy-handler.ts
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import {
isUndefined,
ObjectDefineProperty,
setPrototypeOf,
getPrototypeOf,
isExtensible,
getOwnPropertyDescriptor,
getOwnPropertyNames,
getOwnPropertySymbols,
preventExtensions,
isFunction,
getOwnPropertyDescriptors,
freeze,
isTrue,
} from './shared';
import {
SecureEnvironment,
SecureProxyTarget,
SecureValue,
SecureObject,
RawConstructor,
RawFunction,
SecureShadowTarget,
} from './environment';
export function getSecureDescriptor(descriptor: PropertyDescriptor, env: SecureEnvironment): PropertyDescriptor {
const { value, get, set, writable } = descriptor;
if (isUndefined(writable)) {
// we are dealing with accessors
if (!isUndefined(set)) {
descriptor.set = env.getSecureFunction(set);
}
if (!isUndefined(get)) {
descriptor.get = env.getSecureFunction(get);
}
} else {
descriptor.value = isFunction(value) ?
// we are dealing with a method (optimization)
env.getSecureFunction(value) : env.getSecureValue(value);
}
return descriptor;
}
// equivalent to Object.getOwnPropertyDescriptor, but looks into the whole proto chain
function getPropertyDescriptor(o: any, p: PropertyKey): PropertyDescriptor | undefined {
do {
const d = getOwnPropertyDescriptor(o, p);
if (!isUndefined(d)) {
return d;
}
o = getPrototypeOf(o);
} while (o !== null);
return undefined;
}
function copySecureOwnDescriptors(env: SecureEnvironment, shadowTarget: SecureShadowTarget, target: SecureProxyTarget) {
// TODO: typescript definition for getOwnPropertyDescriptors is wrong, it should include symbols
const descriptors = getOwnPropertyDescriptors(target);
for (const key in descriptors) {
let originalDescriptor = descriptors[key];
originalDescriptor = getSecureDescriptor(originalDescriptor, env);
const shadowTargetDescriptor = getOwnPropertyDescriptor(shadowTarget, key);
if (!isUndefined(shadowTargetDescriptor)) {
if (isTrue(shadowTargetDescriptor.configurable)) {
ObjectDefineProperty(shadowTarget, key, originalDescriptor);
} else if (isTrue(shadowTargetDescriptor.writable)) {
// just in case
shadowTarget[key] = originalDescriptor.value;
} else {
// ignoring... since it is non configurable and non-writable
// usually, arguments, callee, etc.
}
} else {
ObjectDefineProperty(shadowTarget, key, originalDescriptor);
}
}
}
const noop = () => undefined;
/**
* identity preserved through this membrane:
* - symbols
*/
export class SecureProxyHandler implements ProxyHandler<SecureProxyTarget> {
// original target for the proxy
private readonly target: SecureProxyTarget;
// environment object that controls the realm
private readonly env: SecureEnvironment;
constructor(env: SecureEnvironment, target: SecureProxyTarget) {
this.target = target;
this.env = env;
}
// initialization used to avoid the initialization cost
// of an object graph, we want to do it when the
// first interaction happens.
private initialize(shadowTarget: SecureShadowTarget) {
const { target, env } = this;
// adjusting the proto chain of the shadowTarget (recursively)
const rawProto = getPrototypeOf(target);
setPrototypeOf(shadowTarget, env.getSecureValue(rawProto));
// defining own descriptors
copySecureOwnDescriptors(env, shadowTarget, target);
// once the initialization is executed once... the rest is just noop
this.initialize = noop;
// future optimization: hoping that proxies with frozen handlers can be faster
freeze(this);
}
get(shadowTarget: SecureShadowTarget, key: PropertyKey): SecureValue {
this.initialize(shadowTarget);
const desc = getPropertyDescriptor(shadowTarget, key);
if (isUndefined(desc)) {
return desc;
}
const { get } = desc;
if (!isUndefined(get)) {
// this.target is already in registered in the map, which means it
// will always return a valid secure object without having to create it.
const sec = this.env.getSecureValue(this.target);
return Reflect.apply(get, sec, []);
}
return desc.value;
}
set(shadowTarget: SecureShadowTarget, key: PropertyKey, value: SecureValue): boolean {
this.initialize(shadowTarget);
const desc = getPropertyDescriptor(shadowTarget, key);
if (isUndefined(desc)) {
if (isExtensible(shadowTarget)) {
// it should be a descriptor installed on the current shadowTarget
ObjectDefineProperty(shadowTarget, key, {
value,
configurable: true,
enumerable: true,
writable: true,
});
} else {
// non-extensible should throw in strict mode
// TypeError: Cannot add property ${key}, object is not extensible
return false;
}
} else {
// descriptor exists in the shadowRoot or proto chain
const { set, get, writable } = desc;
if (writable === false) {
// TypeError: Cannot assign to read only property '${key}' of object
return false;
} else if (!isUndefined(set)) {
// a setter is available, just call it:
// this.target is already in registered in the map, which means it
// will always return a valid secure object without having to create it.
const sec = this.env.getSecureValue(this.target);
Reflect.apply(set, sec, [value]);
} else if (!isUndefined(get)) {
// a getter without a setter should fail to set in strict mode
// TypeError: Cannot set property ${key} of object which has only a getter
return false;
} else {
// the descriptor is writable, just assign it
shadowTarget[key] = value;
}
}
return true;
}
deleteProperty(shadowTarget: SecureShadowTarget, key: PropertyKey): boolean {
this.initialize(shadowTarget);
delete shadowTarget[key];
return true;
}
apply(shadowTarget: SecureShadowTarget, thisArg: SecureValue, argArray: SecureValue[]): SecureValue {
const { target, env } = this;
this.initialize(shadowTarget);
const rawThisArg = env.getRawValue(thisArg);
const rawArgArray = env.getRawArray(argArray);
const raw = Reflect.apply(target as RawFunction, rawThisArg, rawArgArray);
return env.getSecureValue(raw) as SecureValue;
}
construct(shadowTarget: SecureShadowTarget, argArray: SecureValue[], newTarget: SecureObject): SecureObject {
const { target: RawCtor, env } = this;
this.initialize(shadowTarget);
if (newTarget === undefined) {
throw TypeError();
}
const rawArgArray = env.getRawArray(argArray);
const rawNewTarget = env.getRawValue(newTarget);
const raw = Reflect.construct(RawCtor as RawConstructor, rawArgArray, rawNewTarget);
const sec = env.getSecureValue(raw);
return sec as SecureObject;
}
has(shadowTarget: SecureShadowTarget, key: PropertyKey): boolean {
this.initialize(shadowTarget);
return key in shadowTarget;
}
ownKeys(shadowTarget: SecureShadowTarget): (string | symbol)[] {
this.initialize(shadowTarget);
return [
...getOwnPropertyNames(shadowTarget),
...getOwnPropertySymbols(shadowTarget),
];
}
isExtensible(shadowTarget: SecureShadowTarget): boolean {
this.initialize(shadowTarget);
// No DOM API is non-extensible, but in the sandbox, the author
// might want to make them non-extensible
return isExtensible(shadowTarget);
}
getOwnPropertyDescriptor(shadowTarget: SecureShadowTarget, key: PropertyKey): PropertyDescriptor | undefined {
this.initialize(shadowTarget);
return getOwnPropertyDescriptor(shadowTarget, key);
}
getPrototypeOf(shadowTarget: SecureShadowTarget): SecureValue {
this.initialize(shadowTarget);
// nothing to be done here since the shadowTarget must have the right proto chain
return getPrototypeOf(shadowTarget);
}
setPrototypeOf(shadowTarget: SecureShadowTarget, prototype: SecureValue): boolean {
this.initialize(shadowTarget);
// this operation can only affect the env object graph
setPrototypeOf(shadowTarget, prototype);
return true;
}
preventExtensions(shadowTarget: SecureShadowTarget): boolean {
// this operation can only affect the env object graph
this.initialize(shadowTarget);
preventExtensions(shadowTarget);
return true;
}
defineProperty(shadowTarget: SecureShadowTarget, key: PropertyKey, descriptor: PropertyDescriptor): boolean {
this.initialize(shadowTarget);
// this operation can only affect the env object graph
ObjectDefineProperty(shadowTarget, key, descriptor);
return true;
}
}