-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathindex.ts
More file actions
261 lines (223 loc) · 7.84 KB
/
index.ts
File metadata and controls
261 lines (223 loc) · 7.84 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
type PrototypeOwner = Node | ShadowRoot | MutationObserver | Element;
type TypeofPrototypeOwner =
| typeof Node
| typeof ShadowRoot
| typeof MutationObserver
| typeof Element;
type BasePrototypeCache = {
Node: typeof Node.prototype;
ShadowRoot: typeof ShadowRoot.prototype;
MutationObserver: typeof MutationObserver.prototype;
Element: typeof Element.prototype;
};
const testableAccessors = {
Node: ['childNodes', 'parentNode', 'parentElement', 'textContent'] as const,
ShadowRoot: ['host', 'styleSheets'] as const,
Element: ['shadowRoot', 'querySelector', 'querySelectorAll'] as const,
MutationObserver: [] as const,
} as const;
const testableMethods = {
Node: ['contains', 'getRootNode'] as const,
ShadowRoot: ['getSelection'],
Element: [],
MutationObserver: ['constructor'],
} as const;
const untaintedBasePrototype: Partial<BasePrototypeCache> = {};
type WindowWithZone = typeof globalThis & {
Zone?: {
__symbol__?: (key: string) => string;
};
};
type WindowWithUnpatchedSymbols = typeof globalThis &
Record<string, TypeofPrototypeOwner>;
/*
Angular zone patches many things and can pass the untainted checks below, causing performance issues
Angular zone, puts the unpatched originals on the window, and the names for hose on the zone object.
So, we get the unpatched versions from the window object if they exist.
You can rename Zone, but this is a good enough proxy to avoid going to an iframe to get the untainted versions.
see: https://github.com/angular/angular/issues/26948
*/
function angularZoneUnpatchedAlternative(key: keyof BasePrototypeCache) {
const angularUnpatchedVersionSymbol = (
globalThis as WindowWithZone
)?.Zone?.__symbol__?.(key);
if (
angularUnpatchedVersionSymbol &&
(globalThis as WindowWithUnpatchedSymbols)[angularUnpatchedVersionSymbol]
) {
return (globalThis as WindowWithUnpatchedSymbols)[
angularUnpatchedVersionSymbol
];
} else {
return undefined;
}
}
export function getUntaintedPrototype<T extends keyof BasePrototypeCache>(
key: T,
): BasePrototypeCache[T] {
if (untaintedBasePrototype[key])
return untaintedBasePrototype[key] as BasePrototypeCache[T];
const defaultObj =
angularZoneUnpatchedAlternative(key) ||
(globalThis[key] as TypeofPrototypeOwner);
const defaultPrototype = defaultObj.prototype as BasePrototypeCache[T];
// use list of testable accessors to check if the prototype is tainted
const accessorNames =
key in testableAccessors ? testableAccessors[key] : undefined;
const isUntaintedAccessors = Boolean(
accessorNames &&
// @ts-expect-error 2345
accessorNames.every((accessor: keyof typeof defaultPrototype) =>
Boolean(
Object.getOwnPropertyDescriptor(defaultPrototype, accessor)
?.get?.toString()
.includes('[native code]'),
),
),
);
const methodNames = key in testableMethods ? testableMethods[key] : undefined;
const isUntaintedMethods = Boolean(
methodNames &&
methodNames.every(
// @ts-expect-error 2345
(method: keyof typeof defaultPrototype) =>
typeof defaultPrototype[method] === 'function' &&
defaultPrototype[method]?.toString().includes('[native code]'),
),
);
const isUntainted = isUntaintedAccessors && isUntaintedMethods;
// we're going to default to what we do have
let impl: BasePrototypeCache[T] =
defaultObj.prototype as BasePrototypeCache[T];
// but if it is tainted
if (!isUntainted) {
// try to load a fresh copy from a sandbox iframe
let iframeEl: HTMLIFrameElement | undefined = undefined;
try {
iframeEl = document.createElement('iframe');
iframeEl.hidden = true;
document.body.appendChild(iframeEl);
const win = iframeEl.contentWindow;
if (win) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
const candidate = (win as any)[key].prototype as BasePrototypeCache[T];
if (candidate) {
impl = candidate;
}
}
} finally {
if (iframeEl) {
document.body.removeChild(iframeEl);
}
}
}
untaintedBasePrototype[key] = impl;
return impl;
}
const untaintedAccessorCache: Record<
string,
(this: PrototypeOwner, ...args: unknown[]) => unknown
> = {};
export function getUntaintedAccessor<
K extends keyof BasePrototypeCache,
T extends keyof BasePrototypeCache[K],
>(
key: K,
instance: BasePrototypeCache[K],
accessor: T,
): BasePrototypeCache[K][T] {
const cacheKey = `${key}.${String(accessor)}`;
if (untaintedAccessorCache[cacheKey])
return untaintedAccessorCache[cacheKey].call(
instance,
) as BasePrototypeCache[K][T];
const untaintedPrototype = getUntaintedPrototype(key);
// eslint-disable-next-line @typescript-eslint/unbound-method
const untaintedAccessor = Object.getOwnPropertyDescriptor(
untaintedPrototype,
accessor,
)?.get;
if (!untaintedAccessor) return instance[accessor];
untaintedAccessorCache[cacheKey] = untaintedAccessor;
return untaintedAccessor.call(instance) as BasePrototypeCache[K][T];
}
type BaseMethod<K extends keyof BasePrototypeCache> = (
this: BasePrototypeCache[K],
...args: unknown[]
) => unknown;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const untaintedMethodCache: Record<string, BaseMethod<any>> = {};
export function getUntaintedMethod<
K extends keyof BasePrototypeCache,
T extends keyof BasePrototypeCache[K],
>(
key: K,
instance: BasePrototypeCache[K],
method: T,
): BasePrototypeCache[K][T] {
const cacheKey = `${key}.${String(method)}`;
if (untaintedMethodCache[cacheKey])
return untaintedMethodCache[cacheKey].bind(
instance,
) as BasePrototypeCache[K][T];
const untaintedPrototype = getUntaintedPrototype(key);
const untaintedMethod = untaintedPrototype[method];
if (typeof untaintedMethod !== 'function') return instance[method];
untaintedMethodCache[cacheKey] = untaintedMethod as BaseMethod<K>;
return untaintedMethod.bind(instance) as BasePrototypeCache[K][T];
}
export function childNodes(n: Node): NodeListOf<Node> {
return getUntaintedAccessor('Node', n, 'childNodes');
}
export function parentNode(n: Node): ParentNode | null {
return getUntaintedAccessor('Node', n, 'parentNode');
}
export function parentElement(n: Node): HTMLElement | null {
return getUntaintedAccessor('Node', n, 'parentElement');
}
export function textContent(n: Node): string | null {
return getUntaintedAccessor('Node', n, 'textContent');
}
export function contains(n: Node, other: Node): boolean {
return getUntaintedMethod('Node', n, 'contains')(other);
}
export function getRootNode(n: Node): Node {
return getUntaintedMethod('Node', n, 'getRootNode')();
}
export function host(n: ShadowRoot): Element | null {
if (!n || !('host' in n)) return null;
return getUntaintedAccessor('ShadowRoot', n, 'host');
}
export function styleSheets(n: ShadowRoot): StyleSheetList {
return n.styleSheets;
}
export function shadowRoot(n: Node): ShadowRoot | null {
if (!n || !('shadowRoot' in n)) return null;
return getUntaintedAccessor('Element', n as Element, 'shadowRoot');
}
export function querySelector(n: Element, selectors: string): Element | null {
return getUntaintedAccessor('Element', n, 'querySelector')(selectors);
}
export function querySelectorAll(
n: Element,
selectors: string,
): NodeListOf<Element> {
return getUntaintedAccessor('Element', n, 'querySelectorAll')(selectors);
}
export function mutationObserverCtor(): (typeof MutationObserver)['prototype']['constructor'] {
return getUntaintedPrototype('MutationObserver').constructor;
}
export default {
childNodes,
parentNode,
parentElement,
textContent,
contains,
getRootNode,
host,
styleSheets,
shadowRoot,
querySelector,
querySelectorAll,
mutationObserver: mutationObserverCtor,
};