mirror of
https://github.com/hanzoai/iam.git
synced 2026-11-19 18:37:16 +00:00
The session cookie is SameSite=Lax, which keeps it off requests from other sites but not from other hosts of the same site. Once hanzo.ai signs people in, any page on any *.hanzo.ai host could post a form to /v1/iam/login with type=device and approve an attacker's `hanzo login` code, or with type=code mint an authorization code, or edit the account. sessions.Forgeable is now the one rule for a write the cookie authorizes: Sec-Fetch-Site must be same-origin or none, and a request with a body must carry it as JSON. sessions.Writer resolves the session only past it, and every cookie write goes through it: the login SSO and device branches, callerOf/callerFrom (profile, password rotation, terms, consent, preferences, onboarding, link, unlink, passkey enrollment), and invitation accept. A refused write is 403 before the session is read. The passkey finish, which the challenge cookie authorizes, asks the same rule. A bearer from a client that is not a browser needs no fetch metadata, with or without a cookie riding along. Signing out ends sessions, so sessions.Clear asks the rule too, with one exception: a top-level navigation (Sec-Fetch-Mode navigate, Sec-Fetch-Dest document), which is how a relying party signs a person out. An image, a frame or a sibling's form ends nothing. The federation binding cookie is __Host-hanzo_fed and only that name is read; a test now pins that the bare hanzo_fed name is refused. server.RequireIssuer is the boot check for issuers: it installs IAM_ISSUER and IAM_ISSUER_MAP, then IAM_FEDERATION_ORIGIN_MAP, and returns an error for an empty or non-https IAM_ISSUER (outside IAM_DEV_HOST_RELATIVE=1), a malformed issuer map, or a sign-in map that would strand the binding. `iam serve` calls it, and a host that embeds IAM must call it before NewApp. devFallbackIssuer and the lazy env resolvers are gone: a process that never installed an issuer has none, so every issuer read is an error, discovery answers 503, no token is minted and none is accepted, where it used to answer https://hanzo.id. No issuer value changes. Co-authored-by: Hanzo Dev <[email protected]>
204 lines
7.3 KiB
Go
204 lines
7.3 KiB
Go
// Copyright 2026 Hanzo AI, Inc.
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
package oidc
|
|
|
|
import (
|
|
"io"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hanzoai/orm"
|
|
"github.com/zap-proto/zip"
|
|
|
|
"github.com/hanzoai/iam/pkg/store"
|
|
)
|
|
|
|
// A code sent to an address no account holds proves the address and nothing else.
|
|
// Sign-in then answers SignupRequired; the account is made only by a second
|
|
// request that carries the accepted terms.
|
|
|
|
func codeSignupServer(t *testing.T, signup bool) (*zip.App, orm.DB, *fakeSender) {
|
|
t.Helper()
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "hanzo-cloud", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: signup, codeSignin: true, orgChoice: "create"})
|
|
seedOrg(t, db, "hanzo")
|
|
sent := &fakeSender{}
|
|
bindSender(t, sent)
|
|
return app, db, sent
|
|
}
|
|
|
|
func codeLoginReq(t *testing.T, app *zip.App, addr, code string, extra map[string]any) map[string]any {
|
|
t.Helper()
|
|
body := map[string]any{
|
|
"organization": "hanzo", "username": addr, "code": code,
|
|
"application": "hanzo-cloud", "clientId": "hanzo-cloud",
|
|
"redirectUri": testRedirect, "scope": "openid", "type": "code",
|
|
}
|
|
for k, v := range extra {
|
|
body[k] = v
|
|
}
|
|
_, raw := do(t, app, jsonReq("POST", PathLogin, body))
|
|
return decode(t, raw)
|
|
}
|
|
|
|
var accepted = map[string]any{"create": true, "terms": "2026-09", "aup": "2026-09"}
|
|
|
|
func TestCodeSignup_NewAddressIsToldSignupRequiredAndNothingIsMade(t *testing.T) {
|
|
const addr = "[email protected]"
|
|
app, db, sent := codeSignupServer(t, true)
|
|
code := sentCode(t, app, sent, addr)
|
|
|
|
m := codeLoginReq(t, app, addr, code, nil)
|
|
if m["status"] != "ok" || m["data"] != SignupRequired {
|
|
t.Fatalf("want ok/%s, got %v", SignupRequired, m)
|
|
}
|
|
if u, _ := store.GetSignupByEmail(tctx(), db, "hanzo", addr); u != nil {
|
|
t.Fatal("an account was made before the person accepted anything")
|
|
}
|
|
// The code was not spent: the create that follows uses the same one.
|
|
if m := codeLoginReq(t, app, addr, code, accepted); m["status"] != "ok" {
|
|
t.Fatalf("the code was spent by the probe: %v", m)
|
|
}
|
|
}
|
|
|
|
func TestCodeSignup_CreateMakesAPasswordlessAccountAndGrants(t *testing.T) {
|
|
const addr = "[email protected]"
|
|
app, db, sent := codeSignupServer(t, true)
|
|
code := sentCode(t, app, sent, addr)
|
|
|
|
m := codeLoginReq(t, app, addr, code, accepted)
|
|
if g, _ := m["data"].(string); m["status"] != "ok" || g == "" || g == SignupRequired {
|
|
t.Fatalf("no grant was minted: %v", m)
|
|
}
|
|
u, err := store.GetSignupByEmail(tctx(), db, "hanzo", addr)
|
|
if err != nil || u == nil {
|
|
t.Fatalf("the account is missing: %v", err)
|
|
}
|
|
// IsAdmin is the founder of their own org, the rule provisionFederatedUser
|
|
// follows; SuperAdmin is owner == "admin" and is never reached.
|
|
if u.PasswordHash != "" || !u.EmailVerified || u.Owner == "admin" {
|
|
t.Fatalf("wrong shape: hash=%q verified=%v owner=%s", u.PasswordHash, u.EmailVerified, u.Owner)
|
|
}
|
|
tm, ok := u.TermsAccepted()
|
|
if !ok || tm.Terms != "2026-09" || tm.AUP != "2026-09" || tm.Method != methodEmailCode || tm.Time == "" {
|
|
t.Fatalf("acceptance not recorded: %+v ok=%v", tm, ok)
|
|
}
|
|
// One use: the same code cannot make or sign in anything again.
|
|
if m := codeLoginReq(t, app, addr, code, nil); m["status"] != "error" {
|
|
t.Fatalf("a spent code was honoured: %v", m)
|
|
}
|
|
}
|
|
|
|
func TestCodeSignup_CreateNeedsTheAcceptedTerms(t *testing.T) {
|
|
const addr = "[email protected]"
|
|
app, db, sent := codeSignupServer(t, true)
|
|
code := sentCode(t, app, sent, addr)
|
|
|
|
m := codeLoginReq(t, app, addr, code, map[string]any{"create": true})
|
|
if m["status"] != "error" {
|
|
t.Fatalf("an account was offered without terms: %v", m)
|
|
}
|
|
if u, _ := store.GetSignupByEmail(tctx(), db, "hanzo", addr); u != nil {
|
|
t.Fatal("an account was made without terms")
|
|
}
|
|
if m := codeLoginReq(t, app, addr, code, accepted); m["status"] != "ok" {
|
|
t.Fatalf("the refused attempt cost the person the code: %v", m)
|
|
}
|
|
}
|
|
|
|
func TestCodeSignup_WrongCodeIsTheOpaqueRefusal(t *testing.T) {
|
|
const addr = "[email protected]"
|
|
app, db, sent := codeSignupServer(t, true)
|
|
sentCode(t, app, sent, addr)
|
|
|
|
for _, extra := range []map[string]any{nil, accepted} {
|
|
m := codeLoginReq(t, app, addr, "000000", extra)
|
|
if msg, _ := m["msg"].(string); m["status"] != "error" || msg != "the code is incorrect or has expired" {
|
|
t.Fatalf("a wrong code was not refused opaquely: %v", m)
|
|
}
|
|
}
|
|
if u, _ := store.GetSignupByEmail(tctx(), db, "hanzo", addr); u != nil {
|
|
t.Fatal("an account was made on a wrong code")
|
|
}
|
|
}
|
|
|
|
func TestCodeSignup_ExistingAccountSignsInAsBefore(t *testing.T) {
|
|
const addr = "[email protected]"
|
|
app, db, sent := codeSignupServer(t, true)
|
|
seedUserInOrg(t, db, "hanzo", "ada", addr, "correct horse battery staple")
|
|
code := sentCode(t, app, sent, addr)
|
|
|
|
m := codeLoginReq(t, app, addr, code, accepted)
|
|
if g, _ := m["data"].(string); m["status"] != "ok" || g == "" || g == SignupRequired {
|
|
t.Fatalf("an existing account did not sign in: %v", m)
|
|
}
|
|
u, _ := store.GetUserByName(tctx(), db, "hanzo", "ada")
|
|
if _, ok := u.TermsAccepted(); ok {
|
|
t.Fatal("signing in recorded an acceptance it never asked for")
|
|
}
|
|
}
|
|
|
|
func TestCodeSignup_DisabledSignupIsTheOpaqueRefusal(t *testing.T) {
|
|
const addr = "[email protected]"
|
|
app, db, sent := codeSignupServer(t, false)
|
|
code := sentCode(t, app, sent, addr)
|
|
|
|
for _, extra := range []map[string]any{nil, accepted} {
|
|
m := codeLoginReq(t, app, addr, code, extra)
|
|
if msg, _ := m["msg"].(string); m["status"] != "error" || msg != "the code is incorrect or has expired" {
|
|
t.Fatalf("an app with sign-up off revealed more: %v", m)
|
|
}
|
|
}
|
|
if u, _ := store.GetSignupByEmail(tctx(), db, "hanzo", addr); u != nil {
|
|
t.Fatal("an account was made where sign-up is off")
|
|
}
|
|
}
|
|
|
|
func TestPutTerms_RecordsTheCallersAcceptanceAndNobodyElses(t *testing.T) {
|
|
const addr = "[email protected]"
|
|
app, db, sent := codeSignupServer(t, true)
|
|
seedUserInOrg(t, db, "hanzo", "other", "[email protected]", "correct horse battery staple")
|
|
code := sentCode(t, app, sent, addr)
|
|
|
|
resp, _ := do(t, app, jsonReq("POST", PathLogin, map[string]any{
|
|
"organization": "hanzo", "username": addr, "code": code, "create": true, "terms": "t1", "aup": "a1",
|
|
"application": "hanzo-cloud", "clientId": "hanzo-cloud",
|
|
"redirectUri": testRedirect, "scope": "openid", "type": "code",
|
|
}))
|
|
cookies := resp.Cookies()
|
|
if len(cookies) == 0 {
|
|
t.Fatal("no session was opened")
|
|
}
|
|
|
|
put := func(body string, withCookie bool) map[string]any {
|
|
req := fromPage(jsonReq("PUT", PathTerms, nil))
|
|
req.Body = io.NopCloser(strings.NewReader(body))
|
|
req.ContentLength = int64(len(body))
|
|
if withCookie {
|
|
for _, c := range cookies {
|
|
req.AddCookie(c)
|
|
}
|
|
}
|
|
_, raw := do(t, app, req)
|
|
return decode(t, raw)
|
|
}
|
|
if m := put(`{"terms":"t2","aup":"a2"}`, false); m["status"] != "error" {
|
|
t.Fatalf("an anonymous caller recorded terms: %v", m)
|
|
}
|
|
if m := put(`{"terms":"","aup":"a2"}`, true); m["status"] != "error" {
|
|
t.Fatalf("an empty version was accepted: %v", m)
|
|
}
|
|
if m := put(`{"terms":"t2","aup":"a2"}`, true); m["status"] != "ok" {
|
|
t.Fatalf("the caller could not accept: %v", m)
|
|
}
|
|
u, _ := store.GetSignupByEmail(tctx(), db, "hanzo", addr)
|
|
if tm, ok := u.TermsAccepted(); !ok || tm.Terms != "t2" || tm.Method != methodSignedIn {
|
|
t.Fatalf("not recorded on the caller: %+v", tm)
|
|
}
|
|
other, _ := store.GetUserByName(tctx(), db, "hanzo", "other")
|
|
if _, ok := other.TermsAccepted(); ok {
|
|
t.Fatal("somebody else's row was touched")
|
|
}
|
|
}
|