Skip to content

Commit

Permalink
Fix wrongly overridden toString function from Function constructor (c…
Browse files Browse the repository at this point in the history
…lose #999) (#1004)
  • Loading branch information
LavrovArtem authored and churkin committed Dec 28, 2016
1 parent 4765399 commit 542fc4c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/client/sandbox/native-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class NativeMethods {
this.XMLHttpRequest = win.XMLHttpRequest;
this.Image = win.Image;
this.Function = win.Function;
this.functionToString = win.Function.toString;
this.FontFace = win.FontFace;
this.StorageEvent = win.StorageEvent;
this.MutationObserver = win.MutationObserver;
Expand Down
9 changes: 6 additions & 3 deletions src/client/sandbox/node/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export default class WindowSandbox extends SandboxBase {
};
window.Image.prototype = nativeMethods.Image.prototype;

window.Function = function (...args) {
var FunctionWrapper = function (...args) {
var functionBodyArgIndex = args.length - 1;

if (typeof args[functionBodyArgIndex] === 'string')
Expand All @@ -237,12 +237,15 @@ export default class WindowSandbox extends SandboxBase {
return nativeMethods.Function.apply(this, args);
};

window.Function = FunctionWrapper;
window.Function.prototype = nativeMethods.Function.prototype;
window.Function.prototype.constructor = window.Function;
window.Function.prototype.constructor = FunctionWrapper;

// NOTE: We need to create function which returns string without calling toString every time
// because if the Function.prototype.toString is overridden it can be the cause of recursion
window.Function.toString = () => nativeFunctionToString;
window.Function.toString = function () {
return this === FunctionWrapper ? nativeFunctionToString : nativeMethods.functionToString.call(this);
};

if (typeof window.history.pushState === 'function' && typeof window.history.replaceState === 'function') {
window.history.pushState = function () {
Expand Down
8 changes: 5 additions & 3 deletions test/client/fixtures/sandbox/node/window-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ test('window.onerror must be overriden (B238830)', function () {
});

test('the constructor field of a function should return a wrapped Function object (GH-913)', function () {
var f = function () {
var f = function f () {
};

strictEqual(f.constructor, Function);
Expand All @@ -143,12 +143,14 @@ test('the constructor field of a function should return a wrapped Function objec

/*eslint-disable no-extend-native*/
Function.prototype.toString = function () {
toString.call(this);
var str = toString.call(this);

ok(true);

return str;
};

f.toString();
strictEqual(f.toString().replace(/\s+/g, ''), 'functionf(){}');

Function.prototype.toString = nativeToString;
/*eslint-enable no-extend-native*/
Expand Down

0 comments on commit 542fc4c

Please sign in to comment.