mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
util.IsSuperAdmin compared the resolved user's owner to "admin", a second predicate beside the one the host boundary decides, and the two disagreed. An admin-org application (no orgs, owner claim admin), an admin-org person's sk- key, and an org spelled "admin " (the parse trimmed orgs[0]) were each SuperAdmin here and refused by the boundary. The host now states the verdict: object.SetSuper, which hanzo-inc/cloud fills with principal.Super, the X-User-IsAdmin its boundary deletes from every request and re-mints from authz.Claims.Sudo. Every HTTP seam that turns a token into a principal goes through object.Verify, which stamps it on iam.User.Sudo (never decoded); IsSuperAdmin and ScopeOwner read that and compare no org. util.AdminOrg is gone; the rows that are the platform's own are named by authz.AdminOrg. With no host there is no SuperAdmin, and a ZAP door is not the host: target deletes X-User-IsAdmin from a ZAP-built request, and zapPrincipal carries a verdict only when the HTTP bridge vouched for the request it was handed. A key is never the SuperAdmin, as at the host.
262 lines
9.3 KiB
Go
262 lines
9.3 KiB
Go
// Copyright 2023-2025 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 (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
iam "github.com/hanzoai/ai/internal/iam"
|
|
"github.com/hanzoai/ai/object"
|
|
"github.com/luxfi/zap"
|
|
)
|
|
|
|
// These reads name a row by id and sit behind the coarse filter, which asks only
|
|
// whether the caller administers SOME organization — a thing every customer's own
|
|
// admin is. It never asks which one, so the id was the whole address. Each read
|
|
// asks its own row whose it is now, and answers a row it does not reach the way
|
|
// it answers a row that is not there.
|
|
func TestAByIdReadStaysInsideTheCallersOrg(t *testing.T) {
|
|
withStore(t)
|
|
people := withIAM(t)
|
|
|
|
mark := "kind-of-private-marker"
|
|
for _, seed := range []struct {
|
|
name string
|
|
write func() error
|
|
}{
|
|
{"template", func() error {
|
|
_, e := object.AddTemplate(&object.Template{Owner: "victim", Name: "r", Manifest: mark})
|
|
return e
|
|
}},
|
|
{"asset", func() error {
|
|
_, e := object.AddAsset(&object.Asset{Owner: "victim", Name: "r", DisplayName: mark})
|
|
return e
|
|
}},
|
|
{"form", func() error {
|
|
_, e := object.AddForm(&object.Form{Owner: "victim", Name: "r", DisplayName: mark})
|
|
return e
|
|
}},
|
|
{"graph", func() error { _, e := object.AddGraph(&object.Graph{Owner: "victim", Name: "r", Text: mark}); return e }},
|
|
{"scan", func() error {
|
|
_, e := object.AddScan(&object.Scan{Owner: "victim", Name: "r", DisplayName: mark})
|
|
return e
|
|
}},
|
|
{"session", func() error { _, e := object.AddSession(&object.Session{Owner: "victim", Name: "r"}); return e }},
|
|
{"vector", func() error {
|
|
_, e := object.AddVector(&object.Vector{Owner: "victim", Name: "r", Text: mark})
|
|
return e
|
|
}},
|
|
{"workflow", func() error {
|
|
_, e := object.AddWorkflow(&object.Workflow{Owner: "victim", Name: "r", Text: mark}, "en")
|
|
return e
|
|
}},
|
|
{"article", func() error {
|
|
_, e := object.AddArticle(&object.Article{Owner: "victim", Name: "r", DisplayName: mark})
|
|
return e
|
|
}},
|
|
} {
|
|
if err := seed.write(); err != nil {
|
|
t.Fatalf("seeding a %s: %v", seed.name, err)
|
|
}
|
|
}
|
|
|
|
// An admin — of a DIFFERENT organization.
|
|
mallory := people.signedIn(t, &iam.User{Owner: "acme", Name: "mallory", IsAdmin: true})
|
|
|
|
for _, read := range []struct {
|
|
route string
|
|
call func(*ApiController)
|
|
}{
|
|
{"get-template", (*ApiController).GetTemplate},
|
|
{"get-asset", (*ApiController).GetAsset},
|
|
{"get-form", (*ApiController).GetForm},
|
|
{"get-graph", (*ApiController).GetGraph},
|
|
{"get-scan", (*ApiController).GetScan},
|
|
{"get-session", (*ApiController).GetSingleSession},
|
|
{"get-vector", (*ApiController).GetVector},
|
|
{"get-workflow", (*ApiController).GetWorkflow},
|
|
{"get-article", (*ApiController).GetArticle},
|
|
} {
|
|
c := as(visit("GET", "/v1/ai/"+read.route+"?id=victim/r"), mallory)
|
|
read.call(c)
|
|
body := sent(c)
|
|
if strings.Contains(body, mark) || strings.Contains(body, `"owner":"victim"`) {
|
|
t.Errorf("%s answered another organization's row: %s", read.route, body)
|
|
}
|
|
}
|
|
|
|
// Its own organization still reads it.
|
|
owner := people.signedIn(t, &iam.User{Owner: "victim", Name: "val", IsAdmin: true})
|
|
c := as(visit("GET", "/v1/ai/get-template?id=victim/r"), owner)
|
|
c.GetTemplate()
|
|
if !strings.Contains(sent(c), mark) {
|
|
t.Errorf("the organization that owns it was refused: %s", sent(c))
|
|
}
|
|
}
|
|
|
|
// The same question on the other surface. These twins take the id out of the
|
|
// payload instead of the query, and two of them asked only whether the caller was
|
|
// signed in at all.
|
|
func TestAZapReadByIdStaysInsideTheCallersOrg(t *testing.T) {
|
|
withStore(t)
|
|
people := withIAM(t)
|
|
|
|
mark := "kind-of-private-marker"
|
|
if _, err := object.AddAsset(&object.Asset{Owner: "victim", Name: "r", DisplayName: mark}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := object.AddScan(&object.Scan{Owner: "victim", Name: "r", DisplayName: mark}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := object.AddGraph(&object.Graph{Owner: "victim", Name: "r", Text: mark}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := object.AddApplication(&object.Application{Owner: "victim", Name: "r", DisplayName: mark}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
mallory := people.signedIn(t, &iam.User{Owner: "acme", Name: "mallory", IsAdmin: true})
|
|
|
|
for _, read := range []struct {
|
|
name string
|
|
call func(context.Context, string, []byte) (*zap.Message, error)
|
|
}{
|
|
{"assets.get-one", zapGetAssetHandler},
|
|
{"scans.get-one", zapGetScanHandler},
|
|
{"graphs.get-one", zapGetGraphHandler},
|
|
{"applications.get-one", zapGetApplicationHandler},
|
|
} {
|
|
msg, err := read.call(context.Background(), mallory, []byte(`{"id":"victim/r"}`))
|
|
if err != nil {
|
|
t.Fatalf("%s: %v", read.name, err)
|
|
}
|
|
if got := string(msg.Root().Bytes(object.CloudRespBody)); strings.Contains(got, mark) {
|
|
t.Errorf("%s answered another organization's row: %s", read.name, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A chat's tenant is its Organization — Owner is the namespace every chat shares
|
|
// — so a listing that scoped on Owner, or on nothing, showed every organization's
|
|
// conversations to any organization's admin.
|
|
func TestTheGlobalChatListingStaysInsideTheCallersOrg(t *testing.T) {
|
|
withStore(t)
|
|
people := withIAM(t)
|
|
|
|
for _, c := range []*object.Chat{
|
|
{Owner: chatOwner, Name: "theirs", Organization: "victim", DisplayName: "victim-conversation"},
|
|
{Owner: chatOwner, Name: "ours", Organization: "acme", DisplayName: "acme-conversation"},
|
|
} {
|
|
if _, err := object.AddChat(c); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
mallory := people.signedIn(t, &iam.User{Owner: "acme", Name: "mallory", IsAdmin: true})
|
|
|
|
// Both shapes of the listing: the whole set, and a page of it.
|
|
for _, target := range []string{
|
|
"/v1/ai/get-global-chats",
|
|
"/v1/ai/get-global-chats?p=1&pageSize=100",
|
|
} {
|
|
c := as(visit("GET", target), mallory)
|
|
c.GetGlobalChats()
|
|
body := sent(c)
|
|
if strings.Contains(body, "victim-conversation") {
|
|
t.Errorf("%s carried another organization's chat: %s", target, body)
|
|
}
|
|
if !strings.Contains(body, "acme-conversation") {
|
|
t.Errorf("%s did not carry the caller's own chat: %s", target, body)
|
|
}
|
|
}
|
|
|
|
// The SuperAdmin — the host's verdict — reads every organization's; an
|
|
// admin-org token the host did not vouch for reads its own.
|
|
host(t)
|
|
root := people.signedIn(t, &iam.User{Owner: "admin", Name: "root", IsAdmin: true})
|
|
c := as(visit("GET", "/v1/ai/get-global-chats"), root)
|
|
c.GetGlobalChats()
|
|
if strings.Contains(sent(c), "victim-conversation") {
|
|
t.Errorf("an admin-org token with no host verdict was shown every organization: %s", sent(c))
|
|
}
|
|
c = as(visit("GET", "/v1/ai/get-global-chats"), root)
|
|
c.Fiber().Request().Header.Set("X-User-IsAdmin", "true")
|
|
c.GetGlobalChats()
|
|
if !strings.Contains(sent(c), "victim-conversation") {
|
|
t.Errorf("the SuperAdmin was not shown every organization: %s", sent(c))
|
|
}
|
|
}
|
|
|
|
// The listings named "global" were the whole table, and the word was doing the
|
|
// scoping. They show the SuperAdmin everything and everybody else their own.
|
|
func TestAGlobalListingStaysInsideTheCallersOrg(t *testing.T) {
|
|
withStore(t)
|
|
people := withIAM(t)
|
|
|
|
mark := "kind-of-private-marker"
|
|
for _, org := range []string{"victim", "acme"} {
|
|
name := org + "-row"
|
|
if _, err := object.AddArticle(&object.Article{Owner: org, Name: name, DisplayName: mark}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := object.AddForm(&object.Form{Owner: org, Name: name, DisplayName: mark}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := object.AddGraph(&object.Graph{Owner: org, Name: name, Text: mark}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := object.AddVector(&object.Vector{Owner: org, Name: name, Text: mark}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := object.AddWorkflow(&object.Workflow{Owner: org, Name: name, Text: mark}, "en"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
mallory := people.signedIn(t, &iam.User{Owner: "acme", Name: "mallory", IsAdmin: true})
|
|
host(t)
|
|
root := people.signedIn(t, &iam.User{Owner: "admin", Name: "root", IsAdmin: true})
|
|
|
|
for _, listing := range []struct {
|
|
route string
|
|
call func(*ApiController)
|
|
}{
|
|
{"get-global-articles", (*ApiController).GetGlobalArticles},
|
|
{"get-global-forms", (*ApiController).GetGlobalForms},
|
|
{"get-global-graphs", (*ApiController).GetGlobalGraphs},
|
|
{"get-global-vectors", (*ApiController).GetGlobalVectors},
|
|
{"get-global-workflows", (*ApiController).GetGlobalWorkflows},
|
|
} {
|
|
c := as(visit("GET", "/v1/ai/"+listing.route), mallory)
|
|
listing.call(c)
|
|
body := sent(c)
|
|
if strings.Contains(body, `"victim-row"`) {
|
|
t.Errorf("%s carried another organization's row: %s", listing.route, body)
|
|
}
|
|
if !strings.Contains(body, `"acme-row"`) {
|
|
t.Errorf("%s did not carry the caller's own row: %s", listing.route, body)
|
|
}
|
|
|
|
c = as(visit("GET", "/v1/ai/"+listing.route), root)
|
|
c.Fiber().Request().Header.Set("X-User-IsAdmin", "true")
|
|
listing.call(c)
|
|
if !strings.Contains(sent(c), `"victim-row"`) {
|
|
t.Errorf("%s did not show the SuperAdmin every organization: %s", listing.route, sent(c))
|
|
}
|
|
}
|
|
}
|