Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[SecurityBundle] Throw on using both "id" and "migrate_from" option in hasher config #51670

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

Conversation

ogizanagi
Copy link
Contributor

Q A
Branch? 6.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets N/A...
License MIT
Doc PR N/A

I stumbled upon this DX issue lately when trying to define a custom hasher service while handling password migration with a legacy one using migrate_from.

Such a config:

security:
    # https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
    password_hashers:
        legacy: md5
        App\User\Identity:
            id: App\Security\CustomHasher
            migrate_from:
                - legacy

will not indicate any issue to the developper, while the migrate_from is completely ignored and will not wrap the CustomHasher into a MigratingPasswordHasher with the legacy extra hasher ; which means the password migration will not work as expected.

➜ This PR suggests to throw a configuration exception with a hint regarding this.

Perhaps it would be great to make this possible for Symfony 7.1?

@ogizanagi ogizanagi requested a review from chalasr as a code owner September 15, 2023 16:03
@carsonbot carsonbot added Status: Needs Review DX DX = Developer eXperience (anything that improves the experience of using Symfony) Feature SecurityBundle labels Sep 15, 2023
@carsonbot carsonbot added this to the 6.4 milestone Sep 15, 2023
@carsonbot carsonbot changed the title [SecurityBundle][DX] Throw on using both "id" and "migrate_from" option in hasher config [SecurityBundle] Throw on using both "id" and "migrate_from" option in hasher config Sep 15, 2023
@@ -435,6 +435,10 @@ private function addPasswordHashersSection(ArrayNodeDefinition $rootNode): void
->canBeUnset()
->performNoDeepMerging()
->beforeNormalization()->ifString()->then(fn ($v) => ['algorithm' => $v])->end()
->validate()
->ifTrue(fn ($v) => isset($v['migrate_from'], $v['id']) && 0 !== \count($v['migrate_from']))
->thenInvalid('You cannot use "migrate_from" when using a custom service id.')
Copy link
Member

Choose a reason for hiding this comment

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

As it was silently ignored before, it would be better to trigger a deprecation than throwing an error, to avoid breaking projects in 6.4

Copy link
Contributor Author

@ogizanagi ogizanagi Sep 18, 2023

Choose a reason for hiding this comment

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

Since it would mean such projects are configured in a way the developers think it'll handle the password migration properly, but is not, making them aware of the issue the hard way during upgrade might be better?

The fix for removing this exception is straightforward enough, unless this config is hidden somehow in a dependency or whatever (but I would expect the issue would have been already spotted in such a case).

@chalasr
Copy link
Member

chalasr commented Sep 15, 2023

What issue could arise if we just make it work as a bugfix instead?

@ogizanagi
Copy link
Contributor Author

What issue could arise if we just make it work as a bugfix instead?

@chalasr : Do you mean applying this patch to lower branches as well, or making the configuration work with custom hasher ids?

@chalasr
Copy link
Member

chalasr commented Sep 18, 2023

@ogizanagi I mean making it work as it is the expected behavior to me. Even if we just didn’t think about it, if making it work doesn’t have much potential to break userland code then I think we should do it instead of deprecating to make it work later.

@ogizanagi
Copy link
Contributor Author

ogizanagi commented Sep 18, 2023

@chalasr : Alright, done in #51686

nicolas-grekas added a commit that referenced this pull request Sep 19, 2023
…th custom hasher service with security bundle config (ogizanagi)

This PR was merged into the 5.4 branch.

Discussion
----------

[SecurityBundle][PasswordHasher] Fix password migration with custom hasher service with security bundle config

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #51670 (alternative)
| License       | MIT
| Doc PR        | N/A

Supersedes #51670 as a bug fix, so the security config properly uses a for the `migrate_from` when a `MigratingPasswordHasher` when a custom hasher service id is used:

```yaml
security:
    password_hashers:
        legacy: md5
        App\User\Identity:
            id: App\Security\CustomHasher
            migrate_from:
                - legacy
```

---

> **Note**
> 6.3 patch : 6.3...ogizanagi:63-security-custom-hasher-migrate

Commits
-------

4d65f30 [SecurityBundle][PasswordHasher] Fix password migration with custom hasher service with security bundle config
@ogizanagi ogizanagi deleted the throw-on-both-hasher-id-migrate branch September 19, 2023 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DX DX = Developer eXperience (anything that improves the experience of using Symfony) Feature SecurityBundle Status: Needs Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants