When forming a JSON data structure, are there any security risks with allowing the field names to be chosen by an attacker? You can assume they'll be quoted properly. I'd like to know if allowing the attribute names to be attacker-chosen poses any additional risks over allowing the attribute values to be attacker-chosen.
Example: I might have a JSON value such as the following:
{
"name": "me",
"interests": "sewing",
"comment": "hello world!"
}
Assume that both the attribute names (name, interests, comment) and attribute values (me, sewing, hello world!) can be freely chosen by the attacker. Also assume that they are properly quoted (e.g., they are always surrounded by double quotes; we escape backslashes and quote signs, to prevent breaking out of the quotes).
Is there anything I need to worry about that's specific to attacker-chosen attribute names?
Do I need to worry about an attacker choosing an attribute name like toString or valueOf or length or 0? Or names that start with an underscore?
valueOfortoStringmethods: well, that's exactly the question I am looking to have answered. Could an attacker who specifies an attribute namedvalueOfortoStringcause any security problems for my application? I can't tell. Could it cause problems, or not? If yes, can you give an example? – D.W. Nov 14 '11 at 4:49.valueOf()and.toString()are special. They get invoked implicitly by the Javascript interpreter all over the place, in places where you'd never guess (even though you never invoked them explicitly in your code). That's why I asked about them specifically. – D.W. Nov 14 '11 at 17:39