Skip to content

Instantly share code, notes, and snippets.

@DaniAkash
Created May 10, 2019 12:04
Show Gist options
  • Save DaniAkash/02f2e9e01232370172a98eca532900de to your computer and use it in GitHub Desktop.
Save DaniAkash/02f2e9e01232370172a98eca532900de to your computer and use it in GitHub Desktop.
A javascript object merge operation that will not allow prototype pollution to happen
var merge = function(target, source) {
for(var attr in source) {
if(attr === "__proto__") continue; // Do not merge the property if it's name is __proto__
if(typeof(target[attr]) === "object" && typeof(source[attr]) === "object") {
merge(target[attr], source[attr]);
} else {
target[attr] = source[attr];
}
}
return target;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment