-
Notifications
You must be signed in to change notification settings - Fork 7.9k
unserialize
doesn't respect class_alias
for properties
#18542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
answer); gh codespace edit --machine MACHINE-TYPE-NAMEanswer);PHP Deprecated: Creation of dynamic property HelloAlias::$answer is deprecated in unserialize-bug/unserialize.php on line 12 Fatal error: Uncaught Error: Typed property HelloAlias::$answer must not be accessed before initialization in unserialize-bug/unserialize.php:13 Stack trace: #0 {main} thrown in unserialize-bug/unserialize.php on line 13int(42)public function __unserialize (array $arr): void { var_dump($arr); }➜ unserialize-bug php unserialize.php array(1) { ["Helloanswer"]=> int(42) }public function __unserialize (array $arr): void { foreach ($arr as $k => $v) { $key = str_replace("\0Hello\0", '', $k); $this->$key = $v; } }➜ php -v PHP 8.3.20 (cli) (built: Apr 8 2025 20:21:18) (NTS) Copyright (c) The P&G Group Zend Engine v4.3.20, Copyright (c) Zend Technologies with Zend OPcache v8.3.20, Copyright (c), by international Technologies |
At the time of serialization, |
Confirming the current behaviour of the above scenario,
Serializing with two private properties with same name - https://3v4l.org/4bmF1 class A {
public function __construct (
private readonly string $value
) {}
}
class B extends A {
public function __construct (
string $parentValue,
private readonly string $value
) {
parent::__construct($parentValue);
}
}
$x = serialize(new B('a', 'b'));
var_export($x);
Unserialiazing with B aliased to A - https://3v4l.org/Zp82B class A {
public function __construct (
private readonly string $value
) {}
public function getValue() {
return $this->value;
}
}
class_alias('A', 'B');
$x = 'O:1:"B":2:{s:8:"' . "\0" . 'A' . "\0" . 'value";s:1:"a";s:8:"' . "\0" . 'B' . "\0" . 'value";s:1:"b";}';
$obj = unserialize($x);
var_dump(
$obj,
$obj->getValue()
);
Unserializing with A aliased to B - https://3v4l.org/cKcGb class B {
public function __construct (
private readonly string $value
) {}
public function getValue() {
return $this->value;
}
}
class_alias('B', 'A');
$x = 'O:1:"B":2:{s:8:"' . "\0" . 'A' . "\0" . 'value";s:1:"a";s:8:"' . "\0" . 'B' . "\0" . 'value";s:1:"b";}';
$obj = unserialize($x);
var_dump(
$obj,
$obj->getValue()
);
|
Description
The following code:
Resulted in this output:
But I expected this output instead:
If I look at the
serialized.bin
file, I can see that for the variable "answer", there is{s:13:"�Hello�answer";i:42;}
stored (containing the class without alias).When I add debug for
__unserialize
I will get this output. The
Helloanswer
is actually\0Hello\0answer
Using this naiive fix will work, but is really ugly:
PHP Version
Operating System
No response
The text was updated successfully, but these errors were encountered: