Skip to content
Open
Show file tree
Hide file tree
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
14 changes: 3 additions & 11 deletions internal/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,19 +550,11 @@ func (u *User) Authenticate(ctx context.Context, tx *storage.Connection, passwor
compareErr := crypto.CompareHashAndPassword(ctx, hash, password)

if !strings.HasPrefix(hash, crypto.Argon2Prefix) && !strings.HasPrefix(hash, crypto.FirebaseScryptPrefix) {
// check if cost exceeds default cost or is too low
cost, err := bcrypt.Cost([]byte(hash))
if err != nil {
// A stored bcrypt hash that cannot be parsed is an error, not a
// plain password mismatch.
if _, err := bcrypt.Cost([]byte(hash)); err != nil {
return compareErr == nil, false, err
}

if cost > bcrypt.DefaultCost || cost == bcrypt.MinCost {
// don't bother with encrypting the password in Authenticate
// since it's handled separately
if err := u.SetPassword(ctx, password, false, "", ""); err != nil {
return compareErr == nil, false, err
}
}
}

return compareErr == nil, encrypt && (es == nil || es.ShouldReEncrypt(encryptionKeyID)), nil
Expand Down
29 changes: 14 additions & 15 deletions internal/models/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/supabase/auth/internal/crypto"
"github.com/supabase/auth/internal/storage"
"github.com/supabase/auth/internal/storage/test"
"golang.org/x/crypto/bcrypt"
)

const modelsTestConfig = "../../hack/test.env"
Expand Down Expand Up @@ -884,19 +883,16 @@ func (ts *UserTestSuite) TestNewUserWithPasswordHashFailure() {
func (ts *UserTestSuite) TestAuthenticate() {
// every case uses "test" as the password
cases := []struct {
desc string
hash string
expectedHashCost int
desc string
hash string
}{
{
desc: "Invalid bcrypt hash cost of 11",
hash: "$2y$11$4lH57PU7bGATpRcx93vIoObH3qDmft/pytbOzDG9/1WsyNmN5u4di",
expectedHashCost: bcrypt.MinCost,
desc: "bcrypt hash cost of 11",
hash: "$2y$11$4lH57PU7bGATpRcx93vIoObH3qDmft/pytbOzDG9/1WsyNmN5u4di",
},
{
desc: "Valid bcrypt hash cost of 10",
hash: "$2y$10$va66S4MxFrH6G6L7BzYl0.QgcYgvSr/F92gc.3botlz7bG4p/g/1i",
expectedHashCost: bcrypt.DefaultCost,
desc: "bcrypt hash cost of 10",
hash: "$2y$10$va66S4MxFrH6G6L7BzYl0.QgcYgvSr/F92gc.3botlz7bG4p/g/1i",
},
}

Expand All @@ -907,14 +903,17 @@ func (ts *UserTestSuite) TestAuthenticate() {
require.NoError(ts.T(), ts.db.Create(u))
require.NotNil(ts.T(), u)

isAuthenticated, _, err := u.Authenticate(context.Background(), ts.db, "test", nil, false, "")
// a wrong password does not authenticate and never replaces the hash
isAuthenticated, _, err := u.Authenticate(context.Background(), ts.db, "wrong", nil, false, "")
require.NoError(ts.T(), err)
require.True(ts.T(), isAuthenticated)
require.False(ts.T(), isAuthenticated)
require.Equal(ts.T(), c.hash, *u.EncryptedPassword)

// check hash cost
hashCost, err := bcrypt.Cost([]byte(*u.EncryptedPassword))
// the correct password authenticates and leaves the hash as it is
isAuthenticated, _, err = u.Authenticate(context.Background(), ts.db, "test", nil, false, "")
require.NoError(ts.T(), err)
require.Equal(ts.T(), c.expectedHashCost, hashCost)
require.True(ts.T(), isAuthenticated)
require.Equal(ts.T(), c.hash, *u.EncryptedPassword)
})
}
}
Expand Down
Loading