PYTHON-2820 Test serialization of BSON with embedded null bytes in strings (#723)

This commit is contained in:
Shane Harvey 2021-09-10 10:23:27 -07:00 committed by GitHub
parent 7a4b617b5d
commit 90d4c6f19f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 6 deletions

View File

@ -51,6 +51,10 @@
{
"description": "Invalid subdocument: bad string length in field",
"bson": "1C00000003666F6F001200000002626172000500000062617A000000"
},
{
"description": "Null byte in sub-document key",
"bson": "150000000378000D00000010610000010000000000"
}
]
}

View File

@ -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"
}
]

View File

@ -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\"}}}"
}
]
}

View File

@ -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: