From db733a6bf4d75e92d833056289a44fe1e0b3afa4 Mon Sep 17 00:00:00 2001 From: Gabriel20xx Date: Tue, 1 Sep 2026 21:51:15 +0200 Subject: [PATCH] Fix critical login/session bug: Store.find() Scan missing Role column Store.find() (backing FindByIdentifier - used by login - FindByID, and FindSessionUser, used on every authenticated request) selected 7 columns but only Scan()'d 6 destinations, missing &user.Role entirely: find user: find auth user: number of field descriptions must equal number of destinations, got 7 and 6 This broke essentially all login and session validation, in every project vendoring this package, against a real Postgres database. It went undetected because the existing test suite uses a hand-written fake store in each consumer's own httpapi package that never exercises the real query/Scan pair - only a real driver round-trip surfaces a destination-count mismatch like this. Adds backend/auth/store_integration_test.go (skipped unless DATABASE_URL is set, so it never runs in default CI) covering create, find-by-identifier, find-by-id, find-by-session, and list-users against a real database, to catch this class of regression going forward. Verified against a real Postgres container: reproduced the exact reported error before the fix, passes after. Co-Authored-By: Claude Sonnet 5 --- backend/auth/store.go | 2 +- backend/auth/store_integration_test.go | 104 +++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 backend/auth/store_integration_test.go diff --git a/backend/auth/store.go b/backend/auth/store.go index fbeb70b..49444f9 100644 --- a/backend/auth/store.go +++ b/backend/auth/store.go @@ -423,7 +423,7 @@ func (s *Store) validate() error { func (s *Store) find(ctx context.Context, query string, arg any) (*User, error) { var user User err := s.pool.QueryRow(ctx, query, arg).Scan( - &user.ID, &user.Username, &user.Email, &user.PasswordHash, &user.CreatedAt, &user.UpdatedAt, + &user.ID, &user.Username, &user.Email, &user.PasswordHash, &user.Role, &user.CreatedAt, &user.UpdatedAt, ) if errors.Is(err, pgx.ErrNoRows) { return nil, nil diff --git a/backend/auth/store_integration_test.go b/backend/auth/store_integration_test.go new file mode 100644 index 0000000..ff312b0 --- /dev/null +++ b/backend/auth/store_integration_test.go @@ -0,0 +1,104 @@ +package auth + +import ( + "context" + "os" + "testing" + "time" + + "repos.gfranz.ch/Gabriel20xx/shared/backend/postgres" +) + +// TestStoreIntegration exercises Store against a real PostgreSQL database, +// covering exactly what the fake stores unit tests (in each consumer's own +// httpapi package) use instead cannot: a mismatch between a query's column +// list and Scan's destination count only surfaces against a real driver +// round-trip. Skipped unless DATABASE_URL points at a scratch database - +// never run by default in CI; point it at a local/throwaway database to run +// it, e.g.: +// +// DATABASE_URL=postgres://user:pass@localhost:5432/scratch?sslmode=disable go test ./auth/... -run TestStoreIntegration +func TestStoreIntegration(t *testing.T) { + url := os.Getenv("DATABASE_URL") + if url == "" { + t.Skip("DATABASE_URL not set; skipping Postgres integration test") + } + + ctx := context.Background() + pool, err := postgres.Open(ctx, postgres.DefaultConfig(url)) + if err != nil { + t.Fatalf("open pool: %v", err) + } + // Registered before the DeleteAllUsers cleanup below so it runs after + // that one (t.Cleanup runs in LIFO order). + t.Cleanup(pool.Close) + + if err := EnsureSchema(ctx, pool, SchemaOptions{SingleUser: false, APITokens: true}); err != nil { + t.Fatalf("EnsureSchema() error = %v", err) + } + + store := NewStore(pool) + if err := store.DeleteAllUsers(ctx); err != nil { + t.Fatalf("DeleteAllUsers() (pre-test cleanup) error = %v", err) + } + t.Cleanup(func() { + if err := store.DeleteAllUsers(context.Background()); err != nil { + t.Logf("cleanup: DeleteAllUsers() error = %v", err) + } + }) + + hash, err := HashPassword("correct horse battery staple") + if err != nil { + t.Fatalf("HashPassword() error = %v", err) + } + + // The very first account is promoted to superadmin; this also exercises + // CreateUser's own Scan (7 RETURNING columns), separately from find's. + created, err := store.CreateUser(ctx, "root", "root@example.com", hash) + if err != nil { + t.Fatalf("CreateUser() error = %v", err) + } + if created.Role != RoleSuperAdmin { + t.Fatalf("first CreateUser() role = %q, want %q", created.Role, RoleSuperAdmin) + } + + byIdentifier, err := store.FindByIdentifier(ctx, "root") + if err != nil { + t.Fatalf("FindByIdentifier() error = %v", err) + } + if byIdentifier == nil || byIdentifier.ID != created.ID || byIdentifier.Role != RoleSuperAdmin { + t.Fatalf("FindByIdentifier() = %+v, want a superadmin match for %+v", byIdentifier, created) + } + + byID, err := store.FindByID(ctx, created.ID) + if err != nil { + t.Fatalf("FindByID() error = %v", err) + } + if byID == nil || byID.Username != "root" || byID.Role != RoleSuperAdmin { + t.Fatalf("FindByID() = %+v, want username %q and superadmin role", byID, "root") + } + + _, hashedToken, err := NewSessionToken() + if err != nil { + t.Fatalf("NewSessionToken() error = %v", err) + } + if err := store.CreateSession(ctx, created.ID, hashedToken, time.Now().Add(time.Hour)); err != nil { + t.Fatalf("CreateSession() error = %v", err) + } + + bySession, err := store.FindSessionUser(ctx, hashedToken) + if err != nil { + t.Fatalf("FindSessionUser() error = %v", err) + } + if bySession == nil || bySession.ID != created.ID { + t.Fatalf("FindSessionUser() = %+v, want a match for %+v", bySession, created) + } + + users, err := store.ListUsers(ctx) + if err != nil { + t.Fatalf("ListUsers() error = %v", err) + } + if len(users) != 1 || users[0].ID != created.ID { + t.Fatalf("ListUsers() = %+v, want exactly the created user", users) + } +}