Skip to content

Commit 93bd10b

Browse files
authored
PHPC-1897: Test serialization of BSON with embedded null bytes in strings (#1255)
* BSON corpus prose tests for null bytes For tests that already existed we just add a reference to the prose test * BSON corpus tests for null bytes Synced with mongodb/specifications@c64fc79 This sync also removes some old DBRef tests, which should have been removed in 6929bcb. * Bump libmongoc to 1.19.1 to pull in changes from CDRIVER-4083
1 parent a4740e8 commit 93bd10b

15 files changed

+115
-8
lines changed

src/LIBMONGOC_VERSION_CURRENT

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.19.0
1+
1.19.1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
Document type (sub-documents): Null byte in sub-document key
3+
--DESCRIPTION--
4+
Generated by scripts/convert-bson-corpus-tests.php
5+
6+
DO NOT EDIT THIS FILE
7+
--FILE--
8+
<?php
9+
10+
require_once __DIR__ . '/../utils/basic.inc';
11+
12+
$bson = hex2bin('150000000378000D00000010610000010000000000');
13+
14+
throws(function() use ($bson) {
15+
var_dump(toPHP($bson));
16+
}, 'MongoDB\Driver\Exception\UnexpectedValueException');
17+
18+
?>
19+
===DONE===
20+
<?php exit(0); ?>
21+
--EXPECT--
22+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
23+
===DONE===

tests/bson-corpus/regex-decodeError-001.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
Regular Expression type: embedded null in pattern
2+
Regular Expression type: Null byte in pattern string
33
--DESCRIPTION--
44
Generated by scripts/convert-bson-corpus-tests.php
55

tests/bson-corpus/regex-decodeError-002.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
Regular Expression type: embedded null in flags
2+
Regular Expression type: Null byte in flags string
33
--DESCRIPTION--
44
Generated by scripts/convert-bson-corpus-tests.php
55

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
Top-level document validity: Null byte in document key
3+
--DESCRIPTION--
4+
Generated by scripts/convert-bson-corpus-tests.php
5+
6+
DO NOT EDIT THIS FILE
7+
--FILE--
8+
<?php
9+
10+
require_once __DIR__ . '/../utils/basic.inc';
11+
12+
$bson = hex2bin('0D000000107800000100000000');
13+
14+
throws(function() use ($bson) {
15+
var_dump(toPHP($bson));
16+
}, 'MongoDB\Driver\Exception\UnexpectedValueException');
17+
18+
?>
19+
===DONE===
20+
<?php exit(0); ?>
21+
--EXPECT--
22+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
23+
===DONE===

tests/bson-corpus/top-parseError-041.phpt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
Top-level document validity: Bad $maxKey (extra field)
2+
Top-level document validity: Null byte in document key
33
--DESCRIPTION--
44
Generated by scripts/convert-bson-corpus-tests.php
55

@@ -10,7 +10,7 @@ DO NOT EDIT THIS FILE
1010
require_once __DIR__ . '/../utils/basic.inc';
1111

1212
throws(function() {
13-
fromJSON('{"a" : {"$maxKey" : 1, "unrelated": true}}');
13+
fromJSON('{"a\\u0000": 1 }');
1414
}, 'MongoDB\Driver\Exception\UnexpectedValueException');
1515

1616
?>

tests/bson-corpus/top-parseError-042.phpt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
Top-level document validity: Bad DBpointer (extra field)
2+
Top-level document validity: Null byte in sub-document key
33
--DESCRIPTION--
44
Generated by scripts/convert-bson-corpus-tests.php
55

@@ -10,7 +10,7 @@ DO NOT EDIT THIS FILE
1010
require_once __DIR__ . '/../utils/basic.inc';
1111

1212
throws(function() {
13-
fromJSON('{"a": {"$dbPointer": {"a": {"$numberInt": "1"}, "$id": {"$oid": "56e1fc72e0c917e9c4714161"}, "c": {"$numberInt": "2"}, "$ref": "b"}}}');
13+
fromJSON('{"a" : {"b\\u0000": 1 }}');
1414
}, 'MongoDB\Driver\Exception\UnexpectedValueException');
1515

1616
?>
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Top-level document validity: Null byte in $regularExpression pattern
3+
--DESCRIPTION--
4+
Generated by scripts/convert-bson-corpus-tests.php
5+
6+
DO NOT EDIT THIS FILE
7+
--FILE--
8+
<?php
9+
10+
require_once __DIR__ . '/../utils/basic.inc';
11+
12+
throws(function() {
13+
fromJSON('{"a" : {"$regularExpression" : { "pattern": "b\\u0000", "options" : "i"}}}');
14+
}, 'MongoDB\Driver\Exception\UnexpectedValueException');
15+
16+
?>
17+
===DONE===
18+
<?php exit(0); ?>
19+
--EXPECT--
20+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
21+
===DONE===
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Top-level document validity: Null byte in $regularExpression options
3+
--DESCRIPTION--
4+
Generated by scripts/convert-bson-corpus-tests.php
5+
6+
DO NOT EDIT THIS FILE
7+
--FILE--
8+
<?php
9+
10+
require_once __DIR__ . '/../utils/basic.inc';
11+
12+
throws(function() {
13+
fromJSON('{"a" : {"$regularExpression" : { "pattern": "b", "options" : "i\\u0000"}}}');
14+
}, 'MongoDB\Driver\Exception\UnexpectedValueException');
15+
16+
?>
17+
===DONE===
18+
<?php exit(0); ?>
19+
--EXPECT--
20+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
21+
===DONE===

tests/bson/bson-fromPHP_error-006.phpt

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
--TEST--
22
MongoDB\BSON\fromPHP(): PHP documents with null bytes in field name
3+
--DESCRIPTION--
4+
BSON Corpus spec prose test #1
35
--FILE--
46
<?php
57

@@ -25,6 +27,11 @@ echo throws(function() {
2527
fromPHP((object) ["a\0" => 1]);
2628
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
2729

30+
echo "\nTesting nested array with one trailing null byte in field name\n";
31+
echo throws(function() {
32+
fromPHP(['a' => ["b\0" => 1]]);
33+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
34+
2835
?>
2936
===DONE===
3037
<?php exit(0); ?>
@@ -44,4 +51,8 @@ BSON keys cannot contain null bytes. Unexpected null byte after "".
4451
Testing object with one trailing null byte in field name
4552
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
4653
BSON keys cannot contain null bytes. Unexpected null byte after "a".
54+
55+
Testing nested array with one trailing null byte in field name
56+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
57+
BSON keys cannot contain null bytes. Unexpected null byte after "b".
4758
===DONE===

tests/bson/bson-regex-serialization_error-002.phpt

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
--TEST--
22
MongoDB\BSON\Regex unserialization does not allow pattern or flags to contain null bytes (Serializable interface)
3+
--DESCRIPTION--
4+
BSON Corpus spec prose test #1
35
--FILE--
46
<?php
57

tests/bson/bson-regex-serialization_error-004.phpt

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
--TEST--
22
MongoDB\BSON\Regex unserialization does not allow pattern or flags to contain null bytes (__serialize and __unserialize)
3+
--DESCRIPTION--
4+
BSON Corpus spec prose test #1
35
--SKIPIF--
46
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
57
<?php skip_if_php_version('<', '7.4.0'); ?>

tests/bson/bson-regex-set_state_error-002.phpt

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
--TEST--
22
MongoDB\BSON\Regex::__set_state() does not allow pattern or flags to contain null bytes
3+
--DESCRIPTION--
4+
BSON Corpus spec prose test #1
35
--FILE--
46
<?php
57

tests/bson/bson-regex_error-003.phpt

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
--TEST--
22
MongoDB\BSON\Regex::__construct() does not allow pattern or flags to contain null bytes
3+
--DESCRIPTION--
4+
BSON Corpus spec prose test #1
35
--FILE--
46
<?php
57

0 commit comments

Comments
 (0)