Skip to content

Logic to exclude object keys that are themselves objects is imperfect #771

@eamonnmcmanus

Description

@eamonnmcmanus

JSONObject contains this logic to prevent a key in a JSON object from being another JSON object or an array:

            case '{':
            case '[':
                if(prev=='{') {
                    throw x.syntaxError("A JSON Object can not directly nest another JSON Object or JSON Array.");
                }

However this doesn't cover all cases. For example, JSONObject currently accepts this string:

{"a":1, [{"b": 2}]: 3}

which it interprets as an object with two keys, one that is the string a and one that is the string [{"b":2}].

Strict JSON, of course, only accepts string literals as keys in objects. JSON-java is more liberal, but it does balk at nested objects since the fix for #654. The example above suggests that that fix was incomplete. I think it would make sense to disallow nested objects or arrays as keys always. They're not valid JSON and it's unlikely that anyone is relying on them for legitimate purposes. Meanwhile they can straightforwardly be used for DoS attacks, similar to #654 and #758.

I have a proposed fix which I will send shortly.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions