A continuation of HomeBox the inventory and organization system built for the Home User
fix: email case sensitivity
Matthew Kilgorefbf8c8505b47
diff --git a/backend/internal/core/services/service_user_email_case_test.go b/backend/internal/core/services/service_user_email_case_test.gonew file mode 100644index 00000000..133bd322--- /dev/null+++ b/backend/internal/core/services/service_user_email_case_test.go@@ -0,0 +1,46 @@+package services++import (+ "context"+ "strings"+ "testing"++ "github.com/stretchr/testify/assert"+ "github.com/stretchr/testify/require"+)++// TestRegister_EmailCaseInsensitiveUniqueness proves that registering the same+// email in different cases is rejected as a duplicate, and that login still works+// for the single account (the case-variant account-takeover/DoS fix).+func TestRegister_EmailCaseInsensitiveUniqueness(t *testing.T) {+ ctx := context.Background()++ base := strings.ToLower(fk.Str(10))+ lower := base + "@example.com"+ upper := strings.ToUpper(base) + "@EXAMPLE.COM"+ const password = "correct-horse-battery-staple"++ _, err := tSvc.User.RegisterUser(ctx, UserRegistration{+ Name: fk.Str(8),+ Email: lower,+ Password: password,+ })+ require.NoError(t, err, "first registration should succeed")++ // Registering a case variant must be rejected as a duplicate.+ _, err = tSvc.User.RegisterUser(ctx, UserRegistration{+ Name: fk.Str(8),+ Email: upper,+ Password: password,+ })+ require.Error(t, err, "registering a case-variant of an existing email must be rejected")++ // Login must still work for both case forms — a single account exists.+ tok, err := tSvc.User.Login(ctx, lower, password, false)+ require.NoError(t, err, "login with the lowercase email should succeed")+ assert.NotEmpty(t, tok.Raw)++ tok, err = tSvc.User.Login(ctx, upper, password, false)+ require.NoError(t, err, "login with an uppercase variant should succeed (case-insensitive)")+ assert.NotEmpty(t, tok.Raw)+}diff --git a/backend/internal/data/repo/repo_users.go b/backend/internal/data/repo/repo_users.goindex 6a65cfa0..b1c3ea42 100644--- a/backend/internal/data/repo/repo_users.go+++ b/backend/internal/data/repo/repo_users.go@@ -2,6 +2,7 @@ package repoimport ("context"+ "strings""github.com/google/uuid""github.com/samber/lo"@@ -17,6 +18,17 @@ type UserRepository struct {db *ent.Client}+// normalizeEmail canonicalizes an email address for storage and lookup. Emails are+// treated case-insensitively throughout the app (login uses a case-folding lookup),+// but the database UNIQUE constraint on users.email is case-sensitive. Storing a+// lowercased, trimmed form makes that constraint reject case-variant duplicates —+// without it an attacker could register [email protected] against an existing+// [email protected], after which the case-insensitive login lookup matches >1 row+// and denies both accounts access.+func normalizeEmail(email string) string {+ return strings.ToLower(strings.TrimSpace(email))+}+type (// UserCreate is the Data object contain the requirements of creating a user// in the database. It should to create users from an API unless the user has@@ -120,7 +132,7 @@ func (r *UserRepository) GetOneEmail(ctx context.Context, email string) (UserOutdefer span.End()out, err := mapUserOutErr(r.db.User.Query().- Where(user.EmailEqualFold(email)).+ Where(user.EmailEqualFold(normalizeEmail(email))).WithGroups().Only(ctx),)@@ -148,7 +160,7 @@ func (r *UserRepository) GetOneEmailNoEdges(ctx context.Context, email string) (defer span.End()out, err := mapUserOutErr(r.db.User.Query().- Where(user.EmailEqualFold(email)).+ Where(user.EmailEqualFold(normalizeEmail(email))).Only(ctx),)if err != nil {@@ -203,7 +215,7 @@ func (r *UserRepository) createUserWithMembership(q := tx.User.Create().SetName(usr.Name).- SetEmail(usr.Email).+ SetEmail(normalizeEmail(usr.Email)).SetIsSuperuser(usr.IsSuperuser).SetDefaultGroupID(usr.DefaultGroupID)@@ -295,7 +307,7 @@ func (r *UserRepository) Update(ctx context.Context, id uuid.UUID, data UserUpdaq := r.db.User.Update().Where(user.ID(id)).SetName(data.Name).- SetEmail(data.Email)+ SetEmail(normalizeEmail(data.Email))_, err := q.Save(ctx)recordSpanError(span, err)diff --git a/backend/internal/data/repo/repo_users_test.go b/backend/internal/data/repo/repo_users_test.goindex 94ffbc22..9179018e 100644--- a/backend/internal/data/repo/repo_users_test.go+++ b/backend/internal/data/repo/repo_users_test.go@@ -31,7 +31,9 @@ func TestUserRepo_GetOneEmail(t *testing.T) {assert.NotNil(foundUser)require.NoError(t, err)- assert.Equal(user.Email, foundUser.Email)+ // Emails are canonicalized (lowercased/trimmed) on storage to keep the+ // case-sensitive DB unique constraint effective; compare against that form.+ assert.Equal(normalizeEmail(user.Email), foundUser.Email)assert.Equal(user.Name, foundUser.Name)// Cleanup@@ -49,7 +51,7 @@ func TestUserRepo_GetOneId(t *testing.T) {assert.NotNil(foundUser)require.NoError(t, err)- assert.Equal(user.Email, foundUser.Email)+ assert.Equal(normalizeEmail(user.Email), foundUser.Email)assert.Equal(user.Name, foundUser.Name)// Cleanup