How can we properly sanitize a user-provided JSON string to prevent any vulnerabilities before executing JSON.parse(untrustedString)
?
My main concern revolves around prototype pollution, but I'm also interested in knowing about any other potential risks to watch out for. If prototype pollution is the only issue to worry about, I assume we can address it using regular expressions. However, I suspect there may be other security concerns as well?
For instance, I came across an article discussing the risks associated with parsing untrusted JSON and then creating a duplicate of the object.:
Imagine receiving some malicious JSON data via an endpoint.
{ "user": { "__proto__": { "admin": true } } }
If this JSON payload is processed by
JSON.parse
, it will generate an object with a__proto__
property. If the copying mechanism operates as described in the example, it will transfer the admin property onto the prototype ofreq.session.user
!