Skip to content

Commit 8a582e3

Browse files
committed
serializer: more unserialize_callback_func INI tests with different types of string callables
1 parent 667681b commit 8a582e3

8 files changed

Lines changed: 275 additions & 0 deletions
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--TEST--
2+
unserialize_callback_func with partially deprecated callable string
3+
--INI--
4+
unserialize_callback_func=parent::my_unserialize
5+
--FILE--
6+
<?php
7+
8+
class TesterParent {
9+
public static function my_unserialize($name) {
10+
echo 'callback_called in ', __CLASS__ , PHP_EOL;
11+
eval('class Foo {}');
12+
}
13+
}
14+
15+
class TesterChild extends TesterParent {
16+
public static function my_unserialize($name) {
17+
echo 'callback_called in ', __CLASS__ , PHP_EOL;
18+
eval('class Foo {}');
19+
}
20+
21+
public function unserialize(string $str) {
22+
return unserialize($str);
23+
}
24+
}
25+
26+
$s = 'O:3:"FOO":0:{}';
27+
try {
28+
$o = unserialize($s);
29+
var_dump($o);
30+
} catch (Throwable $e) {
31+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
32+
}
33+
34+
try {
35+
$tester = new TesterChild();
36+
$o = $tester->unserialize($s);
37+
var_dump($o);
38+
} catch (Throwable $e) {
39+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
40+
}
41+
echo "Done";
42+
43+
?>
44+
--EXPECTF--
45+
Error: Invalid callback parent::my_unserialize, cannot access "parent" when no class scope is active
46+
47+
Deprecated: Use of "parent" in callables is deprecated in %s on line %d
48+
callback_called in TesterParent
49+
object(Foo)#3 (0) {
50+
}
51+
Done
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
unserialize_callback_func with fully qualified name function
3+
--INI--
4+
unserialize_callback_func=\my_global_fn
5+
--FILE--
6+
<?php
7+
function my_global_fn($name) {
8+
echo "callback_called\n";
9+
eval('class Foo {}');
10+
}
11+
12+
$o = unserialize('O:3:"FOO":0:{}');
13+
14+
var_dump($o);
15+
16+
echo "Done";
17+
?>
18+
--EXPECT--
19+
callback_called
20+
object(Foo)#1 (0) {
21+
}
22+
Done
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
unserialize_callback_func with fully qualified named namespaced function
3+
--INI--
4+
unserialize_callback_func=\php\test\my_global_fn
5+
--FILE--
6+
<?php
7+
8+
namespace php\test {
9+
function my_global_fn($name) {
10+
echo "callback_called\n";
11+
eval('class Foo {}');
12+
}
13+
}
14+
namespace {
15+
$o = unserialize('O:3:"FOO":0:{}');
16+
17+
var_dump($o);
18+
19+
echo "Done";
20+
}
21+
?>
22+
--EXPECT--
23+
callback_called
24+
object(Foo)#1 (0) {
25+
}
26+
Done
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
unserialize_callback_func with function name containing null bytes
3+
--FILE--
4+
<?php
5+
6+
ini_set('unserialize_callback_func', "foo\0butno");
7+
8+
function foo(string $name) {
9+
echo "callback_called\n";
10+
eval('class Foo {}');
11+
}
12+
13+
$s = 'O:3:"FOO":0:{}';
14+
try {
15+
$o = unserialize($s);
16+
var_dump($o);
17+
} catch (Throwable $e) {
18+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
19+
}
20+
21+
echo "Done";
22+
?>
23+
--EXPECT--
24+
Error: Invalid callback foo, function "foo" not found or invalid function name
25+
Done
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
unserialize_callback_func with private non-static method
3+
--INI--
4+
unserialize_callback_func=Tester::my_unserialize
5+
--FILE--
6+
<?php
7+
8+
class Tester {
9+
private function my_unserialize($name) {
10+
echo "callback_called\n";
11+
eval('class Foo {}');
12+
}
13+
14+
public function unserialize(string $str) {
15+
return unserialize($str);
16+
}
17+
}
18+
19+
$s = 'O:3:"FOO":0:{}';
20+
try {
21+
$o = unserialize($s);
22+
var_dump($o);
23+
} catch (Throwable $e) {
24+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
25+
}
26+
27+
try {
28+
$tester = new Tester();
29+
$o = $tester->unserialize($s);
30+
var_dump($o);
31+
} catch (Throwable $e) {
32+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
33+
}
34+
echo "Done";
35+
36+
?>
37+
--EXPECT--
38+
Error: Invalid callback Tester::my_unserialize, non-static method Tester::my_unserialize() cannot be called statically
39+
callback_called
40+
object(Foo)#3 (0) {
41+
}
42+
Done
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
unserialize_callback_func with public non-static method
3+
--INI--
4+
unserialize_callback_func=Tester::my_unserialize
5+
--FILE--
6+
<?php
7+
8+
class Tester {
9+
public function my_unserialize($name) {
10+
echo "callback_called\n";
11+
eval('class Foo {}');
12+
}
13+
14+
public function unserialize(string $str) {
15+
return unserialize($str);
16+
}
17+
}
18+
19+
$s = 'O:3:"FOO":0:{}';
20+
try {
21+
$o = unserialize($s);
22+
var_dump($o);
23+
} catch (Throwable $e) {
24+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
25+
}
26+
27+
try {
28+
$tester = new Tester();
29+
$o = $tester->unserialize($s);
30+
var_dump($o);
31+
} catch (Throwable $e) {
32+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
33+
}
34+
echo "Done";
35+
36+
?>
37+
--EXPECT--
38+
Error: Invalid callback Tester::my_unserialize, non-static method Tester::my_unserialize() cannot be called statically
39+
callback_called
40+
object(Foo)#3 (0) {
41+
}
42+
Done
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
unserialize_callback_func with public static method
3+
--INI--
4+
unserialize_callback_func=Tester::my_unserialize
5+
--FILE--
6+
<?php
7+
8+
class Tester {
9+
public static function my_unserialize($name) {
10+
echo "callback_called\n";
11+
eval('class Foo {}');
12+
}
13+
}
14+
15+
$o = unserialize('O:3:"FOO":0:{}');
16+
17+
var_dump($o);
18+
19+
echo "Done";
20+
21+
?>
22+
--EXPECT--
23+
callback_called
24+
object(Foo)#1 (0) {
25+
}
26+
Done
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
unserialize_callback_func with private static method
3+
--INI--
4+
unserialize_callback_func=Tester::my_unserialize
5+
--FILE--
6+
<?php
7+
8+
class Tester {
9+
private static function my_unserialize($name) {
10+
echo "callback_called\n";
11+
eval('class Foo {}');
12+
}
13+
14+
public static function unserialize(string $str) {
15+
return unserialize($str);
16+
}
17+
}
18+
19+
$s = 'O:3:"FOO":0:{}';
20+
try {
21+
$o = unserialize($s);
22+
var_dump($o);
23+
} catch (Throwable $e) {
24+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
25+
}
26+
27+
try {
28+
$o = Tester::unserialize($s);
29+
var_dump($o);
30+
} catch (Throwable $e) {
31+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
32+
}
33+
echo "Done";
34+
35+
?>
36+
--EXPECT--
37+
Error: Invalid callback Tester::my_unserialize, cannot access private method Tester::my_unserialize()
38+
callback_called
39+
object(Foo)#2 (0) {
40+
}
41+
Done

0 commit comments

Comments
 (0)