feat(judgement): refuse privileged and undeclared actions in chat (#2071)
Connector actions and lookups now declare privilege change and secret access, and an MCP server declares both per installation. Chat withholds and refuses any tool that has either effect or declared nothing, in judgement and at execution, so an approval cannot convert the refusal. Workflows and the dashboard are unchanged.
This commit is contained in:
19 files changed
+660
-73
No files matched your search
@@ -91,8 +91,8 @@ func TestTurnActsWithItsOwnersAccess(t *testing.T) {
|
||||
Toolbox: fakeToolbox{}, Judge: fakeJudge{}, Memory: fakeMemory{},
|
||||
}
|
||||
m := &Module{orgID: "default", store: &fakeStore{sessions: map[uuid.UUID]store.Session{}, turns: map[uuid.UUID]store.Turn{}, events: map[uuid.UUID][]store.Event{}}, log: slog.New(slog.DiscardHandler), hub: hub.New()}
|
||||
action := connectors.Declaration{Connector: "pager", Kind: connectors.KindAction, Name: "page"}
|
||||
lookup := connectors.Declaration{Connector: "pager", Kind: connectors.KindLookup, Name: "who"}
|
||||
action := connectors.Declaration{Connector: "pager", Privileged: noPrivilegedEffects, Kind: connectors.KindAction, Name: "page"}
|
||||
lookup := connectors.Declaration{Connector: "pager", Privileged: noPrivilegedEffects, Kind: connectors.KindLookup, Name: "who"}
|
||||
cases := []struct {
|
||||
name, owner string
|
||||
run func(context.Context) error
|
||||
@@ -232,7 +232,7 @@ func TestOmittedToolCannotRun(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
decl := connectors.Declaration{Connector: "pager", Kind: connectors.KindLookup, Name: "who"}
|
||||
decl := connectors.Declaration{Connector: "pager", Privileged: noPrivilegedEffects, Kind: connectors.KindLookup, Name: "who"}
|
||||
if _, err := m.call(ctx, d, store.Turn{ID: uuid.New()}, decl, jsontext.Value(`{}`)); !errors.Is(err, errToolUnavailable) {
|
||||
t.Fatalf("call error = %v, want %v", err, errToolUnavailable)
|
||||
}
|
||||
@@ -309,9 +309,9 @@ type collisionBox struct{}
|
||||
|
||||
func (collisionBox) Declarations() []connectors.Declaration {
|
||||
return []connectors.Declaration{
|
||||
{Connector: "a_b", Kind: connectors.KindLookup, Name: "c"},
|
||||
{Connector: "a", Kind: connectors.KindLookup, Name: "b_c"},
|
||||
{Connector: "run", Kind: connectors.KindLookup, Name: "list"},
|
||||
{Connector: "a_b", Privileged: noPrivilegedEffects, Kind: connectors.KindLookup, Name: "c"},
|
||||
{Connector: "a", Privileged: noPrivilegedEffects, Kind: connectors.KindLookup, Name: "b_c"},
|
||||
{Connector: "run", Privileged: noPrivilegedEffects, Kind: connectors.KindLookup, Name: "list"},
|
||||
}
|
||||
}
|
||||
func (collisionBox) Tool(string) (connectors.Tool, bool) { return fakeTool{}, true }
|
||||
@@ -406,8 +406,8 @@ type fakeToolbox struct{ Toolbox }
|
||||
|
||||
func (fakeToolbox) Declarations() []connectors.Declaration {
|
||||
return []connectors.Declaration{
|
||||
{Connector: "pager", Kind: connectors.KindAction, Name: "page", Description: "Page the on-call person", Schema: jsontext.Value(`{"type":"object"}`)},
|
||||
{Connector: "pager", Kind: connectors.KindLookup, Name: "who", Description: "Who is on call", Schema: jsontext.Value(`{"type":"object"}`)},
|
||||
{Connector: "pager", Privileged: noPrivilegedEffects, Kind: connectors.KindAction, Name: "page", Description: "Page the on-call person", Schema: jsontext.Value(`{"type":"object"}`)},
|
||||
{Connector: "pager", Privileged: noPrivilegedEffects, Kind: connectors.KindLookup, Name: "who", Description: "Who is on call", Schema: jsontext.Value(`{"type":"object"}`)},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -565,3 +565,7 @@ func (f *fakeStore) Events(_ context.Context, turn uuid.UUID, after int64) ([]st
|
||||
func (f *fakeStore) EventsOf(context.Context, uuid.UUID, []string) ([]store.Event, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// noPrivilegedEffects is a declaration that states it has no privileged effect,
|
||||
// the only kind chat offers.
|
||||
var noPrivilegedEffects = connectors.PrivilegedEffects{Declared: true}
|
||||
@@ -0,0 +1,81 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"encoding/json/jsontext"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/platform/hub"
|
||||
"github.com/samcharles93/archie-core/modules/chat/internal/store"
|
||||
"github.com/samcharles93/archie-core/modules/connectors"
|
||||
"github.com/samcharles93/archie-core/modules/governance"
|
||||
)
|
||||
|
||||
type effectsBox struct {
|
||||
decls []connectors.Declaration
|
||||
tool *countingTool
|
||||
}
|
||||
|
||||
func (b effectsBox) Declarations() []connectors.Declaration { return b.decls }
|
||||
func (b effectsBox) Tool(string) (connectors.Tool, bool) { return b.tool, true }
|
||||
|
||||
// TestChatOffersAndRunsOnlyDeclaredHarmlessTools guards the chat prohibition:
|
||||
// even for the organisation owner, a tool that changes privileges, accesses
|
||||
// secrets or never declared its effects is not described to the model, and a
|
||||
// call to it is refused before anything runs, including a call made with a
|
||||
// declaration that was harmless when it was offered.
|
||||
func TestChatOffersAndRunsOnlyDeclaredHarmlessTools(t *testing.T) {
|
||||
declare := func(name string, p connectors.PrivilegedEffects) connectors.Declaration {
|
||||
return connectors.Declaration{Connector: "iam", Kind: connectors.KindLookup, Name: name, Privileged: p}
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
decl connectors.Declaration
|
||||
offered bool
|
||||
}{
|
||||
{name: "declared harmless", decl: declare("who", noPrivilegedEffects), offered: true},
|
||||
{name: "privilege change", decl: declare("grant", connectors.PrivilegedEffects{Declared: true, PrivilegeChange: true})},
|
||||
{name: "secret access", decl: declare("export", connectors.PrivilegedEffects{Declared: true, SecretAccess: true})},
|
||||
{name: "never declared", decl: declare("legacy", connectors.PrivilegedEffects{})},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
people := fakePeople{alice: person(alice, governance.RoleOwner)}
|
||||
box := effectsBox{decls: []connectors.Declaration{tt.decl}, tool: &countingTool{}}
|
||||
d := Deps{People: people, Access: governance.NewEngine(governance.ShippedOrgPolicies(governance.DefaultOrgID)), Toolbox: box}
|
||||
m := &Module{
|
||||
orgID: "default", log: slog.New(slog.DiscardHandler), hub: hub.New(),
|
||||
store: &fakeStore{sessions: map[uuid.UUID]store.Session{}, turns: map[uuid.UUID]store.Turn{}, events: map[uuid.UUID][]store.Event{}},
|
||||
}
|
||||
ctx, err := m.actingAs(t.Context(), d, alice)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
turn := store.Turn{ID: uuid.New(), SessionID: uuid.New(), Owner: alice}
|
||||
if _, ok := m.tools(ctx, d, turn)[toolName("iam", tt.decl.Name)]; ok != tt.offered {
|
||||
t.Fatalf("offered = %v, want %v", ok, tt.offered)
|
||||
}
|
||||
|
||||
// The model can name a tool that was never offered, or one whose
|
||||
// declaration changed after it was offered.
|
||||
stale := tt.decl
|
||||
stale.Privileged = noPrivilegedEffects
|
||||
_, err = m.call(ctx, d, turn, stale, jsontext.Value(`{}`))
|
||||
if tt.offered {
|
||||
if err != nil {
|
||||
t.Fatalf("call = %v, want it to run", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if !errors.Is(err, errToolProhibited) {
|
||||
t.Fatalf("call = %v, want %v", err, errToolProhibited)
|
||||
}
|
||||
if box.tool.lookups != 0 {
|
||||
t.Fatalf("a prohibited tool ran %d times", box.tool.lookups)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -79,11 +79,11 @@ func taintDeps(t *testing.T, fs *fakeStore, box Toolbox, judge *taintJudge) (*Mo
|
||||
}
|
||||
|
||||
func untrustedLookup() connectors.Declaration {
|
||||
return connectors.Declaration{Connector: "web", Kind: connectors.KindLookup, Name: "fetch", Schema: jsontext.Value(`{"type":"object"}`)}
|
||||
return connectors.Declaration{Connector: "web", Privileged: noPrivilegedEffects, Kind: connectors.KindLookup, Name: "fetch", Schema: jsontext.Value(`{"type":"object"}`)}
|
||||
}
|
||||
|
||||
func untrustedAction() connectors.Declaration {
|
||||
return connectors.Declaration{Connector: "mail", Kind: connectors.KindAction, Name: "send", Schema: jsontext.Value(`{"type":"object"}`)}
|
||||
return connectors.Declaration{Connector: "mail", Privileged: noPrivilegedEffects, Kind: connectors.KindAction, Name: "send", Schema: jsontext.Value(`{"type":"object"}`)}
|
||||
}
|
||||
|
||||
// TestUntrustedOutputTaintsTheSession guards requirement 1 and 2: output from
|
||||
@@ -95,7 +95,7 @@ func TestUntrustedOutputTaintsTheSession(t *testing.T) {
|
||||
judge := &taintJudge{level: judgement.ActQuietly}
|
||||
box := trustBox{decls: []connectors.Declaration{
|
||||
untrustedLookup(),
|
||||
{Connector: "internal", Kind: connectors.KindLookup, Name: "runs", TrustLevel: connectors.TrustTrusted, Schema: jsontext.Value(`{"type":"object"}`)},
|
||||
{Connector: "internal", Privileged: noPrivilegedEffects, Kind: connectors.KindLookup, Name: "runs", TrustLevel: connectors.TrustTrusted, Schema: jsontext.Value(`{"type":"object"}`)},
|
||||
}}
|
||||
m, d, session := taintDeps(t, fs, box, judge)
|
||||
ctx, err := m.actingAs(t.Context(), d, alice)
|
||||
|
||||
+20
-8
@@ -22,7 +22,7 @@ import (
|
||||
)
|
||||
|
||||
// principal is who hard rules see acting when the chat agent calls an action.
|
||||
const principal = "chat"
|
||||
const principal = judgement.ChatWorkflow
|
||||
|
||||
// actingAs is ctx carrying the principal of owner, the person whose
|
||||
// conversation it is, as a request they made would carry it. It is read from
|
||||
@@ -87,6 +87,9 @@ func (m *Module) tools(ctx context.Context, d Deps, t store.Turn) core.ToolSet {
|
||||
// the name is withheld from both.
|
||||
ambiguous := map[string]bool{}
|
||||
for _, decl := range d.Toolbox.Declarations() {
|
||||
if decl.Privileged.ProhibitedInChat() {
|
||||
continue
|
||||
}
|
||||
need := governance.ActionRun
|
||||
if decl.Kind == connectors.KindLookup {
|
||||
need = governance.ActionRead
|
||||
@@ -143,16 +146,21 @@ func toolSummaries(set core.ToolSet) []prompts.ChatTool {
|
||||
// else exists.
|
||||
var errToolUnavailable = errors.New("that tool isn't available right now")
|
||||
|
||||
// declared reports whether the running registry still declares exactly the
|
||||
// tool this call was offered for. A connector that stopped, or was replaced by
|
||||
// one declaring fewer tools, leaves the old call refused.
|
||||
func declared(d Deps, decl connectors.Declaration) bool {
|
||||
// errToolProhibited refuses a call to a tool chat never runs: one that changes
|
||||
// privileges, accesses secrets or doesn't declare its effects.
|
||||
var errToolProhibited = errors.New("chat can't run that tool: it changes privileges, accesses secrets or doesn't declare its effects. Use the dashboard")
|
||||
|
||||
// declared returns what the running registry declares now for the tool this
|
||||
// call was offered for. A connector that stopped, or was replaced by one
|
||||
// declaring fewer tools, leaves the old call refused. The effects are read
|
||||
// from the running declaration, not the one the call was offered with.
|
||||
func declared(d Deps, decl connectors.Declaration) (connectors.Declaration, bool) {
|
||||
for _, current := range d.Toolbox.Declarations() {
|
||||
if current.Connector == decl.Connector && current.Name == decl.Name && current.Kind == decl.Kind {
|
||||
return true
|
||||
return current, true
|
||||
}
|
||||
}
|
||||
return false
|
||||
return connectors.Declaration{}, false
|
||||
}
|
||||
|
||||
// call runs a lookup, or judges an action and runs it only as decided. An
|
||||
@@ -166,9 +174,13 @@ func (m *Module) call(ctx context.Context, d Deps, t store.Turn, decl connectors
|
||||
if err := m.allowed(ctx, d, need, governance.KindConnector, decl.Connector); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !declared(d, decl) {
|
||||
current, ok := declared(d, decl)
|
||||
if !ok {
|
||||
return "", errToolUnavailable
|
||||
}
|
||||
if current.Privileged.ProhibitedInChat() {
|
||||
return "", errToolProhibited
|
||||
}
|
||||
tool, ok := d.Toolbox.Tool(decl.Connector)
|
||||
if !ok {
|
||||
return "", errToolUnavailable
|
||||
|
||||
Reference in new issue
Block a user