Skip to content

Commit 9fdc7d2

Browse files
GODRIVER-2083: Test serialization of BSON with embedded null bytes in strings (#784)
1 parent 51951ab commit 9fdc7d2

File tree

6 files changed

+43
-3
lines changed

6 files changed

+43
-3
lines changed

bson/bson_corpus_spec_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,10 @@ func runTest(t *testing.T, file string) {
350350
case "0x00", "0x05", "0x13":
351351
var doc D
352352
err := UnmarshalExtJSON([]byte(s), true, &doc)
353+
// Null bytes are validated when marshaling to BSON
354+
if strings.Contains(p.Description, "Null") {
355+
_, err = Marshal(doc)
356+
}
353357
expectError(t, err, fmt.Sprintf("%s: expected parse error", p.Description))
354358
default:
355359
t.Errorf("Update test to check for parse errors for type %s", test.BsonType)

bson/marshal_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,13 @@ func TestNullBytes(t *testing.T) {
302302
})
303303
}
304304
})
305+
306+
t.Run("sub document field name", func(t *testing.T) {
307+
doc := D{{"foo", D{{"foobar", D{{"a\x00", "foobar"}}}}}}
308+
res, err := Marshal(doc)
309+
wantErr := errors.New("BSON element key cannot contain null bytes")
310+
assert.Equal(t, wantErr, err, "expected Marshal error %v, got error %v with result %q", wantErr, err, Raw(res))
311+
})
305312
}
306313

307314
func TestMarshalExtJSONIndent(t *testing.T) {

data/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
}

data/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
]

data/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
}

x/bsonx/bsoncore/bsoncore_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,12 @@ func TestNullBytes(t *testing.T) {
935935
})
936936
}
937937
})
938+
t.Run("sub document field name", func(t *testing.T) {
939+
createDocFn := func() {
940+
NewDocumentBuilder().StartDocument("foobar").AppendDocument("a\x00", []byte("foo")).FinishDocument()
941+
}
942+
assertBSONCreationPanics(t, createDocFn, invalidKeyPanicMsg)
943+
})
938944
}
939945

940946
func compareDecimal128(d1, d2 primitive.Decimal128) bool {

0 commit comments

Comments
 (0)