-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add max_depth option to unserialize() #4742
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 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
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,159 @@ | ||
--TEST-- | ||
Bug #78549: Stack overflow due to nested serialized input | ||
--FILE-- | ||
<?php | ||
|
||
function create_nested_data($depth, $prefix, $suffix, $inner = 'i:0;') { | ||
return str_repeat($prefix, $depth) . $inner . str_repeat($suffix, $depth); | ||
} | ||
|
||
echo "Invalid max_depth:\n"; | ||
var_dump(unserialize('i:0;', ['max_depth' => 'foo'])); | ||
var_dump(unserialize('i:0;', ['max_depth' => -1])); | ||
|
||
echo "Array:\n"; | ||
var_dump(unserialize( | ||
create_nested_data(128, 'a:1:{i:0;', '}'), | ||
['max_depth' => 128] | ||
) !== false); | ||
var_dump(unserialize( | ||
create_nested_data(129, 'a:1:{i:0;', '}'), | ||
['max_depth' => 128] | ||
)); | ||
|
||
echo "Object:\n"; | ||
var_dump(unserialize( | ||
create_nested_data(128, 'O:8:"stdClass":1:{i:0;', '}'), | ||
['max_depth' => 128] | ||
) !== false); | ||
var_dump(unserialize( | ||
create_nested_data(129, 'O:8:"stdClass":1:{i:0;', '}'), | ||
['max_depth' => 128] | ||
)); | ||
|
||
// Default depth is 4096 | ||
echo "Default depth:\n"; | ||
var_dump(unserialize(create_nested_data(4096, 'a:1:{i:0;', '}')) !== false); | ||
var_dump(unserialize(create_nested_data(4097, 'a:1:{i:0;', '}'))); | ||
|
||
// Depth can also be adjusted using ini setting | ||
echo "Ini setting:\n"; | ||
ini_set("unserialize_max_depth", 128); | ||
var_dump(unserialize(create_nested_data(128, 'a:1:{i:0;', '}')) !== false); | ||
var_dump(unserialize(create_nested_data(129, 'a:1:{i:0;', '}'))); | ||
|
||
// But an explicitly specified depth still takes precedence | ||
echo "Ini setting overridden:\n"; | ||
var_dump(unserialize( | ||
create_nested_data(256, 'a:1:{i:0;', '}'), | ||
['max_depth' => 256] | ||
) !== false); | ||
var_dump(unserialize( | ||
create_nested_data(257, 'a:1:{i:0;', '}'), | ||
['max_depth' => 256] | ||
)); | ||
|
||
// Reset ini setting to a large value, | ||
// so it's clear that it won't be used in the following. | ||
ini_set("unserialize_max_depth", 4096); | ||
|
||
class Test implements Serializable { | ||
public function serialize() { | ||
return ''; | ||
} | ||
public function unserialize($str) { | ||
// Should fail, due to combined nesting level | ||
var_dump(unserialize(create_nested_data(129, 'a:1:{i:0;', '}'))); | ||
// Should succeeed, below combined nesting level | ||
var_dump(unserialize(create_nested_data(128, 'a:1:{i:0;', '}')) !== false); | ||
} | ||
} | ||
echo "Nested unserialize combined depth limit:\n"; | ||
var_dump(is_array(unserialize( | ||
create_nested_data(128, 'a:1:{i:0;', '}', 'C:4:"Test":0:{}'), | ||
['max_depth' => 256] | ||
))); | ||
|
||
class Test2 implements Serializable { | ||
public function serialize() { | ||
return ''; | ||
} | ||
public function unserialize($str) { | ||
// If depth limit is overridden, the depth should be counted | ||
// from zero again. | ||
var_dump(unserialize( | ||
create_nested_data(257, 'a:1:{i:0;', '}'), | ||
['max_depth' => 256] | ||
)); | ||
var_dump(unserialize( | ||
create_nested_data(256, 'a:1:{i:0;', '}'), | ||
['max_depth' => 256] | ||
) !== false); | ||
} | ||
} | ||
echo "Nested unserialize overridden depth limit:\n"; | ||
var_dump(is_array(unserialize( | ||
create_nested_data(64, 'a:1:{i:0;', '}', 'C:5:"Test2":0:{}'), | ||
['max_depth' => 128] | ||
))); | ||
|
||
?> | ||
--EXPECTF-- | ||
Invalid max_depth: | ||
|
||
Warning: unserialize(): max_depth should be int in %s on line %d | ||
bool(false) | ||
|
||
Warning: unserialize(): max_depth cannot be negative in %s on line %d | ||
bool(false) | ||
Array: | ||
bool(true) | ||
|
||
Warning: unserialize(): Maximum depth of 128 exceeded. The depth limit can be changed using the max_depth unserialize() option or the unserialize_max_depth ini setting in %s on line %d | ||
|
||
Notice: unserialize(): Error at offset 1157 of 1294 bytes in %s on line %d | ||
bool(false) | ||
Object: | ||
bool(true) | ||
|
||
Warning: unserialize(): Maximum depth of 128 exceeded. The depth limit can be changed using the max_depth unserialize() option or the unserialize_max_depth ini setting in %s on line %d | ||
|
||
Notice: unserialize(): Error at offset 2834 of 2971 bytes in %s on line %d | ||
bool(false) | ||
Default depth: | ||
bool(true) | ||
|
||
Warning: unserialize(): Maximum depth of 4096 exceeded. The depth limit can be changed using the max_depth unserialize() option or the unserialize_max_depth ini setting in %s on line %d | ||
|
||
Notice: unserialize(): Error at offset 36869 of 40974 bytes in %s on line %d | ||
bool(false) | ||
Ini setting: | ||
bool(true) | ||
|
||
Warning: unserialize(): Maximum depth of 128 exceeded. The depth limit can be changed using the max_depth unserialize() option or the unserialize_max_depth ini setting in %s on line %d | ||
|
||
Notice: unserialize(): Error at offset 1157 of 1294 bytes in %s on line %d | ||
bool(false) | ||
Ini setting overridden: | ||
bool(true) | ||
|
||
Warning: unserialize(): Maximum depth of 256 exceeded. The depth limit can be changed using the max_depth unserialize() option or the unserialize_max_depth ini setting in %s on line %d | ||
|
||
Notice: unserialize(): Error at offset 2309 of 2574 bytes in %s on line %d | ||
bool(false) | ||
Nested unserialize combined depth limit: | ||
|
||
Warning: unserialize(): Maximum depth of 256 exceeded. The depth limit can be changed using the max_depth unserialize() option or the unserialize_max_depth ini setting in %s on line %d | ||
|
||
Notice: unserialize(): Error at offset 1157 of 1294 bytes in %s on line %d | ||
bool(false) | ||
bool(true) | ||
bool(true) | ||
Nested unserialize overridden depth limit: | ||
|
||
Warning: unserialize(): Maximum depth of 256 exceeded. The depth limit can be changed using the max_depth unserialize() option or the unserialize_max_depth ini setting in %s on line %d | ||
|
||
Notice: unserialize(): Error at offset 2309 of 2574 bytes in %s on line %d | ||
bool(false) | ||
bool(true) | ||
bool(true) |
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
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.
Uh oh!
There was an error while loading. Please reload this page.