Skip to content

Commit 7cee523

Browse files
committed
Issue json-c#867 - also disallow control characters in keys
1 parent 7bca694 commit 7cee523

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

json_tokener.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,12 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *
12501250
state = json_tokener_state_string_escape;
12511251
break;
12521252
}
1253+
else if ((tok->flags & JSON_TOKENER_STRICT) && (unsigned char)c <= 0x1f)
1254+
{
1255+
// Disallow control characters in strict mode
1256+
tok->err = json_tokener_error_parse_string;
1257+
goto out;
1258+
}
12531259
if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
12541260
{
12551261
printbuf_memappend_checked(tok->pb, case_start,

tests/test_parse.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,10 @@ struct incremental_step
611611
"\x10\x11\x12\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\"",
612612
-1, -1, json_tokener_success, 1, 0 },
613613

614+
{ "{\"0\x01\x02\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" \
615+
"\x10\x11\x12\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\":1}",
616+
-1, -1, json_tokener_success, 1, 0 },
617+
614618
// Test control chars again, this time in strict mode, which should fail
615619
{ "\"\x01\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
616620
{ "\"\x02\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
@@ -644,6 +648,38 @@ struct incremental_step
644648
{ "\"\x1e\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
645649
{ "\"\x1f\"", -1, 1, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
646650

651+
{ "{\"\x01\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
652+
{ "{\"\x02\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
653+
{ "{\"\x03\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
654+
{ "{\"\x04\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
655+
{ "{\"\x05\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
656+
{ "{\"\x06\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
657+
{ "{\"\x07\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
658+
{ "{\"\x08\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
659+
{ "{\"\x09\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
660+
{ "{\"\x0a\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
661+
{ "{\"\x0b\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
662+
{ "{\"\x0c\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
663+
{ "{\"\x0d\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
664+
{ "{\"\x0e\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
665+
{ "{\"\x0f\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
666+
{ "{\"\x10\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
667+
{ "{\"\x11\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
668+
{ "{\"\x12\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
669+
{ "{\"\x13\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
670+
{ "{\"\x14\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
671+
{ "{\"\x15\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
672+
{ "{\"\x16\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
673+
{ "{\"\x17\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
674+
{ "{\"\x18\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
675+
{ "{\"\x19\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
676+
{ "{\"\x1a\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
677+
{ "{\"\x1b\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
678+
{ "{\"\x1c\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
679+
{ "{\"\x1d\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
680+
{ "{\"\x1e\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
681+
{ "{\"\x1f\":1}", -1, 2, json_tokener_error_parse_string, 1, JSON_TOKENER_STRICT },
682+
647683
{NULL, -1, -1, json_tokener_success, 0, 0},
648684
};
649685

tests/test_parse.expected

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@ json_tokener_parse_ex(tok, 11
297297
json_tokener_parse_ex(tok, {"1�":1} , 8) ... OK: got correct error: invalid utf-8 string
298298
json_tokener_parse_ex(tok, "0
299299
", 36) ... OK: got object of type [string]: "0\u0001\u0002\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f"
300+
json_tokener_parse_ex(tok, {"0
301+
":1}, 40) ... OK: got object of type [object]: { "0\u0001\u0002\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f": 1 }
300302
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
301303
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
302304
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
@@ -329,5 +331,37 @@ json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid
329331
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
330332
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
331333
json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence
332-
End Incremental Tests OK=237 ERROR=0
334+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
335+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
336+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
337+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
338+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
339+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
340+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
341+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
342+
json_tokener_parse_ex(tok, {" ":1} , 7) ... OK: got correct error: invalid string sequence
343+
json_tokener_parse_ex(tok, {"
344+
":1} , 7) ... OK: got correct error: invalid string sequence
345+
json_tokener_parse_ex(tok, {" ":1} , 7) ... OK: got correct error: invalid string sequence
346+
json_tokener_parse_ex(tok, {" ":1} , 7) ... OK: got correct error: invalid string sequence
347+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
348+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
349+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
350+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
351+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
352+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
353+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
354+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
355+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
356+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
357+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
358+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
359+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
360+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
361+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
362+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
363+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
364+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
365+
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
366+
End Incremental Tests OK=269 ERROR=0
333367
==================================

0 commit comments

Comments
 (0)