Files
iam/internal/oidc/user_test.go
zeekayandHanzo Dev 8467a88ca0 consent: the answer belongs to the person it is about
The endpoint was self-scoped and fail-closed, but the record it guarded is a
property on the user row, and other writers reach that row.

ONE WRITER. users.Update is a full-row write any org admin may perform on any
member, and its server-owned carry-forward list did not include the consent
record — so one request both FORGED an answer (by sending one) and DESTROYED a
real one (by sending a body with no properties, which is what a partial client
sends), silently and unaudited. It now carries the stored record, and only that
record: every other property still comes from the body, so the console's admin
properties editor keeps working. users.Create dropped nothing, so provisioning
an account could pre-grant training permission in the new member's name; a
create body's consent is now discarded the way a body's credential already was.
The one caller entitled to state an answer at create time is the signup screen,
where the person answers for themselves, and it says so through a seam that is
off the wire. update-preferences shallow-merged any key including this one, an
unvalidated and unaudited second writer of the record that most needs a single
one; it now refuses the key and says where to answer instead.

AN ANSWER YOU DID NOT SEND IS NOT AN ANSWER YOU CHANGED. The wire shape took a
plain bool and a plain string, so a screen saving one switch silently revoked
the other — {"training":"granted"} also said insights=false. Both fields are
pointers now and the record merges field-wise under the row lock, so absent
means untouched. The published description said it merged; now it does.

EVIDENCE. The audit row is what makes a grant demonstrable, and it was
best-effort: written after the fact, dropped on error, and only for a change to
the training answer. It now covers the whole record, commits on the SAME
transaction as the answer, and fails the request if it cannot be written — a
consent we cannot evidence is worth less than one we never claimed. Its action
is reserved, so the generic audit CRUD can no longer mint a grant nobody gave or
delete the row recording a refusal. The ingress address is dropped: behind
hanzoai/ingress it identified our own pod while still being personal data we
would owe a retention answer for.

The write half also refused nothing, so a value the read half normalizes away
could be stored for a later reader to guess at. Encoding an answer now validates
it, in the one place a Consent becomes bytes.

scripts/mutate.py lands the strict runner beside the table it scores: a mutant
counts as killed only if the anchor is unique, the tree builds, the named test
matches and then fails. The table grows 11 -> 20 rows covering the new guards.
20/20 killed. make test green, race-clean.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-04 23:45:43 -07:00

151 lines
5.4 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package oidc
import (
"fmt"
"sync"
"testing"
"time"
"github.com/hanzoai/orm"
"github.com/hanzoai/iam/internal/users"
"github.com/hanzoai/iam/pkg/schema"
"github.com/hanzoai/iam/pkg/store"
)
// ITEM 2: a full-row user writer (onboard / preferences / the sk- key mint-revoke) must
// not erase the lockout counter a concurrent wrong-password attempt advanced. Every such
// writer now goes through updateUser, which reads the row FRESH under a GetForUpdate row
// lock and writes it back, so the counter (advanced atomically by users.recordAttempt on
// the same row lock) is carried forward, never overwritten by a stale snapshot.
// TestUpdateUser_preservesConcurrentLockoutCount is the crisp differential proof of the
// fix, deterministic (no goroutines). A CONTROL branch reproduces the pre-fix hazard: a
// stale full-row write (user.UpdateCtx on a snapshot loaded before the counter advanced —
// exactly what onboard/preferences/saveUser did) resets the persisted counter to the
// snapshot's stale 0. The FIX branch runs the same sequence through updateUser, which
// reads the row fresh under the lock and preserves the advanced counter.
func TestUpdateUser_preservesConcurrentLockoutCount(t *testing.T) {
db := openTestDB(t)
ctx := tctx()
mk := func(name string) {
if _, err := users.New(db).Create(ctx, &users.CreateInput{
User: schema.User{Owner: "hanzo", Name: name},
Password: "pw",
}); err != nil {
t.Fatalf("create %s: %v", name, err)
}
}
advanceTo3 := func(name string) {
for i := 0; i < 3; i++ { // three FRESH-loaded wrong attempts persist a count of 3
fresh, err := store.GetUserByName(ctx, db, "hanzo", name)
if err != nil || fresh == nil {
t.Fatalf("reload %s: %v", name, err)
}
users.Authenticate(ctx, db, fresh, "WRONG", "", time.Now())
}
got, _ := store.GetUserByName(ctx, db, "hanzo", name)
if got.SigninWrongTimes != 3 {
t.Fatalf("setup: %s persisted count = %d, want 3", name, got.SigninWrongTimes)
}
}
// --- CONTROL: the pre-fix stale full-row write erases the counter. ---
mk("naive")
stale, err := store.GetUserByName(ctx, db, "hanzo", "naive") // snapshot at count 0
if err != nil || stale == nil {
t.Fatalf("load stale snapshot: %v", err)
}
advanceTo3("naive")
stale.DisplayName = "changed"
if err := stale.UpdateCtx(ctx); err != nil { // the pre-fix writer: whole stale row written
t.Fatalf("naive write: %v", err)
}
if got, _ := store.GetUserByName(ctx, db, "hanzo", "naive"); got.SigninWrongTimes != 0 {
t.Fatalf("control: a stale full-row write was expected to RESET the counter (the bug), got %d — control invalid", got.SigninWrongTimes)
}
// --- FIX: updateUser reads fresh under the row lock and preserves the counter. ---
mk("fixed")
if _, err := store.GetUserByName(ctx, db, "hanzo", "fixed"); err != nil { // an equally-stale snapshot exists in the wild
t.Fatalf("load: %v", err)
}
advanceTo3("fixed")
if _, err := updateUser(ctx, db, "hanzo", "fixed", func(_ orm.DB, u *schema.User) error {
u.DisplayName = "changed"
return nil
}); err != nil {
t.Fatalf("updateUser: %v", err)
}
got, _ := store.GetUserByName(ctx, db, "hanzo", "fixed")
if got.SigninWrongTimes != 3 {
t.Fatalf("updateUser reset the lockout counter to %d, want 3 preserved (ITEM 2)", got.SigninWrongTimes)
}
if got.DisplayName != "changed" {
t.Fatalf("updateUser did not apply the intended mutation: DisplayName=%q", got.DisplayName)
}
}
// TestUpdateUser_concurrentWithLockIncrement_exactCount is the -race concurrency proof:
// N full-row updateUser writes (changing an unrelated field) racing N atomic wrong-
// password increments must leave the persisted counter at EXACTLY N. A single lost
// update — an updateUser write reverting a counted increment — would drop it below N and
// re-open the brute-force oracle. Because both take the same row lock, they serialize and
// none is lost.
func TestUpdateUser_concurrentWithLockIncrement_exactCount(t *testing.T) {
db := openTestDB(t)
ctx := tctx()
if _, err := users.New(db).Create(ctx, &users.CreateInput{
User: schema.User{Owner: "hanzo", Name: "race"},
Password: "pw",
}); err != nil {
t.Fatalf("create: %v", err)
}
// Stay strictly below the lock threshold so the count is exact (a locked account
// stops counting), isolating "no lost update" from the lock cap.
N := users.LockThreshold - 1
if N < 2 {
t.Fatalf("need at least 2 to exercise the race")
}
var wg sync.WaitGroup
start := make(chan struct{})
for i := 0; i < N; i++ {
wg.Add(1)
go func() {
defer wg.Done()
<-start
fresh, _ := store.GetUserByName(ctx, db, "hanzo", "race")
users.Authenticate(ctx, db, fresh, "WRONG", "", time.Now())
}()
}
for i := 0; i < N; i++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
<-start
// The full-row writer under test; it must not clobber the counter.
_, _ = updateUser(ctx, db, "hanzo", "race", func(_ orm.DB, u *schema.User) error {
u.DisplayName = fmt.Sprintf("d%d", n)
return nil
})
}(i)
}
close(start)
wg.Wait()
got, err := store.GetUserByName(ctx, db, "hanzo", "race")
if err != nil || got == nil {
t.Fatalf("reload: %v", err)
}
if got.SigninWrongTimes != N {
t.Fatalf("persisted SigninWrongTimes = %d, want exactly %d — a full-row updateUser write erased a concurrent lock increment (ITEM 2)", got.SigninWrongTimes, N)
}
}