From 40fe496eda529fd1d933b56a1022ec32d3cd0b12 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Wed, 24 Jul 2024 23:54:09 +0200 Subject: [PATCH] Fix regression introduced in `GHSA-cjcc-p67m-7qxm` while attaching behavior defined by `__class` array key --- framework/CHANGELOG.md | 3 +-- framework/base/Component.php | 2 +- tests/framework/base/ComponentTest.php | 3 +++ 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 96f41456dcc..0185dbb7835 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,8 +4,7 @@ Yii Framework 2 Change Log 2.0.52 under development ------------------------ -- no changes in this release. - +- Bug #20232: Fix regression introduced in `GHSA-cjcc-p67m-7qxm` while attaching behavior defined by `__class` array key (erickskrauch) 2.0.51 July 18, 2024 -------------------- diff --git a/framework/base/Component.php b/framework/base/Component.php index 2ad62f3259a..7cee3b0720d 100644 --- a/framework/base/Component.php +++ b/framework/base/Component.php @@ -190,7 +190,7 @@ public function __set($name, $value) $name = trim(substr($name, 3)); if ($value instanceof Behavior) { $this->attachBehavior($name, $value); - } elseif (isset($value['class']) && is_subclass_of($value['class'], Behavior::class, true)) { + } elseif ((isset($value['class']) && is_subclass_of($value['class'], Behavior::class)) || (isset($value['__class']) && is_subclass_of($value['__class'], Behavior::class))) { $this->attachBehavior($name, Yii::createObject($value)); } elseif (is_string($value) && is_subclass_of($value, Behavior::class, true)) { $this->attachBehavior($name, Yii::createObject($value)); diff --git a/tests/framework/base/ComponentTest.php b/tests/framework/base/ComponentTest.php index f01d21476f3..dca3c37bd91 100644 --- a/tests/framework/base/ComponentTest.php +++ b/tests/framework/base/ComponentTest.php @@ -341,6 +341,9 @@ public function testAttachBehavior() $this->assertTrue($component->hasProperty('p')); $component->test(); $this->assertTrue($component->behaviorCalled); + + $component->{'as c'} = ['__class' => NewBehavior::class]; + $this->assertNotNull($component->getBehavior('c')); } public function testAttachBehaviors()