323 lines
10 KiB
Go
323 lines
10 KiB
Go
package pier_test
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.golem15.com/golem15/summercms/modules/backpack"
|
|
"git.golem15.com/golem15/summercms/modules/bouncer"
|
|
"git.golem15.com/golem15/summercms/modules/pact"
|
|
"git.golem15.com/golem15/summercms/modules/party"
|
|
)
|
|
|
|
type warningTestInput struct {
|
|
ID int `json:"id" path:"id" jsonschema:"item id"`
|
|
}
|
|
|
|
// warningBackendPlugin registers a backend-guarded route set and MCP tools
|
|
// without implementing pact.HasTokenScopes.
|
|
type warningBackendPlugin struct {
|
|
id string
|
|
mwName string
|
|
mountMW bool
|
|
perms []pact.Permission
|
|
tools []pact.MCPTool
|
|
registerMW bool
|
|
}
|
|
|
|
func (p warningBackendPlugin) ID() string { return p.id }
|
|
func (p warningBackendPlugin) Requires() []string { return nil }
|
|
func (p warningBackendPlugin) Register(*backpack.App) error { return nil }
|
|
func (p warningBackendPlugin) Boot(*backpack.App) error { return nil }
|
|
func (p warningBackendPlugin) Permissions() []pact.Permission {
|
|
return p.perms
|
|
}
|
|
|
|
func (p warningBackendPlugin) Middlewares() map[string]pact.Middleware {
|
|
if !p.registerMW {
|
|
return nil
|
|
}
|
|
name := p.mwName
|
|
if name == "" {
|
|
name = "backend"
|
|
}
|
|
return map[string]pact.Middleware{
|
|
name: func(next http.Handler) http.Handler { return next },
|
|
}
|
|
}
|
|
|
|
func (p warningBackendPlugin) Routes(r pact.Router) error {
|
|
if !p.mountMW {
|
|
return nil
|
|
}
|
|
mw := p.mwName
|
|
if mw == "" {
|
|
mw = "backend"
|
|
}
|
|
noop := func(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"ok":true}`))
|
|
}
|
|
r.Group("/"+strings.ReplaceAll(p.id, ".", "-"), []string{mw}, func(g pact.Router) {
|
|
g.Get("/items/{id}", noop)
|
|
g.Post("/items/{id}", noop)
|
|
g.Delete("/items/{id}", noop)
|
|
})
|
|
return nil
|
|
}
|
|
|
|
func (p warningBackendPlugin) MCP() pact.MCP {
|
|
return pact.MCP{Tools: p.tools}
|
|
}
|
|
|
|
// warningScopedPlugin extends warningBackendPlugin with pact.HasTokenScopes.
|
|
type warningScopedPlugin struct {
|
|
warningBackendPlugin
|
|
scopes []pact.TokenScope
|
|
}
|
|
|
|
func (p warningScopedPlugin) TokenScopes() []pact.TokenScope { return p.scopes }
|
|
|
|
// warningUserGuard is a minimal non-backend ("user") MCP guard provider.
|
|
type warningUserGuard struct{}
|
|
|
|
func (warningUserGuard) MCPGuardName() string { return "user" }
|
|
func (warningUserGuard) MCPTokenGuarded(mw []string) bool {
|
|
for _, m := range mw {
|
|
if m == "user.auth" {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
func (warningUserGuard) MCPRequirements([]string) (pact.MCPRequirements, error) {
|
|
return pact.MCPRequirements{}, nil
|
|
}
|
|
func (warningUserGuard) MCPAllows(context.Context, pact.MCPSubject, pact.MCPRequirements, []string) (bool, error) {
|
|
return true, nil
|
|
}
|
|
func (warningUserGuard) MCPDispatch(_ context.Context, subject pact.MCPSubject, _ string) (pact.MCPDispatch, error) {
|
|
return pact.MCPDispatch{Principal: &bouncer.Principal{ID: subject.UserID}, Guards: []string{"user"}}, nil
|
|
}
|
|
|
|
func countWarningLines(logs string) int {
|
|
n := 0
|
|
for _, line := range strings.Split(logs, "\n") {
|
|
if strings.Contains(line, "level=WARN") && strings.Contains(line, "not covered by any non-* TokenScope") {
|
|
n++
|
|
}
|
|
}
|
|
return n
|
|
}
|
|
|
|
func TestUncoveredBackendWriteToolsWarning(t *testing.T) {
|
|
allMethods := []string{"GET", "HEAD", "POST", "PUT", "PATCH", "DELETE"}
|
|
|
|
t.Run("warns_once_per_plugin_for_uncovered_backend_writes", func(t *testing.T) {
|
|
buf := captureLogs(t)
|
|
app := pierAppWith(t, map[string]string{"mcp.yaml": "enabled: false\n"}, &acmeGuard{})
|
|
plugin := warningBackendPlugin{
|
|
id: "acme.blog",
|
|
mountMW: true,
|
|
registerMW: true,
|
|
perms: []pact.Permission{
|
|
{Code: "acme.blog.manage", Tab: "Blog", Label: "Manage"},
|
|
},
|
|
tools: []pact.MCPTool{
|
|
{
|
|
Name: "blog_get_item", Title: "Get item", Description: "Read item.",
|
|
Method: http.MethodGet, Path: "/acme-blog/items/{id}", Input: warningTestInput{},
|
|
Permissions: []string{"acme.blog.manage"},
|
|
},
|
|
{
|
|
Name: "blog_delete_item", Title: "Delete item", Description: "Delete item.",
|
|
Method: http.MethodDelete, Path: "/acme-blog/items/{id}", Input: warningTestInput{},
|
|
Permissions: []string{"acme.blog.manage"},
|
|
},
|
|
{
|
|
Name: "blog_create_item", Title: "Create item", Description: "Create item.",
|
|
Method: http.MethodPost, Path: "/acme-blog/items/{id}", Input: warningTestInput{},
|
|
Permissions: []string{"acme.blog.manage"},
|
|
},
|
|
},
|
|
}
|
|
host, err := mountPier(t, app, []party.Plugin{plugin})
|
|
if err != nil {
|
|
t.Fatalf("mountPier must not fail boot on uncovered write tools: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = host.pier.Close() })
|
|
|
|
logs := buf.String()
|
|
if got := countWarningLines(logs); got != 1 {
|
|
t.Fatalf("warning lines = %d, want 1; logs:\n%s", got, logs)
|
|
}
|
|
for _, want := range []string{
|
|
"plugin=acme.blog",
|
|
"pact.HasTokenScopes",
|
|
"blog_create_item",
|
|
"blog_delete_item",
|
|
} {
|
|
if !strings.Contains(logs, want) {
|
|
t.Fatalf("logs missing %q:\n%s", want, logs)
|
|
}
|
|
}
|
|
if strings.Index(logs, "blog_create_item") > strings.Index(logs, "blog_delete_item") {
|
|
t.Fatalf("expected sorted tool names in warning log:\n%s", logs)
|
|
}
|
|
if strings.Contains(logs, "blog_get_item") {
|
|
t.Fatalf("GET tool must not appear in uncovered write warning:\n%s", logs)
|
|
}
|
|
})
|
|
|
|
t.Run("silent_when_covered_by_token_scope", func(t *testing.T) {
|
|
buf := captureLogs(t)
|
|
app := pierAppWith(t, map[string]string{"mcp.yaml": "enabled: false\n"}, &acmeGuard{})
|
|
plugin := warningScopedPlugin{
|
|
warningBackendPlugin: warningBackendPlugin{
|
|
id: "acme.shop",
|
|
mountMW: true,
|
|
registerMW: true,
|
|
perms: []pact.Permission{
|
|
{Code: "acme.shop.manage", Tab: "Shop", Label: "Manage"},
|
|
},
|
|
tools: []pact.MCPTool{
|
|
{
|
|
Name: "shop_create_item", Title: "Create item", Description: "Create item.",
|
|
Method: http.MethodPost, Path: "/acme-shop/items/{id}", Input: warningTestInput{},
|
|
Permissions: []string{"acme.shop.manage"},
|
|
},
|
|
},
|
|
},
|
|
scopes: []pact.TokenScope{
|
|
{
|
|
Name: "shop", Label: "Shop", Description: "Manage shop",
|
|
Permissions: []string{"acme.shop.*"}, Methods: allMethods,
|
|
},
|
|
},
|
|
}
|
|
host, err := mountPier(t, app, []party.Plugin{plugin})
|
|
if err != nil {
|
|
t.Fatalf("mountPier: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = host.pier.Close() })
|
|
|
|
if got := countWarningLines(buf.String()); got != 0 {
|
|
t.Fatalf("expected 0 warning lines, got %d:\n%s", got, buf.String())
|
|
}
|
|
})
|
|
|
|
t.Run("silent_when_covered_by_companion_plugin_scope", func(t *testing.T) {
|
|
buf := captureLogs(t)
|
|
app := pierAppWith(t, map[string]string{"mcp.yaml": "enabled: false\n"}, &acmeGuard{})
|
|
core := warningBackendPlugin{
|
|
id: "acme.core",
|
|
mountMW: true,
|
|
registerMW: true,
|
|
perms: []pact.Permission{
|
|
{Code: "acme.core.write", Tab: "Core", Label: "Write"},
|
|
},
|
|
tools: []pact.MCPTool{
|
|
{
|
|
Name: "core_create_item", Title: "Create item", Description: "Create item.",
|
|
Method: http.MethodPost, Path: "/acme-core/items/{id}", Input: warningTestInput{},
|
|
Permissions: []string{"acme.core.write"},
|
|
},
|
|
},
|
|
}
|
|
companion := warningScopedPlugin{
|
|
warningBackendPlugin: warningBackendPlugin{id: "acme.companion"},
|
|
scopes: []pact.TokenScope{
|
|
{
|
|
Name: "core:write", Label: "Core write", Description: "Write core items",
|
|
Permissions: []string{"acme.core.*"}, Methods: allMethods,
|
|
},
|
|
},
|
|
}
|
|
host, err := mountPier(t, app, []party.Plugin{core, companion})
|
|
if err != nil {
|
|
t.Fatalf("mountPier: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = host.pier.Close() })
|
|
|
|
if got := countWarningLines(buf.String()); got != 0 {
|
|
t.Fatalf("expected 0 warning lines when companion covers write tool, got %d:\n%s", got, buf.String())
|
|
}
|
|
})
|
|
|
|
t.Run("warns_when_write_tool_has_empty_permissions", func(t *testing.T) {
|
|
buf := captureLogs(t)
|
|
app := pierAppWith(t, map[string]string{"mcp.yaml": "enabled: false\n"}, &acmeGuard{})
|
|
plugin := warningScopedPlugin{
|
|
warningBackendPlugin: warningBackendPlugin{
|
|
id: "acme.emptyperm",
|
|
mountMW: true,
|
|
registerMW: true,
|
|
perms: []pact.Permission{
|
|
{Code: "acme.emptyperm.manage", Tab: "Empty", Label: "Manage"},
|
|
},
|
|
tools: []pact.MCPTool{
|
|
{
|
|
Name: "emptyperm_create_item", Title: "Create item", Description: "Create item.",
|
|
Method: http.MethodPost, Path: "/acme-emptyperm/items/{id}", Input: warningTestInput{},
|
|
Permissions: nil,
|
|
},
|
|
},
|
|
},
|
|
scopes: []pact.TokenScope{
|
|
{
|
|
Name: "emptyperm", Label: "Emptyperm", Description: "Manage emptyperm",
|
|
Permissions: []string{"acme.emptyperm.*"}, Methods: allMethods,
|
|
},
|
|
},
|
|
}
|
|
host, err := mountPier(t, app, []party.Plugin{plugin})
|
|
if err != nil {
|
|
t.Fatalf("mountPier: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = host.pier.Close() })
|
|
|
|
logs := buf.String()
|
|
if got := countWarningLines(logs); got != 1 {
|
|
t.Fatalf("warning lines = %d, want 1; logs:\n%s", got, logs)
|
|
}
|
|
if !strings.Contains(logs, "emptyperm_create_item") || !strings.Contains(logs, "plugin=acme.emptyperm") {
|
|
t.Fatalf("expected warning naming emptyperm_create_item and acme.emptyperm:\n%s", logs)
|
|
}
|
|
})
|
|
|
|
t.Run("skips_user_guarded_write_tools", func(t *testing.T) {
|
|
buf := captureLogs(t)
|
|
app := pierAppWith(t, map[string]string{"mcp.yaml": "enabled: false\n"}, nil)
|
|
reg := pact.NewMCPGuards()
|
|
if err := reg.Register(warningUserGuard{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := app.Publish(reg); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
plugin := warningBackendPlugin{
|
|
id: "acme.portal",
|
|
mwName: "user.auth",
|
|
mountMW: true,
|
|
registerMW: true,
|
|
tools: []pact.MCPTool{
|
|
{
|
|
Name: "portal_create_item", Title: "Create item", Description: "Create user item.",
|
|
Method: http.MethodPost, Path: "/acme-portal/items/{id}", Input: warningTestInput{},
|
|
},
|
|
},
|
|
}
|
|
host, err := mountPier(t, app, []party.Plugin{plugin})
|
|
if err != nil {
|
|
t.Fatalf("mountPier: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = host.pier.Close() })
|
|
|
|
if got := countWarningLines(buf.String()); got != 0 {
|
|
t.Fatalf("expected 0 warning lines for user-guarded write tool, got %d:\n%s", got, buf.String())
|
|
}
|
|
})
|
|
}
|