Skip to content

Commit 86d3a8f

Browse files
committed
PHPC-208: Restrict ODS to "__pclass" fieldnames
1 parent 848acb0 commit 86d3a8f

File tree

3 files changed

+40
-36
lines changed

3 files changed

+40
-36
lines changed

src/bson.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#undef MONGOC_LOG_DOMAIN
5858
#define MONGOC_LOG_DOMAIN "PHONGO-BSON"
5959

60+
#define PHONGO_ODM_FIELD_NAME "__pclass"
6061

6162
PHP_MINIT_FUNCTION(bson)
6263
{
@@ -207,7 +208,7 @@ bool php_phongo_bson_visit_binary(const bson_iter_t *iter ARG_UNUSED, const char
207208
zval *zchild = NULL;
208209
TSRMLS_FETCH();
209210

210-
if (v_subtype == 0x80) {
211+
if (v_subtype == 0x80 && strcmp(key, PHONGO_ODM_FIELD_NAME) ==0) {
211212
((php_phongo_bson_state *)data)->odm = zend_fetch_class((char *)v_binary, v_binary_len, ZEND_FETCH_CLASS_AUTO|ZEND_FETCH_CLASS_SILENT TSRMLS_CC);
212213
if (((php_phongo_bson_state *)data)->odm) {
213214
return false;
@@ -596,7 +597,7 @@ void object_to_bson(zval *object, const char *key, long key_len, bson_t *bson TS
596597

597598
bson_append_array_begin(bson, key, key_len, &child);
598599
if (instanceof_function(Z_OBJCE_P(object), php_phongo_persistable_ce TSRMLS_CC)) {
599-
bson_append_binary(&child, "__", -1, 0x80, (const uint8_t *)Z_OBJCE_P(object)->name, strlen(Z_OBJCE_P(object)->name));
600+
bson_append_binary(&child, PHONGO_ODM_FIELD_NAME, -1, 0x80, (const uint8_t *)Z_OBJCE_P(object)->name, strlen(Z_OBJCE_P(object)->name));
600601
}
601602
zval_to_bson(retval, PHONGO_BSON_NONE, &child, NULL TSRMLS_CC);
602603
bson_append_array_end(bson, &child);
@@ -738,7 +739,7 @@ PHONGO_API void zval_to_bson(zval *data, phongo_bson_flags_t flags, bson_t *bson
738739
if (instanceof_function(Z_OBJCE_P(data), php_phongo_persistable_ce TSRMLS_CC)) {
739740
zval *retval;
740741

741-
bson_append_binary(bson, "__", -1, 0x80, (const uint8_t *)Z_OBJCE_P(data)->name, strlen(Z_OBJCE_P(data)->name));
742+
bson_append_binary(bson, PHONGO_ODM_FIELD_NAME, -1, 0x80, (const uint8_t *)Z_OBJCE_P(data)->name, strlen(Z_OBJCE_P(data)->name));
742743

743744
zend_call_method_with_0_params(&data, NULL, NULL, BSON_SERIALIZE_FUNC_NAME, &retval);
744745
if(retval) {

tests/bson/bson-encode-003.phpt

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ foreach($tests as $n => $test) {
4646
<?php exit(0); ?>
4747
--EXPECT--
4848
Test#0 { "stuff" : [ { "$type" : "80", "$binary" : "TXlDbGFzcw==" }, "class", "data" ] }
49-
0 : 3f 00 00 00 04 73 74 75 66 66 00 33 00 00 00 05 [?....stuff.3....]
50-
10 : 5f 5f 00 07 00 00 00 80 4d 79 43 6c 61 73 73 02 [__......MyClass.]
51-
20 : 72 61 6e 64 6f 6d 00 06 00 00 00 63 6c 61 73 73 [random.....class]
52-
30 : 00 02 30 00 05 00 00 00 64 61 74 61 00 00 00 [..0.....data...]
49+
0 : 45 00 00 00 04 73 74 75 66 66 00 39 00 00 00 05 [E....stuff.9....]
50+
10 : 5f 5f 70 63 6c 61 73 73 00 07 00 00 00 80 4d 79 [__pclass......My]
51+
20 : 43 6c 61 73 73 02 72 61 6e 64 6f 6d 00 06 00 00 [Class.random....]
52+
30 : 00 63 6c 61 73 73 00 02 30 00 05 00 00 00 64 61 [.class..0.....da]
53+
40 : 74 61 00 00 00 [ta...]
5354
array(1) {
5455
["stuff"]=>
5556
object(MyClass)#6 (1) {
@@ -63,10 +64,10 @@ array(1) {
6364
}
6465
}
6566
Test#1 { "stuff" : [ { "$type" : "80", "$binary" : "TXlDbGFzczI=" }, 1, 2, 3 ] }
66-
0 : 37 00 00 00 04 73 74 75 66 66 00 2b 00 00 00 05 [7....stuff.+....]
67-
10 : 5f 5f 00 08 00 00 00 80 4d 79 43 6c 61 73 73 32 [__......MyClass2]
68-
20 : 10 30 00 01 00 00 00 10 31 00 02 00 00 00 10 32 [.0......1......2]
69-
30 : 00 03 00 00 00 00 00 [.......]
67+
0 : 3d 00 00 00 04 73 74 75 66 66 00 31 00 00 00 05 [=....stuff.1....]
68+
10 : 5f 5f 70 63 6c 61 73 73 00 08 00 00 00 80 4d 79 [__pclass......My]
69+
20 : 43 6c 61 73 73 32 10 30 00 01 00 00 00 10 31 00 [Class2.0......1.]
70+
30 : 02 00 00 00 10 32 00 03 00 00 00 00 00 [.....2.......]
7071
array(1) {
7172
["stuff"]=>
7273
object(MyClass2)#7 (1) {
@@ -82,14 +83,15 @@ array(1) {
8283
}
8384
}
8485
Test#2 { "stuff" : [ [ { "$type" : "80", "$binary" : "TXlDbGFzcw==" }, "class", "data" ], [ { "$type" : "80", "$binary" : "TXlDbGFzczI=" }, 1, 2, 3 ] ] }
85-
0 : 75 00 00 00 04 73 74 75 66 66 00 69 00 00 00 04 [u....stuff.i....]
86-
10 : 30 00 33 00 00 00 05 5f 5f 00 07 00 00 00 80 4d [0.3....__......M]
87-
20 : 79 43 6c 61 73 73 02 72 61 6e 64 6f 6d 00 06 00 [yClass.random...]
88-
30 : 00 00 63 6c 61 73 73 00 02 30 00 05 00 00 00 64 [..class..0.....d]
89-
40 : 61 74 61 00 00 04 31 00 2b 00 00 00 05 5f 5f 00 [ata...1.+....__.]
90-
50 : 08 00 00 00 80 4d 79 43 6c 61 73 73 32 10 30 00 [.....MyClass2.0.]
91-
60 : 01 00 00 00 10 31 00 02 00 00 00 10 32 00 03 00 [.....1......2...]
92-
70 : 00 00 00 00 00 [.....]
86+
0 : 81 00 00 00 04 73 74 75 66 66 00 75 00 00 00 04 [.....stuff.u....]
87+
10 : 30 00 39 00 00 00 05 5f 5f 70 63 6c 61 73 73 00 [0.9....__pclass.]
88+
20 : 07 00 00 00 80 4d 79 43 6c 61 73 73 02 72 61 6e [.....MyClass.ran]
89+
30 : 64 6f 6d 00 06 00 00 00 63 6c 61 73 73 00 02 30 [dom.....class..0]
90+
40 : 00 05 00 00 00 64 61 74 61 00 00 04 31 00 31 00 [.....data...1.1.]
91+
50 : 00 00 05 5f 5f 70 63 6c 61 73 73 00 08 00 00 00 [...__pclass.....]
92+
60 : 80 4d 79 43 6c 61 73 73 32 10 30 00 01 00 00 00 [.MyClass2.0.....]
93+
70 : 10 31 00 02 00 00 00 10 32 00 03 00 00 00 00 00 [.1......2.......]
94+
80 : 00 [.]
9395
array(1) {
9496
["stuff"]=>
9597
array(2) {

tests/bson/bson-encode-004.phpt

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,24 @@ object(Person)#%d (5) {
7373
string(24) "Hannes confidential info"
7474
}
7575
Test { "0" : [ { "$type" : "80", "$binary" : "UGVyc29u" }, "Hannes", 42, [ [ { "$type" : "80", "$binary" : "QWRkcmVzcw==" }, 94086, "USA" ], [ { "$type" : "80", "$binary" : "QWRkcmVzcw==" }, 200, "Iceland" ] ], [ [ { "$type" : "80", "$binary" : "UGVyc29u" }, "Jeremy", 21, { }, { } ] ] ] }
76-
0 : 07 01 00 00 04 30 00 ff 00 00 00 05 5f 5f 00 06 [.....0......__..]
77-
10 : 00 00 00 80 50 65 72 73 6f 6e 02 6e 61 6d 65 00 [....Person.name.]
78-
20 : 07 00 00 00 48 61 6e 6e 65 73 00 10 61 67 65 00 [....Hannes..age.]
79-
30 : 2a 00 00 00 04 61 64 64 72 65 73 73 00 6d 00 00 [*....address.m..]
80-
40 : 00 04 30 00 2f 00 00 00 05 5f 5f 00 07 00 00 00 [..0./....__.....]
81-
50 : 80 41 64 64 72 65 73 73 10 7a 69 70 00 86 6f 01 [.Address.zip..o.]
82-
60 : 00 02 63 6f 75 6e 74 72 79 00 04 00 00 00 55 53 [..country.....US]
83-
70 : 41 00 00 04 31 00 33 00 00 00 05 5f 5f 00 07 00 [A...1.3....__...]
84-
80 : 00 00 80 41 64 64 72 65 73 73 10 7a 69 70 00 c8 [...Address.zip..]
85-
90 : 00 00 00 02 63 6f 75 6e 74 72 79 00 08 00 00 00 [....country.....]
86-
A0 : 49 63 65 6c 61 6e 64 00 00 00 04 66 72 69 65 6e [Iceland....frien]
87-
B0 : 64 73 00 52 00 00 00 04 30 00 4a 00 00 00 05 5f [ds.R....0.J...._]
88-
C0 : 5f 00 06 00 00 00 80 50 65 72 73 6f 6e 02 6e 61 [_......Person.na]
89-
D0 : 6d 65 00 07 00 00 00 4a 65 72 65 6d 79 00 10 61 [me.....Jeremy..a]
90-
E0 : 67 65 00 15 00 00 00 03 61 64 64 72 65 73 73 00 [ge......address.]
91-
F0 : 05 00 00 00 00 03 66 72 69 65 6e 64 73 00 05 00 [......friends...]
92-
100 : 00 00 00 00 00 00 00 [.......]
76+
0 : 1f 01 00 00 04 30 00 17 01 00 00 05 5f 5f 70 63 [.....0......__pc]
77+
10 : 6c 61 73 73 00 06 00 00 00 80 50 65 72 73 6f 6e [lass......Person]
78+
20 : 02 6e 61 6d 65 00 07 00 00 00 48 61 6e 6e 65 73 [.name.....Hannes]
79+
30 : 00 10 61 67 65 00 2a 00 00 00 04 61 64 64 72 65 [..age.*....addre]
80+
40 : 73 73 00 79 00 00 00 04 30 00 35 00 00 00 05 5f [ss.y....0.5...._]
81+
50 : 5f 70 63 6c 61 73 73 00 07 00 00 00 80 41 64 64 [_pclass......Add]
82+
60 : 72 65 73 73 10 7a 69 70 00 86 6f 01 00 02 63 6f [ress.zip..o...co]
83+
70 : 75 6e 74 72 79 00 04 00 00 00 55 53 41 00 00 04 [untry.....USA...]
84+
80 : 31 00 39 00 00 00 05 5f 5f 70 63 6c 61 73 73 00 [1.9....__pclass.]
85+
90 : 07 00 00 00 80 41 64 64 72 65 73 73 10 7a 69 70 [.....Address.zip]
86+
A0 : 00 c8 00 00 00 02 63 6f 75 6e 74 72 79 00 08 00 [......country...]
87+
B0 : 00 00 49 63 65 6c 61 6e 64 00 00 00 04 66 72 69 [..Iceland....fri]
88+
C0 : 65 6e 64 73 00 58 00 00 00 04 30 00 50 00 00 00 [ends.X....0.P...]
89+
D0 : 05 5f 5f 70 63 6c 61 73 73 00 06 00 00 00 80 50 [.__pclass......P]
90+
E0 : 65 72 73 6f 6e 02 6e 61 6d 65 00 07 00 00 00 4a [erson.name.....J]
91+
F0 : 65 72 65 6d 79 00 10 61 67 65 00 15 00 00 00 03 [eremy..age......]
92+
100 : 61 64 64 72 65 73 73 00 05 00 00 00 00 03 66 72 [address.......fr]
93+
110 : 69 65 6e 64 73 00 05 00 00 00 00 00 00 00 00 [iends..........]
9394
array(1) {
9495
[0]=>
9596
object(Person)#%d2 (5) {

0 commit comments

Comments
 (0)