Skip to content

Make Redis, RedisArray, RedisCluster and RedisSentinel not serializable - #2938

Merged
michael-grunder merged 2 commits into
phpredis:developfrom
nicolas-grekas:not-serializable
Sep 29, 2026
Merged

michael-grunder merged 2 commits into
phpredis:developfrom
nicolas-grekas:not-serializable

Conversation

@nicolas-grekas

@nicolas-grekas nicolas-grekas commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

serialize() turns these objects into empty ones, and unserialize() creates them without their connection, which can crash PHP:

unserialize('O:12:"RedisCluster":0:{}')->acl('x', 'y'); // segfault

This flags them with @not-serializable, so that both throw, like for PDO or CurlHandle, and like php/php-src#21694 did for XMLWriter, XMLReader, SNMP and tidy. Serializers that honor the flag, like symfony/php-ext-deepclone, then reject them too. Before PHP 8.1, which has no such flag, they get the handlers that deny serialization instead.

serialize() turned them into empty objects, and unserialize() created
objects without their connection, which can crash, eg calling acl() on
such a RedisCluster segfaults. On PHP 8.1+, both now throw instead.
@michael-grunder

michael-grunder commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

This PR looks good, but only protects the classes when PHP >= 8.1

I had to look it up but apparently the older way is as follows:

static inline void redis_class_deny_serialization(zend_class_entry *ce) {
#if PHP_VERSION_ID < 80100
    ce->serialize = zend_class_serialize_deny;
    ce->unserialize = zend_class_unserialize_deny;
#endif
}

Could use the above function after class registration.

I'm also happy to merge this as-is and then add that in a subsequent commit.

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.
@nicolas-grekas

Copy link
Copy Markdown
Contributor Author

Thanks, PR updated: before PHP 8.1, the classes now get the deny handlers, and the test runs on 7.4 and 8.0 too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants