forked from DevExpress/testcafe-hammerhead
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
317 lines (240 loc) · 13.4 KB
/
index.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import SandboxBase from '../../base';
import IframeSandbox from '../../iframe';
import nativeMethods from '../../native-methods';
import domProcessor from '../../../dom-processor';
import settings from '../../../settings';
import { isIE } from '../../../utils/browser';
import { isIframeWithoutSrc, getFrameElement, isImgElement, isShadowUIElement } from '../../../utils/dom';
import DocumentWriter from './writer';
import ShadowUI from './../../shadow-ui';
import INTERNAL_PROPS from '../../../../processing/dom/internal-properties';
import LocationAccessorsInstrumentation from '../../code-instrumentation/location';
import { overrideDescriptor, createOverriddenDescriptor, overrideFunction } from '../../../utils/overriding';
import NodeSandbox from '../index';
import { getDestinationUrl, isSpecialPage, convertToProxyUrl, getCrossDomainProxyOrigin } from '../../../utils/url';
import { getReferrer } from '../../../utils/destination-location';
import DocumentTitleStorageInitializer from './title-storage-initializer';
import CookieSandbox from '../../cookie';
export default class DocumentSandbox extends SandboxBase {
writers = new Map<Document, DocumentWriter>();
constructor (private readonly _nodeSandbox: NodeSandbox,
private readonly _shadowUI: ShadowUI,
private readonly _cookieSandbox: CookieSandbox,
private readonly _iframeSandbox: IframeSandbox,
private readonly _documentTitleStorageInitializer?: DocumentTitleStorageInitializer) {
super();
}
static forceProxySrcForImageIfNecessary (element: Element): void {
if (isImgElement(element) && settings.get().forceProxySrcForImage)
element[INTERNAL_PROPS.forceProxySrcForImage] = true;
}
private static _isDocumentInDesignMode (doc: HTMLDocument): boolean {
return doc.designMode === 'on';
}
private _isUninitializedIframeWithoutSrc (win: Window): boolean {
const frameElement = getFrameElement(win);
return win !== win.top && frameElement && isIframeWithoutSrc(frameElement) &&
!IframeSandbox.isIframeInitialized(frameElement as HTMLIFrameElement);
}
private _beforeDocumentCleaned () {
this._nodeSandbox.mutation.onBeforeDocumentCleaned(this.document);
}
private _onDocumentClosed () {
this._nodeSandbox.mutation.onDocumentClosed(this.document);
}
private static _shouldEmitDocumentCleanedEvents (doc) {
if (isIE) {
if (doc.readyState !== 'loading')
return true;
const window = doc.defaultView;
if (window[INTERNAL_PROPS.documentWasCleaned])
return false;
const iframe = window && getFrameElement(window);
return iframe && isIframeWithoutSrc(iframe);
}
return doc.readyState !== 'loading' && doc.readyState !== 'uninitialized';
}
private _performDocumentWrite (doc: Document, args, ln?: boolean) {
const shouldEmitEvents = DocumentSandbox._shouldEmitDocumentCleanedEvents(this.document);
if (shouldEmitEvents)
this._beforeDocumentCleaned();
const result = this.writers.get(doc).write(args, ln, shouldEmitEvents);
// NOTE: B234357
if (!shouldEmitEvents)
this._nodeSandbox.processNodes(null, this.document);
return result;
}
private static _definePropertyDescriptor (owner, childOfOwner, prop, overriddenDescriptor) {
// NOTE: The 'URL', 'domain' and 'referrer' properties are non configurable in IE and Edge
if (!overriddenDescriptor.configurable) {
// NOTE: property doesn't redefined yet
if (!childOfOwner.hasOwnProperty(prop))
nativeMethods.objectDefineProperty(childOfOwner, prop, overriddenDescriptor);
}
else
nativeMethods.objectDefineProperty(owner, prop, overriddenDescriptor);
}
iframeDocumentOpen (window, document, args) {
const iframe = window.frameElement;
const result = nativeMethods.documentOpen.apply(document, args);
nativeMethods.objectDefineProperty(window, INTERNAL_PROPS.documentWasCleaned, { value: true, configurable: true });
this._nodeSandbox.iframeSandbox.onIframeBeganToRun(iframe);
return result;
}
attach (window, document, partialInitializationForNotLoadedIframe = false) {
if (!this.writers.size)
super.attach(window, document);
if (!this.writers.has(document)) {
this.writers.set(document, new DocumentWriter(window, document));
this._nodeSandbox.mutation.on(this._nodeSandbox.mutation.BEFORE_DOCUMENT_CLEANED_EVENT, () => {
this.writers.set(document, new DocumentWriter(window, document));
});
}
const documentSandbox = this;
const docPrototype = window.Document.prototype;
const overriddenMethods = {
open: function (this: Document, ...args: [string?, string?, string?, boolean?]) {
const isUninitializedIframe = documentSandbox._isUninitializedIframeWithoutSrc(window);
if (!isUninitializedIframe)
documentSandbox._beforeDocumentCleaned();
if (isIE)
return window.parent[INTERNAL_PROPS.hammerhead].sandbox.node.doc.iframeDocumentOpen(window, this, args);
const result = nativeMethods.documentOpen.apply(this, args);
// NOTE: Chrome does not remove the "%hammerhead%" property from window
// after document.open call
const objectDefinePropertyFn = window[INTERNAL_PROPS.hammerhead]
? window[INTERNAL_PROPS.hammerhead].nativeMethods.objectDefineProperty
: window.Object.defineProperty;
objectDefinePropertyFn(window, INTERNAL_PROPS.documentWasCleaned, { value: true, configurable: true });
if (!isUninitializedIframe)
documentSandbox._nodeSandbox.mutation.onDocumentCleaned(window, this);
else {
const iframe = getFrameElement(window);
if (iframe)
documentSandbox._iframeSandbox.processIframe(iframe);
}
return result;
},
close: function (this: Document, ...args: []) {
// NOTE: IE11 raise the "load" event only when the document.close method is called. We need to
// restore the overridden document.open and document.write methods before Hammerhead injection, if the
// window is not initialized.
if (isIE && !IframeSandbox.isWindowInited(window))
nativeMethods.restoreDocumentMeths(window, this);
// NOTE: IE doesn't run scripts in iframe if iframe.documentContent.designMode equals 'on' (GH-871)
if (DocumentSandbox._isDocumentInDesignMode(this))
ShadowUI.removeSelfRemovingScripts(this);
const result = nativeMethods.documentClose.apply(this, args);
if (!documentSandbox._isUninitializedIframeWithoutSrc(window))
documentSandbox._onDocumentClosed();
const iframe = getFrameElement(window);
// NOTE: Firefox misses the Hammerhead instance after the iframe.contentDocument.close function calling (GH-1821)
if (iframe)
documentSandbox._nodeSandbox.iframeSandbox.onIframeBeganToRun(iframe as HTMLIFrameElement);
return result;
},
write: function () {
return documentSandbox._performDocumentWrite(this, arguments);
},
writeln: function () {
return documentSandbox._performDocumentWrite(this, arguments, true);
}
};
overrideFunction(window[nativeMethods.documentOpenPropOwnerName].prototype, 'open', overriddenMethods.open);
overrideFunction(window[nativeMethods.documentClosePropOwnerName].prototype, 'close', overriddenMethods.close);
overrideFunction(window[nativeMethods.documentWritePropOwnerName].prototype, 'write', overriddenMethods.write);
overrideFunction(window[nativeMethods.documentWriteLnPropOwnerName].prototype, 'writeln', overriddenMethods.writeln);
overrideFunction(document, 'open', overriddenMethods.open);
overrideFunction(document, 'close', overriddenMethods.close);
overrideFunction(document, 'write', overriddenMethods.write);
overrideFunction(document, 'writeln', overriddenMethods.writeln);
if (document.open !== overriddenMethods.open)
overrideFunction(document, 'open', overriddenMethods.open);
overrideFunction(docPrototype, 'createElement', function (this: Document, ...args: [string, ElementCreationOptions?]) {
const el = nativeMethods.createElement.apply(this, args);
DocumentSandbox.forceProxySrcForImageIfNecessary(el);
domProcessor.processElement(el, convertToProxyUrl);
documentSandbox._nodeSandbox.processNodes(el);
return el;
});
overrideFunction(docPrototype, 'createElementNS', function (this: Document, ...args: [string, string, (string | ElementCreationOptions)?]) {
const el = nativeMethods.createElementNS.apply(this, args);
DocumentSandbox.forceProxySrcForImageIfNecessary(el);
domProcessor.processElement(el, convertToProxyUrl);
documentSandbox._nodeSandbox.processNodes(el);
return el;
});
overrideFunction(docPrototype, 'createDocumentFragment', function (this: Document, ...args: []) {
const fragment = nativeMethods.createDocumentFragment.apply(this, args);
documentSandbox._nodeSandbox.processNodes(fragment);
return fragment;
});
const htmlDocPrototype = window.HTMLDocument.prototype;
let storedDomain = '';
if (nativeMethods.documentDocumentURIGetter) {
overrideDescriptor(docPrototype, 'documentURI', {
getter: function () {
return getDestinationUrl(nativeMethods.documentDocumentURIGetter.call(this));
}
});
}
const referrerOverriddenDescriptor = createOverriddenDescriptor(docPrototype, 'referrer', {
getter: function () {
const referrer = getDestinationUrl(nativeMethods.documentReferrerGetter.call(this));
if (referrer === getCrossDomainProxyOrigin() + '/')
return getReferrer();
return isSpecialPage(referrer) ? '' : referrer;
}
});
DocumentSandbox._definePropertyDescriptor(docPrototype, htmlDocPrototype, 'referrer', referrerOverriddenDescriptor);
const urlOverriddenDescriptor = createOverriddenDescriptor(docPrototype, 'URL', {
getter: function () {
// eslint-disable-next-line no-restricted-properties
return LocationAccessorsInstrumentation.getLocationWrapper(this).href;
}
});
DocumentSandbox._definePropertyDescriptor(docPrototype, htmlDocPrototype, 'URL', urlOverriddenDescriptor);
const domainPropertyOwner = nativeMethods.objectHasOwnProperty.call(docPrototype, 'domain')
? docPrototype
: htmlDocPrototype;
const domainOverriddenDescriptor = createOverriddenDescriptor(domainPropertyOwner, 'domain', {
getter: () => {
// eslint-disable-next-line no-restricted-properties
return storedDomain || LocationAccessorsInstrumentation.getLocationWrapper(window).hostname;
},
setter: value => {
storedDomain = value;
}
});
DocumentSandbox._definePropertyDescriptor(domainPropertyOwner, htmlDocPrototype, 'domain', domainOverriddenDescriptor);
overrideDescriptor(docPrototype, 'styleSheets', {
getter: function () {
const styleSheets = nativeMethods.documentStyleSheetsGetter.call(this);
return documentSandbox._shadowUI._filterStyleSheetList(styleSheets, styleSheets.length);
}
});
const documentCookiePropOwnerPrototype = window[nativeMethods.documentCookiePropOwnerName].prototype;
overrideDescriptor(documentCookiePropOwnerPrototype, 'cookie', {
getter: () => documentSandbox._cookieSandbox.getCookie(),
setter: value => documentSandbox._cookieSandbox.setCookie(String(value))
});
overrideDescriptor(docPrototype, 'activeElement', {
getter: function (this: Document) {
const activeElement = nativeMethods.documentActiveElementGetter.call(this);
if (activeElement && isShadowUIElement(activeElement))
return documentSandbox._shadowUI.getLastActiveElement() || this.body;
return activeElement;
}
});
if (this._documentTitleStorageInitializer && !partialInitializationForNotLoadedIframe) {
overrideDescriptor(docPrototype, 'title', {
getter: function () {
return documentSandbox._documentTitleStorageInitializer.storage.getTitle();
} ,
setter: function (value) {
documentSandbox._documentTitleStorageInitializer.storage.setTitle(value);
}
});
}
}
}