Skip to content

[FORM] Prevent forms from extending itself as a parent #24387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
32 changes: 23 additions & 9 deletions src/Symfony/Component/Form/FormRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Form;

use Symfony\Component\Form\Exception\ExceptionInterface;
use Symfony\Component\Form\Exception\LogicException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Exception\InvalidArgumentException;

Expand Down Expand Up @@ -44,6 +45,8 @@ class FormRegistry implements FormRegistryInterface
*/
private $resolvedTypeFactory;

private $checkedTypes = array();

/**
* @param FormExtensionInterface[] $extensions An array of FormExtensionInterface
* @param ResolvedFormTypeFactoryInterface $resolvedTypeFactory The factory for resolved form types
Expand Down Expand Up @@ -106,18 +109,29 @@ private function resolveType(FormTypeInterface $type)
$parentType = $type->getParent();
$fqcn = get_class($type);

foreach ($this->extensions as $extension) {
$typeExtensions = array_merge(
if (isset($this->checkedTypes[$fqcn])) {
$types = implode(' > ', array_merge(array_keys($this->checkedTypes), array($fqcn)));
throw new LogicException(sprintf('Circular reference detected for form "%s" (%s).', $fqcn, $types));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is detected for a form type, not for a form.

}

$this->checkedTypes[$fqcn] = true;

try {
foreach ($this->extensions as $extension) {
$typeExtensions = array_merge(
$typeExtensions,
$extension->getTypeExtensions($fqcn)
);
}

return $this->resolvedTypeFactory->createResolvedType(
$type,
$typeExtensions,
$extension->getTypeExtensions($fqcn)
$parentType ? $this->getType($parentType) : null
);
} finally {
unset($this->checkedTypes[$fqcn]);
}

return $this->resolvedTypeFactory->createResolvedType(
$type,
$typeExtensions,
$parentType ? $this->getType($parentType) : null
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Tests\Fixtures;

use Symfony\Component\Form\AbstractType;

class FormWithSameParentType extends AbstractType
{
public function getParent()
{
return self::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Tests\Fixtures;

use Symfony\Component\Form\AbstractType;

class RecursiveFormTypeBar extends AbstractType
{
public function getParent()
{
return RecursiveFormTypeBaz::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Tests\Fixtures;

use Symfony\Component\Form\AbstractType;

class RecursiveFormTypeBaz extends AbstractType
{
public function getParent()
{
return RecursiveFormTypeFoo::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Tests\Fixtures;

use Symfony\Component\Form\AbstractType;

class RecursiveFormTypeFoo extends AbstractType
{
public function getParent()
{
return RecursiveFormTypeBar::class;
}
}
34 changes: 34 additions & 0 deletions src/Symfony/Component/Form/Tests/FormRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
use Symfony\Component\Form\FormTypeGuesserChain;
use Symfony\Component\Form\ResolvedFormType;
use Symfony\Component\Form\ResolvedFormTypeFactoryInterface;
use Symfony\Component\Form\Tests\Fixtures\FormWithSameParentType;
use Symfony\Component\Form\Tests\Fixtures\RecursiveFormTypeBar;
use Symfony\Component\Form\Tests\Fixtures\RecursiveFormTypeBaz;
use Symfony\Component\Form\Tests\Fixtures\RecursiveFormTypeFoo;
use Symfony\Component\Form\Tests\Fixtures\FooSubType;
use Symfony\Component\Form\Tests\Fixtures\FooType;
use Symfony\Component\Form\Tests\Fixtures\FooTypeBarExtension;
Expand Down Expand Up @@ -156,6 +160,36 @@ public function testGetTypeConnectsParent()
$this->assertSame($resolvedType, $this->registry->getType(get_class($type)));
}

/**
* @expectedException \Symfony\Component\Form\Exception\LogicException
* @expectedExceptionMessage Circular reference detected for form "Symfony\Component\Form\Tests\Fixtures\FormWithSameParentType" (Symfony\Component\Form\Tests\Fixtures\FormWithSameParentType > Symfony\Component\Form\Tests\Fixtures\FormWithSameParentType).
*/
public function testFormCannotHaveItselfAsAParent()
{
$type = new FormWithSameParentType();

$this->extension2->addType($type);

$this->registry->getType(FormWithSameParentType::class);
}

/**
* @expectedException \Symfony\Component\Form\Exception\LogicException
* @expectedExceptionMessage Circular reference detected for form "Symfony\Component\Form\Tests\Fixtures\RecursiveFormTypeFoo" (Symfony\Component\Form\Tests\Fixtures\RecursiveFormTypeFoo > Symfony\Component\Form\Tests\Fixtures\RecursiveFormTypeBar > Symfony\Component\Form\Tests\Fixtures\RecursiveFormTypeBaz > Symfony\Component\Form\Tests\Fixtures\RecursiveFormTypeFoo).
*/
public function testRecursiveFormDependencies()
{
$foo = new RecursiveFormTypeFoo();
$bar = new RecursiveFormTypeBar();
$baz = new RecursiveFormTypeBaz();

$this->extension2->addType($foo);
$this->extension2->addType($bar);
$this->extension2->addType($baz);

$this->registry->getType(RecursiveFormTypeFoo::class);
}

/**
* @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException
*/
Expand Down