Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
Keep token management inside the caller's organizations
rcourtmanfb4feee411ea
diff --git a/internal/api/security_token_org_containment_test.go b/internal/api/security_token_org_containment_test.gonew file mode 100644index 0000000000..820509efc9--- /dev/null+++ b/internal/api/security_token_org_containment_test.go@@ -0,0 +1,331 @@+package api++import (+ "encoding/json"+ "net/http"+ "net/http/httptest"+ "strings"+ "testing"++ "github.com/rcourtman/pulse-go-rewrite/internal/config"+)++// The token inventory is global. A token bound to one organization must not+// list, read, rename, delete or rotate (and so obtain the raw value of) a+// token that belongs to another organization. Instance-wide full-access+// tokens keep managing every token.+type orgTokenFixture struct {+ router *Router+ cfg *config.Config+ // orgA is a full-access token bound to org-a; def is a full-access token+ // in the default organization; orgB belongs to org-b.+ orgA, def, orgB config.APITokenRecord+}++func newOrgTokenFixture(t *testing.T) *orgTokenFixture {+ t.Helper()+ mk := func(raw, name, org string, scopes []string) config.APITokenRecord {+ record, err := config.NewAPITokenRecord(raw, name, scopes)+ if err != nil {+ t.Fatalf("new token record %s: %v", name, err)+ }+ record.OrgID = org+ return *record+ }+ f := &orgTokenFixture{+ orgA: mk("org-a-wildcard-token.12345678", "org-a", "org-a", []string{config.ScopeWildcard}),+ def: mk("default-wildcard-token.12345678", "default", "default", []string{config.ScopeWildcard}),+ orgB: mk("org-b-scoped-token.12345678", "org-b", "org-b", []string{config.ScopeMonitoringRead}),+ }+ dataDir := t.TempDir()+ f.cfg = &config.Config{DataPath: dataDir, ConfigPath: dataDir, APITokens: []config.APITokenRecord{f.orgA, f.def, f.orgB}}+ f.cfg.SortAPITokens()+ f.router = &Router{config: f.cfg, persistence: config.NewConfigPersistence(dataDir)}+ return f+}++// call runs a token-management handler as the given caller token.+func (f *orgTokenFixture) call(caller *config.APITokenRecord, method, path string, handler func(http.ResponseWriter, *http.Request)) *httptest.ResponseRecorder {+ req := httptest.NewRequest(method, path, strings.NewReader(`{"name":"renamed"}`))+ attachAPITokenRecord(req, caller)+ rec := httptest.NewRecorder()+ handler(rec, req)+ return rec+}++func (f *orgTokenFixture) ids() map[string]bool {+ config.Mu.RLock()+ defer config.Mu.RUnlock()+ out := map[string]bool{}+ for _, record := range f.cfg.APITokens {+ out[record.ID] = true+ }+ return out+}++func TestTokenManagementHidesOtherOrganizationsTokensFromBoundCallers(t *testing.T) {+ f := newOrgTokenFixture(t)++ rec := f.call(&f.orgA, http.MethodGet, "/api/security/tokens", f.router.handleListAPITokens)+ if rec.Code != http.StatusOK {+ t.Fatalf("list status = %d (%s)", rec.Code, rec.Body.String())+ }+ var list struct {+ Tokens []struct {+ ID string `json:"id"`+ } `json:"tokens"`+ }+ if err := json.Unmarshal(rec.Body.Bytes(), &list); err != nil {+ t.Fatalf("decode list: %v", err)+ }+ if len(list.Tokens) != 1 || list.Tokens[0].ID != f.orgA.ID {+ t.Fatalf("org-a caller should list only its own organization's tokens, got %+v", list.Tokens)+ }++ for _, target := range []config.APITokenRecord{f.def, f.orgB} {+ rec = f.call(&f.orgA, http.MethodGet, "/api/security/tokens/"+target.ID, f.router.handleGetAPIToken)+ if rec.Code != http.StatusNotFound {+ t.Errorf("GET other-org token %s = %d, want 404", target.Name, rec.Code)+ }+ }++ rec = f.call(&f.orgA, http.MethodGet, "/api/security/tokens/"+f.orgA.ID, f.router.handleGetAPIToken)+ if rec.Code != http.StatusOK {+ t.Errorf("GET own-organization token = %d, want 200", rec.Code)+ }+}++func TestTokenManagementRefusesChangesAcrossOrganizations(t *testing.T) {+ f := newOrgTokenFixture(t)+ before := f.ids()++ for _, tc := range []struct {+ name string+ method string+ path string+ handler func(http.ResponseWriter, *http.Request)+ }{+ {"rename default token", http.MethodPatch, "/api/security/tokens/" + f.def.ID, f.router.handleUpdateAPIToken},+ {"delete default token", http.MethodDelete, "/api/security/tokens/" + f.def.ID, f.router.handleDeleteAPIToken},+ {"rotate default token", http.MethodPost, "/api/security/tokens/" + f.def.ID + "/rotate", f.router.handleRotateAPIToken},+ {"rename org-b token", http.MethodPatch, "/api/security/tokens/" + f.orgB.ID, f.router.handleUpdateAPIToken},+ {"delete org-b token", http.MethodDelete, "/api/security/tokens/" + f.orgB.ID, f.router.handleDeleteAPIToken},+ {"rotate org-b token", http.MethodPost, "/api/security/tokens/" + f.orgB.ID + "/rotate", f.router.handleRotateAPIToken},+ } {+ rec := f.call(&f.orgA, tc.method, tc.path, tc.handler)+ if rec.Code != http.StatusNotFound {+ t.Errorf("%s of another organization's token = %d (%s), want 404", tc.name, rec.Code, rec.Body.String())+ }+ if strings.Contains(rec.Body.String(), `"token"`) {+ t.Errorf("%s response leaked a credential: %s", tc.name, rec.Body.String())+ }+ }++ after := f.ids()+ if len(after) != len(before) {+ t.Fatalf("token inventory changed: before %v after %v", before, after)+ }+ for id := range before {+ if !after[id] {+ t.Fatalf("token %s disappeared after refused changes", id)+ }+ }+ config.Mu.RLock()+ defer config.Mu.RUnlock()+ for _, record := range f.cfg.APITokens {+ if record.Name == "renamed" {+ t.Fatalf("token %s was renamed across organizations", record.ID)+ }+ }+}++func TestTokenManagementStillWorksWithinAnOrganization(t *testing.T) {+ f := newOrgTokenFixture(t)++ rec := f.call(&f.orgA, http.MethodPost, "/api/security/tokens/"+f.orgA.ID+"/rotate", f.router.handleRotateAPIToken)+ if rec.Code != http.StatusOK {+ t.Fatalf("an org-a caller rotating an org-a token = %d (%s), want 200", rec.Code, rec.Body.String())+ }+ var rotated struct {+ Token string `json:"token"`+ Record struct {+ ID string `json:"id"`+ OrgID string `json:"orgId"`+ } `json:"record"`+ }+ if err := json.Unmarshal(rec.Body.Bytes(), &rotated); err != nil {+ t.Fatalf("decode rotation response: %v", err)+ }+ if strings.TrimSpace(rotated.Token) == "" {+ t.Fatalf("rotation returned no credential: %s", rec.Body.String())+ }+ if ids := f.ids(); ids[f.orgA.ID] {+ t.Fatal("the rotated token's old ID is still in the inventory")+ }+ config.Mu.RLock()+ defer config.Mu.RUnlock()+ for _, record := range f.cfg.APITokens {+ if record.Name == f.orgA.Name && record.ID != f.orgA.ID && record.OrgID != "org-a" {+ t.Fatalf("rotated token lost its organization binding: %q", record.OrgID)+ }+ }+}++func TestInstanceWideTokenStillManagesEveryOrganization(t *testing.T) {+ f := newOrgTokenFixture(t)++ rec := f.call(&f.def, http.MethodPost, "/api/security/tokens/"+f.orgB.ID+"/rotate", f.router.handleRotateAPIToken)+ if rec.Code != http.StatusOK {+ t.Fatalf("an instance-wide token rotating an org-b token = %d (%s), want 200", rec.Code, rec.Body.String())+ }+}++func TestCallerTokenMayManageRules(t *testing.T) {+ mk := func(scopes []string, org string, orgs ...string) *config.APITokenRecord {+ record, err := config.NewAPITokenRecord("rule-token-"+org+".12345678", org, scopes)+ if err != nil {+ t.Fatalf("new token record: %v", err)+ }+ record.OrgID = org+ record.OrgIDs = orgs+ return record+ }+ wild := []string{config.ScopeWildcard}+ scoped := []string{config.ScopeSettingsWrite}+ cases := []struct {+ name string+ caller, target *config.APITokenRecord+ want bool+ }{+ {"a session caller manages anything", nil, mk(wild, "org-a"), true},+ {"instance-wide manages other orgs", mk(wild, "default"), mk(wild, "org-a"), true},+ {"org-bound wildcard cannot manage the default org", mk(wild, "org-a"), mk(wild, "default"), false},+ {"org-bound wildcard cannot manage another org", mk(wild, "org-a"), mk(scoped, "org-b"), false},+ {"org-bound wildcard manages its own org", mk(wild, "org-a"), mk(scoped, "org-a"), true},+ {"scoped default caller manages default tokens", mk(scoped, "default"), mk(scoped, "default"), true},+ {"scoped default caller cannot reach org-a", mk(scoped, "default"), mk(scoped, "org-a"), false},+ {"multi-org caller covers both of its orgs", mk(scoped, "", "org-a", "org-b"), mk(scoped, "", "org-a", "org-b"), true},+ {"multi-org caller cannot cover a third org", mk(scoped, "", "org-a", "org-b"), mk(scoped, "", "org-a", "org-c"), false},+ {"org list takes precedence over a stale org id", mk(wild, "org-a", "default"), mk(wild, "default"), true},+ {"multi-org wildcard caller cannot touch an instance-wide target", mk(wild, "", "default", "org-a"), mk(wild, "default"), false},+ {"multi-org wildcard caller manages a scoped default-org target", mk(wild, "", "default", "org-a"), mk(scoped, "default"), true},+ {"instance-wide caller manages an instance-wide target", mk(wild, "default"), mk(wild, "default"), true},+ {"a scoped caller is left to the scope checks for an instance-wide target", mk(scoped, "default"), mk(wild, "default"), true},+ }+ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ if got := callerTokenMayManage(tc.caller, tc.target); got != tc.want {+ t.Fatalf("callerTokenMayManage = %v, want %v", got, tc.want)+ }+ })+ }+}++func TestMultiOrganizationCallerCannotReachOrMintInstanceWideTokens(t *testing.T) {+ f := newOrgTokenFixture(t)+ multi, err := config.NewAPITokenRecord("multi-org-wildcard.12345678", "multi", []string{config.ScopeWildcard})+ if err != nil {+ t.Fatalf("new token record: %v", err)+ }+ multi.OrgID = ""+ multi.OrgIDs = []string{"default", "org-a"}+ config.Mu.Lock()+ f.cfg.APITokens = append(f.cfg.APITokens, *multi)+ f.cfg.SortAPITokens()+ config.Mu.Unlock()++ // Its organizations are a superset of the default-only wildcard target, but+ // rotating that target would hand it an instance-wide credential.+ rec := f.call(multi, http.MethodPost, "/api/security/tokens/"+f.def.ID+"/rotate", f.router.handleRotateAPIToken)+ if rec.Code != http.StatusNotFound || strings.Contains(rec.Body.String(), `"token"`) {+ t.Fatalf("multi-org caller rotating an instance-wide token = %d (%s), want 404 without a credential", rec.Code, rec.Body.String())+ }++ inventorySize := func() int {+ config.Mu.RLock()+ defer config.Mu.RUnlock()+ return len(f.cfg.APITokens)+ }+ create := func(caller *config.APITokenRecord, body string) *httptest.ResponseRecorder {+ req := httptest.NewRequest(http.MethodPost, "/api/security/tokens", strings.NewReader(body))+ attachAPITokenRecord(req, caller)+ rec := httptest.NewRecorder()+ f.router.handleCreateAPIToken(rec, req)+ return rec+ }+ // Omitting scopes means full access; in the default organization that is an+ // instance-wide token.+ before := inventorySize()+ if rec := create(multi, `{"name":"escape"}`); rec.Code != http.StatusForbidden {+ t.Fatalf("multi-org caller minting an instance-wide token = %d (%s), want 403", rec.Code, rec.Body.String())+ }+ if rec := create(&f.orgA, `{"name":"escape"}`); rec.Code != http.StatusForbidden {+ t.Fatalf("org-a caller minting a default-organization full-access token = %d (%s), want 403", rec.Code, rec.Body.String())+ }+ if got := inventorySize(); got != before {+ t.Fatalf("refused creations still changed the inventory: %d tokens, want %d", got, before)+ }+ // A scoped token is not instance-wide, and an instance-wide caller may mint anything.+ if rec := create(multi, `{"name":"scoped","scopes":["monitoring:read"]}`); rec.Code >= 300 {+ t.Fatalf("multi-org caller minting a scoped token = %d (%s), want success", rec.Code, rec.Body.String())+ }+ if rec := create(&f.def, `{"name":"another"}`); rec.Code >= 300 {+ t.Fatalf("instance-wide caller minting a full-access token = %d (%s), want success", rec.Code, rec.Body.String())+ }+}++// A caller that cannot mint an instance-wide token must not be able to+// promote one it can create: edit a scoped default-organization token to full+// access.+func TestMultiOrganizationCallerCannotPromoteATokenToInstanceWide(t *testing.T) {+ f := newOrgTokenFixture(t)+ multi, err := config.NewAPITokenRecord("multi-org-promoter.12345678", "multi", []string{config.ScopeWildcard})+ if err != nil {+ t.Fatalf("new token record: %v", err)+ }+ multi.OrgID = ""+ multi.OrgIDs = []string{"default", "org-a"}+ scoped, err := config.NewAPITokenRecord("scoped-default-target.12345678", "scoped", []string{config.ScopeMonitoringRead})+ if err != nil {+ t.Fatalf("new token record: %v", err)+ }+ config.Mu.Lock()+ f.cfg.APITokens = append(f.cfg.APITokens, *multi, *scoped)+ f.cfg.SortAPITokens()+ config.Mu.Unlock()++ patch := func(caller *config.APITokenRecord, body string) *httptest.ResponseRecorder {+ req := httptest.NewRequest(http.MethodPatch, "/api/security/tokens/"+scoped.ID, strings.NewReader(body))+ attachAPITokenRecord(req, caller)+ rec := httptest.NewRecorder()+ f.router.handleUpdateAPIToken(rec, req)+ return rec+ }+ scopesNow := func() []string {+ config.Mu.RLock()+ defer config.Mu.RUnlock()+ for _, record := range f.cfg.APITokens {+ if record.ID == scoped.ID {+ return record.Scopes+ }+ }+ return nil+ }++ if rec := patch(multi, `{"scopes":["*"]}`); rec.Code != http.StatusForbidden {+ t.Fatalf("multi-org caller promoting a token to full access = %d (%s), want 403", rec.Code, rec.Body.String())+ }+ if got := scopesNow(); len(got) != 1 || got[0] != config.ScopeMonitoringRead {+ t.Fatalf("a refused promotion changed the token's scopes to %v", got)+ }+ // Narrowing or sidegrading stays allowed, as does an instance-wide caller promoting.+ if rec := patch(multi, `{"scopes":["monitoring:write"]}`); rec.Code >= 300 {+ t.Fatalf("multi-org caller changing a token to another scoped set = %d (%s), want success", rec.Code, rec.Body.String())+ }+ if rec := patch(&f.def, `{"scopes":["*"]}`); rec.Code >= 300 {+ t.Fatalf("instance-wide caller promoting a token = %d (%s), want success", rec.Code, rec.Body.String())+ }+ if got := scopesNow(); len(got) != 1 || got[0] != config.ScopeWildcard {+ t.Fatalf("an instance-wide caller's promotion left scopes %v, want full access", got)+ }+}diff --git a/internal/api/security_tokens.go b/internal/api/security_tokens.goindex 1a55523c4e..c474f2f93f 100644--- a/internal/api/security_tokens.go+++ b/internal/api/security_tokens.go@@ -203,6 +203,13 @@ func (r *Router) createAPITokenRecord(if record.OrgID == "" {record.OrgID = "default"}+ // A full-access token in the default organization is instance-wide. A+ // caller that is not itself instance-wide must not mint one, even if it+ // holds every scope, because it would escape its own organization binding.+ if callerToken := getAPITokenRecordFromRequest(req); callerToken != nil &&+ tokenHasInstanceWideAuthority(record) && !tokenHasInstanceWideAuthority(callerToken) {+ return "", nil, fmt.Errorf("Cannot grant scope %q in the default organization: your token is not an instance-wide full-access token", config.ScopeWildcard)+ }setAPITokenOwnerUserID(record, apiTokenOwnerUserIDForRequest(r.config, req))if err := mergeAPITokenMetadata(record, metadata); err != nil {return "", nil, err@@ -242,6 +249,53 @@ func (r *Router) createAPITokenRecord(}// handleListAPITokens returns all configured API tokens (metadata only).+// tokenOrgSet returns the organizations a token is bound to. OrgIDs takes+// precedence over OrgID, and an unbound token belongs to the default org.+func tokenOrgSet(record *config.APITokenRecord) []string {+ bound := record.GetBoundOrgs()+ if len(bound) == 0 {+ return []string{"default"}+ }+ orgs := make([]string, 0, len(bound))+ for _, orgID := range bound {+ orgID = strings.TrimSpace(orgID)+ if orgID == "" {+ orgID = "default"+ }+ orgs = append(orgs, orgID)+ }+ return orgs+}++// callerTokenMayManage reports whether an API-token caller may see or change+// a token. The token inventory is global, so a caller bound to one+// organization must not reach another organization's credentials. Instance-wide+// full-access tokens keep managing every token, and callers that are not+// tokens (browser sessions) are unaffected.+func callerTokenMayManage(caller, target *config.APITokenRecord) bool {+ if caller == nil || tokenHasInstanceWideAuthority(caller) {+ return true+ }+ // An instance-wide token is the most powerful credential, and rotating it+ // returns its raw value. A caller holding full-access scope passes the+ // scope containment checks, so unless it is itself instance-wide it must+ // not touch one, even when its organizations are a superset of the+ // target's. Callers without full access are refused by those checks.+ if caller.HasScope(config.ScopeWildcard) && tokenHasInstanceWideAuthority(target) {+ return false+ }+ allowed := make(map[string]struct{})+ for _, orgID := range tokenOrgSet(caller) {+ allowed[orgID] = struct{}{}+ }+ for _, orgID := range tokenOrgSet(target) {+ if _, ok := allowed[orgID]; !ok {+ return false+ }+ }+ return true+}+func (r *Router) handleListAPITokens(w http.ResponseWriter, req *http.Request) {if req.Method != http.MethodGet {http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)@@ -251,9 +305,13 @@ func (r *Router) handleListAPITokens(w http.ResponseWriter, req *http.Request) {config.Mu.RLock()defer config.Mu.RUnlock()+ callerToken := getAPITokenRecordFromRequest(req)tokens := make([]apiTokenDTO, 0, len(r.config.APITokens))- for _, record := range r.config.APITokens {- tokens = append(tokens, toAPITokenDTO(record))+ for idx := range r.config.APITokens {+ if !callerTokenMayManage(callerToken, &r.config.APITokens[idx]) {+ continue+ }+ tokens = append(tokens, toAPITokenDTO(r.config.APITokens[idx]))}w.Header().Set("Content-Type", "application/json")@@ -278,8 +336,10 @@ func (r *Router) handleGetAPIToken(w http.ResponseWriter, req *http.Request) {config.Mu.RLock()defer config.Mu.RUnlock()- for _, record := range r.config.APITokens {- if record.ID != tokenID {+ callerToken := getAPITokenRecordFromRequest(req)+ for idx := range r.config.APITokens {+ record := r.config.APITokens[idx]+ if record.ID != tokenID || !callerTokenMayManage(callerToken, &r.config.APITokens[idx]) {continue}w.Header().Set("Content-Type", "application/json")@@ -444,6 +504,12 @@ func (r *Router) handleUpdateAPIToken(w http.ResponseWriter, req *http.Request)return}+ if !callerTokenMayManage(getAPITokenRecordFromRequest(req), &r.config.APITokens[tokenIndex]) {+ r.auditTokenEvent(req, "token_updated", false, fmt.Sprintf("Organization boundary denied: caller cannot manage token id=%s", tokenID))+ http.Error(w, "Token not found", http.StatusNotFound)+ return+ }+storedRecord := r.config.APITokens[tokenIndex]oldRecord := storedRecord.Clone()@@ -470,6 +536,16 @@ func (r *Router) handleUpdateAPIToken(w http.ResponseWriter, req *http.Request)return}}+ // Widening a default-organization token to full access makes it+ // instance-wide, which only an instance-wide caller may create.+ promoted := oldRecord.Clone()+ promoted.Scopes = append([]string(nil), scopes...)+ if tokenHasInstanceWideAuthority(&promoted) && !tokenHasInstanceWideAuthority(callerToken) {+ r.auditTokenEvent(req, "token_updated", false,+ fmt.Sprintf("Scope update denied: caller cannot make token id=%s instance-wide", tokenID))+ http.Error(w, fmt.Sprintf("Cannot grant scope %q in the default organization: your token is not an instance-wide full-access token", config.ScopeWildcard), http.StatusForbidden)+ return+ }}}@@ -603,6 +679,15 @@ func (r *Router) handleDeleteAPIToken(w http.ResponseWriter, req *http.Request)// limited-scope token from deleting a broader token and forcing rotation or// lockout on a more privileged credential.if callerToken := getAPITokenRecordFromRequest(req); callerToken != nil {+ if !callerTokenMayManage(callerToken, removed) {+ // Put the record back before returning so the in-memory config is unchanged.+ r.config.APITokens = append(r.config.APITokens, *removed)+ r.config.SortAPITokens()+ r.auditTokenEvent(req, "token_deleted", false,+ fmt.Sprintf("Organization boundary denied: caller cannot manage token id=%s", tokenID))+ http.Error(w, "Token not found", http.StatusNotFound)+ return+ }for _, targetScope := range removed.Scopes {if !callerToken.HasScope(targetScope) {// Put the record back before returning so the in-memory config is unchanged.@@ -681,7 +766,16 @@ func (r *Router) handleRotateAPIToken(w http.ResponseWriter, req *http.Request)// Scope escalation prevention: if the caller is an API token, they can only// rotate tokens whose scopes are a subset of their own. This prevents a// limited-scope token from rotating a wildcard token to obtain its raw value.+ // The same applies to organizations: a caller bound to one organization+ // must not rotate, and so obtain the raw value of, another organization's+ // token.if callerToken := getAPITokenRecordFromRequest(req); callerToken != nil {+ if !callerTokenMayManage(callerToken, &oldRecord) {+ r.auditTokenEvent(req, "token_rotated", false,+ fmt.Sprintf("Organization boundary denied: caller cannot manage token id=%s", tokenID))+ http.Error(w, "Token not found", http.StatusNotFound)+ return+ }for _, targetScope := range oldRecord.Scopes {if !callerToken.HasScope(targetScope) {r.auditTokenEvent(req, "token_rotated", false,