Claude skill prototype pollution via JSON merge and deep clone — SA-SKL-PROTOTYPE3
Prototype pollution via JSON merge and deep-clone operations remains one of the highest-impact vulnerability classes in Node.js MCP skills because the attack is invisible in simple unit tests: an attacker injects properties into Object.prototype via crafted JSON, and every subsequent object literal in the process inherits those properties without any further code change. SA-SKL-PROTOTYPE3 covers four merge/clone patterns that introduce prototype pollution beyond the classic node-serialize vector.
Why merge-based prototype pollution is distinct from deserialization attacks
The well-known node-serialize prototype pollution requires the library to call eval() on a crafted payload. Merge-based pollution is simpler and more pervasive: it flows through ordinary JavaScript property assignment. Any function that copies keys from an attacker-controlled object into another object — whether via Object.assign, lodash _.merge, a custom deep-clone, or the spread operator — becomes a pollution vector if it fails to skip the special keys __proto__ and constructor. Because MCP skills frequently accept JSON payloads to configure audit rules, merge skill metadata, or build permission objects, these patterns appear in almost every non-trivial skill codebase.
Once Object.prototype is polluted, the injected property is readable from every plain object in the same process. If the skill checks obj.isAdmin without first verifying the property exists on obj itself, an attacker who set Object.prototype.isAdmin = true can bypass authorization checks across the entire skill, often without touching the code path that performs the check.
SA-SKL-PROTOTYPE3 in scope: PROTOTYPE3-001 (Object.assign with __proto__ key), PROTOTYPE3-002 (lodash _.merge deep pollution), PROTOTYPE3-003 (JSON.parse + spread operator), PROTOTYPE3-004 (constructor.prototype path traversal in recursive merge). Each pattern includes SkillAudit detection rules and correct fixes.
PROTOTYPE3-001 — Object.assign({}, req.body) copies __proto__ key
Object.assign(target, source) copies all enumerable own properties from source to target. When the JSON body parser produces a plain object from {"__proto__": {"isAdmin": true}}, the __proto__ key is an own property of that object — it was parsed as a string key, not treated as the prototype accessor. When Object.assign then copies it onto another plain object, JavaScript interprets the assignment as setting the prototype of the target object, effectively writing to Object.prototype. Any subsequent code that checks user.isAdmin without an Object.prototype.hasOwnProperty guard returns true for every user. The fix is to use Object.create(null) as the merge target (which has no prototype chain to pollute) or to validate parsed JSON with a schema that rejects __proto__ and constructor keys before merging.
// VULNERABLE — Object.assign copies __proto__ key from req.body
import express from 'express';
export async function updateSkillConfig(req, res) {
// Attacker sends: {"__proto__": {"isAdmin": true, "bypassAudit": true}}
const config = Object.assign({}, req.body); // Pollutes Object.prototype
// Later, any object check is polluted:
const user = {};
console.log(user.isAdmin); // true — inherited from Object.prototype
console.log(user.bypassAudit); // true — same pollution
if (user.isAdmin) {
// Attacker reaches this branch for every user
return res.json({ adminData: getAllSecretAudits() });
}
}
// FIXED — use Object.create(null) to avoid prototype chain, validate keys
import Ajv from 'ajv';
const configSchema = {
type: 'object',
properties: {
name: { type: 'string' },
enabled: { type: 'boolean' },
threshold: { type: 'number' },
},
additionalProperties: false, // Rejects __proto__ and constructor keys
};
const validate = new Ajv().compile(configSchema);
export async function updateSkillConfig(req, res) {
if (!validate(req.body)) {
return res.status(400).json({ error: 'Invalid config schema', details: validate.errors });
}
// Safe: schema validation has already rejected __proto__ and other dangerous keys
const config = Object.assign(Object.create(null), req.body);
// Explicit own-property check — not pollutable even without schema guard
const user = await getUser(req.userId);
if (Object.prototype.hasOwnProperty.call(user, 'isAdmin') && user.isAdmin) {
return res.json({ adminData: getAllSecretAudits() });
}
res.json({ config });
}
Object.assign({}, req.body) or Object.assign(target, untrustedObject) without prior key validation copies the parsed __proto__ key and pollutes Object.prototype, making every subsequent object in the process inherit attacker-controlled properties; fix by validating input with a schema that uses additionalProperties: false and by using Object.prototype.hasOwnProperty.call() for all security-critical property checks. Detection: SkillAudit flags Object.assign calls where the source operand traces to req.body, req.query, or JSON.parse output without an intervening schema validation step.PROTOTYPE3-002 — lodash _.merge() with untrusted nested object
Lodash _.merge() performs a deep recursive merge that traverses nested objects. Prior to lodash 4.17.17 (CVE-2019-10744), _.merge({}, {"__proto__": {"isAdmin": true}}) directly polluted Object.prototype. The fix was merged into lodash, but projects pinned to older versions, or those using compatible libraries like merge, deepmerge, or extend that have their own patch timelines, remain vulnerable. Beyond version pinning, any deep merge that follows a __proto__ key during traversal is exploitable. The practical test: if ({}).isAdmin is true after a merge, the library is vulnerable. The fix is to upgrade to lodash 4.17.17+ and to audit all deep-merge libraries against the same test case.
// VULNERABLE — outdated lodash _.merge used to merge skill metadata
import _ from 'lodash'; // version 4.17.15 — vulnerable
export async function mergeSkillMetadata(base: object, override: unknown) {
// Attacker sends override = {"__proto__": {"isAdmin": true}}
// _.merge traverses __proto__ key and assigns isAdmin to Object.prototype
const merged = _.merge({}, base, override);
// All objects in the process now have isAdmin === true:
const opts = {};
if (opts.isAdmin) {
// Attacker reaches admin branch on every request after this merge
return getAllPrivateSkills();
}
return merged;
}
// FIXED — upgrade lodash; validate untrusted input before merge; sanitize keys
import _ from 'lodash'; // version 4.17.21 — patched
function sanitizeDeep(obj: unknown, depth = 0): unknown {
if (depth > 10 || obj === null || typeof obj !== 'object') return obj;
const safe: Record<string, unknown> = Object.create(null);
for (const key of Object.keys(obj as object)) {
// Reject keys that target the prototype chain
if (key === '__proto__' || key === 'constructor' || key === 'prototype') continue;
safe[key] = sanitizeDeep((obj as Record<string, unknown>)[key], depth + 1);
}
return safe;
}
export async function mergeSkillMetadata(base: object, override: unknown) {
const safeOverride = sanitizeDeep(override);
const merged = _.merge({}, base, safeOverride);
return merged;
}
_.merge() in versions before 4.17.17 and similar deep-merge libraries traverse the __proto__ key during recursive merging, polluting Object.prototype with attacker-controlled properties; fix by upgrading to lodash 4.17.21+, by sanitizing input with a recursive key-filter that strips __proto__, constructor, and prototype keys before merging. Detection: SkillAudit flags _.merge(), deepmerge(), extend(), or similar deep-merge calls where the source argument traces to untrusted input without prior key sanitization.PROTOTYPE3-003 — JSON.parse + spread operator pollution
A common misconception is that JSON.parse('{"__proto__": {"isAdmin": true}}') produces an object with a polluted prototype. It does not — JSON.parse creates a plain object where __proto__ is a regular own property with no special behavior. The pollution only occurs when that parsed object is subsequently spread into another object using Object.assign or the spread operator { ...parsed }. This two-step pattern — parse, then spread — is so common in MCP skill configuration handling that the initial parse step creates a false sense of safety. If a code reviewer sees JSON.parse(input) and considers the step "safe," but the spread that follows immediately after is not flagged, the pollution occurs silently. The fix is the same schema validation before the spread, and using Object.create(null) as the spread target for security-critical merges.
// VULNERABLE — JSON.parse is safe alone, but the subsequent spread pollutes
export function applySettings(settingsJson: string, defaults: object) {
const parsed = JSON.parse(settingsJson);
// parsed.__proto__ is a regular own property here — not yet dangerous
// But the spread copies it as a property assignment, which DOES pollute:
const settings = { ...defaults, ...parsed };
// ^^^^^^ This assignment pollutes Object.prototype
const check = {};
// check.isAdmin is now true if attacker sent {"__proto__":{"isAdmin":true}}
return settings;
}
// FIXED — validate JSON schema before spreading; sanitize prototype keys
import Ajv from 'ajv';
const settingsSchema = {
type: 'object',
properties: {
theme: { type: 'string', enum: ['dark', 'light'] },
notifications: { type: 'boolean' },
maxAudits: { type: 'number', minimum: 1, maximum: 100 },
},
additionalProperties: false,
};
const validateSettings = new Ajv().compile(settingsSchema);
export function applySettings(settingsJson: string, defaults: object) {
let parsed: unknown;
try {
parsed = JSON.parse(settingsJson);
} catch {
throw new Error('Invalid JSON in settings');
}
if (!validateSettings(parsed)) {
throw new Error('Settings schema validation failed');
}
// Safe to spread: schema validation rejected __proto__ and constructor
return { ...defaults, ...(parsed as object) };
}
JSON.parse output spread into an object literal via { ...parsed } or Object.assign({}, parsed) pollutes Object.prototype when the parsed JSON contains a __proto__ key; the pollution does not occur at parse time and is easy to miss in code review; fix by validating parsed JSON against a schema with additionalProperties: false before spreading. Detection: SkillAudit flags spread or Object.assign operations where the source is the direct output of JSON.parse, req.body, or YAML.load without an intervening schema validation step.PROTOTYPE3-004 — constructor.prototype pollution via recursive merge path traversal
Even when a custom deep-merge function guards against __proto__, it may overlook the constructor.prototype path. An attacker who sends {"constructor": {"prototype": {"isAdmin": true}}} causes the recursive merge to follow the constructor key (the object's constructor function), then set prototype.isAdmin = true on it — which is functionally identical to setting Object.prototype.isAdmin = true for objects instantiated by the same constructor. This attack is more targeted than __proto__ pollution: if the merge function only checks for __proto__, the constructor path bypass succeeds. The fix is to block all three dangerous keys: __proto__, constructor, and prototype — in every recursive merge function and in schema additionalProperties: false configurations.
// VULNERABLE — custom deep merge blocks __proto__ but misses constructor path
function deepMerge(target: Record<string, unknown>, source: Record<string, unknown>) {
for (const key of Object.keys(source)) {
if (key === '__proto__') continue; // Guards __proto__ — but not constructor
if (typeof source[key] === 'object' && source[key] !== null &&
typeof target[key] === 'object' && target[key] !== null) {
deepMerge(target[key] as Record<string, unknown>,
source[key] as Record<string, unknown>);
} else {
target[key] = source[key];
}
}
return target;
}
// Attacker sends: {"constructor": {"prototype": {"isAdmin": true}}}
// deepMerge follows constructor key → assigns prototype.isAdmin → pollutes
// FIXED — block all three dangerous path segments
const DANGEROUS_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
function deepMerge(
target: Record<string, unknown>,
source: Record<string, unknown>,
depth = 0
): Record<string, unknown> {
if (depth > 10) return target; // Guard against deeply nested DOS
for (const key of Object.keys(source)) {
// Reject all prototype-chain manipulation keys
if (DANGEROUS_KEYS.has(key)) continue;
if (
typeof source[key] === 'object' && source[key] !== null &&
!Array.isArray(source[key]) &&
typeof target[key] === 'object' && target[key] !== null
) {
deepMerge(
target[key] as Record<string, unknown>,
source[key] as Record<string, unknown>,
depth + 1
);
} else {
target[key] = source[key];
}
}
return target;
}
__proto__ and not against constructor and prototype allow the path {"constructor": {"prototype": {"isAdmin": true}}} to pollute the shared prototype; fix by adding all three dangerous key names to the skip set in every recursive merge function. Detection: SkillAudit flags recursive merge functions that contain a key === '__proto__' guard but do not also check key === 'constructor' and key === 'prototype'.