Skip to content

Commit ba9c308

Browse files
committed
CDRIVER-3970: Remove DBRef validation from Extended JSON parser (#800)
* CDRIVER-3970: Remove DBRef validation from Extended JSON parser Documents that resemble but do not conform to a DBRef should be left as-is. Syncs BSON corpus tests with mongodb/specifications@a274fbf Skips were added for additional corpus tests unrelated to this spec change. Use hex escaping instead of unicode for MSVC compatibility
1 parent 8970194 commit ba9c308

File tree

14 files changed

+169
-74
lines changed

14 files changed

+169
-74
lines changed

src/libbson/src/bson/bson-json.c

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ static const char *read_state_names[] = {FOREACH_READ_STATE (GENERATE_STRING)};
103103
BS (DECIMAL128) \
104104
BS (DBPOINTER) \
105105
BS (SYMBOL) \
106-
BS (DBREF) \
107106
BS (UUID)
108107

109108
typedef enum {
@@ -131,8 +130,6 @@ typedef enum {
131130
typedef struct {
132131
int i;
133132
bson_json_frame_type_t type;
134-
bool has_ref;
135-
bool has_id;
136133
bson_t bson;
137134
} bson_json_stack_frame_t;
138135

@@ -279,8 +276,6 @@ _noop (void)
279276
#define FRAME_TYPE_HAS_BSON(_type) \
280277
((_type) == BSON_JSON_FRAME_SCOPE || (_type) == BSON_JSON_FRAME_DBPOINTER)
281278
#define STACK_HAS_BSON FRAME_TYPE_HAS_BSON (STACK_FRAME_TYPE)
282-
#define STACK_HAS_REF STACK_ELE (0, has_ref)
283-
#define STACK_HAS_ID STACK_ELE (0, has_id)
284279
#define STACK_PUSH(frame_type) \
285280
do { \
286281
if (bson->n >= (STACK_MAX - 1)) { \
@@ -309,8 +304,6 @@ _noop (void)
309304
#define STACK_PUSH_DOC(statement) \
310305
do { \
311306
STACK_PUSH (BSON_JSON_FRAME_DOC); \
312-
STACK_HAS_REF = false; \
313-
STACK_HAS_ID = false; \
314307
if (bson->n != 0) { \
315308
statement; \
316309
} \
@@ -713,7 +706,6 @@ _bson_json_read_integer (bson_json_reader_t *reader, uint64_t val, int64_t sign)
713706
case BSON_JSON_LF_DECIMAL128:
714707
case BSON_JSON_LF_DBPOINTER:
715708
case BSON_JSON_LF_SYMBOL:
716-
case BSON_JSON_LF_DBREF:
717709
default:
718710
_bson_json_read_set_error (reader,
719711
"Unexpected integer %s%" PRIu64
@@ -1123,12 +1115,6 @@ _bson_json_read_string (bson_json_reader_t *reader, /* IN */
11231115
bson_append_symbol (
11241116
STACK_BSON_CHILD, key, (int) len, (const char *) val, (int) vlen);
11251117
break;
1126-
case BSON_JSON_LF_DBREF:
1127-
/* the "$ref" of a {$ref: "...", $id: ... }, append normally */
1128-
bson_append_utf8 (
1129-
STACK_BSON_CHILD, key, (int) len, (const char *) val, (int) vlen);
1130-
bson->read_state = BSON_JSON_REGULAR;
1131-
break;
11321118
case BSON_JSON_LF_SCOPE:
11331119
case BSON_JSON_LF_TIMESTAMP_T:
11341120
case BSON_JSON_LF_TIMESTAMP_I:
@@ -1402,20 +1388,6 @@ _bson_json_read_map_key (bson_json_reader_t *reader, /* IN */
14021388
}
14031389
} else {
14041390
_bson_json_save_map_key (bson, val, len);
1405-
1406-
/* in x: {$ref: "collection", $id: {$oid: "..."}, $db: "..." } */
1407-
if (bson->n > 0) {
1408-
if (!strcmp ("$ref", (const char *) val)) {
1409-
STACK_HAS_REF = true;
1410-
bson->read_state = BSON_JSON_IN_BSON_TYPE;
1411-
bson->bson_state = BSON_JSON_LF_DBREF;
1412-
} else if (!strcmp ("$id", (const char *) val)) {
1413-
STACK_HAS_ID = true;
1414-
} else if (!strcmp ("$db", (const char *) val)) {
1415-
bson->read_state = BSON_JSON_IN_BSON_TYPE;
1416-
bson->bson_state = BSON_JSON_LF_DBREF;
1417-
}
1418-
}
14191391
}
14201392
}
14211393

@@ -1820,11 +1792,6 @@ _bson_json_read_end_map (bson_json_reader_t *reader) /* IN */
18201792
bson->read_state = BSON_JSON_IN_BSON_TYPE_DBPOINTER_STARTMAP;
18211793
STACK_POP_DBPOINTER;
18221794
} else {
1823-
if (STACK_HAS_ID != STACK_HAS_REF) {
1824-
_bson_json_read_set_error (
1825-
reader, "%s", "DBRef object must have both $ref and $id keys");
1826-
}
1827-
18281795
STACK_POP_DOC (
18291796
bson_append_document_end (STACK_BSON_PARENT, STACK_BSON_CHILD));
18301797
}

src/libbson/tests/json/bson_corpus/array.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,22 @@
1414
"canonical_extjson": "{\"a\" : [{\"$numberInt\": \"10\"}]}"
1515
},
1616
{
17-
"description": "Single Element Array with index set incorrectly",
17+
"description": "Single Element Array with index set incorrectly to empty string",
1818
"degenerate_bson": "130000000461000B00000010000A0000000000",
1919
"canonical_bson": "140000000461000C0000001030000A0000000000",
2020
"canonical_extjson": "{\"a\" : [{\"$numberInt\": \"10\"}]}"
2121
},
2222
{
23-
"description": "Single Element Array with index set incorrectly",
23+
"description": "Single Element Array with index set incorrectly to ab",
2424
"degenerate_bson": "150000000461000D000000106162000A0000000000",
2525
"canonical_bson": "140000000461000C0000001030000A0000000000",
2626
"canonical_extjson": "{\"a\" : [{\"$numberInt\": \"10\"}]}"
27+
},
28+
{
29+
"description": "Multi Element Array with duplicate indexes",
30+
"degenerate_bson": "1b000000046100130000001030000a000000103000140000000000",
31+
"canonical_bson": "1b000000046100130000001030000a000000103100140000000000",
32+
"canonical_extjson": "{\"a\" : [{\"$numberInt\": \"10\"}, {\"$numberInt\": \"20\"}]}"
2733
}
2834
],
2935
"decodeErrors": [

src/libbson/tests/json/bson_corpus/code.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
{
2222
"description": "two-byte UTF-8 (\u00e9)",
2323
"canonical_bson": "190000000261000D000000C3A9C3A9C3A9C3A9C3A9C3A90000",
24-
"canonical_extjson": "{\"a\" : \"éééééé\"}"
24+
"canonical_extjson": "{\"a\" : \"\\u00e9\\u00e9\\u00e9\\u00e9\\u00e9\\u00e9\"}"
2525
},
2626
{
2727
"description": "three-byte UTF-8 (\u2606)",
2828
"canonical_bson": "190000000261000D000000E29886E29886E29886E298860000",
29-
"canonical_extjson": "{\"a\" : \"☆☆☆☆\"}"
29+
"canonical_extjson": "{\"a\" : \"\\u2606\\u2606\\u2606\\u2606\"}"
3030
},
3131
{
3232
"description": "Embedded nulls",

src/libbson/tests/json/bson_corpus/code_w_scope.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
{
2727
"description": "Unicode and embedded null in code string, empty scope",
2828
"canonical_bson": "1A0000000F61001200000005000000C3A9006400050000000000",
29-
"canonical_extjson": "{\"a\" : {\"$code\" : \"é\\u0000d\", \"$scope\" : {}}}"
29+
"canonical_extjson": "{\"a\" : {\"$code\" : \"\\u00e9\\u0000d\", \"$scope\" : {}}}"
3030
}
3131
],
3232
"decodeErrors": [

src/libbson/tests/json/bson_corpus/datetime.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
"description" : "Y10K",
2626
"canonical_bson" : "1000000009610000DC1FD277E6000000",
2727
"canonical_extjson" : "{\"a\":{\"$date\":{\"$numberLong\":\"253402300800000\"}}}"
28+
},
29+
{
30+
"description": "leading zero ms",
31+
"canonical_bson": "10000000096100D1D6D6CC3B01000000",
32+
"relaxed_extjson": "{\"a\" : {\"$date\" : \"2012-12-24T12:15:30.001Z\"}}",
33+
"canonical_extjson": "{\"a\" : {\"$date\" : {\"$numberLong\" : \"1356351330001\"}}}"
2834
}
2935
],
3036
"decodeErrors": [

src/libbson/tests/json/bson_corpus/dbref.json

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"description": "DBRef",
2+
"description": "Document type (DBRef sub-documents)",
33
"bson_type": "0x03",
44
"valid": [
55
{
@@ -26,6 +26,26 @@
2626
"description": "Document with key names similar to those of a DBRef",
2727
"canonical_bson": "3e0000000224726566000c0000006e6f742d612d646272656600072469640058921b3e6e32ab156a22b59e022462616e616e6100050000007065656c0000",
2828
"canonical_extjson": "{\"$ref\": \"not-a-dbref\", \"$id\": {\"$oid\": \"58921b3e6e32ab156a22b59e\"}, \"$banana\": \"peel\"}"
29+
},
30+
{
31+
"description": "DBRef with additional dollar-prefixed and dotted fields",
32+
"canonical_bson": "48000000036462726566003c0000000224726566000b000000636f6c6c656374696f6e00072469640058921b3e6e32ab156a22b59e10612e62000100000010246300010000000000",
33+
"canonical_extjson": "{\"dbref\": {\"$ref\": \"collection\", \"$id\": {\"$oid\": \"58921b3e6e32ab156a22b59e\"}, \"a.b\": {\"$numberInt\": \"1\"}, \"$c\": {\"$numberInt\": \"1\"}}}"
34+
},
35+
{
36+
"description": "Sub-document resembles DBRef but $id is missing",
37+
"canonical_bson": "26000000036462726566001a0000000224726566000b000000636f6c6c656374696f6e000000",
38+
"canonical_extjson": "{\"dbref\": {\"$ref\": \"collection\"}}"
39+
},
40+
{
41+
"description": "Sub-document resembles DBRef but $ref is not a string",
42+
"canonical_bson": "2c000000036462726566002000000010247265660001000000072469640058921b3e6e32ab156a22b59e0000",
43+
"canonical_extjson": "{\"dbref\": {\"$ref\": {\"$numberInt\": \"1\"}, \"$id\": {\"$oid\": \"58921b3e6e32ab156a22b59e\"}}}"
44+
},
45+
{
46+
"description": "Sub-document resembles DBRef but $db is not a string",
47+
"canonical_bson": "4000000003646272656600340000000224726566000b000000636f6c6c656374696f6e00072469640058921b3e6e32ab156a22b59e1024646200010000000000",
48+
"canonical_extjson": "{\"dbref\": {\"$ref\": \"collection\", \"$id\": {\"$oid\": \"58921b3e6e32ab156a22b59e\"}, \"$db\": {\"$numberInt\": \"1\"}}}"
2949
}
3050
]
3151
}

src/libbson/tests/json/bson_corpus/document.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@
1717
"description": "Single-character key subdoc",
1818
"canonical_bson": "160000000378000E0000000261000200000062000000",
1919
"canonical_extjson": "{\"x\" : {\"a\" : \"b\"}}"
20+
},
21+
{
22+
"description": "Dollar-prefixed key in sub-document",
23+
"canonical_bson": "170000000378000F000000022461000200000062000000",
24+
"canonical_extjson": "{\"x\" : {\"$a\" : \"b\"}}"
25+
},
26+
{
27+
"description": "Dollar as key in sub-document",
28+
"canonical_bson": "160000000378000E0000000224000200000061000000",
29+
"canonical_extjson": "{\"x\" : {\"$\" : \"a\"}}"
30+
},
31+
{
32+
"description": "Dotted key in sub-document",
33+
"canonical_bson": "180000000378001000000002612E62000200000063000000",
34+
"canonical_extjson": "{\"x\" : {\"a.b\" : \"c\"}}"
35+
},
36+
{
37+
"description": "Dot as key in sub-document",
38+
"canonical_bson": "160000000378000E000000022E000200000061000000",
39+
"canonical_extjson": "{\"x\" : {\".\" : \"a\"}}"
2040
}
2141
],
2242
"decodeErrors": [

src/libbson/tests/json/bson_corpus/double.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@
2828
"relaxed_extjson": "{\"d\" : -1.0001220703125}"
2929
},
3030
{
31-
"description": "1.23456789012345677E+18",
32-
"canonical_bson": "1000000001640081E97DF41022B14300",
33-
"canonical_extjson": "{\"d\" : {\"$numberDouble\": \"1234567890123456768.0\"}}",
34-
"relaxed_extjson": "{\"d\" : 1234567890123456768.0}"
31+
"description": "1.2345678921232E+18",
32+
"canonical_bson": "100000000164002a1bf5f41022b14300",
33+
"canonical_extjson": "{\"d\" : {\"$numberDouble\": \"1.2345678921232E+18\"}}",
34+
"relaxed_extjson": "{\"d\" : 1.2345678921232E+18}"
3535
},
3636
{
37-
"description": "-1.23456789012345677E+18",
38-
"canonical_bson": "1000000001640081E97DF41022B1C300",
39-
"canonical_extjson": "{\"d\" : {\"$numberDouble\": \"-1234567890123456768.0\"}}",
40-
"relaxed_extjson": "{\"d\" : -1234567890123456768.0}"
37+
"description": "-1.2345678921232E+18",
38+
"canonical_bson": "100000000164002a1bf5f41022b1c300",
39+
"canonical_extjson": "{\"d\" : {\"$numberDouble\": \"-1.2345678921232E+18\"}}",
40+
"relaxed_extjson": "{\"d\" : -1.2345678921232E+18}"
4141
},
4242
{
4343
"description": "0.0",

0 commit comments

Comments
 (0)