Skip to content

Commit c736c6d

Browse files
prevent users from instantiating internal classes from data because it sets arbitrary properties before calling the constructor which can have weird consequences
1 parent 917e223 commit c736c6d

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

ext/reflection/php_reflection.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5145,6 +5145,11 @@ ZEND_METHOD(ReflectionClass, newInstanceFromData)
51455145

51465146
GET_REFLECTION_OBJECT_PTR(ce);
51475147

5148+
if (ce->type == ZEND_INTERNAL_CLASS) {
5149+
zend_throw_exception_ex(reflection_exception_ptr, 0, "Class %s is an internal class that cannot be instantiated from data", ZSTR_VAL(ce->name));
5150+
RETURN_THROWS();
5151+
}
5152+
51485153
ZEND_PARSE_PARAMETERS_START(1, 2)
51495154
Z_PARAM_ARRAY_HT(data)
51505155
Z_PARAM_OPTIONAL
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
ReflectionClass::newInstanceFromData - internal class
3+
--FILE--
4+
<?php
5+
6+
$rcDateTime = new ReflectionClass('DateTime');
7+
$rcPDOStatement = new ReflectionClass('PDOStatement');
8+
9+
try
10+
{
11+
$rcDateTime->newInstanceFromData([], ['now', new DateTimeZone('UTC')]);
12+
}
13+
catch(Throwable $e)
14+
{
15+
echo "Exception: " . $e->getMessage() . "\n";
16+
}
17+
18+
try
19+
{
20+
$rcPDOStatement->newInstanceFromData(['a' => 123]);
21+
}
22+
catch(Throwable $e)
23+
{
24+
echo "Exception: " . $e->getMessage() . "\n";
25+
}
26+
27+
?>
28+
--EXPECTF--
29+
Exception: Class DateTime is an internal class that cannot be instantiated from data
30+
Exception: Class PDOStatement is an internal class that cannot be instantiated from data

0 commit comments

Comments
 (0)