Repository navigation
zend_API: Verify property types in object_properties_load()
#23639
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
Merged
TimWolla
merged 6 commits into
php:master
from
TimWolla:object-properties-load-type-check
Sep 15, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5f404e2
zend_API: Verify property types in `object_properties_load()`
TimWolla e531c2d
zend_API: Fix various issues in `object_properties_load()`
ndossche 0ddaa68
NEWS
TimWolla 8be1197
random: Remove now-obsolete manual `$engine` type check in `Randomize…
TimWolla d4f0012
zend_API: Make `object_properties_load()` use `strict_types=1`
TimWolla 797221f
Block creation of readonly reference properties
ndossche File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| --TEST-- | ||
| GH-23639 (object_properties_load allows creating readonly reference properties) | ||
| --CREDITS-- | ||
| arnaud-lb | ||
| ndossche | ||
| --XFAIL-- | ||
| Test can only succeed when GH-23629 is also merged | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| class Time_Duration { | ||
| public int $seconds; | ||
| public int $nanoseconds; | ||
| public bool $negative; | ||
| } | ||
|
|
||
| $d = new Time_Duration(); | ||
| $d->seconds = 1; | ||
| $a = [$d, &$d->seconds]; | ||
|
|
||
| $payload = serialize($a); | ||
|
|
||
| try { | ||
| unserialize(str_replace('Time_Duration', 'Time\\Duration', $payload)); | ||
| } catch (Throwable $e) { | ||
| do { | ||
| echo $e::class, ": ", $e->getMessage(), "\n"; | ||
| } while ($e = $e->getPrevious()); | ||
| } | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| Exception: Invalid serialization data for Time\Duration object | ||
| Error: Cannot indirectly modify readonly property Time\Duration::$seconds |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| --TEST-- | ||
| GH-9708: object_properties_load() bypasses typed property checks | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| try { | ||
| unserialize('O:17:"Random\Randomizer":1:{i:0;a:1:{s:6:"engine";N;}}'); | ||
| } catch (Throwable $e) { | ||
| echo $e::class, ': ', $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| Exception: Invalid serialization data for Random\Randomizer object |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| --TEST-- | ||
| GH-9707: object_properties_load crashes in debug mode when unserializing references to typed properties in php 8.1+ | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| class Foo extends ArrayObject { | ||
| public int $a = 0; | ||
| public int $b = 0; | ||
| } | ||
|
|
||
| $f = new Foo(); | ||
| $r = &$f->a; | ||
| $f->b = &$r; | ||
|
|
||
| $f->b = 1; | ||
| var_dump($unserialized = unserialize(serialize($f))); | ||
|
|
||
| $unserialized->b = 2; | ||
|
|
||
| var_dump($unserialized); | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| object(Foo)#%d (3) { | ||
| ["a"]=> | ||
| &int(1) | ||
| ["b"]=> | ||
| &int(1) | ||
| ["storage":"ArrayObject":private]=> | ||
| array(0) { | ||
| } | ||
| } | ||
| object(Foo)#%d (3) { | ||
| ["a"]=> | ||
| &int(2) | ||
| ["b"]=> | ||
| &int(2) | ||
| ["storage":"ArrayObject":private]=> | ||
| array(0) { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| --TEST-- | ||
| GH-9708: object_properties_load() bypasses typed property checks | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| class Foo extends ArrayObject { | ||
| public int $a = 5; | ||
| public string $b = "10"; | ||
| } | ||
|
|
||
| try { | ||
| // a = "10", b = 5 | ||
| unserialize('O:3:"Foo":4:{i:0;i:0;i:1;a:0:{}i:2;a:2:{s:1:"a";s:2:"10";s:1:"b";i:5;}i:3;N;}'); | ||
| } catch (Throwable $e) { | ||
| echo $e::class, ': ', $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| TypeError: Cannot assign string to property Foo::$a of type int |
Oops, something went wrong.
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.
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.
This is orthogonal but may be worth investigating: I think that this sequence is dangerous as well due to destructor behaviour, should probably use the zend_safe_assign_to_variable_noref or alike
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.
Test cases:
This would not be resolved by #20001 as unserialize() must delay dtors until the very end.
This could be fixed separately as it's pre-existing.
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.
Yeah, leaving this for a future fix. This PR already greatly improves the situation and it's necessary to unblock the Time\Duration unserialization.