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
4 changes: 2 additions & 2 deletions internal/api/password.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/supabase/auth/internal/api/apierrors"
)

// BCrypt hashed passwords have a 72 character limit
// BCrypt hashed passwords have a 72-byte limit
const MaxPasswordLength = 72

// WeakPasswordError encodes an error that a password does not meet strength
Expand All @@ -31,7 +31,7 @@ func (a *API) checkPasswordStrength(ctx context.Context, password string) error
if len(password) > MaxPasswordLength {
return apierrors.NewBadRequestError(
apierrors.ErrorCodeValidationFailed,
"Password cannot be longer than %v characters",
"Password cannot be longer than %v bytes",
MaxPasswordLength,
)
}
Expand Down
35 changes: 35 additions & 0 deletions internal/api/password_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package api

import (
"context"
"net/http"
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -116,3 +118,36 @@ func TestPasswordStrengthChecks(t *testing.T) {
}
}
}

func TestPasswordStrengthMaximumLengthBytes(t *testing.T) {
api := &API{config: &conf.GlobalConfiguration{
Password: conf.PasswordConfiguration{MinLength: 6},
}}

for _, tc := range []struct {
name string
password string
tooLong bool
}{
{"ASCII at limit", strings.Repeat("a", 72), false},
{"ASCII over limit", strings.Repeat("a", 73), true},
{"Korean at limit", strings.Repeat("가", 24), false},
{"Korean over limit", strings.Repeat("가", 25), true},
{"emoji at limit", strings.Repeat("😀", 18), false},
{"emoji over limit", strings.Repeat("😀", 19), true},
} {
t.Run(tc.name, func(t *testing.T) {
err := api.checkPasswordStrength(context.Background(), tc.password)
if !tc.tooLong {
require.NoError(t, err)
return
}

var httpErr *HTTPError
require.ErrorAs(t, err, &httpErr)
require.Equal(t, http.StatusBadRequest, httpErr.HTTPStatus)
require.Equal(t, apierrors.ErrorCodeValidationFailed, httpErr.ErrorCode)
require.Equal(t, "Password cannot be longer than 72 bytes", httpErr.Message)
})
}
}