PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 10, 2022

[FIXED] unserialize() Please specify classes allowed for unserialization in 2nd argument

 January 10, 2022     php, php-7.3, serialization, symfony     No comments   

Issue

In my Symfony application I have a User entity which is serialized. In the unserialize() method, I did this:

    public function unserialize($serialized)
    {
        [
            $this->id,
            $this->email,
            $this->password,
            $this->enabled
        ] = unserialize($serialized);
    }

But PhpStorm underlines in red unserialize($serialized) with the following message:

Please specify classes allowed for unserialization in 2nd argument.

I don't know what I'm supposed to use as a second argument. After some research, I saw that we could put this:

unserialize($serializeObj, ["allowed_classes" => true]);

But I also found this:

unserialize(
    $serializedData,
    ['allowed_classes' => ['Class1', 'Class2']]
);

I'm a little confused, I don't know what I should put in my case so that PhpStorm doesn't complain about this.


Solution

If you are actually serializing an array, and not a class instance, you just need to pass false as allowed classes.

 public function unserialize($serialized)
{
        [
            $this->id,
            $this->email,
            $this->password,
            $this->enabled
        ] = unserialize($serialized, ['allowed_classes' => false]);
}

If you are serializing the whole entity, you need to pass the class you expect to be instantiated from the unserialization

So let's assume the class is App\Entity\User,

public function unserialize($serialized) {

    $new = unserialize($serialized, ['allowed_classes' => [ User::class ]]);
    $this->id       = $new->getId();
    $this->$email   = $new->getEmail();
    $this->password = $new->getPassword();
    $this->enabled  = $new->isEnabled();

}

I'm assuming you have have getter methods in the entity for the sake of simplicity.



Answered By - yivi
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing