-
-
Notifications
You must be signed in to change notification settings - Fork 847
fix: remove panic() and relay password hashing errors to UI #1014
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,20 @@ | ||
| package password | ||
|
|
||
| import "golang.org/x/crypto/bcrypt" | ||
| import ( | ||
| "errors" | ||
|
|
||
| "golang.org/x/crypto/bcrypt" | ||
| ) | ||
|
|
||
| var ErrUnexpectedError = errors.New("unexpected error") | ||
|
|
||
| // CreatePassword returns a hashed version of the given password. | ||
| func CreatePassword(pw string, strength int) []byte { | ||
| func CreatePassword(pw string, strength int) ([]byte, error) { | ||
| hashedPassword, err := bcrypt.GenerateFromPassword([]byte(pw), strength) | ||
| if err != nil { | ||
| panic(err) | ||
| if err != nil && err != bcrypt.ErrPasswordTooLong { | ||
| err = ErrUnexpectedError | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we mask the error here, we don't get the initial error anywhere, meaning if a user misconfigures the passstrength, it will always return unexpected error without any log about the actual error. I think we can pass the full error without masking into the 500 internal server error, and with the extra validation the length check is done before and results in a 400. |
||
| } | ||
| return hashedPassword | ||
| return hashedPassword, err | ||
| } | ||
|
|
||
| // ComparePassword compares a hashed password with its possible plaintext equivalent. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,32 @@ | ||
| package password | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "golang.org/x/crypto/bcrypt" | ||
| ) | ||
|
|
||
| func TestPasswordSuccess(t *testing.T) { | ||
| password := CreatePassword("secret", 5) | ||
| password, err := CreatePassword("secret", 5) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, true, ComparePassword(password, []byte("secret"))) | ||
| } | ||
|
|
||
| func TestPasswordFailure(t *testing.T) { | ||
| password := CreatePassword("secret", 5) | ||
| password, err := CreatePassword("secret", 5) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, false, ComparePassword(password, []byte("secretx"))) | ||
| } | ||
|
|
||
| func TestBCryptFailure(t *testing.T) { | ||
| assert.Panics(t, func() { CreatePassword("secret", 12312) }) | ||
| func TestBCryptoTooLongErrorIsReturned(t *testing.T) { | ||
| _, err := CreatePassword(strings.Repeat("a", 100), 5) | ||
| assert.ErrorIs(t, err, bcrypt.ErrPasswordTooLong) | ||
| } | ||
|
|
||
| func TestBCryptErrorIsMasked(t *testing.T) { | ||
| _, err := CreatePassword("secret", 12312) | ||
| assert.ErrorIs(t, err, ErrUnexpectedError) | ||
| } |
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.
Have you thought about adding validation to the model, and letting the password handling as is? E.g.
This likely makes the error message more explicit. I'm okay with the current solution, but this may be a better solution.
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.
We will have to hardcode this number. I can see in the future users might request we migrate to other password hashes so I didn't go that route.
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.
Yeah, but the error message will be better, currently it's a 500 internal server error for something that the user provided an invalid value for. How about we do both? Your change and the extra validation?