-
Notifications
You must be signed in to change notification settings - Fork 24.2k
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
[10.x] Update fixture hash to match testing cost #6259
Conversation
I think this should be updated with 'password' => Hash::make('password'), Because changes from laravel/framework#48791 causes
during database seeding |
No, the entire idea is that we don't need to run the hasher for each seed operation which would slow down tests, etc. If this PR fails seeding because of laravel/framework#48791 then we might need to re-consider that @timacdonald? |
What about the database seeding, where the cost should be 12 instead of 4? |
I've just pushed a change so that the lower cost is only used while running unit tests. |
database/factories/UserFactory.php
Outdated
'password' => App::runningUnitTests() | ||
? '$2y$04$Pnnh9Ay6phg2joDy5EDzE.icSkpTHvQFew2kSGPK06PyUDwpBuORG' // "password" | ||
: '$2y$12$Z/vhVO3e.UXKaG11EWgxc.EL7uej3Pi1M0Pq0orF5cbFGtyVh0V3C', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May I suggest if this gets accepted there should be comment as to why this; I bet ppl will be confused by this. I know I am when I look just the code, even knowing the intention of the PR :-}
Wait, now I'm a bit confused why this was a problem @bastien-phi? In what situation would the lower cost be an issue for you? |
The problem is not about a lower or higher cost, the problem comes from a de-synchronization between the provided hash and the configured bcrypt If the hash and the configuration do not match, the Current implementation is correct but I agree with @mfn, it could be tricky to understand why the implementation is like that (especially in user-land code). I stand by it could be cleaner to use
|
@bastien-phi no I mean why can't we just switch to |
Oh sorry I wasn't clear in my first message. If we define the hash directly with '$2y$04$Pnnh9Ay6phg2joDy5EDzE.icSkpTHvQFew2kSGPK06PyUDwpBuORG', it would cause a RuntimeException during database seeding because of the changes in laravel/framework#48791 as the rounds in the hash (4) will mismatch the rounds define in the application (12 by default). |
@bastien-phi right, thanks. @timacdonald would it be an idea to hardcode the rounds to 4 when running tests here: https://github.com/laravel/laravel/blob/10.x/tests/CreatesApplication.php#L18 We used to do that in the past iirc. Then we can just define |
@driesvints don't forget that the developers can change hash algorithm and use argon instead of bcrypt... |
@bastien-phi in that case it’s best that they update the hash itself. |
It's defined in the
(https://github.com/laravel/laravel/blob/10.x/phpunit.xml#L22) If the developer changes this value and things break, it should be obvious enough that what broke it. Same as with switching to Argon. |
@valorin ah good call. In that case there shouldn’t be anything wrong with just using the rounds 4 version. |
What about class UserFactory extends Factory
{
public static string $password;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => static::$password ??= Hash::make('password'), ? It brings simplicity and also makes sure that generated hash is consistent with current configuration (algorithm and parameters) |
@bastien-phi updated to your suggestion |
* Update fixture hash to match testing cost * Conditionally use lower cost in tests * use hash facade and memoize * remove import --------- Co-authored-by: Taylor Otwell <[email protected]>
Even though this has already been merged, I wanted to leave a note that using It would treat the hard coded hash as plain text input and get rehash, then compare the rehashed value to the hardcoded hash and fail. Only using Not sure if this points at some underlying inconsistency between mysql and pgsql drivers? |
Sounds like you've got hashed casting enabled on the model - the database driver shouldn't be doing any hashing. |
Only if it's enabled in the default Laravel project. I only did the bare minimum to set up pgsql instead of mysql and everything else is standard. |
The hashed cast is enabled by default: Line 43 in 2254e8e
|
Shouldn't this also affect mysql driver then? The problem is that the behavior is different. |
Matches the lower cost for unit testing.