Files
iam/internal/webauthn/authz_write_test.go
zeekay a23166bbb3 In a held org an admin changes no member's sign-in, and a client's minted token names the client
An admin of an org whose name the platform holds (authz.IsHeldOrg) could set a
member's password, address, phone or second factor, or file a passkey or a token
for them, and then sign in as them with nothing in the token or the trail to
say so: staff could take over any customer living in hanzo. users.Credentials is
the rule now, read by users.Update (and SCIM and the legacy verb through it),
the MFA target and authz.AuthorizeUser: there only the person, a SuperAdmin or
a platform application holding the user-administration capability changes how
another account signs in, and each such change is audited. Profile edits are
the admin's as before, and a customer org's admin still runs its own members.

A confidential client on IAM_TOKEN_EXCHANGE_APPS minted a token for any user
in any tenant over /v1/iam/tokens/issue, bearing that user's identity and no
trace of the client. The client proves itself and not the person, so the token
now names it in act, lives at most an hour, is audited, and is minted only for
the people the client's org signs in. An acted-for token writes no account and
founds no org.
2026-10-17 08:34:08 -07:00

86 lines
3.0 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package webauthn_test
// A passkey authenticates its User, so REGISTERING one for a person is acting for
// that account. The write authorizes that subject the same way the list authorizes
// reading it — otherwise a caller files a credential of its own under
// User=admin/root and the public signin ceremony, which offers a person's
// credentials by User, hands it to the attacker's authenticator. These drive the
// REAL router (newRig seeds admin/cert-hanzo), so the Guard attaches a principal
// and the write's gate runs.
import (
"context"
"io"
"net/http/httptest"
"strings"
"testing"
"github.com/hanzoai/orm"
"github.com/hanzoai/iam/internal/testhttp"
"github.com/hanzoai/iam/pkg/schema"
)
// write posts one credential registration as sub and returns the status.
func (r *rig) write(t *testing.T, sub, body string) int {
t.Helper()
req := httptest.NewRequest("POST", keys, strings.NewReader(body))
req.Host = "hanzo.id"
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+r.bearer(t, sub))
resp, err := testhttp.Do(r.app, req)
if err != nil {
t.Fatalf("POST: %v", err)
}
_, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close()
return resp.StatusCode
}
func (r *rig) credsFor(t *testing.T, user string) int {
t.Helper()
rows, err := orm.TypedQuery[schema.WebauthnCredential](r.db).Filter("User=", user).GetAll(context.Background())
if err != nil {
t.Fatalf("query %s: %v", user, err)
}
return len(rows)
}
// The passkey-plant is refused, and nothing lands under admin/root — so the signin
// ceremony that offers a person's credentials by User has none of the attacker's to
// offer.
func TestAdd_refusesAPasskeyPlantedForAReservedUser(t *testing.T) {
r := newRig(t)
before := r.credsFor(t, "admin/root")
if st := r.write(t, "acme/boss",
`{"owner":"acme","name":"plant","user":"admin/root","credentialId":"AAAA","publicKey":"AAAA","userVerified":true}`); st != 403 {
t.Fatalf("planting a passkey for admin/root: status=%d, want 403", st)
}
if after := r.credsFor(t, "admin/root"); after != before {
t.Fatalf("a credential landed under admin/root: %d -> %d", before, after)
}
}
// The same gate refuses a plant aimed at ANOTHER tenant's user.
func TestAdd_refusesAPasskeyPlantedForAnotherTenant(t *testing.T) {
r := newRig(t)
if st := r.write(t, "acme/boss",
`{"owner":"acme","name":"plant","user":"orgb/carol","credentialId":"AAAA","publicKey":"AAAA"}`); st != 403 {
t.Fatalf("planting a passkey for orgb/carol: status=%d, want 403", st)
}
}
// An org-admin may still register a passkey for its OWN member — the gate pins the
// subject, it does not forbid the enrollment an admin may legitimately do.
func TestAdd_allowsAPasskeyForAnOwnMember(t *testing.T) {
r := newRig(t)
if st := r.write(t, "acme/boss",
`{"owner":"acme","name":"alice-newkey","user":"acme/alice","credentialId":"AAAA","publicKey":"AAAA"}`); st != 200 {
t.Fatalf("registering a passkey for an own member: status=%d, want 200", st)
}
}