File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 23
23
import os
24
24
import pickle
25
25
import re
26
+ import struct
26
27
import sys
27
28
import tempfile
28
29
import uuid
@@ -489,6 +490,33 @@ def test_basic_encode(self):
489
490
b"\x00 " ,
490
491
)
491
492
493
+ def test_bad_code (self ):
494
+ # Assert that decoding invalid Code with scope does not include a field name.
495
+ def generate_payload (length : int ) -> bytes :
496
+ string_size = length - 0x1E
497
+
498
+ return bytes .fromhex (
499
+ struct .pack ("<I" , length ).hex () # payload size
500
+ + "0f" # type "code with scope"
501
+ + "3100" # key (cstring)
502
+ + "0a000000" # c_w_s_size
503
+ + "04000000" # code_size
504
+ + "41004200" # code (cstring)
505
+ + "feffffff" # scope_size
506
+ + "02" # type "string"
507
+ + "3200" # key (cstring)
508
+ + struct .pack ("<I" , string_size ).hex () # string size
509
+ + "00" * string_size # value (cstring)
510
+ # next bytes is a field name for type \x00
511
+ # type \x00 is invalid so bson throws an exception
512
+ )
513
+
514
+ for i in range (100 ):
515
+ payload = generate_payload (0x54F + i )
516
+ with self .assertRaisesRegex (InvalidBSON , "invalid" ) as ctx :
517
+ bson .decode (payload )
518
+ self .assertNotIn ("fieldname" , str (ctx .exception ))
519
+
492
520
def test_unknown_type (self ):
493
521
# Repr value differs with major python version
494
522
part = "type {!r} for fieldname 'foo'" .format (b"\x14 " )
You can’t perform that action at this time.
0 commit comments