All articles

Find and fix PHP problems

Restrict Object Classes When Calling PHP unserialize()

See how allowed_classes changes object restoration in PHP, and reject incomplete objects before accessing properties or calling methods.

Editorial illustration for Restrict Object Classes When Calling PHP unserialize()

Pass an allowed_classes option to unserialize() when you know which object class you expect. An array names the classes PHP may restore; false allows no object classes. If the serialized object's class is excluded, PHP produces a __PHP_Incomplete_Class instead. Check the restored value with instanceof before treating it as your expected object. The option controls class restoration, but it does not make raw untrusted input safe to pass to unserialize().

Pass an allowed_classes list to permit only the object classes you expect

The second argument to unserialize() is an options array. Its allowed_classes entry can be an array of class names. If you omit the entry, PHP treats it as true and attempts to instantiate objects of any class. Supplying a list makes the intended class explicit instead. The options parameter was added in PHP 7.0, so the example below is for PHP 7.0 or later.

This self-contained example creates its own serialized value. It does not require a stored file, another class declaration, or any input from a request. Save it as a PHP file, including the opening <?php tag:

<?php
class SavedNote
{
    public $message;
}

$original = new SavedNote();
$original->message = 'Read this note';
$serialized = serialize($original);

$permitted = unserialize($serialized, [
    'allowed_classes' => ['SavedNote'],
]);

$excluded = unserialize($serialized, [
    'allowed_classes' => [],
]);

// allowed_classes does not affect enumerations.
$noClasses = unserialize($serialized, [
    'allowed_classes' => false,
]);

function reportRestoredValue($label, $value)
{
    if ($value instanceof SavedNote) {
        echo $label . ': ' . $value->message . "\n";
    } elseif ($value instanceof __PHP_Incomplete_Class) {
        echo $label . ": incomplete object rejected\n";
    } else {
        echo $label . ": unexpected value rejected\n";
    }
}

reportRestoredValue('Permitted', $permitted);
reportRestoredValue('Excluded', $excluded);
reportRestoredValue('No classes', $noClasses);

The permitted list contains SavedNote, the class used to create the serialized object. For this example, the expected lines are:

Permitted: Read this note
Excluded: incomplete object rejected
No classes: incomplete object rejected

The method of checking matters as much as the list: reportRestoredValue() reads $value->message only after $value instanceof SavedNote succeeds. Nothing in either rejection branch assumes the restored value is a usable SavedNote.

Use false to disallow classes and identify __PHP_Incomplete_Class results

An empty list excludes SavedNote because it names no permitted classes. Setting allowed_classes to false disallows object classes altogether. Both calls therefore encounter an object whose class is not accepted and restore it as __PHP_Incomplete_Class, rather than as SavedNote. The PHP manual's incomplete-class entry documents this result for classes missing from allowed_classes.

An incomplete object can still carry unserialized properties and a __PHP_Incomplete_Class_Name property containing the serialized class name. Those details can help identify what was in the data, but they are not a reason to use the result as the expected class. In particular, a property that looks like the expected message does not turn an incomplete object into a SavedNote.

There is one boundary to the false example: the unserialize() manual says allowed_classes does not affect enumerations. Do not interpret false as a rule covering enumerations as well as object classes.

Check the restored type before accessing properties or calling methods

Choose the list for the object classes your code intends to accept, then make the consuming code enforce its own expectation. In the example, instanceof SavedNote is the condition for reading the property. instanceof __PHP_Incomplete_Class identifies the two deliberately disallowed results, and the final branch rejects anything else. If subsequent code expects a method instead of a property, put that call behind the same expected-class check.

This is a class restriction, not a general trust check. The PHP manual's warning about user input says not to pass user input directly to unserialize() regardless of the allowed_classes value. Use the option to narrow which classes a restore may produce, and use the type check to avoid mistaking a rejected class for the object your code needs.

← All articles