Skip to content

Instantly share code, notes, and snippets.

@MikeRalphson
Created September 9, 2019 16:58
Show Gist options
  • Save MikeRalphson/8e2813b0f762d43e3968805dd11cb43f to your computer and use it in GitHub Desktop.
Save MikeRalphson/8e2813b0f762d43e3968805dd11cb43f to your computer and use it in GitHub Desktop.
Clean Array.prototype pollution in Javascript/Node.js
Array.prototype.foo = function() { return 'Still callable'; };
function test() {
let count = 0;
for (const e in []) {
console.log('Found '+e);
count++;
Object.defineProperty(Array.prototype, e, {
value: Array.prototype[e],
enumerable: false
});
}
if (!count) console.log('All clean');
}
test();
test();
console.log([].foo());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment