Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions internal/httpapi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2012,6 +2012,9 @@ func (s *Server) handleGetServers(w http.ResponseWriter, r *http.Request) {
if viewAsEval != nil {
serverValues = s.limitServersToProfile(r.Context(), serverValues, viewAs, viewAsEval)
statsValue = statsForServers(serverValues)
} else if limited, ok := s.limitServersToCallerPin(r.Context(), serverValues); ok {
serverValues = limited
statsValue = statsForServers(serverValues)
}

response := contracts.GetServersResponse{
Expand Down Expand Up @@ -2050,6 +2053,9 @@ func (s *Server) handleGetServers(w http.ResponseWriter, r *http.Request) {
if viewAsEval != nil {
servers = s.limitServersToProfile(r.Context(), servers, viewAs, viewAsEval)
stats = statsForServers(servers)
} else if limited, ok := s.limitServersToCallerPin(r.Context(), servers); ok {
servers = limited
stats = statsForServers(servers)
}

response := contracts.GetServersResponse{
Expand Down
30 changes: 30 additions & 0 deletions internal/httpapi/servers_profile_caller_pin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,33 @@ func TestServersProfileCallerPin(t *testing.T) {
})
}
}

// Issue #1682: a bare GET /servers (no ?profile=) by a profile-pinned token must
// be held to the caller's own pin, as GET /tools is. Before the fix the token's
// AllowedServers ("*") was the only gate, so every configured server listed.
func TestServersBareListHeldToCallerPin(t *testing.T) {
for _, branch := range []struct {
name string
withManagement bool
}{{"management service", true}, {"legacy fallback", false}} {
t.Run(branch.name, func(t *testing.T) {
srv, token := callerPinServer(t, branch.withManagement)

data := callerPinGet(t, srv, "/api/v1/servers", token)
assert.Equal(t, map[string]float64{"github": 1}, serverRows(data),
"notion and filesystem are outside the caller's pin; github counts only read_* tools")

stats := data["stats"].(map[string]interface{})
assert.Equal(t, float64(1), stats["total_servers"], "stats must narrow with the list")
assert.Equal(t, float64(1), stats["total_tools"])

// Bare listing equals the listing the same caller gets for its own pin.
own := serverRows(callerPinGet(t, srv, "/api/v1/servers?profile=work-readonly", token))
assert.Equal(t, own, serverRows(data))

// The administrator path is unchanged.
admin := serverRows(callerPinGet(t, srv, "/api/v1/servers", scopeAdminAPIKey))
assert.Len(t, admin, 3, "an administrator still lists every configured server")
})
}
}
26 changes: 26 additions & 0 deletions internal/httpapi/view_as.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,32 @@ func (s *Server) limitServersToProfile(ctx context.Context, servers []contracts.
return out
}

// pinOnlyEvaluator admits every tool: it lets limitServersToProfile apply only
// the caller's own pin (via filterProfileToolRows) when no profile was asked for.
type pinOnlyEvaluator struct{}

func (pinOnlyEvaluator) Evaluate(_, _ string) profile.AccessVerdict {
return profile.AccessVerdict{Visible: true, Callable: true}
}

// limitServersToCallerPin holds a profile-pinned agent token to its own pin on a
// bare GET /servers (no ?profile=), the same way GET /tools already does. A
// pinned token takes its scope from the profile, so its AllowedServers is
// usually "*" and visibleServers alone would list every configured server. The
// result equals GET /servers?profile=<the caller's own pin>. ok is false (and
// servers is untouched) for callers without a pin, so admins and legacy-scoped
// tokens behave exactly as before.
func (s *Server) limitServersToCallerPin(ctx context.Context, servers []contracts.Server) (limited []contracts.Server, ok bool) {
if !callerHasProfilePin(ctx) {
return servers, false
}
req := &viewAsRequest{subject: profile.AccessSubject{
Kind: profile.AccessSubjectProfile,
Profile: auth.AuthContextFromContext(ctx).ProfilePin,
}}
return s.limitServersToProfile(ctx, servers, req, pinOnlyEvaluator{}), true
}

// callerHasProfilePin reports whether the request's caller is a
// non-administrator bound to a profile (an agent token with a pin).
func callerHasProfilePin(ctx context.Context) bool {
Expand Down
Loading