From 4c0e93936bf1ae22c61a96b7c7e51b58437b0746 Mon Sep 17 00:00:00 2001 From: Stojan Dimitrovski Date: Fri, 20 Nov 2026 16:33:38 +0100 Subject: [PATCH] fix(models): remove in-memory bcrypt rehash from User.Authenticate Authenticate replaced u.EncryptedPassword with a fresh default-cost hash of the submitted password whenever the stored bcrypt cost was above the default or at the minimum. The sign-in handler never saves that value (the only write is the DB-encryption re-encryption path, which regenerates the hash itself), so the rehash had no durable effect. It also ran regardless of whether the password matched, leaving the in-memory user holding a hash of a possibly wrong password. Remove the rehash and keep the parse-error behaviour for malformed stored hashes. Update TestAuthenticate to assert that the stored hash is left untouched for both correct and incorrect passwords. Co-Authored-By: Claude Sonnet 5.5 --- internal/models/user.go | 14 +++----------- internal/models/user_test.go | 29 ++++++++++++++--------------- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/internal/models/user.go b/internal/models/user.go index 661c206670..92c2aeeb7b 100644 --- a/internal/models/user.go +++ b/internal/models/user.go @@ -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 diff --git a/internal/models/user_test.go b/internal/models/user_test.go index 57136c9a17..314ab62bf1 100644 --- a/internal/models/user_test.go +++ b/internal/models/user_test.go @@ -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" @@ -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", }, } @@ -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) }) } }