diff --git a/test/bson_corpus/document.json b/test/bson_corpus/document.json index 10bf637d6..698e7ae90 100644 --- a/test/bson_corpus/document.json +++ b/test/bson_corpus/document.json @@ -51,6 +51,10 @@ { "description": "Invalid subdocument: bad string length in field", "bson": "1C00000003666F6F001200000002626172000500000062617A000000" + }, + { + "description": "Null byte in sub-document key", + "bson": "150000000378000D00000010610000010000000000" } ] } diff --git a/test/bson_corpus/regex.json b/test/bson_corpus/regex.json index c62b019cd..223802169 100644 --- a/test/bson_corpus/regex.json +++ b/test/bson_corpus/regex.json @@ -54,11 +54,11 @@ ], "decodeErrors": [ { - "description": "embedded null in pattern", + "description": "Null byte in pattern string", "bson": "0F0000000B610061006300696D0000" }, { - "description": "embedded null in flags", + "description": "Null byte in flags string", "bson": "100000000B61006162630069006D0000" } ] diff --git a/test/bson_corpus/top.json b/test/bson_corpus/top.json index fe5be0eae..9c649b5e3 100644 --- a/test/bson_corpus/top.json +++ b/test/bson_corpus/top.json @@ -79,6 +79,10 @@ { "description": "Document truncated mid-key", "bson": "1200000002666F" + }, + { + "description": "Null byte in document key", + "bson": "0D000000107800000100000000" } ], "parseErrors": [ @@ -241,7 +245,22 @@ { "description": "Bad DBpointer (extra field)", "string": "{\"a\": {\"$dbPointer\": {\"a\": {\"$numberInt\": \"1\"}, \"$id\": {\"$oid\": \"56e1fc72e0c917e9c4714161\"}, \"c\": {\"$numberInt\": \"2\"}, \"$ref\": \"b\"}}}" + }, + { + "description" : "Null byte in document key", + "string" : "{\"a\\u0000\": 1 }" + }, + { + "description" : "Null byte in sub-document key", + "string" : "{\"a\" : {\"b\\u0000\": 1 }}" + }, + { + "description": "Null byte in $regularExpression pattern", + "string": "{\"a\" : {\"$regularExpression\" : { \"pattern\": \"b\\u0000\", \"options\" : \"i\"}}}" + }, + { + "description": "Null byte in $regularExpression options", + "string": "{\"a\" : {\"$regularExpression\" : { \"pattern\": \"b\", \"options\" : \"i\\u0000\"}}}" } - ] } diff --git a/test/test_bson_corpus.py b/test/test_bson_corpus.py index 98a4cf305..68703cc70 100644 --- a/test/test_bson_corpus.py +++ b/test/test_bson_corpus.py @@ -31,7 +31,7 @@ from bson.binary import STANDARD from bson.codec_options import CodecOptions from bson.decimal128 import Decimal128 from bson.dbref import DBRef -from bson.errors import InvalidBSON, InvalidId +from bson.errors import InvalidBSON, InvalidDocument, InvalidId from bson.json_util import JSONMode from bson.son import SON @@ -51,6 +51,8 @@ _NON_PARSE_ERRORS = set([ # This variant of $numberLong may have been generated by an old version # of mongoexport. 'Bad $numberLong (number, not string)', + # We parse Regex flags with extra characters, including nulls. + 'Null byte in $regularExpression options', ]) _DEPRECATED_BSON_TYPES = { @@ -198,10 +200,14 @@ def create_test(case_spec): decode_extjson(parse_error_case['string']) else: try: - decode_extjson(parse_error_case['string']) + doc = decode_extjson(parse_error_case['string']) + # Null bytes are validated when encoding to BSON. + if 'Null' in description: + to_bson(doc) raise AssertionError('exception not raised for test ' 'case: ' + description) - except (ValueError, KeyError, TypeError, InvalidId): + except (ValueError, KeyError, TypeError, InvalidId, + InvalidDocument): pass elif bson_type == '0x05': try: