Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Form\Extension\Core\Type\ColorType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TransformationFailureExtension;
use Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension;
Expand Down Expand Up @@ -113,6 +114,13 @@
->args([service('translator')->ignoreOnInvalid()])
->tag('form.type')

->set('form.type.password', PasswordType::class)
->args([
service('security.password_hasher')->ignoreOnInvalid(),
service('form.property_accessor'),
])
->tag('form.type')

->set('form.type_extension.form.transformation_failure_handling', TransformationFailureExtension::class)
->args([service('translator')->ignoreOnInvalid()])
->tag('form.type_extension', ['extended-type' => FormType::class])
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Deprecate calling `FormErrorIterator::children()` if the current element is not iterable.
* Allow to pass `TranslatableMessage` objects to the `help` option
* Add `hash_mapping` option to `PasswordType`

5.3
---
Expand Down
7 changes: 5 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory;
use Symfony\Component\Form\ChoiceList\Factory\PropertyAccessDecorator;
use Symfony\Component\Form\Extension\Core\Type\TransformationFailureExtension;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
Expand All @@ -31,12 +32,14 @@ class CoreExtension extends AbstractExtension
private $propertyAccessor;
private $choiceListFactory;
private $translator;
private $passwordHasher;

public function __construct(PropertyAccessorInterface $propertyAccessor = null, ChoiceListFactoryInterface $choiceListFactory = null, TranslatorInterface $translator = null)
public function __construct(PropertyAccessorInterface $propertyAccessor = null, ChoiceListFactoryInterface $choiceListFactory = null, TranslatorInterface $translator = null, UserPasswordHasherInterface $passwordHasher = null)
{
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
$this->choiceListFactory = $choiceListFactory ?? new CachingFactoryDecorator(new PropertyAccessDecorator(new DefaultChoiceListFactory(), $this->propertyAccessor));
$this->translator = $translator;
$this->passwordHasher = $passwordHasher;
}

protected function loadTypes()
Expand All @@ -58,7 +61,7 @@ protected function loadTypes()
new Type\LocaleType(),
new Type\MoneyType(),
new Type\NumberType(),
new Type\PasswordType(),
new Type\PasswordType($this->passwordHasher, $this->propertyAccessor),
new Type\PercentType(),
new Type\RadioType(),
new Type\RangeType(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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\Extension\Core\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;

/**
* @author Sébastien Alfaiate <s.alfaiate@webarea.fr>
*/
class PasswordHasherListener implements EventSubscriberInterface
{
private $passwordHasher;
private $propertyAccessor;

public function __construct(UserPasswordHasherInterface $passwordHasher = null, PropertyAccessorInterface $propertyAccessor = null)
{
$this->passwordHasher = $passwordHasher;
$this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor();
}

public static function getSubscribedEvents()
{
return [
FormEvents::SUBMIT => ['hashPassword', -256],
Copy link
Member

Choose a reason for hiding this comment

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

if hash_mapping is referring to the same property path as the mapped field then we are hashing the plain password before validating it, which looks wrong.

Copy link
Member

Choose a reason for hiding this comment

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

Also, if something goes wrong during plain password validation you can end up with a wrong hashed password (from #42807 (review) in the DB if your object is a Doctrine entity and you flush changes, but also potentially when serializing the user in the session if the edited user is the currently authenticated user)

Copy link
Contributor Author

@Seb33300 Seb33300 Sep 5, 2021

Choose a reason for hiding this comment

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

The purpose of this PR is to store the password hash in a different property.
I did the change so hash_mapping cannot refer to itself.

And the PasswordType field should be unmapped. Maybe I should force it if hash_mapping is used?

Also, if some validation constraints are put on the entity password property (the hashed one), for example a NotBlank, the hash needs to be populated before the validation to work.

And regarding your second comment, if something goes wrong during the validation, "the issue" you described is not really specific to this feature.
All others fields of the form are also already populate in the entity before validation. The same behavior already happens even if we don't use this new feature.

];
}

public function hashPassword(FormEvent $event)
{
$form = $event->getForm();
$parentForm = $form->getParent();

if ($parentForm && $parentForm->getConfig()->getType()->getInnerType() instanceof RepeatedType) {
$parentForm = $parentForm->getParent();
}

if ($parentForm && ($user = $parentForm->getData()) && ($user instanceof PasswordAuthenticatedUserInterface)) {
$this->propertyAccessor->setValue(
$user,
$form->getConfig()->getOption('hash_mapping'),
$this->passwordHasher->hashPassword($user, $event->getData())
);
}
}
}
29 changes: 29 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/PasswordType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,41 @@
namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Exception\InvalidConfigurationException;
use Symfony\Component\Form\Extension\Core\EventListener\PasswordHasherListener;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;

class PasswordType extends AbstractType
{
private $passwordHasher;
private $propertyAccessor;

public function __construct(UserPasswordHasherInterface $passwordHasher = null, PropertyAccessorInterface $propertyAccessor = null)
{
$this->passwordHasher = $passwordHasher;
$this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor();
}

/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
if ($options['hash_mapping']) {
if ($options['hash_mapping'] == $builder->getName()) {
throw new InvalidConfigurationException('The hash_mapping property must be a different.');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if I had to use InvalidConfigurationException or InvalidOptionsException

}
$builder->addEventSubscriber(new PasswordHasherListener($this->passwordHasher, $this->propertyAccessor));
}
}

/**
* {@inheritdoc}
*/
Expand All @@ -36,6 +64,7 @@ public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'always_empty' => true,
'hash_mapping' => null,
Copy link
Member

Choose a reason for hiding this comment

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

In this new approach hash_mapping must be different from property_path to avoid the issue explained above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes you are right, and this is also the reason we cannot use the previous PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

'trim' => false,
'invalid_message' => function (Options $options, $previousValue) {
return ($options['legacy_error_messages'] ?? true)
Expand Down