Skip to content

Commit 90d4c6f

Browse files
authored
PYTHON-2820 Test serialization of BSON with embedded null bytes in strings (#723)
1 parent 7a4b617 commit 90d4c6f

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

test/bson_corpus/document.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
{
5252
"description": "Invalid subdocument: bad string length in field",
5353
"bson": "1C00000003666F6F001200000002626172000500000062617A000000"
54+
},
55+
{
56+
"description": "Null byte in sub-document key",
57+
"bson": "150000000378000D00000010610000010000000000"
5458
}
5559
]
5660
}

test/bson_corpus/regex.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@
5454
],
5555
"decodeErrors": [
5656
{
57-
"description": "embedded null in pattern",
57+
"description": "Null byte in pattern string",
5858
"bson": "0F0000000B610061006300696D0000"
5959
},
6060
{
61-
"description": "embedded null in flags",
61+
"description": "Null byte in flags string",
6262
"bson": "100000000B61006162630069006D0000"
6363
}
6464
]

test/bson_corpus/top.json

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@
7979
{
8080
"description": "Document truncated mid-key",
8181
"bson": "1200000002666F"
82+
},
83+
{
84+
"description": "Null byte in document key",
85+
"bson": "0D000000107800000100000000"
8286
}
8387
],
8488
"parseErrors": [
@@ -241,7 +245,22 @@
241245
{
242246
"description": "Bad DBpointer (extra field)",
243247
"string": "{\"a\": {\"$dbPointer\": {\"a\": {\"$numberInt\": \"1\"}, \"$id\": {\"$oid\": \"56e1fc72e0c917e9c4714161\"}, \"c\": {\"$numberInt\": \"2\"}, \"$ref\": \"b\"}}}"
248+
},
249+
{
250+
"description" : "Null byte in document key",
251+
"string" : "{\"a\\u0000\": 1 }"
252+
},
253+
{
254+
"description" : "Null byte in sub-document key",
255+
"string" : "{\"a\" : {\"b\\u0000\": 1 }}"
256+
},
257+
{
258+
"description": "Null byte in $regularExpression pattern",
259+
"string": "{\"a\" : {\"$regularExpression\" : { \"pattern\": \"b\\u0000\", \"options\" : \"i\"}}}"
260+
},
261+
{
262+
"description": "Null byte in $regularExpression options",
263+
"string": "{\"a\" : {\"$regularExpression\" : { \"pattern\": \"b\", \"options\" : \"i\\u0000\"}}}"
244264
}
245-
246265
]
247266
}

test/test_bson_corpus.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from bson.codec_options import CodecOptions
3232
from bson.decimal128 import Decimal128
3333
from bson.dbref import DBRef
34-
from bson.errors import InvalidBSON, InvalidId
34+
from bson.errors import InvalidBSON, InvalidDocument, InvalidId
3535
from bson.json_util import JSONMode
3636
from bson.son import SON
3737

@@ -51,6 +51,8 @@
5151
# This variant of $numberLong may have been generated by an old version
5252
# of mongoexport.
5353
'Bad $numberLong (number, not string)',
54+
# We parse Regex flags with extra characters, including nulls.
55+
'Null byte in $regularExpression options',
5456
])
5557

5658
_DEPRECATED_BSON_TYPES = {
@@ -198,10 +200,14 @@ def run_test(self):
198200
decode_extjson(parse_error_case['string'])
199201
else:
200202
try:
201-
decode_extjson(parse_error_case['string'])
203+
doc = decode_extjson(parse_error_case['string'])
204+
# Null bytes are validated when encoding to BSON.
205+
if 'Null' in description:
206+
to_bson(doc)
202207
raise AssertionError('exception not raised for test '
203208
'case: ' + description)
204-
except (ValueError, KeyError, TypeError, InvalidId):
209+
except (ValueError, KeyError, TypeError, InvalidId,
210+
InvalidDocument):
205211
pass
206212
elif bson_type == '0x05':
207213
try:

0 commit comments

Comments
 (0)