Skip to content

Commit b10bb97

Browse files
erickskrauchterabytesoftw
authored andcommitted
Fix regression of regression introduced in GHSA-cjcc-p67m-7qxm and yiisoft#20232
1 parent 06d438b commit b10bb97

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

framework/base/Component.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,9 @@ public function __set($name, $value)
193193
$this->attachBehavior($name, $value);
194194
} elseif ($value instanceof \Closure) {
195195
$this->attachBehavior($name, call_user_func($value));
196-
} elseif ((isset($value['class']) && is_subclass_of($value['class'], Behavior::class)) || (isset($value['__class']) && is_subclass_of($value['__class'], Behavior::class))) {
196+
} elseif (isset($value['__class']) && is_subclass_of($value['__class'], Behavior::class)) {
197+
$this->attachBehavior($name, Yii::createObject($value));
198+
} elseif (!isset($value['__class']) && isset($value['class']) && is_subclass_of($value['class'], Behavior::class)) {
197199
$this->attachBehavior($name, Yii::createObject($value));
198200
} elseif (is_string($value) && is_subclass_of($value, Behavior::class, true)) {
199201
$this->attachBehavior($name, Yii::createObject($value));

tests/framework/base/ComponentTest.php

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -330,26 +330,48 @@ public function testAttachBehavior(): void
330330
$this->assertTrue($component->hasProperty('p'));
331331
$component->test();
332332
$this->assertTrue($component->behaviorCalled);
333-
}
334333

335-
public function testAs()
336-
{
334+
$this->assertSame($behavior, $component->detachBehavior('a'));
335+
$this->assertFalse($component->hasProperty('p'));
336+
try {
337+
$component->test();
338+
$this->fail('Expected exception ' . UnknownMethodException::class . " wasn't thrown");
339+
} catch (UnknownMethodException $e) {
340+
// Expected
341+
}
342+
337343
$component = new NewComponent();
338-
$component->{'as a'} = new NewBehavior();
344+
$component->{'as b'} = ['class' => NewBehavior::class];
345+
$this->assertInstanceOf(NewBehavior::class, $component->getBehavior('b'));
339346
$this->assertTrue($component->hasProperty('p'));
340347
$component->test();
341348
$this->assertTrue($component->behaviorCalled);
342349

343-
$component->{'as b'} = ['class' => NewBehavior::class];
344-
$this->assertNotNull($component->getBehavior('b'));
345-
346350
$component->{'as c'} = ['__class' => NewBehavior::class];
347351
$this->assertNotNull($component->getBehavior('c'));
348352

349-
$component->{'as d'} = function () {
353+
$component->{'as d'} = [
354+
'__class' => NewBehavior2::class,
355+
'class' => NewBehavior::class,
356+
];
357+
$this->assertInstanceOf(NewBehavior2::class, $component->getBehavior('d'));
358+
359+
// CVE-2024-4990
360+
try {
361+
$component->{'as e'} = [
362+
'__class' => 'NotExistsBehavior',
363+
'class' => NewBehavior::class,
364+
];
365+
$this->fail('Expected exception ' . InvalidConfigException::class . " wasn't thrown");
366+
} catch (InvalidConfigException $e) {
367+
$this->assertSame('Class is not of type yii\base\Behavior or its subclasses', $e->getMessage());
368+
}
369+
370+
$component = new NewComponent();
371+
$component->{'as f'} = function () {
350372
return new NewBehavior();
351373
};
352-
$this->assertNotNull($component->getBehavior('d'));
374+
$this->assertNotNull($component->getBehavior('f'));
353375
}
354376

355377
public function testAttachBehaviors(): void

0 commit comments

Comments
 (0)