Add allowed_classes_callback to unserialize() #19087
Open
+247
−9
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I want to propose an added
allowed_classes_callback
option tounserialize()
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 atrigger_error()
orexit()
.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 thisallowed_classes_callback
.Feedback is very much appreciated.