Skip to content

Commit 8efe686

Browse files
Deny serialization before PHP 8.1 too
ZEND_ACC_NOT_SERIALIZABLE exists since PHP 8.1. Before, the zend_class_(un)serialize_deny handlers make serialize() throw, and unserialize() too for the C: format, while the O: format fails with a warning without creating the object.
1 parent 90e24a5 commit 8efe686

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

‎msgpack_class.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "php.h"
2+
#include "zend_interfaces.h"
23

34
#include "php_msgpack.h"
45
#include "msgpack_pack.h"
@@ -517,6 +518,9 @@ void msgpack_init_class() /* {{{ */ {
517518
msgpack_ce->create_object = php_msgpack_base_new;
518519
#if PHP_VERSION_ID >= 80100
519520
msgpack_ce->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE;
521+
#else
522+
msgpack_ce->serialize = zend_class_serialize_deny;
523+
msgpack_ce->unserialize = zend_class_unserialize_deny;
520524
#endif
521525
memcpy(&msgpack_handlers, zend_get_std_object_handlers(),sizeof msgpack_handlers);
522526
msgpack_handlers.offset = offsetof(php_msgpack_base_t, object);
@@ -532,6 +536,9 @@ void msgpack_init_class() /* {{{ */ {
532536
msgpack_unpacker_ce->create_object = php_msgpack_unpacker_new;
533537
#if PHP_VERSION_ID >= 80100
534538
msgpack_unpacker_ce->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE;
539+
#else
540+
msgpack_unpacker_ce->serialize = zend_class_serialize_deny;
541+
msgpack_unpacker_ce->unserialize = zend_class_unserialize_deny;
535542
#endif
536543
memcpy(&msgpack_unpacker_handlers, zend_get_std_object_handlers(),sizeof msgpack_unpacker_handlers);
537544
msgpack_unpacker_handlers.offset = offsetof(php_msgpack_unpacker_t, object);

‎tests/144.phpt‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
--TEST--
22
MessagePack and MessagePackUnpacker cannot be serialized
3-
--SKIPIF--
4-
<?php
5-
if (PHP_VERSION_ID < 80100) {
6-
echo "skip requires PHP 8.1+";
7-
}
83
--FILE--
94
<?php
105
if(!extension_loaded('msgpack')) {
@@ -19,9 +14,12 @@ foreach ([new MessagePack(), new MessagePackUnpacker()] as $object) {
1914
}
2015
}
2116

17+
// Before PHP 8.1, only the C: format reaches the handler that throws
18+
$format = PHP_VERSION_ID < 80100 ? 'C' : 'O';
19+
2220
foreach (['MessagePack', 'MessagePackUnpacker'] as $class) {
2321
try {
24-
unserialize('O:' . strlen($class) . ':"' . $class . '":0:{}');
22+
unserialize($format . ':' . strlen($class) . ':"' . $class . '":0:{}');
2523
} catch (Exception $e) {
2624
echo $e->getMessage(), PHP_EOL;
2725
}

0 commit comments

Comments
 (0)