Skip to content

Commit

Permalink
JSON.parse(): bail if we don't get a string key (in non-relaxed mode)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobrippling committed Nov 27, 2023
1 parent 693bd55 commit 7186d4c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/jswrap_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ JsVar *jswrap_json_parse_internal(JSONFlags flags) {
JsVar *obj = jsvNewObject(); if (!obj) return 0;
jslGetNextToken(); // {
while ((lex->tk == LEX_STR || lex->tk == LEX_ID) && !jspHasError()) {
if (!(flags&JSON_DROP_QUOTES)) {
jslMatch(LEX_STR);
return obj;
if (!(flags&JSON_DROP_QUOTES) && !jslMatch(LEX_STR)) {
jsvUnLock(obj);
return 0;
}
JsVar *key = jsvAsArrayIndexAndUnLock(jslGetTokenValueAsVar());
jslGetNextToken();
Expand Down
20 changes: 19 additions & 1 deletion tests/test_json_object.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
// JSON shouldn't print stuff like __proto__ and constructor

function A() {}
function A() {}
var a = new A();

result = JSON.stringify(a)=="{}";

function assertEq(a, b) {
if (typeof a === "object")
// note: doesn't check keys in b
for (var k in a)
assertEq(a[k], b[k]);
else if (a !== b)
throw new Error("mismatch, " + a + " != " + b);
}

// no exceptions thrown:
assertEq(JSON.parse('{"a": 1}'), {"a": 1});
assertEq(JSON.parse('["4", 5, "six"]'), ["4", 5, "six"]);
assertEq(JSON.parse('["4", 5, "six", {"x": 5}]'), ["4", 5, "six", {"x": 5}]);
assertEq(JSON.parse('""'), "");
assertEq(JSON.parse('5'), 5);
assertEq(JSON.parse('[]'), []);
assertEq(JSON.parse('{}'), {});

0 comments on commit 7186d4c

Please sign in to comment.