-
-
Notifications
You must be signed in to change notification settings - Fork 849
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,30 @@ | ||
| package password | ||
|
|
||
| import "golang.org/x/crypto/bcrypt" | ||
| import ( | ||
| "errors" | ||
|
|
||
| "golang.org/x/crypto/bcrypt" | ||
| ) | ||
|
|
||
| var ErrUnexpectedError = errors.New("unexpected error") | ||
|
|
||
| func ValidateNewPassword(pw string) error { | ||
| if pw == "" { | ||
|
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. This is already checked by the binding on the property (
Member
Author
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.
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. Ahh true, it bytes and not len on string. Then nevermind about the validation on the binding. Can we do this as extra check for the create password method in the and remove the masking described in https://github.com/gotify/server/pull/1014/changes#r3666911979. |
||
| return errors.New("password must not be empty") | ||
| } | ||
| if len([]byte(pw)) > 72 { | ||
| return bcrypt.ErrPasswordTooLong | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // 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) | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.