Files
iam/pkg/schema/mask_invariant_test.go
zeekay 6f2c48960a an ambiguous identifier is opaque, and a masked secret is carried
Three regressions an adversarial pass proved, all introduced by the corrections
that landed before it.

AN ACCOUNT-EXISTENCE ORACLE ON TWO UNAUTHENTICATED DOORS. Refusing to pick
between duplicate rows is right; echoing that refusal to the caller is not.
/v1/iam/login emitted the resolver's own words BEFORE any credential check, and
/v1/iam/verification-codes did the same, so "email address matches more than one
account" told a stranger that the address they typed is real and which addresses
are worth attacking. Both now answer exactly as they answer an address that
matches nobody -- the opaque refusal the login path already spent care making
indistinguishable from "no such user". The operator still learns everything from
the log. This is MY defect first: ErrPhoneAmbiguous shipped earlier today with
the same propagation, so login covers both errors, not just the new one.

A BEARER THAT SKIPPED THE MASK. MfaRememberDigest is what presenting a remembered
browser proves, so a reader who can see it walks past the second factor -- and it
carries a live json tag, so leaving it out of User.Mask published it on every user
read. It is masked now, and carried, because this file's own rule is that what a
reader cannot see a writer cannot state.

That rule was held together by nothing but proximity: both functions hand-list
their fields, so a secret added to one and forgotten in the other compiles,
passes and ships -- which is how the authenticator seed was lost and how this
digest came to be neither masked nor carried. It is now derived instead of
restated: a reflective test masks a fully-populated user and asserts every field
the mask blanked is a field the carry restores, and the converse. Drop either
line and it fails by name.

SCIM RE-PROVISIONED ACCOUNTS THAT ALREADY EXISTED. Create/Update normalize the
email they store; the SCIM filter still compared the caller's raw spelling, so an
IdP sending "[email protected]" found nobody.

The oracle test earned its place the hard way: the first draft compared two
requests that both died at an earlier guard, so it passed with the leak fully
restored. It now asserts the control request reached the send before comparing,
and with the leak back it fails naming the exact string it leaked.
2026-08-07 23:47:57 -07:00

106 lines
3.4 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package schema
import (
"reflect"
"testing"
)
// What a reader cannot see, a writer cannot state — enforced, not assumed.
//
// Mask and CarrySecretsFrom are one decision read in two directions, and the file
// they live in says so. Until now the only thing holding them together was
// proximity: both hand-list their fields, so a secret added to one and forgotten
// in the other compiles, passes, and ships. That is exactly how the authenticator
// seed was lost — masked but not carried, so every full-row self-write wiped it —
// and how MfaRememberDigest came to be neither.
//
// This test derives the rule instead of restating it: mask a fully-populated user,
// and every string field the mask BLANKED must be a field CarrySecretsFrom
// restores. Add a secret to Mask and forget its sibling, and this fails by
// construction with the field's name.
func TestEveryMaskedFieldIsCarried(t *testing.T) {
// A user with every string field set to a distinct non-empty value, so
// "blanked" and "carried" are both observable.
full := &User{}
v := reflect.ValueOf(full).Elem()
for i := 0; i < v.NumField(); i++ {
if f := v.Field(i); f.Kind() == reflect.String && f.CanSet() {
f.SetString("set-" + v.Type().Field(i).Name)
}
}
masked := full.Mask()
mv := reflect.ValueOf(masked).Elem()
// The write path: a body derived from a masked read, posted back.
writer := *masked
writer.CarrySecretsFrom(full)
wv := reflect.ValueOf(&writer).Elem()
for i := 0; i < v.NumField(); i++ {
name := v.Type().Field(i).Name
if v.Field(i).Kind() != reflect.String {
continue
}
blanked := mv.Field(i).String() == "" && v.Field(i).String() != ""
if !blanked {
continue
}
if got := wv.Field(i).String(); got != v.Field(i).String() {
t.Errorf("%s is masked but not carried: a full-row write ERASES it (got %q, want the stored value)", name, got)
}
}
}
// The converse: a field CarrySecretsFrom restores must be one Mask blanks.
// Carrying a field the reader can already see is not a secret being protected —
// it is a field the writer has been silently forbidden from changing, which is a
// bug that presents as "my edit did not save".
func TestEveryCarriedFieldIsMasked(t *testing.T) {
prior := &User{}
pv := reflect.ValueOf(prior).Elem()
for i := 0; i < pv.NumField(); i++ {
if f := pv.Field(i); f.Kind() == reflect.String && f.CanSet() {
f.SetString("prior-" + pv.Type().Field(i).Name)
}
}
// An incoming write that states a NEW value for every string field.
incoming := &User{}
iv := reflect.ValueOf(incoming).Elem()
for i := 0; i < iv.NumField(); i++ {
if f := iv.Field(i); f.Kind() == reflect.String && f.CanSet() {
f.SetString("incoming-" + iv.Type().Field(i).Name)
}
}
incoming.CarrySecretsFrom(prior)
blank := (&User{}).Mask()
sample := &User{}
sv := reflect.ValueOf(sample).Elem()
for i := 0; i < sv.NumField(); i++ {
if f := sv.Field(i); f.Kind() == reflect.String && f.CanSet() {
f.SetString("x")
}
}
maskedSample := reflect.ValueOf(sample.Mask()).Elem()
_ = blank
for i := 0; i < iv.NumField(); i++ {
name := iv.Type().Field(i).Name
if iv.Field(i).Kind() != reflect.String {
continue
}
overwritten := iv.Field(i).String() == pv.Field(i).String()
if !overwritten {
continue
}
if maskedSample.Field(i).String() != "" {
t.Errorf("%s is carried but NOT masked: the writer cannot change a field the reader can see", name)
}
}
}