Skip to content
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

Add bcrypt hashing for password #8

Merged
merged 2 commits into from
Dec 22, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public static function make(): Forms\Components\Field
->confirmed()
->visible(fn (Forms\Get $get) => $get('is_login'))
->password()
->dehydrateStateUsing(fn ($state) => bcrypt($state))
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Prevent potential double-hashing of passwords

The current implementation has several security concerns:

  1. It will hash an already hashed password if the form is resubmitted
  2. It doesn't handle null/empty password states
  3. It might double-hash existing passwords during updates

Apply this diff to fix these issues:

-            ->dehydrateStateUsing(fn ($state) => bcrypt($state))
+            ->dehydrateStateUsing(function ($state) {
+                if (empty($state)) {
+                    return $state;
+                }
+                // Only hash if the password is modified/new
+                if (strlen($state) < 60 || !password_get_info($state)['algo']) {
+                    return bcrypt($state);
+                }
+                return $state;
+            })
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
->dehydrateStateUsing(fn ($state) => bcrypt($state))
->dehydrateStateUsing(function ($state) {
if (empty($state)) {
return $state;
}
// Only hash if the password is modified/new
if (strlen($state) < 60 || !password_get_info($state)['algo']) {
return bcrypt($state);
}
return $state;
})

->maxLength(255);
}
}