Skip to content

[DeepClone] Reject more internal classes that keep their state out of their properties - #708

Merged
nicolas-grekas merged 3 commits into
1.xfrom
deepclone-internal-state
Sep 29, 2026
Merged

nicolas-grekas merged 3 commits into
1.xfrom
deepclone-internal-state

Conversation

@nicolas-grekas

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

Copy link
Copy Markdown
Member
Q A
Branch? 1.x
Bug fix? yes
New feature? no
Deprecations? no
Issues -
License MIT

Builds on #703.

The extension rejects the internal classes that keep their state out of their properties and declare no serialization API, in deepclone_to_array(), deepclone_hydrate() and, with symfony/php-ext-deepclone#53, deepclone_from_array(). The polyfill can't see which classes do so and knew only the ones of zip, xmlreader, xmlwriter, snmp and tidy: it exported a Redis or an Imagick without its state, and deepclone_from_array() created them, which can crash PHP, eg when a bare Relay\Table is destroyed or a clone of a bare AMQPConnection is dumped. It now also knows the classes of the redis, relay, imagick, amqp, apcu and msgpack extensions, and rejects them before creating a prototype, together with their user subclasses that declare no serialization API. Classes of other extensions stay accepted.

Like the extension, deepclone_from_array() now also rejects the internal classes whose state serialize() loses, like IteratorIterator, as deepclone_hydrate() already did, and both reject their user subclasses too, unless they declare a serialization API.

@nicolas-grekas
nicolas-grekas force-pushed the deepclone-internal-state branch from 7ef7d08 to 9b36ac3 Compare September 29, 2026 10:46
nicolas-grekas added a commit to symfony/php-ext-deepclone that referenced this pull request Sep 29, 2026
…from_array() (nicolas-grekas)

This PR was merged into the main branch.

Discussion
----------

Reject classes that keep their state internally in deepclone_from_array()

Builds on #50 and #52.

`deepclone_to_array()` and `deepclone_hydrate()` reject the internal classes that keep their state out of their properties and declare no serialization API, like `Redis`, `Imagick` or the AMQP and Relay classes, but `deepclone_from_array()` created them when a payload named them, eg one produced by the polyfill. `unserialize()` creates them that way too, and some crash PHP then: `RedisCluster::acl()` or `ImagickDraw::affine()` segfault on such objects, and a `Relay\Table` does when destroyed.

The class lookup of `deepclone_from_array()` now applies the same rule, which the three functions share in one helper, and throws `NotInstantiableException`. User classes that extend such a class without declaring a serialization API are rejected too, in all three functions: a subclass of `RedisCluster` crashed the same way. For user classes, that's one more flag test per class and call. `IteratorIterator` and the other internal iterators wrapping another one are rejected too, where #50 created them like `unserialize()` does, and so are their user subclasses. Payloads that carry serialized strings still create whatever `unserialize()` creates from them, unless `$allowed_classes` excludes it.

The polyfill gets the same change in symfony/polyfill#708, for the classes of the extensions it knows, as it can't tell which classes keep their state internally.

Commits
-------

f7b2c3c Reject classes that keep their state internally in deepclone_from_array()
@nicolas-grekas
nicolas-grekas force-pushed the deepclone-internal-state branch from 9b36ac3 to ba91111 Compare September 29, 2026 15:25
…deepclone_from_array() and deepclone_hydrate() (nicolas-grekas)

This PR was merged into the 1.x branch.

Discussion
----------

[DeepClone] Reject and create classes like the extension in deepclone_from_array() and deepclone_hydrate()

| Q             | A
| ------------- | ---
| Branch?       | 1.x
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | -
| License       | MIT

`deepclone_from_array()` now creates `IteratorIterator` and the other classes whose state `serialize()` loses, like `unserialize()` and the extension do, while `deepclone_to_array()` keeps rejecting them. `deepclone_hydrate()` creates their user subclasses, like the extension that checks only internal ones.

Classes extending an anonymous one through `class_alias()` are rejected, like `serialize()` does, and serialized objects of a payload that `unserialize()` fails to decode throw the extension's `ValueError`, with what `unserialize()` threw as previous exception. Messages match the extension's too: declared class names, `Type "X"` in `deepclone_hydrate()`, names cut at NUL bytes.

This builds on #699. The extension gets the matching changes in symfony/php-ext-deepclone#50.

Commits
-------

0fab20e [DeepClone] Reject and create classes like the extension in deepclone_from_array() and deepclone_hydrate()
@nicolas-grekas
nicolas-grekas force-pushed the deepclone-internal-state branch from ba91111 to 14a31d4 Compare September 29, 2026 15:38
@nicolas-grekas
nicolas-grekas merged commit 032781f into 1.x Sep 29, 2026
40 checks passed
@nicolas-grekas
nicolas-grekas deleted the deepclone-internal-state branch September 29, 2026 15:38
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.

1 participant