Skip to content

Commit cf10176

Browse files
committed
rewrite: support excluding wrapper variables from being added, if an existing declaration already exists, eg. if 'var document = ' is found,
don't add a 'let document = ' override bump to 3.7.14
1 parent 6895131 commit cf10176

File tree

2 files changed

+47
-37
lines changed

2 files changed

+47
-37
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@webrecorder/wombat",
3-
"version": "3.7.13",
3+
"version": "3.7.14",
44
"main": "index.js",
55
"license": "AGPL-3.0-or-later",
66
"author": "Ilya Kreymer, Webrecorder Software",

src/wombat.js

+46-36
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ function Wombat($wbwindow, wbinfo) {
6464

6565
this.SKIP_OWN_FUNC_PROPS = ['name', 'length', '__WB_is_native_func__', 'arguments', 'caller', 'callee', 'prototype'];
6666

67+
this.OVERRIDE_PROPS = [
68+
'window',
69+
'self',
70+
'document',
71+
'location',
72+
'top',
73+
'parent',
74+
'frames',
75+
'opener'
76+
];
77+
6778
/** @type {function(qualifiedName: string, value: string): void} */
6879
this.wb_setAttribute = $wbwindow.Element.prototype.setAttribute;
6980

@@ -696,32 +707,28 @@ Wombat.prototype.skipWrapScriptBasedOnType = function(scriptType) {
696707
* @param {?string} text
697708
* @return {boolean}
698709
*/
699-
Wombat.prototype.skipWrapScriptTextBasedOnText = function(text) {
710+
Wombat.prototype.skipWrapScriptTextBasedOnText = function(text, excludes) {
700711
if (
701712
!text ||
702713
text.indexOf(this.WB_ASSIGN_FUNC) >= 0 ||
703714
text.indexOf('<') === 0
704715
) {
705716
return true;
706717
}
707-
var override_props = [
708-
'window',
709-
'self',
710-
'document',
711-
'location',
712-
'top',
713-
'parent',
714-
'frames',
715-
'opener'
716-
];
717718

718-
for (var i = 0; i < override_props.length; i++) {
719-
if (text.indexOf(override_props[i]) >= 0) {
720-
return false;
719+
let found = false;
720+
721+
for (const prop of this.OVERRIDE_PROPS) {
722+
const inx = text.indexOf(prop);
723+
if (inx >= 0) {
724+
if (text.indexOf('var ' + prop) >= 0) {
725+
excludes.push(prop);
726+
}
727+
found = true;
721728
}
722729
}
723730

724-
return true;
731+
return !found;
725732
};
726733

727734
/**
@@ -829,24 +836,23 @@ Wombat.prototype.retrieveWBOSRC = function(elem) {
829836
* @param {?string} scriptText
830837
* @return {string}
831838
*/
832-
Wombat.prototype.wrapScriptTextJsProxy = function(scriptText) {
833-
return (
839+
Wombat.prototype.wrapScriptTextJsProxy = function(scriptText, excludes = []) {
840+
let prefix =
834841
'var _____WB$wombat$assign$function_____ = function(name) {return ' +
835842
'(self._wb_wombat && self._wb_wombat.local_init && self._wb_wombat.local_init(name)) || self[name]; };\n' +
836843
'if (!self.__WB_pmw) { self.__WB_pmw = function(obj) { ' +
837-
'this.__WB_source = obj; return this; } }\n{\n' +
838-
'let window = _____WB$wombat$assign$function_____("window");\n' +
839-
'let globalThis = _____WB$wombat$assign$function_____("globalThis");\n' +
840-
'let self = _____WB$wombat$assign$function_____("self");\n' +
841-
'let document = _____WB$wombat$assign$function_____("document");\n' +
842-
'let location = _____WB$wombat$assign$function_____("location");\n' +
843-
'let top = _____WB$wombat$assign$function_____("top");\n' +
844-
'let parent = _____WB$wombat$assign$function_____("parent");\n' +
845-
'let frames = _____WB$wombat$assign$function_____("frames");\n' +
846-
'let opener = _____WB$wombat$assign$function_____("opener");\n{\n' +
847-
scriptText.replace(this.DotPostMessageRe, '.__WB_pmw(self.window)$1') +
848-
'\n\n}}'
849-
);
844+
'this.__WB_source = obj; return this; } }\n{\n';
845+
846+
for (const prop of this.OVERRIDE_PROPS) {
847+
if (!excludes.includes(prop)) {
848+
prefix += `let ${prop} = _____WB$wombat$assign$function_____("${prop}");\n`;
849+
}
850+
}
851+
852+
prefix += '{\n';
853+
854+
return prefix + scriptText.replace(this.DotPostMessageRe, '.__WB_pmw(self.window)$1') +
855+
'\n\n}}';
850856
};
851857

852858
/**
@@ -2185,8 +2191,9 @@ Wombat.prototype.rewriteScript = function(elem) {
21852191
}
21862192
if (this.skipWrapScriptBasedOnType(elem.type)) return false;
21872193
var text = elem.textContent.trim();
2188-
if (this.skipWrapScriptTextBasedOnText(text)) return false;
2189-
elem.textContent = this.wrapScriptTextJsProxy(text);
2194+
var excludes = [];
2195+
if (this.skipWrapScriptTextBasedOnText(text, excludes)) return false;
2196+
elem.textContent = this.wrapScriptTextJsProxy(text, excludes);
21902197
if (this.wb_info.injectDocClose && elem.textContent.trim().length) {
21912198
elem.textContent += ';document.close();';
21922199
}
@@ -2849,13 +2856,15 @@ Wombat.prototype.rewriteHTMLAssign = function(thisObj, oSetter, newValue) {
28492856
res = this.rewriteHtml(newValue);
28502857
}
28512858

2859+
var excludes = [];
2860+
28522861
// likely actual JS, not tags
28532862
if (res === newValue) {
28542863
if (
28552864
!this.skipWrapScriptBasedOnType(thisObj.type) &&
2856-
!this.skipWrapScriptTextBasedOnText(newValue)
2865+
!this.skipWrapScriptTextBasedOnText(newValue, excludes)
28572866
) {
2858-
res = this.wrapScriptTextJsProxy(res);
2867+
res = this.wrapScriptTextJsProxy(res, excludes);
28592868
}
28602869
}
28612870
} else {
@@ -2884,9 +2893,10 @@ Wombat.prototype.rewriteEvalArg = function(rawEvalOrWrapper, evalArg, extraArg)
28842893
if (this.$wbwindow.TrustedScript && (evalArg instanceof this.$wbwindow.TrustedScript)) {
28852894
evalArg = evalArg.toString();
28862895
}
2896+
var excludes = [];
28872897
var toBeEvald =
2888-
this.isString(evalArg) && !this.skipWrapScriptTextBasedOnText(evalArg)
2889-
? this.wrapScriptTextJsProxy(evalArg)
2898+
this.isString(evalArg) && !this.skipWrapScriptTextBasedOnText(evalArg, excludes)
2899+
? this.wrapScriptTextJsProxy(evalArg, excludes)
28902900
: this.otherEvalRewrite(evalArg);
28912901
return rawEvalOrWrapper(toBeEvald, extraArg);
28922902
};

0 commit comments

Comments
 (0)