Files
ai/controllers/usage_origin_test.go
zeekayandHanzo Dev 826dfba107 the host that answered is an admin column, and every routed call records one
Two facts about one call had been collapsed into one. `provider` is the SKU we
sold — hanzo, enso — and it is what a customer should see. The host that
actually served the bytes is a different fact, and the ledger could not be asked
for it: `origin` was written by five surfaces and read by nobody.

The read now projects the ONE row per audience instead of answering the question
twice. Non-admin is the DEFAULT and the zero value, so the customer surface is
unchanged and the upstream cannot be disclosed by forgetting to hide it — only
by proving the predicate. The admin lens GAINS a column; it rewrites none.

The lens is not a second permission check. resolveCloudUsageScope already
evaluates util.IsSuperAdmin + own-brand to decide scope, and now returns the
lens from that same evaluation, so "whose rows" and "which columns" cannot
answer differently about one caller. A sibling brand's admin authenticates,
is pinned, and reads the customer shape.

Two layers, because they fail differently: under the customer lens `origin` is
never named in the SELECT, so the host is not in the process serving the
response, and it is separately never assigned. Being wrong takes two mistakes.

On the write side the invariant is now whole: a record naming a routed provider
also names its host, 33 of 33. It was 10 of 33 — and the gap fell on the ZAP
error paths, where the provider sat in scope and a reroute is exactly what had
just fired. A label outlives the thing it names; the address does not.

Pinned by an AST scan rather than a runtime check, because the defect compiles,
runs, and writes a complete-looking row forever. The scan fails if it matches
nothing, so it cannot pass by scanning the wrong thing.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-13 17:16:36 -07:00

114 lines
3.4 KiB
Go

// Copyright 2023-2026 Hanzo AI Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controllers
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"path/filepath"
"strings"
"testing"
)
// TestRoutedRecordNamesItsHost — a record that names a routed provider must also
// name the host that answered.
//
// Provider is our LABEL for a route; Origin is the address the bytes went to.
// Recording only the label records what we intended to call, and a fallback or a
// reroute is exactly the moment those two stop agreeing — the moment the label
// keeps reading correct while being wrong. Both together can be asked whether
// they still match; that answer is a measurement rather than a belief.
//
// This is a source-level pin because the defect is invisible at runtime: a
// constructor that omits Origin compiles, runs, and writes a complete-looking row
// forever. The gap was NOT hypothetical — the three ZAP error paths each set
// Provider from a provider that was right there in scope, and left the host empty
// on precisely the calls where a reroute had fired.
//
// Scope is deliberately narrow: only literals whose Provider comes from a provider
// VALUE in scope (provider.X / actualProvider.X). A surface that names a provider
// by constant string has no address to derive and is not asked for one.
func TestRoutedRecordNamesItsHost(t *testing.T) {
files, err := filepath.Glob("*.go")
if err != nil {
t.Fatalf("glob: %v", err)
}
var missing []string
var checked int
for _, f := range files {
if strings.HasSuffix(f, "_test.go") {
continue
}
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, f, nil, 0)
if err != nil {
t.Fatalf("parse %s: %v", f, err)
}
ast.Inspect(file, func(n ast.Node) bool {
lit, isLit := n.(*ast.CompositeLit)
if !isLit {
return true
}
if id, isID := lit.Type.(*ast.Ident); !isID || id.Name != "usageRecord" {
return true
}
var routed, hasOrigin bool
for _, elt := range lit.Elts {
kv, isKV := elt.(*ast.KeyValueExpr)
if !isKV {
continue
}
key, isID := kv.Key.(*ast.Ident)
if !isID {
continue
}
switch key.Name {
case "Provider":
// Routed == the value is a field/method of a provider in scope.
if sel, ok := kv.Value.(*ast.SelectorExpr); ok {
if base, ok := sel.X.(*ast.Ident); ok {
routed = base.Name == "provider" || base.Name == "actualProvider"
}
}
case "Origin":
hasOrigin = true
}
}
if !routed {
return true
}
checked++
if !hasOrigin {
missing = append(missing, fmt.Sprintf("%s:%d", f, fset.Position(lit.Pos()).Line))
}
return true
})
}
if checked == 0 {
t.Fatal("found no routed usageRecord literals — the scan matched nothing, so it proves nothing")
}
if len(missing) > 0 {
t.Errorf("%d routed usage record(s) name a provider but not the host that answered:\n %s",
len(missing), strings.Join(missing, "\n "))
}
}