Skip to content

Commit b63c10f

Browse files
committed
PHPC-310: Make BSON types final and disable serialization for now
PHONGO_CE_INIT() is used to both make the classes final and disable serialization. Additionally, we provide __wakeUp() to ensure deserialization is also disabled (per PHPC-190). Eventually, BSON types will allow serialization (see PHPC-460) and the included error tests may be revised.
1 parent e12c3b3 commit b63c10f

24 files changed

+376
-0
lines changed

src/BSON/Binary.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ static zend_function_entry php_phongo_binary_me[] = {
123123
PHP_ME(Binary, __construct, ai_Binary___construct, ZEND_ACC_PUBLIC)
124124
PHP_ME(Binary, getData, ai_Binary_getData, ZEND_ACC_PUBLIC)
125125
PHP_ME(Binary, getType, ai_Binary_getType, ZEND_ACC_PUBLIC)
126+
PHP_ME(Manager, __wakeUp, NULL, ZEND_ACC_PUBLIC)
126127
PHP_FE_END
127128
};
128129

@@ -184,6 +185,7 @@ PHP_MINIT_FUNCTION(Binary)
184185
INIT_NS_CLASS_ENTRY(ce, BSON_NAMESPACE, "Binary", php_phongo_binary_me);
185186
php_phongo_binary_ce = zend_register_internal_class(&ce TSRMLS_CC);
186187
php_phongo_binary_ce->create_object = php_phongo_binary_create_object;
188+
PHONGO_CE_INIT(php_phongo_binary_ce);
187189

188190
zend_class_implements(php_phongo_binary_ce TSRMLS_CC, 1, php_phongo_type_ce);
189191

src/BSON/Javascript.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ ZEND_END_ARG_INFO();
8585

8686
static zend_function_entry php_phongo_javascript_me[] = {
8787
PHP_ME(Javascript, __construct, ai_Javascript___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
88+
PHP_ME(Manager, __wakeUp, NULL, ZEND_ACC_PUBLIC)
8889
PHP_FE_END
8990
};
9091

@@ -169,6 +170,7 @@ PHP_MINIT_FUNCTION(Javascript)
169170
INIT_NS_CLASS_ENTRY(ce, BSON_NAMESPACE, "Javascript", php_phongo_javascript_me);
170171
php_phongo_javascript_ce = zend_register_internal_class(&ce TSRMLS_CC);
171172
php_phongo_javascript_ce->create_object = php_phongo_javascript_create_object;
173+
PHONGO_CE_INIT(php_phongo_javascript_ce);
172174

173175
zend_class_implements(php_phongo_javascript_ce TSRMLS_CC, 1, php_phongo_type_ce);
174176

src/BSON/MaxKey.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ PHONGO_API zend_class_entry *php_phongo_maxkey_ce;
5050

5151

5252
static zend_function_entry php_phongo_maxkey_me[] = {
53+
PHP_ME(Manager, __wakeUp, NULL, ZEND_ACC_PUBLIC)
5354
PHP_FE_END
5455
};
5556

@@ -94,6 +95,7 @@ PHP_MINIT_FUNCTION(MaxKey)
9495
INIT_NS_CLASS_ENTRY(ce, BSON_NAMESPACE, "MaxKey", php_phongo_maxkey_me);
9596
php_phongo_maxkey_ce = zend_register_internal_class(&ce TSRMLS_CC);
9697
php_phongo_maxkey_ce->create_object = php_phongo_maxkey_create_object;
98+
PHONGO_CE_INIT(php_phongo_maxkey_ce);
9799

98100
zend_class_implements(php_phongo_maxkey_ce TSRMLS_CC, 1, php_phongo_type_ce);
99101

src/BSON/MinKey.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ PHONGO_API zend_class_entry *php_phongo_minkey_ce;
5050

5151

5252
static zend_function_entry php_phongo_minkey_me[] = {
53+
PHP_ME(Manager, __wakeUp, NULL, ZEND_ACC_PUBLIC)
5354
PHP_FE_END
5455
};
5556

@@ -94,6 +95,7 @@ PHP_MINIT_FUNCTION(MinKey)
9495
INIT_NS_CLASS_ENTRY(ce, BSON_NAMESPACE, "MinKey", php_phongo_minkey_me);
9596
php_phongo_minkey_ce = zend_register_internal_class(&ce TSRMLS_CC);
9697
php_phongo_minkey_ce->create_object = php_phongo_minkey_create_object;
98+
PHONGO_CE_INIT(php_phongo_minkey_ce);
9799

98100
zend_class_implements(php_phongo_minkey_ce TSRMLS_CC, 1, php_phongo_type_ce);
99101

src/BSON/ObjectID.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ ZEND_END_ARG_INFO();
116116
static zend_function_entry php_phongo_objectid_me[] = {
117117
PHP_ME(ObjectID, __construct, ai_ObjectID___construct, ZEND_ACC_PUBLIC)
118118
PHP_ME(ObjectID, __toString, ai_ObjectID___toString, ZEND_ACC_PUBLIC)
119+
PHP_ME(Manager, __wakeUp, NULL, ZEND_ACC_PUBLIC)
119120
PHP_FE_END
120121
};
121122

@@ -186,6 +187,7 @@ PHP_MINIT_FUNCTION(ObjectID)
186187
INIT_NS_CLASS_ENTRY(ce, BSON_NAMESPACE, "ObjectID", php_phongo_objectid_me);
187188
php_phongo_objectid_ce = zend_register_internal_class(&ce TSRMLS_CC);
188189
php_phongo_objectid_ce->create_object = php_phongo_objectid_create_object;
190+
PHONGO_CE_INIT(php_phongo_objectid_ce);
189191

190192
zend_class_implements(php_phongo_objectid_ce TSRMLS_CC, 1, php_phongo_type_ce);
191193

src/BSON/Regex.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ static zend_function_entry php_phongo_regex_me[] = {
152152
PHP_ME(Regex, getPattern, ai_Regex_getPattern, ZEND_ACC_PUBLIC)
153153
PHP_ME(Regex, getFlags, ai_Regex_getFlags, ZEND_ACC_PUBLIC)
154154
PHP_ME(Regex, __toString, ai_Regex___toString, ZEND_ACC_PUBLIC)
155+
PHP_ME(Manager, __wakeUp, NULL, ZEND_ACC_PUBLIC)
155156
PHP_FE_END
156157
};
157158

@@ -219,6 +220,7 @@ PHP_MINIT_FUNCTION(Regex)
219220
INIT_NS_CLASS_ENTRY(ce, BSON_NAMESPACE, "Regex", php_phongo_regex_me);
220221
php_phongo_regex_ce = zend_register_internal_class(&ce TSRMLS_CC);
221222
php_phongo_regex_ce->create_object = php_phongo_regex_create_object;
223+
PHONGO_CE_INIT(php_phongo_regex_ce);
222224

223225
zend_class_implements(php_phongo_regex_ce TSRMLS_CC, 1, php_phongo_type_ce);
224226

src/BSON/Timestamp.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ ZEND_END_ARG_INFO();
103103
static zend_function_entry php_phongo_timestamp_me[] = {
104104
PHP_ME(Timestamp, __construct, ai_Timestamp___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
105105
PHP_ME(Timestamp, __toString, ai_Timestamp___toString, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
106+
PHP_ME(Manager, __wakeUp, NULL, ZEND_ACC_PUBLIC)
106107
PHP_FE_END
107108
};
108109

@@ -163,6 +164,7 @@ PHP_MINIT_FUNCTION(Timestamp)
163164
INIT_NS_CLASS_ENTRY(ce, BSON_NAMESPACE, "Timestamp", php_phongo_timestamp_me);
164165
php_phongo_timestamp_ce = zend_register_internal_class(&ce TSRMLS_CC);
165166
php_phongo_timestamp_ce->create_object = php_phongo_timestamp_create_object;
167+
PHONGO_CE_INIT(php_phongo_timestamp_ce);
166168

167169
zend_class_implements(php_phongo_timestamp_ce TSRMLS_CC, 1, php_phongo_type_ce);
168170

src/BSON/UTCDateTime.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ static zend_function_entry php_phongo_utcdatetime_me[] = {
135135
PHP_ME(UTCDateTime, __construct, ai_UTCDateTime___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
136136
PHP_ME(UTCDateTime, __toString, ai_UTCDateTime___toString, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
137137
PHP_ME(UTCDateTime, toDateTime, ai_UTCDateTime_toDateTime, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
138+
PHP_ME(Manager, __wakeUp, NULL, ZEND_ACC_PUBLIC)
138139
PHP_FE_END
139140
};
140141

@@ -203,6 +204,7 @@ PHP_MINIT_FUNCTION(UTCDateTime)
203204
INIT_NS_CLASS_ENTRY(ce, BSON_NAMESPACE, "UTCDateTime", php_phongo_utcdatetime_me);
204205
php_phongo_utcdatetime_ce = zend_register_internal_class(&ce TSRMLS_CC);
205206
php_phongo_utcdatetime_ce->create_object = php_phongo_utcdatetime_create_object;
207+
PHONGO_CE_INIT(php_phongo_utcdatetime_ce);
206208

207209
zend_class_implements(php_phongo_utcdatetime_ce TSRMLS_CC, 1, php_phongo_type_ce);
208210

tests/bson/bson-binary_error-002.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
BSON BSON\Binary cannot be extended
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
use MongoDB\BSON as BSON;
10+
11+
class MyBinary extends BSON\Binary {}
12+
13+
?>
14+
===DONE===
15+
<?php exit(0); ?>
16+
--EXPECTF--
17+
Fatal error: Class MyBinary may not inherit from final class (%SBSON\Binary) in %s on line %d

tests/bson/bson-binary_error-003.phpt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
BSON BSON\Binary cannot be serialized
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
use MongoDB\BSON as BSON;
10+
11+
echo throws(function() {
12+
serialize(new BSON\Binary('foo', BSON\Binary::TYPE_GENERIC));
13+
}, 'Exception'), "\n";
14+
15+
echo throws(function() {
16+
$classname = BSON_NAMESPACE . '\Binary';
17+
unserialize(sprintf('C:%d:"%s":0:{}', strlen($classname), $classname));
18+
}, 'Exception'), "\n";
19+
20+
?>
21+
===DONE===
22+
<?php exit(0); ?>
23+
--EXPECTF--
24+
OK: Got Exception
25+
Serialization of '%SBSON\Binary' is not allowed
26+
OK: Got Exception
27+
Unserialization of '%SBSON\Binary' is not allowed
28+
===DONE===
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
BSON BSON\Javascript cannot be extended
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
use MongoDB\BSON as BSON;
10+
11+
class MyJavascript extends BSON\Javascript {}
12+
13+
?>
14+
===DONE===
15+
<?php exit(0); ?>
16+
--EXPECTF--
17+
Fatal error: Class MyJavascript may not inherit from final class (%SBSON\Javascript) in %s on line %d
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
BSON BSON\Javascript cannot be serialized
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
use MongoDB\BSON as BSON;
10+
11+
echo throws(function() {
12+
serialize(new BSON\Javascript('function() {}'));
13+
}, 'Exception'), "\n";
14+
15+
echo throws(function() {
16+
$classname = BSON_NAMESPACE . '\Javascript';
17+
unserialize(sprintf('C:%d:"%s":0:{}', strlen($classname), $classname));
18+
}, 'Exception'), "\n";
19+
20+
?>
21+
===DONE===
22+
<?php exit(0); ?>
23+
--EXPECTF--
24+
OK: Got Exception
25+
Serialization of '%SBSON\Javascript' is not allowed
26+
OK: Got Exception
27+
Unserialization of '%SBSON\Javascript' is not allowed
28+
===DONE===

tests/bson/bson-maxkey_error-001.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
BSON BSON\MaxKey cannot be extended
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
use MongoDB\BSON as BSON;
10+
11+
class MyMaxKey extends BSON\MaxKey {}
12+
13+
?>
14+
===DONE===
15+
<?php exit(0); ?>
16+
--EXPECTF--
17+
Fatal error: Class MyMaxKey may not inherit from final class (%SBSON\MaxKey) in %s on line %d

tests/bson/bson-maxkey_error-002.phpt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
BSON BSON\MaxKey cannot be serialized
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
use MongoDB\BSON as BSON;
10+
11+
echo throws(function() {
12+
serialize(new BSON\MaxKey());
13+
}, 'Exception'), "\n";
14+
15+
echo throws(function() {
16+
$classname = BSON_NAMESPACE . '\MaxKey';
17+
unserialize(sprintf('C:%d:"%s":0:{}', strlen($classname), $classname));
18+
}, 'Exception'), "\n";
19+
20+
?>
21+
===DONE===
22+
<?php exit(0); ?>
23+
--EXPECTF--
24+
OK: Got Exception
25+
Serialization of '%SBSON\MaxKey' is not allowed
26+
OK: Got Exception
27+
Unserialization of '%SBSON\MaxKey' is not allowed
28+
===DONE===

tests/bson/bson-minkey_error-001.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
BSON BSON\MinKey cannot be extended
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
use MongoDB\BSON as BSON;
10+
11+
class MyMinKey extends BSON\MinKey {}
12+
13+
?>
14+
===DONE===
15+
<?php exit(0); ?>
16+
--EXPECTF--
17+
Fatal error: Class MyMinKey may not inherit from final class (%SBSON\MinKey) in %s on line %d

tests/bson/bson-minkey_error-002.phpt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
BSON BSON\MinKey cannot be serialized
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
use MongoDB\BSON as BSON;
10+
11+
echo throws(function() {
12+
serialize(new BSON\MinKey());
13+
}, 'Exception'), "\n";
14+
15+
echo throws(function() {
16+
$classname = BSON_NAMESPACE . '\MinKey';
17+
unserialize(sprintf('C:%d:"%s":0:{}', strlen($classname), $classname));
18+
}, 'Exception'), "\n";
19+
20+
?>
21+
===DONE===
22+
<?php exit(0); ?>
23+
--EXPECTF--
24+
OK: Got Exception
25+
Serialization of '%SBSON\MinKey' is not allowed
26+
OK: Got Exception
27+
Unserialization of '%SBSON\MinKey' is not allowed
28+
===DONE===
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
BSON BSON\ObjectID cannot be extended
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
use MongoDB\BSON as BSON;
10+
11+
class MyObjectID extends BSON\ObjectID {}
12+
13+
?>
14+
===DONE===
15+
<?php exit(0); ?>
16+
--EXPECTF--
17+
Fatal error: Class MyObjectID may not inherit from final class (%SBSON\ObjectID) in %s on line %d
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
BSON BSON\ObjectID cannot be serialized
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
use MongoDB\BSON as BSON;
10+
11+
echo throws(function() {
12+
serialize(new BSON\ObjectID());
13+
}, 'Exception'), "\n";
14+
15+
echo throws(function() {
16+
$classname = BSON_NAMESPACE . '\ObjectID';
17+
unserialize(sprintf('C:%d:"%s":0:{}', strlen($classname), $classname));
18+
}, 'Exception'), "\n";
19+
20+
?>
21+
===DONE===
22+
<?php exit(0); ?>
23+
--EXPECTF--
24+
OK: Got Exception
25+
Serialization of '%SBSON\ObjectID' is not allowed
26+
OK: Got Exception
27+
Unserialization of '%SBSON\ObjectID' is not allowed
28+
===DONE===

tests/bson/bson-regex_error-002.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
BSON BSON\Regex cannot be extended
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
use MongoDB\BSON as BSON;
10+
11+
class MyRegex extends BSON\Regex {}
12+
13+
?>
14+
===DONE===
15+
<?php exit(0); ?>
16+
--EXPECTF--
17+
Fatal error: Class MyRegex may not inherit from final class (%SBSON\Regex) in %s on line %d

0 commit comments

Comments
 (0)