-
Notifications
You must be signed in to change notification settings - Fork 7.8k
RFC: Make unserialize() emit a warning for trailing bytes #9630
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
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
26 changes: 26 additions & 0 deletions
26
ext/standard/tests/serialize/unserialize_extra_data_001.phpt
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,26 @@ | ||
--TEST-- | ||
Test unserialize() with extra data at the end of a valid value | ||
--FILE-- | ||
<?php | ||
|
||
var_dump(unserialize('i:5;i:6;')); | ||
var_dump(unserialize('N;i:6;')); | ||
var_dump(unserialize('b:1;i:6;')); | ||
var_dump(unserialize('a:1:{s:3:"foo";b:1;}i:6;')); | ||
|
||
?> | ||
--EXPECTF-- | ||
Warning: unserialize(): Extra data starting at offset 4 of 8 bytes in %s on line %d | ||
int(5) | ||
|
||
Warning: unserialize(): Extra data starting at offset 2 of 6 bytes in %s on line %d | ||
NULL | ||
|
||
Warning: unserialize(): Extra data starting at offset 4 of 8 bytes in %s on line %d | ||
bool(true) | ||
|
||
Warning: unserialize(): Extra data starting at offset 20 of 24 bytes in %s on line %d | ||
array(1) { | ||
["foo"]=> | ||
bool(true) | ||
} |
42 changes: 42 additions & 0 deletions
42
ext/standard/tests/serialize/unserialize_extra_data_002.phpt
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,42 @@ | ||
--TEST-- | ||
Test unserialize() with extra data at the end of a valid value with nested unserialize | ||
--FILE-- | ||
<?php | ||
|
||
final class Foo { | ||
public $foo; | ||
|
||
public function __unserialize(array $foo) | ||
{ | ||
$this->foo = unserialize($foo['bar']); | ||
} | ||
|
||
public function __serialize(): array | ||
{ | ||
return [ | ||
'bar' => serialize($this->foo) . 'garbage', | ||
]; | ||
} | ||
} | ||
|
||
$f = new Foo; | ||
$f->foo = ['a', 'b', 'c']; | ||
|
||
var_dump(unserialize(serialize($f) . 'garbage')); | ||
|
||
?> | ||
--EXPECTF-- | ||
Warning: unserialize(): Extra data starting at offset 81 of 88 bytes in %s on line %d | ||
|
||
Warning: unserialize(): Extra data starting at offset 42 of 49 bytes in %s on line %d | ||
object(Foo)#2 (1) { | ||
["foo"]=> | ||
array(3) { | ||
[0]=> | ||
string(1) "a" | ||
[1]=> | ||
string(1) "b" | ||
[2]=> | ||
string(1) "c" | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
ext/standard/tests/serialize/unserialize_extra_data_003.phpt
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,42 @@ | ||
--TEST-- | ||
Test unserialize() with extra data at the end of a valid value with Serializable | ||
--FILE-- | ||
<?php | ||
|
||
final class Foo implements Serializable { | ||
public $foo; | ||
|
||
public function unserialize(string $foo) | ||
{ | ||
$this->foo = unserialize($foo); | ||
} | ||
|
||
public function serialize(): string | ||
{ | ||
return serialize($this->foo) . 'garbage'; | ||
} | ||
} | ||
|
||
$f = new Foo; | ||
$f->foo = ['a', 'b', 'c']; | ||
|
||
var_dump(unserialize(serialize($f) . 'garbage')); | ||
|
||
?> | ||
--EXPECTF-- | ||
Deprecated: Foo implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %s on line %d | ||
|
||
Warning: unserialize(): Extra data starting at offset 42 of 49 bytes in %s on line %d | ||
|
||
Warning: unserialize(): Extra data starting at offset 64 of 71 bytes in %s on line %d | ||
object(Foo)#2 (1) { | ||
["foo"]=> | ||
array(3) { | ||
[0]=> | ||
string(1) "a" | ||
[1]=> | ||
string(1) "b" | ||
[2]=> | ||
string(1) "c" | ||
} | ||
} |
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
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.
Making a note of this since I forgot how this works, and not many people are familiar with how the unserializer works:
If this were to start throwing an exception instead in the next php major version, this would call zval_ptr_dtor to free the allocated values instead of falling through, then RETVAL_FALSE, like the above branch does right now.
The implementation of
zval_ptr_dtor
callsgc_check_possible_root()
, so if that's done, we'll properly collect reference cycles when the gc runs if this throws.