From 6d2275cc2604822410731a12c1d808bc2897b2df Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 2 Nov 2026 19:41:41 +0300 Subject: [PATCH] RBAC: filter the application map for restricted roles The per-application map (clients / dependencies) now goes through the same application view check as the overview map, so a role scoped to a namespace only sees applications it may view. Agent: claude Co-Authored-By: Claude Fable 5.1 --- api/api.go | 3 ++- api/ctx.go | 34 ++++++++++++++++++++++++++++++++ api/rbac_filter_test.go | 43 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/api/api.go b/api/api.go index 4d5a974ed..5477febbe 100644 --- a/api/api.go +++ b/api/api.go @@ -1245,7 +1245,8 @@ func (api *Api) Application(w http.ResponseWriter, r *http.Request, u *db.User) app.AddReport(model.AuditReportProfiling, &model.Widget{Profiling: &model.Profiling{ApplicationId: app.Id}, Width: "100%"}) app.AddReport(model.AuditReportTracing, &model.Widget{Tracing: &model.Tracing{ApplicationId: app.Id}, Width: "100%"}) - utils.WriteJson(w, api.WithContext(u, project, cacheStatus, world, views.Application(project, world, app))) + view := api.filterAppMapForUser(u, projectId, world, views.Application(project, world, app)) + utils.WriteJson(w, api.WithContext(u, project, cacheStatus, world, view)) } func (api *Api) Incidents(w http.ResponseWriter, r *http.Request, u *db.User) { diff --git a/api/ctx.go b/api/ctx.go index 1d9a5d67a..5be81aaec 100644 --- a/api/ctx.go +++ b/api/ctx.go @@ -3,6 +3,7 @@ package api import ( "fmt" + "github.com/coroot/coroot/api/views/application" "github.com/coroot/coroot/api/views/overview" "github.com/coroot/coroot/cache" "github.com/coroot/coroot/db" @@ -325,6 +326,39 @@ func (api *Api) filterOverviewForUser(u *db.User, projectId string, w *model.Wor return ov } +// filterAppMapForUser drops the neighbour cards (clients / dependencies) of +// an application map that the user may not view. The Application handler +// only checks the application itself, so a namespace-scoped handoff user +// still saw every application its database talks to, by name, namespace +// and status: the Kubernetes API, the operators, the Coroot agent. The +// Overview map is filtered the same way (filterOverviewForUser). +func (api *Api) filterAppMapForUser(u *db.User, projectId string, w *model.World, v *application.View) *application.View { + if v == nil || v.AppMap == nil || u == nil || w == nil { + return v + } + keep := func(apps []*application.Application) []*application.Application { + out := make([]*application.Application, 0, len(apps)) + for _, a := range apps { + if a == nil { + continue + } + var allowed bool + if app := w.GetApplication(a.Id); app != nil { + allowed = api.canViewApplication(u, projectId, app) + } else { + allowed = api.IsAllowed(u, rbac.Actions.Project(projectId).Application(a.Category, a.Id.Namespace, a.Id.Kind, a.Id.Name).View()) + } + if allowed { + out = append(out, a) + } + } + return out + } + v.AppMap.Clients = keep(v.AppMap.Clients) + v.AppMap.Dependencies = keep(v.AppMap.Dependencies) + return v +} + func (api *Api) renderIncidents(u *db.User, projectId string, w *model.World) map[model.ApplicationCategory]int { res := map[model.ApplicationCategory]int{} if w == nil { diff --git a/api/rbac_filter_test.go b/api/rbac_filter_test.go index 458b44fde..5a752618c 100644 --- a/api/rbac_filter_test.go +++ b/api/rbac_filter_test.go @@ -3,6 +3,7 @@ package api import ( "testing" + "github.com/coroot/coroot/api/views/application" "github.com/coroot/coroot/api/views/overview" "github.com/coroot/coroot/db" "github.com/coroot/coroot/model" @@ -81,3 +82,45 @@ func TestRenderSearchFiltersByApplicationPermission(t *testing.T) { assert.Len(t, search.Applications, 2) assert.False(t, api.hasRestrictedAppAccess(viewer, projectId)) } + +func TestFilterAppMapForUserDropsForeignNeighbours(t *testing.T) { + projectId := "proj1" + roleName := rbac.RoleName("kubero-twenty-production") + roles := staticRoles{ + rbac.NewRole(roleName, + rbac.NewPermission(rbac.ScopeApplication, rbac.ActionView, rbac.Object{ + "project_id": projectId, + "application_namespace": "twenty-production", + "application_kind": "*", + "application_name": "*", + }), + ), + } + api := &Api{roles: roles} + user := &db.User{Email: "kubero+u1+twenty-production@handoff.local", Roles: []rbac.RoleName{roleName}} + + web := model.NewApplication(model.NewApplicationId(projectId, "twenty-production", model.ApplicationKindDeployment, "twenty-kuberoapp-web")) + agent := model.NewApplication(model.NewApplicationId(projectId, "coroot", model.ApplicationKindDeployment, "coroot-cluster-agent")) + operator := model.NewApplication(model.NewApplicationId(projectId, "operators", model.ApplicationKindDeployment, "cloudnative-pg")) + // The Kubernetes API as Coroot sees it: no namespace. + k3s := model.NewApplication(model.NewApplicationId(projectId, "", model.ApplicationKindExternalService, "k3s")) + w := &model.World{Applications: map[model.ApplicationId]*model.Application{ + web.Id: web, agent.Id: agent, operator.Id: operator, k3s.Id: k3s, + }} + view := func() *application.View { + return &application.View{AppMap: &application.AppMap{ + Clients: []*application.Application{{Id: agent.Id}, {Id: web.Id}, {Id: operator.Id}}, + Dependencies: []*application.Application{{Id: k3s.Id}}, + }} + } + + v := api.filterAppMapForUser(user, projectId, w, view()) + require.Len(t, v.AppMap.Clients, 1) + assert.Equal(t, web.Id, v.AppMap.Clients[0].Id) + assert.Empty(t, v.AppMap.Dependencies) + + // No user (auth off): everything stays. + v = api.filterAppMapForUser(nil, projectId, w, view()) + assert.Len(t, v.AppMap.Clients, 3) + assert.Len(t, v.AppMap.Dependencies, 1) +}