Skip to content

Commit

Permalink
fix: pollution to outside world Function.prototype.constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Jul 22, 2019
1 parent e2dc6a3 commit 969f1fe
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/repair/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* a copy has been made, and funtions can only be created by syntax (using eval)
* or by invoking a previously saved reference to the originals.
*/

// todo: this file should be moved out to a separate repo and npm module.
export function repairFunctions() {
const { defineProperties, getPrototypeOf, setPrototypeOf } = Object;
Expand Down Expand Up @@ -45,11 +44,21 @@ export function repairFunctions() {
throw e;
}
const FunctionPrototype = getPrototypeOf(FunctionInstance);
const oldFunctionConstructor = FunctionPrototype.constructor;

function isRunningInRealms() {
const e = new Error().stack;
if (!e) return true;
return e.indexOf('eval') !== -1;
}
// Prevents the evaluation of source when calling constructor on the
// prototype of functions.
const TamedFunction = function() {
throw new TypeError('Not available');
if (isRunningInRealms()) {
throw new TypeError('Not available');
} else {
return oldFunctionConstructor.apply(this, arguments);
}
};
defineProperties(TamedFunction, { name: { value: name } });

Expand Down

0 comments on commit 969f1fe

Please sign in to comment.