-
Notifications
You must be signed in to change notification settings - Fork 18
Unserialize whitelist #37
Changes from 3 commits
427f6bb
0dc335e
b32b4ee
4423d9c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| <?php | ||
| /** | ||
| * Zend Framework (http://framework.zend.com/) | ||
| * | ||
| * @link http://github.com/zendframework/zf2 for the canonical source repository | ||
| * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
|
||
| * @license http://framework.zend.com/license/new-bsd New BSD License | ||
| */ | ||
|
|
||
| namespace Zend\Serializer\Adapter; | ||
|
|
||
| use Zend\Json\Json as ZendJson; | ||
| use Zend\Serializer\Exception; | ||
|
|
||
| class PhpSerializeOptions extends AdapterOptions | ||
| { | ||
| /** | ||
| * The list of allowed classes for unserialization (PHP 7.0+) | ||
| * Possible values: | ||
| * Array of class names that are allowed to be unserialized | ||
| * or true if all classes should be allowed (behavior of pre PHP 7.0) | ||
| * or false if no classes should be allowed | ||
| * | ||
| * @var array|bool | ||
| */ | ||
| protected $unserializeClassWhitelist = true; | ||
|
|
||
| /** | ||
| * @param array|bool $unserializeClassWhitelist | ||
|
||
| * | ||
| * @return PhpSerializeOptions | ||
| */ | ||
| public function setUnserializeClassWhitelist($unserializeClassWhitelist) | ||
| { | ||
| if (($unserializeClassWhitelist !== true) && (PHP_MAJOR_VERSION < 7)) { | ||
| throw new Exception\InvalidArgumentException( | ||
| 'Class whitelist for unserialize() is only available on PHP 7.0 or higher.' | ||
| ); | ||
| } | ||
|
|
||
| $this->unserializeClassWhitelist = $unserializeClassWhitelist; | ||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * @return array|bool | ||
|
||
| */ | ||
| public function getUnserializeClassWhitelist() | ||
| { | ||
| return $this->unserializeClassWhitelist; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -165,4 +165,49 @@ public function testUnserializingInvalidStringRaisesException($string, $expected | |
| $this->expectExceptionMessage($expected); | ||
| $this->adapter->unserialize($string); | ||
| } | ||
|
|
||
| public function testUnserializeNoWhitelistedClasses() | ||
| { | ||
| $value = 'O:8:"stdClass":0:{}'; | ||
|
|
||
| $this->adapter->getOptions()->setUnserializeClassWhitelist(false); | ||
|
||
|
|
||
| $data = $this->adapter->unserialize($value); | ||
|
|
||
| if (PHP_MAJOR_VERSION >= 7) { | ||
| $this->assertNotInstanceOf(\stdClass::class, $data); | ||
| $this->assertInstanceOf('__PHP_Incomplete_Class', $data); | ||
| } else { | ||
| // Pre PHP 7.0 the whitelist should have no effect | ||
| $this->assertInstanceOf(\stdClass::class, $data); | ||
| } | ||
| } | ||
|
|
||
| public function testUnserializeClassNotAllowed() | ||
| { | ||
| $value = 'O:8:"stdClass":0:{}'; | ||
|
|
||
| $this->adapter->getOptions()->setUnserializeClassWhitelist([\My\Dummy::class]); | ||
|
||
|
|
||
| $data = $this->adapter->unserialize($value); | ||
|
|
||
| if (PHP_MAJOR_VERSION >= 7) { | ||
| $this->assertNotInstanceOf(\stdClass::class, $data); | ||
| $this->assertInstanceOf('__PHP_Incomplete_Class', $data); | ||
| } else { | ||
| // Pre PHP 7.0 the whitelist should have no effect | ||
| $this->assertInstanceOf(\stdClass::class, $data); | ||
| } | ||
| } | ||
|
|
||
| public function testUnserializeClassAllowed() | ||
| { | ||
| $value = 'O:8:"stdClass":0:{}'; | ||
|
|
||
| $this->adapter->getOptions()->setUnserializeClassWhitelist([\stdClass::class]); | ||
|
||
|
|
||
| $data = $this->adapter->unserialize($value); | ||
| $this->assertInstanceOf(\stdClass::class, $data); | ||
| $this->assertNotInstanceOf('__PHP_Incomplete_Class', $data); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please import
Traversable