JavaScript · Senior · Question 33 of 75

What's dangerous about a deep-merge function applied to user-supplied JSON?

merge({}, JSON.parse('{"__proto__": {"isAdmin": true}}'));

Explanation

A naive merge assigns target.__proto__.isAdmin = true, and ({}).isAdmin becomes true across the whole app. Protection: skip __proto__, constructor, prototype, use Object.create(null) or Map for dictionaries, validate input with a schema.

Further reading

Problem with a question

JavaScript · Senior · question 33

Test yourself

Take the JavaScript · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz