Files
archie/modules/connectors/privileged_test.go
T
sam bc72655c8e 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.
2026-11-21 21:28:06 +11:00

110 lines
4.1 KiB
Go

package connectors
import (
"context"
"encoding/json/jsontext"
"testing"
"google.golang.org/grpc"
pb "github.com/samcharles93/archie-core/internal/contracts/connector/v1"
)
// effectsClient declares one action with the given effect and resolves it to
// another.
type effectsClient struct {
pb.ConnectorServiceClient
declared, resolved *pb.Effect
}
func (effectsClient) Configure(context.Context, *pb.ConfigureRequest, ...grpc.CallOption) (*pb.ConfigureResponse, error) {
return &pb.ConfigureResponse{}, nil
}
func (c effectsClient) Describe(context.Context, *pb.DescribeRequest, ...grpc.CallOption) (*pb.DescribeResponse, error) {
return &pb.DescribeResponse{Contract: contractVersion, Actions: []*pb.ActionType{{Name: "grant", Effect: c.declared, Idempotent: true}}}, nil
}
func (c effectsClient) Resolve(context.Context, *pb.ResolveRequest, ...grpc.CallOption) (*pb.ResolveResponse, error) {
return &pb.ResolveResponse{Effect: c.resolved}, nil
}
// TestResolveKeepsWhatAnActionDeclared guards the chat prohibition's input: a
// resolution can add a privileged effect for one input, but it can't clear
// what the action declared or declare effects for an action that never did.
func TestResolveKeepsWhatAnActionDeclared(t *testing.T) {
tests := []struct {
name string
declared, resolved *pb.Effect
want PrivilegedEffects
}{
{
name: "the declaration holds when the resolution says nothing",
declared: &pb.Effect{EffectsDeclared: true, PrivilegeChange: true}, resolved: &pb.Effect{},
want: PrivilegedEffects{Declared: true, PrivilegeChange: true},
},
{
name: "a resolution adds a privileged effect",
declared: &pb.Effect{EffectsDeclared: true}, resolved: &pb.Effect{SecretAccess: true},
want: PrivilegedEffects{Declared: true, SecretAccess: true},
},
{
name: "a resolution can't declare for an action that never did",
declared: &pb.Effect{}, resolved: &pb.Effect{EffectsDeclared: true},
want: PrivilegedEffects{},
},
{
name: "a resolution can't clear a declared effect",
declared: &pb.Effect{EffectsDeclared: true, SecretAccess: true}, resolved: &pb.Effect{EffectsDeclared: true},
want: PrivilegedEffects{Declared: true, SecretAccess: true},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c, err := open(t.Context(), "iam", effectsClient{declared: tt.declared, resolved: tt.resolved}, nil, nil)
if err != nil {
t.Fatal(err)
}
res, err := c.Resolve(t.Context(), "grant", jsontext.Value(`{}`))
if err != nil {
t.Fatal(err)
}
if res.Effect.Privileged != tt.want {
t.Fatalf("resolved effects = %+v, want %+v", res.Effect.Privileged, tt.want)
}
if got := c.Declarations()[0].Privileged; got.Declared != tt.declared.GetEffectsDeclared() {
t.Fatalf("declaration effects = %+v, want declared = %v", got, tt.declared.GetEffectsDeclared())
}
})
}
}
// TestServerToolsInheritTheServersDeclaredEffects guards deny by default for an
// MCP server: until an operator declares its effects every tool is undeclared,
// and afterwards every tool carries the declaration.
func TestServerToolsInheritTheServersDeclaredEffects(t *testing.T) {
tests := []struct {
name string
privileged PrivilegedEffects
wantChat bool
}{
{name: "unset", privileged: (*ServerEffects)(nil).privileged(), wantChat: false},
{name: "declared none", privileged: (&ServerEffects{}).privileged(), wantChat: true},
{name: "declared privilege change", privileged: (&ServerEffects{PrivilegeChange: true}).privileged(), wantChat: false},
{name: "declared secret access", privileged: (&ServerEffects{SecretAccess: true}).privileged(), wantChat: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
spec := ServerSpec{Name: "metrics", Privileged: tt.privileged, Tools: []ToolDecl{{Name: "query", Kind: KindLookup}}}
s := inMemory(t, spec, fakeTools(t, "query"))
if err := s.Connect(t.Context()); err != nil {
t.Fatal(err)
}
t.Cleanup(s.Close)
if got := !s.Declarations()[0].Privileged.ProhibitedInChat(); got != tt.wantChat {
t.Fatalf("offered to chat = %v, want %v", got, tt.wantChat)
}
})
}
}