From 6abe5bf0089cd5cbc9891115b80b9ab67f095b9e Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Thu, 25 Jul 2024 10:04:56 +0200 Subject: [PATCH] Fix #20232: 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()