Skip to content

Commit 87572e9

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 0380b54 commit 87572e9

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
@@ -5140,6 +5140,11 @@ ZEND_METHOD(ReflectionClass, newInstanceFromData)
51405140

51415141
GET_REFLECTION_OBJECT_PTR(ce);
51425142

5143+
if (ce->type == ZEND_INTERNAL_CLASS) {
5144+
zend_throw_exception_ex(reflection_exception_ptr, 0, "Class %s is an internal class that cannot be instantiated from data", ZSTR_VAL(ce->name));
5145+
RETURN_THROWS();
5146+
}
5147+
51435148
ZEND_PARSE_PARAMETERS_START(1, 2)
51445149
Z_PARAM_ARRAY_HT(data)
51455150
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)