[php-src] PR #19087: Add allowed_classes_callback to unserialize()
| From: | langemeijer | Date: | Thu, 10 Jul 2025 14:49:47 +0000 |
| Subject: | [php-src] PR #19087: Add allowed_classes_callback to unserialize() | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-32387@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/19087
Author: langemeijer
I want to propose an added
allowed_classes_callback option to
unserialize()
The class name as parsed from the serialized string is passed as a parameter to the callback, it
should return a boolean. true would allow the class, false would block it
the same way 'allowed_classes' would, using __PHP_Incomplete_Class.
The callback will be triggered **after** allowed_classes is evaluated (if present).
This callback would solve a few problems where allowed_classes is not sufficient:
- It would allow for an is_subclass_of() check where for example an interface can be
added to classes that are safe to get unserialized.
- It would allow for better ways to fail, I think the __PHP_Incomplete_Class situation
is kinda yucky. Since this is a callable the developer could just find another solution, for example
throwing an exception, do a trigger_error() or exit().
- This would also allow for fixing legacy applications where it is not exactly clear what is being
unserialized. The callable would return a true value but a E_USER_DEPRECATION is
triggered. This way data can be collected about what classes to allow. Providing a non-disrupting
way to secure unserialize calls. This is especially helpful in very generic unserialize usages like
caches.
- This would make the unserialize_callback_func ini setting redundant for many
use-cases, as any custom autoloading routine could also be executed in this
allowed_classes_callback.
Feedback is very much appreciated.