Skip to content

Commit e2b54e8

Browse files
committed
Merge pull request #576
2 parents 6dc2f9d + 27a4bf3 commit e2b54e8

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

src/bson.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,7 +1384,7 @@ void phongo_zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *bson
13841384
if (strlen(ZSTR_VAL(string_key)) != ZSTR_LEN(string_key)) {
13851385
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "BSON keys cannot contain null bytes. Unexpected null byte after \"%s\".", ZSTR_VAL(string_key));
13861386

1387-
return;
1387+
goto cleanup;
13881388
}
13891389

13901390
if (flags & PHONGO_BSON_ADD_ID) {
@@ -1436,7 +1436,7 @@ void phongo_zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *bson
14361436
if (strlen(string_key) != string_key_len - 1) {
14371437
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "BSON keys cannot contain null bytes. Unexpected null byte after \"%s\".", ZSTR_VAL(string_key));
14381438

1439-
return;
1439+
goto cleanup;
14401440
}
14411441

14421442
if (flags & PHONGO_BSON_ADD_ID) {
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
--TEST--
2+
BSON\fromPHP(): Serializable returns document with null bytes in field name
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/tools.php';
7+
8+
class MySerializable implements MongoDB\BSON\Serializable
9+
{
10+
private $data;
11+
12+
public function __construct($data)
13+
{
14+
$this->data = $data;
15+
}
16+
17+
public function bsonSerialize()
18+
{
19+
return $this->data;
20+
}
21+
}
22+
23+
echo "\nTesting array with one leading null byte in field name\n";
24+
echo throws(function() {
25+
fromPHP(new MySerializable(["\0" => 1]));
26+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
27+
28+
echo "\nTesting array with one trailing null byte in field name\n";
29+
echo throws(function() {
30+
fromPHP(new MySerializable(["a\0" => 1]));
31+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
32+
33+
echo "\nTesting array with multiple null bytes in field name\n";
34+
echo throws(function() {
35+
fromPHP(new MySerializable(["\0\0\0" => 1]));
36+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
37+
38+
/* Per PHPC-884, field names with a leading null byte are ignored when encoding
39+
* a document from an object's property hash table, since PHP uses leading bytes
40+
* to denote protected and private properties. However, in this case the object
41+
* was returned from Serializable::bsonSerialize() and we skip the check for
42+
* protected and private properties. */
43+
echo "\nTesting object with one leading null byte in field name\n";
44+
echo throws(function() {
45+
fromPHP(new MySerializable((object) ["\0" => 1]));
46+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
47+
48+
echo "\nTesting object with one trailing null byte in field name\n";
49+
echo throws(function() {
50+
fromPHP(new MySerializable((object) ["a\0" => 1]));
51+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
52+
53+
echo "\nTesting object with multiple null bytes in field name\n";
54+
echo throws(function() {
55+
fromPHP(new MySerializable((object) ["\0\0\0" => 1]));
56+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
57+
58+
?>
59+
===DONE===
60+
<?php exit(0); ?>
61+
--EXPECT--
62+
Testing array with one leading null byte in field name
63+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
64+
BSON keys cannot contain null bytes. Unexpected null byte after "".
65+
66+
Testing array with one trailing null byte in field name
67+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
68+
BSON keys cannot contain null bytes. Unexpected null byte after "a".
69+
70+
Testing array with multiple null bytes in field name
71+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
72+
BSON keys cannot contain null bytes. Unexpected null byte after "".
73+
74+
Testing object with one leading null byte in field name
75+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
76+
BSON keys cannot contain null bytes. Unexpected null byte after "".
77+
78+
Testing object with one trailing null byte in field name
79+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
80+
BSON keys cannot contain null bytes. Unexpected null byte after "a".
81+
82+
Testing object with multiple null bytes in field name
83+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
84+
BSON keys cannot contain null bytes. Unexpected null byte after "".
85+
===DONE===

0 commit comments

Comments
 (0)