Given a user-provided JSON string, how can we sanitize it before running JSON.parse(untrustedString)
?
My primary concern is about prototype pollution, but I'm also wondering what else I should potentially look out for? If it's just prototype pollution that's a risk, then I assume that could be handled via regex, but I suspect there are additional concerns?
For example, this article on the dangers of parsing untrusted JSON and then creating a copy of the object.:
Now consider some malicious JSON data sent to this endpoint.
{ "user": { "__proto__": { "admin": true } } }
If this JSON is sent,
JSON.parse
will produce an object with a__proto__
property. If the copying library works as described above, it will copy the admin property onto the prototype ofreq.session.user
!