mirror of
https://github.com/hanzoai/iam.git
synced 2026-11-19 18:37:16 +00:00
A code sent where no link can go (an emailed second factor, or a sign-in asked for from a page that is none of the application's) was the bare sentence with no HTML. It is now the sign-in mail without its link: the org's mark and name, the code in its box, the expiry, and the plain wording beside it word for word. An SMS stays the plain message, and the sign-in mail with its link is unchanged (testdata/signin.html).
444 lines
18 KiB
Go
444 lines
18 KiB
Go
// Copyright 2026 Hanzo AI, Inc.
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
package oidc
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/hanzoai/orm"
|
|
"github.com/zap-proto/zip"
|
|
|
|
"github.com/hanzoai/iam/internal/otp"
|
|
"github.com/hanzoai/iam/pkg/schema"
|
|
"github.com/hanzoai/iam/pkg/store"
|
|
)
|
|
|
|
// multipartReq builds a real multipart/form-data POST — the serialized format v1's
|
|
// SendVerificationCode requires (NOT JSON), so the test exercises the multipart
|
|
// parse path, not a urlencoded shortcut.
|
|
func multipartReq(path string, fields map[string]string) *http.Request {
|
|
var body bytes.Buffer
|
|
w := multipart.NewWriter(&body)
|
|
for k, v := range fields {
|
|
_ = w.WriteField(k, v)
|
|
}
|
|
_ = w.Close()
|
|
req := httptest.NewRequest("POST", path, &body)
|
|
req.Header.Set("Content-Type", w.FormDataContentType()) // multipart/form-data; boundary=…
|
|
req.Host = "hanzo.id"
|
|
return req
|
|
}
|
|
|
|
func sendCode(t *testing.T, app *zip.App, fields map[string]string) (int, map[string]any) {
|
|
t.Helper()
|
|
resp, raw := do(t, app, multipartReq(PathVerificationCodes, fields))
|
|
return resp.StatusCode, decode(t, raw)
|
|
}
|
|
|
|
// The happy path parses the multipart form, persists a 6-digit unused code
|
|
// bound to the receiver, and reports ok — and that code then verifies for the
|
|
// account it was sent to while a wrong one fails closed.
|
|
func TestSendVerificationCode_PersistsAndVerifies(t *testing.T) {
|
|
// A send that reports success must actually be able to send: DeliveryConfigured
|
|
// gates the endpoint, so these persist/verify tests bind a sender the way any
|
|
// real deployment does. Unbound, the endpoint refuses rather than answering ok,
|
|
// which is its own test below.
|
|
bindSender(t, &fakeSender{})
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret"})
|
|
seedOrg(t, db, "hanzo")
|
|
seedRichUser(t, db) // [email protected] — exercises the user-resolution branch
|
|
|
|
status, env := sendCode(t, app, map[string]string{
|
|
"dest": "[email protected]",
|
|
"type": "email",
|
|
"applicationId": "admin/conf",
|
|
"captchaType": "none",
|
|
})
|
|
if status != 200 || env["status"] != "ok" {
|
|
t.Fatalf("status=%d env=%v, want 200 ok", status, env)
|
|
}
|
|
|
|
ctx := context.Background()
|
|
rec, err := store.GetLatestVerificationRecord(ctx, db, "hanzo", "[email protected]")
|
|
if err != nil || rec == nil {
|
|
t.Fatalf("verification record not persisted: %v (nil=%v)", err, rec == nil)
|
|
}
|
|
if rec.Type != "email" || rec.IsUsed {
|
|
t.Errorf("record type/used = %q/%v, want email/false", rec.Type, rec.IsUsed)
|
|
}
|
|
if len(rec.Code) != otp.Length {
|
|
t.Errorf("code = %q, want %d digits", rec.Code, otp.Length)
|
|
}
|
|
if rec.User != "hanzo/alice" {
|
|
t.Errorf("record.User = %q, want hanzo/alice (resolved from the dest)", rec.User)
|
|
}
|
|
|
|
// The validation surface: a wrong code, another receiver and another org do
|
|
// not verify, and the persisted code does — spent by the account it was sent to.
|
|
alice, _ := store.GetUserByName(ctx, db, "hanzo", "alice")
|
|
if ok, _ := otp.Consume(ctx, db, alice, "[email protected]", "000000", time.Now()); ok {
|
|
t.Error("a wrong code must not verify")
|
|
}
|
|
if ok, _ := otp.Consume(ctx, db, alice, "[email protected]", rec.Code, time.Now()); ok {
|
|
t.Error("a code must not verify for a different receiver")
|
|
}
|
|
if ok, _ := otp.Prove(ctx, db, "elsewhere", "[email protected]", rec.Code, time.Now()); ok {
|
|
t.Error("a code must not verify for another organization holding the same address")
|
|
}
|
|
if ok, err := otp.Consume(ctx, db, alice, "[email protected]", rec.Code, time.Now()); err != nil || !ok {
|
|
t.Fatalf("correct code must verify: ok=%v err=%v", ok, err)
|
|
}
|
|
}
|
|
|
|
// A urlencoded body reaches the same handler (fiber's FormValue reads both) —
|
|
// the code path is not multipart-only.
|
|
func TestSendVerificationCode_UrlencodedAlsoWorks(t *testing.T) {
|
|
// A send that reports success must actually be able to send: DeliveryConfigured
|
|
// gates the endpoint, so these persist/verify tests bind a sender the way any
|
|
// real deployment does. Unbound, the endpoint refuses rather than answering ok,
|
|
// which is its own test below.
|
|
bindSender(t, &fakeSender{})
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret"})
|
|
seedOrg(t, db, "hanzo")
|
|
|
|
resp, raw := do(t, app, formReq("POST", PathVerificationCodes, url.Values{
|
|
"dest": {"[email protected]"},
|
|
"type": {"email"},
|
|
"applicationId": {"admin/conf"},
|
|
}))
|
|
if env := decode(t, raw); resp.StatusCode != 200 || env["status"] != "ok" {
|
|
t.Fatalf("status=%d env=%v, want 200 ok", resp.StatusCode, env)
|
|
}
|
|
if rec, _ := store.GetLatestVerificationRecord(context.Background(), db, "hanzo", "[email protected]"); rec == nil {
|
|
t.Error("urlencoded send did not persist a record")
|
|
}
|
|
}
|
|
|
|
// Every malformed request returns {status:"error"} on a 200 and persists nothing.
|
|
func TestSendVerificationCode_Errors(t *testing.T) {
|
|
base := func() map[string]string {
|
|
return map[string]string{"dest": "[email protected]", "type": "email", "applicationId": "admin/conf"}
|
|
}
|
|
cases := map[string]func(m map[string]string){
|
|
"missing type": func(m map[string]string) { delete(m, "type") },
|
|
"missing dest": func(m map[string]string) { delete(m, "dest") },
|
|
"applicationId without '/'": func(m map[string]string) { m["applicationId"] = "conf" },
|
|
"application not found": func(m map[string]string) { m["applicationId"] = "admin/ghost" },
|
|
"invalid email": func(m map[string]string) { m["dest"] = "not-an-email" },
|
|
"unsupported type": func(m map[string]string) { m["type"] = "carrier-pigeon" },
|
|
}
|
|
for name, mutate := range cases {
|
|
t.Run(name, func(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret"})
|
|
seedOrg(t, db, "hanzo")
|
|
m := base()
|
|
mutate(m)
|
|
status, env := sendCode(t, app, m)
|
|
if status != 400 || env["status"] != "error" {
|
|
t.Fatalf("status=%d env=%v, want 400 error", status, env)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// With nothing able to deliver, the endpoint says so instead of answering ok.
|
|
//
|
|
// It used to mint the code, persist it, and return {status:"ok"} — defensible as
|
|
// "the code exists", and not what the caller hears: the login screen asked to SEND
|
|
// one, so ok means sent, and the person waits for a message nobody sent. An
|
|
// address that cannot receive one must not be answered ok.
|
|
func TestSendVerificationCode_RefusesWhenNothingCanDeliver(t *testing.T) {
|
|
bindSender(t, nil)
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret"})
|
|
seedOrg(t, db, "hanzo")
|
|
seedRichUser(t, db)
|
|
|
|
status, env := sendCode(t, app, map[string]string{
|
|
"dest": "[email protected]", "type": "email",
|
|
"applicationId": "admin/conf", "checkType": "none", "method": "signup",
|
|
})
|
|
if status != 400 {
|
|
t.Fatalf("transport status = %d, want 400 — a send that cannot happen is a refusal, "+
|
|
"and the transport must say so before the envelope repeats it", status)
|
|
}
|
|
if env["status"] != "error" {
|
|
t.Errorf("status = %v, want error — reporting success for a send that cannot happen "+
|
|
"is what left people waiting on a message nobody sent", env["status"])
|
|
}
|
|
if msg, _ := env["msg"].(string); !strings.Contains(msg, "deliver") {
|
|
t.Errorf("msg = %q, want it to name delivery so the cause is actionable", msg)
|
|
}
|
|
}
|
|
|
|
// A phone code has to survive the person spelling their own number two ways.
|
|
//
|
|
// The record was written under `dest` exactly as typed and matched back with an
|
|
// exact Receiver compare, while the ACCOUNT on the same request resolves through
|
|
// GetUserByPhone, which normalizes first. So "+1 415 555 0134" at send and
|
|
// "+14155550134" at login found the right user and then answered "the code is
|
|
// incorrect or has expired" — a refusal with no honest explanation available to
|
|
// the screen, and one nobody would meet until the first customer of the phone arm.
|
|
func TestPhoneCodeMatchesHoweverTheNumberWasTyped(t *testing.T) {
|
|
for _, tc := range []struct{ sent, typed string }{
|
|
{"+1 (415) 555-0134", "+14155550134"},
|
|
{"+14155550134", "+1 415 555 0134"},
|
|
{"415-555-0134", "4155550134"},
|
|
} {
|
|
t.Run(tc.sent+" then "+tc.typed, func(t *testing.T) {
|
|
sender := &fakeSender{}
|
|
bindSender(t, sender)
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret"})
|
|
seedOrg(t, db, "hanzo")
|
|
// A delivered code is bound to the account it was minted for, so the phone
|
|
// arm needs one to be a credential at all.
|
|
seedPhoneUser(t, db, "carol", tc.sent)
|
|
|
|
status, env := sendCode(t, app, map[string]string{
|
|
"dest": tc.sent, "type": "phone", "applicationId": "admin/conf",
|
|
})
|
|
if status != 200 || env["status"] != "ok" {
|
|
t.Fatalf("send status=%d env=%v, want 200 ok", status, env)
|
|
}
|
|
if len(sender.sent) != 1 {
|
|
t.Fatalf("sender saw %d deliveries, want 1", len(sender.sent))
|
|
}
|
|
// The message went to ONE canonical spelling, which is also the one the
|
|
// record is keyed on.
|
|
m := sender.sent[0]
|
|
if m.To != store.NormalizePhone(tc.sent) {
|
|
t.Errorf("delivered to %q, want the canonical %q", m.To, store.NormalizePhone(tc.sent))
|
|
}
|
|
code := codeIn(t, m.Body)
|
|
|
|
carol, _ := store.GetUserByName(context.Background(), db, "hanzo", "carol")
|
|
ok, err := otp.Consume(context.Background(), db, carol, tc.typed, code, time.Now())
|
|
if err != nil {
|
|
t.Fatalf("consume: %v", err)
|
|
}
|
|
if !ok {
|
|
t.Errorf("a code sent to %q did not verify when typed as %q", tc.sent, tc.typed)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// codeIn reads the six digits out of a delivered message body, so a test can spend
|
|
// the code the person would have typed rather than reaching into the table.
|
|
func codeIn(t *testing.T, body string) string {
|
|
t.Helper()
|
|
digits := regexp.MustCompile(`\d{` + fmt.Sprint(otp.Length) + `}`).FindString(body)
|
|
if digits == "" {
|
|
t.Fatalf("no %d-digit code in the delivered body %q", otp.Length, body)
|
|
}
|
|
return digits
|
|
}
|
|
|
|
// seedPhoneUser creates a user in org "hanzo" carrying a phone number, stored in the
|
|
// canonical form every write site normalizes to.
|
|
func seedPhoneUser(t *testing.T, db orm.DB, name, phone string) {
|
|
t.Helper()
|
|
u := orm.New[schema.User](db)
|
|
u.Owner = "hanzo"
|
|
u.Name = name
|
|
u.Phone = store.NormalizePhone(phone)
|
|
u.SetId("hanzo/" + name)
|
|
if err := u.CreateCtx(context.Background()); err != nil {
|
|
t.Fatalf("seed phone user: %v", err)
|
|
}
|
|
}
|
|
|
|
// An address that identifies two accounts must be answered EXACTLY as an address
|
|
// that identifies none — no oracle on an unauthenticated endpoint.
|
|
//
|
|
// The resolver now refuses to pick between duplicate rows (store.ErrEmailAmbiguous)
|
|
// and that refusal is right. Echoing it here was not: this endpoint takes an
|
|
// address from anyone, so "matches more than one account" tells a stranger the
|
|
// address is real and worth attacking. The endpoint already declines to say
|
|
// whether a user was found; an ambiguous one must join that silence.
|
|
func TestAmbiguousAddressLeaksNothing(t *testing.T) {
|
|
bindSender(t, &fakeSender{})
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret"})
|
|
seedOrg(t, db, "hanzo")
|
|
// Two rows, one address — the state the resolver refuses to choose within.
|
|
seedUserInOrg(t, db, "hanzo", "ada", "[email protected]", "pw-ada")
|
|
seedUserInOrg(t, db, "hanzo", "grace", "[email protected]", "pw-grace")
|
|
|
|
dupStatus, dupBody := sendCode(t, app, map[string]string{
|
|
"dest": "[email protected]", "type": "email",
|
|
"applicationId": "admin/conf", "captchaType": "none",
|
|
})
|
|
noneStatus, noneBody := sendCode(t, app, map[string]string{
|
|
"dest": "[email protected]", "type": "email",
|
|
"applicationId": "admin/conf", "captchaType": "none",
|
|
})
|
|
|
|
// NOT VACUOUS: the absent-address case must reach the SEND, not die at an
|
|
// earlier guard. Two requests that both fail on a missing field would compare
|
|
// equal and prove nothing — which is exactly what the first draft of this test
|
|
// did.
|
|
if noneStatus != 200 || noneBody["status"] != "ok" {
|
|
t.Fatalf("control request did not reach the send (status=%d body=%v) — the comparison below would be vacuous", noneStatus, noneBody)
|
|
}
|
|
// Status AND message must match: either alone would leave the other as the
|
|
// oracle.
|
|
if dupStatus != noneStatus {
|
|
t.Errorf("status differs: ambiguous=%d absent=%d — the difference IS the oracle", dupStatus, noneStatus)
|
|
}
|
|
dupMsg, _ := dupBody["msg"].(string)
|
|
noneMsg, _ := noneBody["msg"].(string)
|
|
if dupMsg != noneMsg {
|
|
t.Errorf("message differs:\n ambiguous: %q\n absent: %q\nan ambiguous address must be indistinguishable from an absent one", dupMsg, noneMsg)
|
|
}
|
|
for _, leak := range []string{"more than one", "ambiguous", "matches"} {
|
|
if strings.Contains(strings.ToLower(dupMsg), leak) {
|
|
t.Errorf("answer names the ambiguity (%q): %q", leak, dupMsg)
|
|
}
|
|
}
|
|
}
|
|
|
|
// landing answers only a registered redirect URI, and only at the exact origin the
|
|
// request came from.
|
|
func TestLanding(t *testing.T) {
|
|
app := &schema.Application{RedirectUris: []string{
|
|
"hanzo://oauth/hanzo",
|
|
"http://localhost:3000/callback",
|
|
"https://hanzo.ai/auth/callback",
|
|
"https://hanzo.ai/second",
|
|
"https://app.hanzo.ai:8443/cb#stale",
|
|
}}
|
|
for _, tc := range []struct{ name, origin, want string }{
|
|
{"match", "https://hanzo.ai", "https://hanzo.ai/auth/callback"},
|
|
{"no origin", "", ""},
|
|
{"null origin", "null", ""},
|
|
{"unregistered origin", "https://evil.example", ""},
|
|
{"sibling host", "https://www.hanzo.ai", ""},
|
|
{"scheme mismatch", "http://hanzo.ai", ""},
|
|
{"port mismatch", "https://hanzo.ai:8443", ""},
|
|
{"explicit default port", "https://hanzo.ai:443", ""},
|
|
{"port match, fragment cut", "https://app.hanzo.ai:8443", "https://app.hanzo.ai:8443/cb"},
|
|
{"http registered", "http://localhost:3000", "http://localhost:3000/callback"},
|
|
{"custom scheme never matches", "hanzo://oauth", ""},
|
|
{"origin with a path", "https://hanzo.ai/", ""},
|
|
{"origin with userinfo", "https://[email protected]", ""},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if got := landing(app, tc.origin); got != tc.want {
|
|
t.Errorf("landing(%q) = %q, want %q", tc.origin, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// seedBrand files the hanzo org as the seed declares it (init_data.json): its name
|
|
// as a person knows it and its site, which the sign-in mail is from.
|
|
func seedBrand(t *testing.T, db orm.DB) {
|
|
t.Helper()
|
|
o := orm.New[schema.Organization](db)
|
|
o.Owner, o.Name, o.DisplayName, o.WebsiteUrl = "admin", "hanzo", "Hanzo", "https://hanzo.ai"
|
|
o.SetId("admin/hanzo")
|
|
if err := o.CreateCtx(context.Background()); err != nil {
|
|
t.Fatalf("seed org: %v", err)
|
|
}
|
|
}
|
|
|
|
// An email sent from a page at a registered origin carries the link; the same email
|
|
// with no Origin or an unregistered one is the code mail (the plain wording beside
|
|
// the designed code alone, with no link), and an SMS is the plain message exactly.
|
|
func TestSendVerificationCode_Link(t *testing.T) {
|
|
plain := func(code string) string {
|
|
return "Your verification code is " + code + ". It expires in 10 minutes. " +
|
|
"If you did not request it, ignore this message and do not share it with anyone."
|
|
}
|
|
for _, tc := range []struct {
|
|
name, origin, typ, dest string
|
|
link bool
|
|
}{
|
|
{"registered origin", "https://hanzo.ai", "email", "[email protected]", true},
|
|
{"no origin", "", "email", "[email protected]", false},
|
|
{"unregistered origin", "https://evil.example", "email", "[email protected]", false},
|
|
{"phone", "https://hanzo.ai", "phone", "+14155550134", false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
sender := &fakeSender{}
|
|
bindSender(t, sender)
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{
|
|
"hanzo://oauth/hanzo", "https://hanzo.ai/auth/callback#old",
|
|
}})
|
|
seedBrand(t, db)
|
|
seedRichUser(t, db)
|
|
seedPhoneUser(t, db, "carol", "+14155550134")
|
|
|
|
req := multipartReq(PathVerificationCodes, map[string]string{
|
|
"dest": tc.dest, "type": tc.typ, "applicationId": "admin/conf",
|
|
})
|
|
if tc.origin != "" {
|
|
req.Header.Set("Origin", tc.origin)
|
|
}
|
|
resp, raw := do(t, app, req)
|
|
if env := decode(t, raw); resp.StatusCode != 200 || env["status"] != "ok" {
|
|
t.Fatalf("status=%d env=%v, want 200 ok", resp.StatusCode, env)
|
|
}
|
|
if len(sender.sent) != 1 {
|
|
t.Fatalf("sender saw %d deliveries, want 1", len(sender.sent))
|
|
}
|
|
m := sender.sent[0]
|
|
rec, err := store.GetLatestVerificationRecord(context.Background(), db, "hanzo", otp.Receiver(tc.dest))
|
|
if err != nil || rec == nil {
|
|
t.Fatalf("no record filed: %v", err)
|
|
}
|
|
code := rec.Code
|
|
|
|
if !tc.link {
|
|
if m.Body != plain(code) {
|
|
t.Errorf("body = %q, want the plain wording", m.Body)
|
|
}
|
|
if tc.typ != "email" {
|
|
if m.HTML != "" || m.Subject != "" {
|
|
t.Errorf("sms subject = %q html = %q, want neither", m.Subject, m.HTML)
|
|
}
|
|
return
|
|
}
|
|
if m.Subject != "Your Hanzo verification code" {
|
|
t.Errorf("subject = %q", m.Subject)
|
|
}
|
|
if !strings.Contains(m.HTML, ">"+code+"</div>") || strings.Contains(m.HTML, "#code=") || strings.Contains(m.HTML, ">Sign in</a>") {
|
|
t.Errorf("html is not the code mail without a link:\n%s", m.HTML)
|
|
}
|
|
return
|
|
}
|
|
// The designed sign-in mail, from the application's org by its own name.
|
|
link := "https://hanzo.ai/auth/callback#code=" + code + "&email=alice%40hanzo.ai"
|
|
want := "Sign in to Hanzo\n\n" +
|
|
"Open this link to sign in:\n" + link + "\n\n" +
|
|
"Or enter this code: " + code + "\n\n" +
|
|
"This link and code expire in 10 minutes. Don't share them with anyone.\n" +
|
|
"If you didn't request this, you can ignore this email."
|
|
if m.Subject != "Your Hanzo sign-in link" || m.Body != want {
|
|
t.Errorf("sent subject %q body\n%q\nwant\n%q", m.Subject, m.Body, want)
|
|
}
|
|
if !strings.Contains(m.HTML, `href="`+strings.ReplaceAll(link, "&", "&")+`"`) || !strings.Contains(m.HTML, ">"+code+"</div>") ||
|
|
!strings.Contains(m.HTML, `src="https://hanzo.ai/email/mark.png"`) {
|
|
t.Errorf("html does not carry the link and the code:\n%s", m.HTML)
|
|
}
|
|
})
|
|
}
|
|
}
|