Skip to content

Commit 84dbfe0

Browse files
committed
PHPC-891: BSON encoding should throw if PHP keys contain null bytes
Since PHP uses leading null bytes in object properties to denote protected and private members, we ignore those keys in lieu of throwing an exception.
1 parent caa4650 commit 84dbfe0

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/bson.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,6 +1393,12 @@ void phongo_zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *bson
13931393
}
13941394
}
13951395

1396+
if (strlen(ZSTR_VAL(string_key)) != ZSTR_LEN(string_key)) {
1397+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "BSON keys cannot contain null bytes. Unexpected null byte after \"%s\".", ZSTR_VAL(string_key));
1398+
1399+
return;
1400+
}
1401+
13961402
if (flags & PHONGO_BSON_ADD_ID) {
13971403
if (!strcmp(ZSTR_VAL(string_key), "_id")) {
13981404
flags &= ~PHONGO_BSON_ADD_ID;
@@ -1439,6 +1445,12 @@ void phongo_zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *bson
14391445
}
14401446
}
14411447

1448+
if (strlen(string_key) != string_key_len - 1) {
1449+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "BSON keys cannot contain null bytes. Unexpected null byte after \"%s\".", ZSTR_VAL(string_key));
1450+
1451+
return;
1452+
}
1453+
14421454
if (flags & PHONGO_BSON_ADD_ID) {
14431455
if (!strcmp(string_key, "_id")) {
14441456
flags &= ~PHONGO_BSON_ADD_ID;

tests/bson/bson-fromPHP-006.phpt

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@ BSON\fromPHP(): PHP documents with null bytes in field name
66
require_once __DIR__ . '/../utils/tools.php';
77

88
echo "\nTesting array with one leading null byte in field name\n";
9-
hex_dump(fromPHP(["\0" => 1]));
9+
echo throws(function() {
10+
fromPHP(["\0" => 1]);
11+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
1012

1113
echo "\nTesting array with one trailing null byte in field name\n";
12-
hex_dump(fromPHP(["a\0" => 1]));
14+
echo throws(function() {
15+
fromPHP(["a\0" => 1]);
16+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
1317

1418
echo "\nTesting array with multiple null bytes in field name\n";
15-
hex_dump(fromPHP(["\0\0\0" => 1]));
19+
echo throws(function() {
20+
fromPHP(["\0\0\0" => 1]);
21+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
1622

1723
/* Per PHPC-884, field names with a leading null byte are ignored when encoding
1824
* a document from an object's property hash table, since PHP uses leading bytes
@@ -21,7 +27,9 @@ echo "\nTesting object with one leading null byte in field name\n";
2127
hex_dump(fromPHP((object) ["\0" => 1]));
2228

2329
echo "\nTesting object with one trailing null byte in field name\n";
24-
hex_dump(fromPHP((object) ["a\0" => 1]));
30+
echo throws(function() {
31+
fromPHP((object) ["a\0" => 1]);
32+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
2533

2634
echo "\nTesting object with multiple null bytes in field name\n";
2735
hex_dump(fromPHP((object) ["\0\0\0" => 1]));
@@ -31,19 +39,23 @@ hex_dump(fromPHP((object) ["\0\0\0" => 1]));
3139
<?php exit(0); ?>
3240
--EXPECT--
3341
Testing array with one leading null byte in field name
34-
0 : 0b 00 00 00 10 00 01 00 00 00 00 [...........]
42+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
43+
BSON keys cannot contain null bytes. Unexpected null byte after "".
3544

3645
Testing array with one trailing null byte in field name
37-
0 : 0c 00 00 00 10 61 00 01 00 00 00 00 [.....a......]
46+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
47+
BSON keys cannot contain null bytes. Unexpected null byte after "a".
3848

3949
Testing array with multiple null bytes in field name
40-
0 : 0b 00 00 00 10 00 01 00 00 00 00 [...........]
50+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
51+
BSON keys cannot contain null bytes. Unexpected null byte after "".
4152

4253
Testing object with one leading null byte in field name
4354
0 : 05 00 00 00 00 [.....]
4455

4556
Testing object with one trailing null byte in field name
46-
0 : 0c 00 00 00 10 61 00 01 00 00 00 00 [.....a......]
57+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
58+
BSON keys cannot contain null bytes. Unexpected null byte after "a".
4759

4860
Testing object with multiple null bytes in field name
4961
0 : 05 00 00 00 00 [.....]

0 commit comments

Comments
 (0)