mirror of
https://github.com/hanzoai/iam.git
synced 2026-11-19 18:37:16 +00:00
The second factor could not be ENROLLED by anyone and, for text and email, could
not be USED by anyone. Both suites were green the whole time, because each side
tested its own shape and the two shapes never met.
WHY NOBODY COULD ENROL
hanzo.id posts every /v1/iam/mfa/setup/* parameter on the QUERY STRING with an
empty body; three of the five handlers read only the body. So `enable` and
`preferred` answered 400 "invalid body" to the exact request the shipped bundle
sends, and any org that set a Required factor was hard-locked out — the enrol
screen has no skip, and its only way forward is the call that 400s.
decode now reads the body, then lets the query fill what the body did not carry.
That is the SAME precedence zip's typed binder applies to every typed op and the
same one loginHandler applies to the authorize parameters; the surface was the
only place in the repo that insisted on one source. The asymmetry that hid it —
initiate and disable ignoring the decode error while three others returned it —
is gone with it.
WHY SMS AND EMAIL WERE UNREACHABLE
MfaPhoneEnabled and MfaEmailEnabled had four readers and NO writer: the gate
consulted flags nothing could set, so those factors were never offered. And even
enabled they could not be completed — the challenge answered NextMfa and
delivered nothing.
Three pieces, each in its own place:
- internal/otp is the one-time code, extracted out of oidc: mint it, word it,
file it, deliver it, spend it. The canonical receiver moves here with it, so
the one form a destination is keyed in now also covers the code a CHALLENGE
sends, not just the two readers in the send endpoint. store.LooksLikePhone is
the one shape test behind it — sign-in's identifier resolution asked the same
question with its own copy.
- factor.Add/Remove/Prefer are the writers those columns never had, and
factor.Destination (moved from oidc) is where a delivered factor's code goes:
the address on the ACCOUNT, never one from the request.
- the gate SENDS the code for the factor it offers, to that same
factor.Destination, so the send key and the verify key are one value.
ENROLMENT NOW PROVES POSSESSION
enable wrote on the strength of a `secret` field alone, accepted an empty
recovery list, and stored whatever digests the body carried. A client that
skipped the advisory verify — or scanned the QR into the wrong app — switched on
a factor no code could answer, and the account was then locked out with no
self-service exit: the gate holds the sign-in before minting, so the person can
never obtain the bearer that disable requires.
enable now requires a passcode and checks it against the secret it is being asked
to store; for sms and email the delivered code IS that proof. Recovery codes are
minted SERVER-side and returned once, so what the person writes down is what the
digests were made from — and there are eight, because one code is a single
mistake away from an account nobody can recover. The separate verify step is
DELETED rather than kept: two calls where the first is unenforced is exactly how
the unanswerable factor got written.
A CHALLENGE IS ANSWERED ONLY BY WHAT IT OFFERED
finishMfa checked only that the answering factor differed from the one already
proven, so an authenticator-only account was passable with an emailed code: a
deliberate choice of TOTP silently degraded to possession of a mailbox. The
offered set now rides on the challenge row — a property of the challenge, not of
either kind's payload, since both kinds have one — and is decided once, when the
sign-in is held.
The verify switch existed TWICE and had already drifted: the password path
accepted a delivered code and the federated path refused it, so an account
holding only email or SMS could answer one login and not the other. There is now
one proveFactor, called by both.
AND THREE MORE THAT ALL SAID "PROCEED" WHEN THEY SHOULD NOT
- PreferredMfaType accepted any string, and factor.Enabled reads that column —
so a factor nobody enrolled reported "MFA is on" and left the gate nothing to
ask for. Prefer is the one writer and holds the invariant: the preferred type
names a factor the account HOLDS.
- the gate's empty allow-list collapsed three situations into a pass. They are
now distinct: nothing enrolled but claimed -> enrolment; held but unservable
(no notify, no address) -> fail closed; already proven -> proceed. That last
one has to hand the sign-in back rather than answer it, or the request hangs
on an empty 200.
- "remember this device" was a deadline on the user row, so a factor proven in
one browser skipped it in EVERY browser. It is now a bearer token held by the
browser that answered, compared against a digest on the account — the same
shape as a recovery code and as the challenge id, no new scheme.
Finally, changing a factor ends the account's other sessions and closes any open
remember window. The session branch of login reaches loginGrant with no
credential at all, so turning 2FA on used to evict nobody — including whoever the
factor was being added because of — for the full 14-day session life.
103 lines
3.1 KiB
Go
103 lines
3.1 KiB
Go
// Copyright 2026 Hanzo AI, Inc.
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
package factor
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/hanzoai/iam/pkg/schema"
|
|
)
|
|
|
|
// THE invariant: PreferredMfaType names a factor the account actually holds.
|
|
// [Enabled] reads that column, so a preferred type nobody enrolled told the login
|
|
// gate "this account has a second factor" and then left it nothing to ask for — the
|
|
// sign-in reported multi-factor and required the password alone.
|
|
func TestPreferRefusesAFactorTheAccountDoesNotHold(t *testing.T) {
|
|
u := &schema.User{}
|
|
for _, mfaType := range []string{SMS, Email, App, "carrier-pigeon"} {
|
|
if err := Prefer(u, mfaType); err != ErrNotHeld {
|
|
t.Fatalf("Prefer(%q) on an empty account = %v, want ErrNotHeld", mfaType, err)
|
|
}
|
|
if u.PreferredMfaType != "" {
|
|
t.Fatalf("Prefer(%q) stored %q anyway", mfaType, u.PreferredMfaType)
|
|
}
|
|
if Enabled(u) {
|
|
t.Fatalf("the account claims multi-factor sign-in after Prefer(%q)", mfaType)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The per-factor columns finally have a writer. MfaPhoneEnabled and MfaEmailEnabled
|
|
// were read four times and set nowhere, so a texted or emailed factor was unreachable
|
|
// for any account that had not been edited in the database.
|
|
func TestAddAndRemoveAreTheOnlyWritersOfAFactor(t *testing.T) {
|
|
u := &schema.User{}
|
|
|
|
Add(u, SMS, "")
|
|
if !u.MfaPhoneEnabled || !Has(u, SMS) {
|
|
t.Fatal("Add(sms) did not record the factor")
|
|
}
|
|
Add(u, Email, "")
|
|
Add(u, App, "SECRET")
|
|
if u.TotpSecret != "SECRET" || !Has(u, App) {
|
|
t.Fatal("Add(app) did not store the secret")
|
|
}
|
|
if got := Held(u); len(got) != 3 {
|
|
t.Fatalf("Held = %v, want all three", got)
|
|
}
|
|
|
|
Remove(u, SMS)
|
|
if u.MfaPhoneEnabled || Has(u, SMS) {
|
|
t.Fatal("Remove(sms) left the factor in place")
|
|
}
|
|
Remove(u, App)
|
|
if u.TotpSecret != "" {
|
|
t.Fatal("Remove(app) left the secret behind")
|
|
}
|
|
}
|
|
|
|
// Dropping the preferred factor must not leave the preference dangling: "" repoints at
|
|
// what remains and clears the column when nothing does, so the account can never claim
|
|
// a factor it no longer holds.
|
|
func TestPreferRepointsWhenTheChosenFactorGoesAway(t *testing.T) {
|
|
u := &schema.User{}
|
|
Add(u, App, "SECRET")
|
|
Add(u, Email, "")
|
|
if err := Prefer(u, App); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
Remove(u, App)
|
|
if err := Prefer(u, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if u.PreferredMfaType != Email {
|
|
t.Fatalf("preferred = %q, want the factor that remains (email)", u.PreferredMfaType)
|
|
}
|
|
|
|
Remove(u, Email)
|
|
if err := Prefer(u, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if u.PreferredMfaType != "" {
|
|
t.Fatalf("preferred = %q with nothing held", u.PreferredMfaType)
|
|
}
|
|
if Enabled(u) {
|
|
t.Fatal("an account with no factors reports multi-factor sign-in")
|
|
}
|
|
}
|
|
|
|
// Delivered names the factors whose code has to be SENT. The authenticator is not one
|
|
// of them, which is why it keeps working with no notify bound at all.
|
|
func TestDeliveredNamesTheSentFactors(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
mfaType string
|
|
want bool
|
|
}{{SMS, true}, {Email, true}, {App, false}, {"carrier-pigeon", false}} {
|
|
if got := Delivered(tc.mfaType); got != tc.want {
|
|
t.Errorf("Delivered(%q) = %v, want %v", tc.mfaType, got, tc.want)
|
|
}
|
|
}
|
|
}
|