Skip to content

Instantly share code, notes, and snippets.

@mikesamuel
Created June 5, 2018 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikesamuel/fac604851f86630adad27a8fa8f6e47c to your computer and use it in GitHub Desktop.
Save mikesamuel/fac604851f86630adad27a8fa8f6e47c to your computer and use it in GitHub Desktop.
JSON.parse that filters out __proto__
JSON.parse = (() => {
const undef = void 0;
const jsonParse = JSON.parse;
function noProtoReviver (key, value) {
if (key === '__proto__') {
console.warn('Removed __proto__ from parsed JSON');
return undef; // Remove property entirely
}
return value;
}
return function parse (text, reviver) {
const compositionOfRevivers = reviver
? (key, value) => {
value = noProtoReviver(key, value);
return (value === undef) ? undef : reviver(key, value);
}
: noProtoReviver;
return jsonParse(text, compositionOfRevivers);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment