Skip to content

Commit fd36e94

Browse files
committed
PHPC-393: Ignore non-public properties when encoding BSON
1 parent 28ad44c commit fd36e94

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

src/bson.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -774,12 +774,28 @@ void phongo_bson_append(bson_t *bson, php_phongo_bson_flags_t flags, const char
774774
}
775775
}
776776

777+
static bool is_public_property(zend_class_entry *ce, const char *prop_name, int prop_name_len TSRMLS_DC) /* {{{ */
778+
{
779+
zend_property_info *property_info;
780+
zval member;
781+
782+
ZVAL_STRINGL(&member, prop_name, prop_name_len, 0);
783+
property_info = zend_get_property_info(ce, &member, 1 TSRMLS_CC);
784+
785+
return (property_info && (property_info->flags & ZEND_ACC_PUBLIC));
786+
}
787+
/* }}} */
788+
777789
PHONGO_API void zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *bson, bson_t **bson_out TSRMLS_DC) /* {{{ */
778790
{
779791
HashPosition pos;
780792
HashTable *ht_data = NULL;
781793
zval *obj_data = NULL;
782794

795+
/* If we will be encoding a class that may contain protected and private
796+
* properties, we'll need to filter them out later. */
797+
bool ht_data_from_properties = false;
798+
783799
switch(Z_TYPE_P(data)) {
784800
case IS_OBJECT:
785801
if (instanceof_function(Z_OBJCE_P(data), php_phongo_serializable_ce TSRMLS_CC)) {
@@ -813,7 +829,10 @@ PHONGO_API void zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *
813829

814830
break;
815831
}
816-
/* break intentionally omitted */
832+
833+
ht_data = Z_OBJ_HT_P(data)->get_properties(data TSRMLS_CC);
834+
ht_data_from_properties = true;
835+
break;
817836

818837
case IS_ARRAY:
819838
ht_data = HASH_OF(data);
@@ -850,11 +869,16 @@ PHONGO_API void zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *
850869
}
851870

852871
if (hash_type == HASH_KEY_IS_STRING) {
853-
if (Z_TYPE_P(data) == IS_OBJECT) {
872+
if (ht_data_from_properties) {
854873
const char *class_name;
855874

856875
zend_unmangle_property_name(key, key_len-1, &class_name, (const char **)&key);
857876
key_len = strlen(key);
877+
878+
/* Ignore non-public properties */
879+
if (!is_public_property(Z_OBJCE_P(data), key, key_len TSRMLS_CC)) {
880+
continue;
881+
}
858882
} else {
859883
/* Chop off the \0 from string lengths */
860884
key_len -= 1;

tests/bson/bson-fromPHP-002.phpt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ hex_dump($s);
2424
===DONE===
2525
<?php exit(0); ?>
2626
--EXPECT--
27-
Test { "foo" : 1, "bar" : 2, "baz" : 3 }
28-
0 : 20 00 00 00 10 66 6f 6f 00 01 00 00 00 10 62 61 [ ....foo......ba]
29-
10 : 72 00 02 00 00 00 10 62 61 7a 00 03 00 00 00 00 [r......baz......]
27+
Test { "baz" : 3 }
28+
0 : 0e 00 00 00 10 62 61 7a 00 03 00 00 00 00 [.....baz......]
3029
===DONE===

tests/bson/bson-fromPHP-003.phpt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,10 @@ Test { "0" : { "$date" : 1416445411987 } }
3737
0 : 10 00 00 00 09 30 00 93 c2 b9 ca 49 01 00 00 00 [.....0.....I....]
3838
Test { "x" : { "$date" : 1416445411987 } }
3939
0 : 10 00 00 00 09 78 00 93 c2 b9 ca 49 01 00 00 00 [.....x.....I....]
40-
Test { "0" : { "foo" : 1, "bar" : 2, "baz" : 3 } }
41-
0 : 28 00 00 00 03 30 00 20 00 00 00 10 66 6f 6f 00 [(....0. ....foo.]
42-
10 : 01 00 00 00 10 62 61 72 00 02 00 00 00 10 62 61 [.....bar......ba]
43-
20 : 7a 00 03 00 00 00 00 00 [z.......]
44-
Test { "x" : { "foo" : 1, "bar" : 2, "baz" : 3 } }
45-
0 : 28 00 00 00 03 78 00 20 00 00 00 10 66 6f 6f 00 [(....x. ....foo.]
46-
10 : 01 00 00 00 10 62 61 72 00 02 00 00 00 10 62 61 [.....bar......ba]
47-
20 : 7a 00 03 00 00 00 00 00 [z.......]
40+
Test { "0" : { "baz" : 3 } }
41+
0 : 16 00 00 00 03 30 00 0e 00 00 00 10 62 61 7a 00 [.....0......baz.]
42+
10 : 03 00 00 00 00 00 [......]
43+
Test { "x" : { "baz" : 3 } }
44+
0 : 16 00 00 00 03 78 00 0e 00 00 00 10 62 61 7a 00 [.....x......baz.]
45+
10 : 03 00 00 00 00 00 [......]
4846
===DONE===

0 commit comments

Comments
 (0)