2019-05-22 23:37:23 +02:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
(function(){
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
let scope;
|
|
|
|
if ((typeof exports) !== "undefined"){
|
|
|
|
scope = exports;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
scope = require.register("./iframeProtection", {});
|
|
|
|
}
|
|
|
|
|
|
|
|
scope.protect = function protect(window, wrappedWindow, singleCallback, allCallback){
|
|
|
|
|
2019-06-04 08:42:31 +02:00
|
|
|
["HTMLIFrameElement", "HTMLFrameElement"].forEach(function(constructorName){
|
|
|
|
const constructor = window[constructorName];
|
|
|
|
const wrappedConstructor = wrappedWindow[constructorName];
|
|
|
|
|
|
|
|
const contentWindowDescriptor = Object.getOwnPropertyDescriptor(
|
|
|
|
constructor.prototype,
|
|
|
|
"contentWindow"
|
|
|
|
);
|
|
|
|
const originalContentWindowGetter = contentWindowDescriptor.get;
|
|
|
|
const contentWindowTemp = {
|
|
|
|
get contentWindow(){
|
|
|
|
var window = originalContentWindowGetter.call(this);
|
|
|
|
if (window){
|
|
|
|
singleCallback(window);
|
|
|
|
}
|
|
|
|
return window;
|
2019-05-22 23:37:23 +02:00
|
|
|
}
|
2019-06-04 08:42:31 +02:00
|
|
|
};
|
|
|
|
contentWindowDescriptor.get = exportFunction(
|
|
|
|
Object.getOwnPropertyDescriptor(contentWindowTemp, "contentWindow").get,
|
|
|
|
window
|
|
|
|
);
|
|
|
|
Object.defineProperty(wrappedConstructor.prototype, "contentWindow", contentWindowDescriptor);
|
|
|
|
|
|
|
|
const contentDocumentDescriptor = Object.getOwnPropertyDescriptor(
|
|
|
|
constructor.prototype,
|
|
|
|
"contentDocument"
|
2019-05-22 23:37:23 +02:00
|
|
|
);
|
2019-06-04 08:42:31 +02:00
|
|
|
const originalContentDocumentGetter = contentDocumentDescriptor.get;
|
|
|
|
const contentDocumentTemp = {
|
|
|
|
get contentDocument(){
|
|
|
|
var document = originalContentDocumentGetter.call(this);
|
|
|
|
if (document){
|
|
|
|
singleCallback(document.defaultView);
|
|
|
|
}
|
|
|
|
return document;
|
2019-05-22 23:37:23 +02:00
|
|
|
}
|
2019-06-04 08:42:31 +02:00
|
|
|
};
|
|
|
|
contentDocumentDescriptor.get = exportFunction(
|
|
|
|
Object.getOwnPropertyDescriptor(contentDocumentTemp, "contentDocument").get,
|
|
|
|
window
|
2019-05-22 23:37:23 +02:00
|
|
|
);
|
2019-06-04 08:42:31 +02:00
|
|
|
Object.defineProperty(wrappedConstructor.prototype, "contentDocument", contentDocumentDescriptor);
|
2019-05-22 23:37:23 +02:00
|
|
|
});
|
|
|
|
[
|
|
|
|
// useless as length could be obtained before the iframe is created and window.frames === window
|
|
|
|
// {
|
|
|
|
// object: wrappedWindow,
|
|
|
|
// methods: [],
|
|
|
|
// getters: ["length", "frames"],
|
|
|
|
// setters: []
|
|
|
|
// },
|
|
|
|
{
|
|
|
|
object: wrappedWindow.Node.prototype,
|
|
|
|
methods: ["appendChild", "insertBefore", "replaceChild"],
|
|
|
|
getters: [],
|
|
|
|
setters: []
|
|
|
|
},
|
|
|
|
{
|
|
|
|
object: wrappedWindow.Element.prototype,
|
|
|
|
methods: [
|
|
|
|
"append", "prepend",
|
|
|
|
"insertAdjacentElement", "insertAdjacentHTML", "insertAdjacentText",
|
|
|
|
"replaceWith"
|
|
|
|
],
|
|
|
|
getters: [],
|
2019-05-28 23:34:27 +02:00
|
|
|
setters: [
|
|
|
|
"innerHTML",
|
|
|
|
"outerHTML"
|
|
|
|
]
|
2019-05-22 23:37:23 +02:00
|
|
|
}
|
|
|
|
].forEach(function(protectionDefinition){
|
|
|
|
const object = protectionDefinition.object;
|
|
|
|
protectionDefinition.methods.forEach(function(method){
|
|
|
|
const descriptor = Object.getOwnPropertyDescriptor(object, method);
|
|
|
|
const original = descriptor.value;
|
|
|
|
descriptor.value = exportFunction(eval(`(function ${method}(){
|
|
|
|
const value = arguments.length?
|
|
|
|
original.apply(this, window.Array.from(arguments)):
|
|
|
|
original.call(this);
|
|
|
|
allCallback();
|
|
|
|
return value;
|
|
|
|
})`), window);
|
|
|
|
Object.defineProperty(object, method, descriptor);
|
|
|
|
});
|
|
|
|
protectionDefinition.getters.forEach(function(property){
|
|
|
|
const descriptor = Object.getOwnPropertyDescriptor(object, property);
|
|
|
|
const temp = eval(`({
|
|
|
|
get ${property}(){
|
|
|
|
const ret = this.${property};
|
|
|
|
allCallback();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
})`);
|
|
|
|
descriptor.get = exportFunction(Object.getOwnPropertyDescriptor(temp, property).get, window);
|
|
|
|
Object.defineProperty(object, property, descriptor);
|
|
|
|
});
|
|
|
|
protectionDefinition.setters.forEach(function(property){
|
|
|
|
const descriptor = Object.getOwnPropertyDescriptor(object, property);
|
2019-05-28 23:34:27 +02:00
|
|
|
const setter = descriptor.set;
|
2019-05-22 23:37:23 +02:00
|
|
|
const temp = eval(`({
|
|
|
|
set ${property}(value){
|
2019-05-28 23:34:27 +02:00
|
|
|
const ret = setter.call(this, value);
|
|
|
|
// const ret = this.${property} = value;
|
2019-05-22 23:37:23 +02:00
|
|
|
allCallback();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
})`);
|
|
|
|
descriptor.set = exportFunction(Object.getOwnPropertyDescriptor(temp, property).set, window);
|
|
|
|
Object.defineProperty(object, property, descriptor);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-05-24 18:30:57 +02:00
|
|
|
// MutationObserver to intercept iFrames while generating the DOM.
|
|
|
|
const observe = function(){
|
|
|
|
var observer = new MutationObserver(allCallback);
|
|
|
|
var observing = false;
|
|
|
|
function observe(){
|
2019-05-29 20:48:56 +02:00
|
|
|
if (
|
|
|
|
!observing &&
|
|
|
|
window.document
|
|
|
|
){
|
|
|
|
observer.observe(window.document, {subtree: true, childList: true});
|
2019-05-24 18:30:57 +02:00
|
|
|
observing = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
observe();
|
|
|
|
window.document.addEventListener("DOMContentLoaded", function(){
|
|
|
|
if (observing){
|
|
|
|
observer.disconnect();
|
|
|
|
observing = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return observe;
|
|
|
|
}();
|
2019-05-22 23:37:23 +02:00
|
|
|
|
|
|
|
// MutationObserver does not trigger fast enough when document.write is used
|
2019-05-24 18:30:57 +02:00
|
|
|
const documentWriteDescriptor = Object.getOwnPropertyDescriptor(
|
|
|
|
wrappedWindow.HTMLDocument.prototype,
|
|
|
|
"write"
|
2019-05-29 00:20:27 +02:00
|
|
|
) || Object.getOwnPropertyDescriptor(
|
|
|
|
wrappedWindow.Document.prototype,
|
|
|
|
"write"
|
2019-05-24 18:30:57 +02:00
|
|
|
);
|
2019-05-22 23:37:23 +02:00
|
|
|
const documentWrite = documentWriteDescriptor.value;
|
2019-06-11 23:09:13 +02:00
|
|
|
documentWriteDescriptor.value = exportFunction(function write(markup){
|
|
|
|
for (let i = 0, l = arguments.length; i < l; i += 1){
|
|
|
|
const str = "" + arguments[i];
|
|
|
|
// weird problem with waterfox and google docs
|
|
|
|
const parts = (
|
|
|
|
str.match(/^\s*<!doctype/i) &&
|
|
|
|
!str.match(/frame/i)
|
|
|
|
)? [str]: str.split(/(?=<)/);
|
|
|
|
const length = parts.length;
|
|
|
|
const scripts = window.document.getElementsByTagName("script");
|
|
|
|
for (let i = 0; i < length; i += 1){
|
|
|
|
documentWrite.call(this, parts[i]);
|
|
|
|
allCallback();
|
|
|
|
if (scripts.length && scripts[scripts.length - 1].src){
|
|
|
|
observe();
|
|
|
|
}
|
2019-05-24 18:30:57 +02:00
|
|
|
}
|
2019-05-22 23:37:23 +02:00
|
|
|
}
|
|
|
|
}, window);
|
|
|
|
Object.defineProperty(wrappedWindow.HTMLDocument.prototype, "write", documentWriteDescriptor);
|
|
|
|
|
2019-05-24 18:30:57 +02:00
|
|
|
const documentWritelnDescriptor = Object.getOwnPropertyDescriptor(
|
|
|
|
wrappedWindow.HTMLDocument.prototype,
|
|
|
|
"writeln"
|
2019-05-29 00:20:27 +02:00
|
|
|
) || Object.getOwnPropertyDescriptor(
|
|
|
|
wrappedWindow.Document.prototype,
|
|
|
|
"writeln"
|
2019-05-24 18:30:57 +02:00
|
|
|
);
|
2019-05-22 23:37:23 +02:00
|
|
|
const documentWriteln = documentWritelnDescriptor.value;
|
2019-06-11 23:09:13 +02:00
|
|
|
documentWritelnDescriptor.value = exportFunction(function writeln(markup){
|
|
|
|
for (let i = 0, l = arguments.length; i < l; i += 1){
|
|
|
|
const str = "" + arguments[i];
|
|
|
|
const parts = str.split(/(?=<)/);
|
|
|
|
const length = parts.length;
|
|
|
|
const scripts = window.document.getElementsByTagName("script");
|
|
|
|
for (let i = 0; i < length; i += 1){
|
|
|
|
documentWrite.call(this, parts[i]);
|
|
|
|
allCallback();
|
|
|
|
if (scripts.length && scripts[scripts.length - 1].src){
|
|
|
|
observe();
|
|
|
|
}
|
2019-05-24 18:30:57 +02:00
|
|
|
}
|
2019-05-22 23:37:23 +02:00
|
|
|
}
|
2019-06-11 23:09:13 +02:00
|
|
|
documentWriteln.call(this, "");
|
2019-05-22 23:37:23 +02:00
|
|
|
}, window);
|
|
|
|
Object.defineProperty(wrappedWindow.HTMLDocument.prototype, "writeln", documentWritelnDescriptor);
|
|
|
|
};
|
|
|
|
}());
|