A lot of the time, I write JavaScript that runs with other scripts or that may contain other scripts. Sometimes these scripts might have changed the prototypes for the primitive objects that I might be using in my code.
Is there a way I can declare my primitive datatypes in JavaScript so that it will reset the prototype if it has been modified? Or so that my scripts would run in a separate scope where the prototype of the primitive is not modified?
// evil script code modify primative on prototype
Boolean.prototype.toString = function() {
return true;
}
let flag = false;
console.log(flag.toString());
// I would expect false to be printed, but because the prototype of the primative if overridden
// the primative value would be wrapped with the Boolean object
// and call the modified toString method and would output true.
Is there any way I can make sure my code is running in a separate scope, or any way I can declare my variables or reset the prototypes to avoid these types of issues?
You can freeze global object prototype but you need your script to run first. It will prevent modification.
I don't know how to freeze global function.
Object.freeze(String.prototype);
Object.freeze(Number.prototype);
Object.freeze(Boolean.prototype);
Object.freeze(Object.prototype);
Object.freeze(Array.prototype);
Object.freeze(Date.prototype);
Object.freeze(Math.prototype);
Object.freeze(Function.prototype);
// evil script code modify primative on prototype
Boolean.prototype.toString = function() {
return true;
}
// For this I don't know how to freeze
window.parseInt = function(number) {
return 'evil'
}
let flag = false;
console.log(flag.toString());
console.log(parseInt(1));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With