Files
iam/pkg/store/email_lookup_test.go
zeekay 4d3861ad98 store: an email address is one value, on both sides of the comparison
Signup lowered the address on the way in (signup.go) and GetUserByEmail compared
the raw spelling on the way out, so anyone who capitalized a letter of their own
address created an account they could never sign into — including through the
auto-login that runs one request later, which answered "the username or password
is incorrect" for the password they had just chosen. Indistinguishable from a
typo, so undiscoverable, and permanent.

Phone already had the cure and its own comment states the rule: applying the
normalizer on only one side "would be a lookup that silently never matches".
NormalizeEmail is that rule for the other identifier — defined beside
NormalizePhone, applied inside the READ, and applied at the one WRITE every
create/update path reaches (users.Create/Update, plus bootstrap, which writes
around it and already states the username rule itself for the same reason). The
four inline ToLower+TrimSpace at the write sites become one call, so there is
nowhere left for the two sides to disagree.

GetUserByEmail also stops picking between rows. The document store hangs no
per-field UNIQUE constraint, so two rows in one org can hold one address — three
already do — and .First() with no ORDER BY handed back whichever the index
yielded, for the caller to authenticate somebody against. An address that names
two accounts names none: ErrEmailAmbiguous, exactly as ErrPhoneAmbiguous. Signup
reads that as "email already exists", which is what it means there.

`iam phones` becomes `iam normalize` and converts both identifiers in one pass.
One rule — an identifier is stored in the form it is compared in — so one command;
a second would be a second place to forget it.
2026-08-07 22:19:47 -07:00

90 lines
3.2 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package store_test
import (
"context"
"errors"
"testing"
"github.com/hanzoai/iam/pkg/model"
"github.com/hanzoai/iam/pkg/store"
)
// The address reaches its owner, however the person typed it. Every write site
// stored the lowered form while this lookup compared the raw spelling, so anyone
// who capitalized a letter of their own address created an account and was then
// told their password was wrong — for the password they had just chosen.
func TestGetUserByEmailMatchesAnySpelling(t *testing.T) {
db := userDB(t)
addUser(t, db, &model.User{Owner: "hanzo", Name: "ada", Email: "[email protected]"})
for _, typed := range []string{
"[email protected]",
"[email protected]",
"[email protected]",
" [email protected] ",
} {
got, err := store.GetUserByEmail(context.Background(), db, "hanzo", typed)
if err != nil {
t.Fatalf("GetUserByEmail(%q): %v", typed, err)
}
if got == nil || got.Name != "ada" {
t.Errorf("GetUserByEmail(%q) = %v, want ada", typed, got)
}
}
}
// THE security property, and it is the phone rule read on the other identifier.
// The document store hangs no per-field UNIQUE constraint, so two rows in one org
// can carry one address; returning either of them would authenticate a person
// against somebody else's account. The live table already holds three such
// addresses, so this is not hypothetical.
func TestGetUserByEmailRefusesAnAmbiguousAddress(t *testing.T) {
db := userDB(t)
addUser(t, db, &model.User{Owner: "hanzo", Name: "ada", Email: "[email protected]"})
addUser(t, db, &model.User{Owner: "hanzo", Name: "grace", Email: "[email protected]"})
got, err := store.GetUserByEmail(context.Background(), db, "hanzo", "[email protected]")
if !errors.Is(err, store.ErrEmailAmbiguous) {
t.Fatalf("err = %v, want ErrEmailAmbiguous — an address naming two accounts must name none", err)
}
if got != nil {
t.Fatalf("a user was returned for an ambiguous address: %s", got.Name)
}
}
// A blank address must never match the many rows that legitimately store none.
// Without the guard this is an authentication oracle: an empty identifier would
// resolve to an arbitrary account.
func TestGetUserByEmailIgnoresBlankInput(t *testing.T) {
db := userDB(t)
addUser(t, db, &model.User{Owner: "hanzo", Name: "ada"})
addUser(t, db, &model.User{Owner: "hanzo", Name: "grace"})
for _, blank := range []string{"", " "} {
got, err := store.GetUserByEmail(context.Background(), db, "hanzo", blank)
if err != nil {
t.Fatalf("GetUserByEmail(%q): %v", blank, err)
}
if got != nil {
t.Fatalf("blank address %q resolved to %s — an empty identifier must match nobody", blank, got.Name)
}
}
}
// The lookup is org-scoped: one tenant's address never reaches another's account.
func TestGetUserByEmailIsOrgScoped(t *testing.T) {
db := userDB(t)
addUser(t, db, &model.User{Owner: "hanzo", Name: "ada", Email: "[email protected]"})
got, err := store.GetUserByEmail(context.Background(), db, "lux", "[email protected]")
if err != nil {
t.Fatalf("GetUserByEmail: %v", err)
}
if got != nil {
t.Fatalf("cross-tenant resolve: org lux reached %s", got.Name)
}
}