feat(36-01): export cabana.TokenScopeCovers and warn on uncovered backend write MCP tools in pier.Mount
This commit is contained in:
@@ -5,6 +5,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||||
|
"git.golem15.com/golem15/summercms/modules/bouncer"
|
||||||
|
"git.golem15.com/golem15/summercms/modules/party"
|
||||||
"git.golem15.com/golem15/summercms/modules/phrasebook"
|
"git.golem15.com/golem15/summercms/modules/phrasebook"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -61,3 +63,52 @@ func TokenScopeLabels(ctx context.Context, app *backpack.App, names []string) []
|
|||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func tokenScopeCatalogFor(app *backpack.App, plugins []party.Plugin) *tokenScopeCatalog {
|
||||||
|
if guard := publishedResolverGuard(app); guard != nil && guard.reg != nil && guard.reg.tokenScopes != nil {
|
||||||
|
return guard.reg.tokenScopes
|
||||||
|
}
|
||||||
|
if len(plugins) > 0 {
|
||||||
|
reg, err := compileTokenCatalog(plugins)
|
||||||
|
if err == nil && reg != nil {
|
||||||
|
return reg.tokenScopes
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
reg, err := tokenCatalogForApp(app)
|
||||||
|
if err == nil && reg != nil {
|
||||||
|
return reg.tokenScopes
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TokenScopeCovers reports whether at least one non-"*" personal API token
|
||||||
|
// scope allows method and covers every permission code in permissions. It uses
|
||||||
|
// the activated cabana catalog on app when available, else compiles from
|
||||||
|
// plugins (falling back to party.Activated). Returns false when permissions is
|
||||||
|
// empty on a non-read-only method, when method is unsupported, or when no
|
||||||
|
// non-"*" scope covers every code.
|
||||||
|
func TokenScopeCovers(app *backpack.App, plugins []party.Plugin, method string, permissions []string) bool {
|
||||||
|
cat := tokenScopeCatalogFor(app, plugins)
|
||||||
|
if cat == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
m := strings.ToUpper(strings.TrimSpace(method))
|
||||||
|
if m == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
var scopes [][]string
|
||||||
|
for _, scope := range cat.ordered {
|
||||||
|
if scope.Name == scopeAll {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !scope.Methods[m] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if scope.Full {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
scopes = append(scopes, scope.Permissions)
|
||||||
|
}
|
||||||
|
return scopeCoversAll(&bouncer.ScopeLimit{Scopes: scopes}, permissions)
|
||||||
|
}
|
||||||
|
|||||||
@@ -113,6 +113,7 @@ func Mount(host Host, app *backpack.App, plugins []party.Plugin) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
warnUncoveredBackendWriteTools(app, plugins, cat)
|
||||||
frontend, err := frontendGuardName(reg)
|
frontend, err := frontendGuardName(reg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
46
modules/pier/uncovered_scopes_warning.go
Normal file
46
modules/pier/uncovered_scopes_warning.go
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package pier
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"slices"
|
||||||
|
|
||||||
|
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||||
|
"git.golem15.com/golem15/summercms/modules/cabana"
|
||||||
|
"git.golem15.com/golem15/summercms/modules/party"
|
||||||
|
)
|
||||||
|
|
||||||
|
// warnUncoveredBackendWriteTools checks every compiled backend non-GET/HEAD
|
||||||
|
// MCP tool against all registered non-"*" TokenScopes and logs one structured
|
||||||
|
// Warn per plugin with uncovered tools so operators notice plugins missing
|
||||||
|
// pact.HasTokenScopes. It never fails boot.
|
||||||
|
func warnUncoveredBackendWriteTools(app *backpack.App, plugins []party.Plugin, cat *catalog) {
|
||||||
|
if cat == nil || len(cat.tools) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
byPlugin := map[string][]string{}
|
||||||
|
var pluginOrder []string
|
||||||
|
for _, t := range cat.tools {
|
||||||
|
if t.guardName != BackendGuard {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if t.method == http.MethodGet || t.method == http.MethodHead {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if cabana.TokenScopeCovers(app, plugins, t.method, t.decl.Permissions) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, seen := byPlugin[t.pluginID]; !seen {
|
||||||
|
pluginOrder = append(pluginOrder, t.pluginID)
|
||||||
|
}
|
||||||
|
byPlugin[t.pluginID] = append(byPlugin[t.pluginID], t.name)
|
||||||
|
}
|
||||||
|
for _, pluginID := range pluginOrder {
|
||||||
|
tools := byPlugin[pluginID]
|
||||||
|
slices.Sort(tools)
|
||||||
|
logger().Warn(
|
||||||
|
"pier: plugin registers backend write MCP tools not covered by any non-* TokenScope; implement pact.HasTokenScopes so scoped tokens and MCP OAuth grants can call them",
|
||||||
|
"plugin", pluginID,
|
||||||
|
"tools", tools,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user