mirror of
https://github.com/hanzoai/iam.git
synced 2026-11-19 18:37:16 +00:00
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]>
84 lines
3.6 KiB
Go
84 lines
3.6 KiB
Go
// Copyright 2026 Hanzo AI, Inc.
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
package oidc
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/hanzoai/orm"
|
|
|
|
"github.com/hanzoai/iam/pkg/schema"
|
|
"github.com/hanzoai/iam/pkg/store"
|
|
)
|
|
|
|
// updateUser is the ONE way the pre-authentication OIDC surface writes back a user
|
|
// row: onboarding's org move, self-service preferences, the confidential-client
|
|
// key mint/revoke, and federated link/unlink all go through it, so every user-row
|
|
// write shares a single correct read-modify-write. mutate edits the FRESH row in place
|
|
// (setting only the fields the caller owns); updateUser persists it and returns it.
|
|
//
|
|
// Two failures it closes — one root cause, a full-row read-modify-write keyed by the
|
|
// wrong key:
|
|
//
|
|
// - RIGHT KEY, BOTH SHAPES. The storage key is resolved via the (owner,name) query
|
|
// path, which stamps the row's REAL orm key: "owner/name" for a migrated legacy
|
|
// row (SetId in the migrator), or a store-assigned surrogate id (a decimal
|
|
// string) for a v2-native users.Create'd row (Create allocates, it never
|
|
// pins the key). A plain orm.Get(owner+"/"+name) resolves ONLY migrated rows, so a
|
|
// post-cutover signup was missed and its key mint/revoke silently 500'd. The
|
|
// lock is taken by that exact resolved key, so both shapes are written.
|
|
//
|
|
// - NO STALE FULL-ROW CLOBBER. The read-modify-write runs inside a GetForUpdate
|
|
// transaction; mutate acts on the row read FRESH under that lock and the whole
|
|
// fresh row is written back, so every field the caller does not touch — notably the
|
|
// lockout counters (SigninWrongTimes / LastSigninWrongTime), advanced by the atomic
|
|
// users.recordAttempt on the same row lock — is carried from the current committed
|
|
// value, never from a snapshot the handler loaded earlier. A concurrent lock
|
|
// increment therefore serializes with this write and cannot be lost (Red PoC4: a
|
|
// stale full-row write dropped the count 5→0, unlocking the account). This mirrors
|
|
// the wallet challenge burn (internal/wallet/store.go) and the lockout counter
|
|
// (internal/users/lockout.go) — the established row-lock pattern in this repo.
|
|
//
|
|
// A missing row is orm.ErrNotFound; mutate's own error aborts the transaction and is
|
|
// returned verbatim. mutate must set only the fields it owns and must not carry in a
|
|
// value captured before the lock — the point is to act on the fresh row. The returned
|
|
// row reflects the committed state (a read-only view; it is bound to the closed
|
|
// transaction, like the wallet burn's returned challenge).
|
|
//
|
|
// mutate also receives the TRANSACTION. A caller whose change is only accountable
|
|
// alongside a second row — a consent answer and the audit row that evidences it —
|
|
// writes both through this handle, so they commit together or not at all. Most
|
|
// callers ignore it; the parameter is there so the one that must not write a
|
|
// half-record has no second mechanism to reach for.
|
|
func updateUser(ctx context.Context, db orm.DB, owner, name string, mutate func(orm.DB, *schema.User) error) (*schema.User, error) {
|
|
keyed, err := store.GetUserByName(ctx, db, owner, name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if keyed == nil {
|
|
return nil, orm.ErrNotFound
|
|
}
|
|
storageID := keyed.Key().Encode()
|
|
|
|
var out *schema.User
|
|
txErr := db.RunInTransaction(ctx, func(tx orm.DB) error {
|
|
fresh, err := orm.GetForUpdate[schema.User](tx, storageID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := mutate(tx, fresh); err != nil {
|
|
return err
|
|
}
|
|
if err := fresh.UpdateCtx(ctx); err != nil {
|
|
return err
|
|
}
|
|
out = fresh
|
|
return nil
|
|
})
|
|
if txErr != nil {
|
|
return nil, txErr
|
|
}
|
|
return out, nil
|
|
}
|