diff --git a/internal/server/admin.go b/internal/server/admin.go index f4da0d5..0505d0d 100644 --- a/internal/server/admin.go +++ b/internal/server/admin.go @@ -43,6 +43,25 @@ func (s *Server) staffActor(r *http.Request) (subject, email string, ok bool) { return subject, strings.TrimSpace(r.Header.Get(headerStaffEmail)), true } +// portalUser is the person the portal names in X-Portal-User-Subject and +// X-Portal-User-Email when it calls with its service token. Like staffActor, +// it is only honoured from a client that holds the users group (the portal's +// service client), so another product client cannot act, or be logged, in a +// user's name by setting a header. A user token speaks for itself: the +// headers are ignored. Without a principal (AUTH_ENABLED=false, dev) they +// are taken as given. +func (s *Server) portalUser(r *http.Request) (subject, email string, ok bool) { + subject = strings.TrimSpace(r.Header.Get(headerPortalUserSubject)) + email = strings.TrimSpace(r.Header.Get(headerPortalUserEmail)) + if subject == "" && email == "" { + return "", "", false + } + if p, has := authn.PrincipalFrom(r.Context()); has && (p.IsUser() || !s.Authz.Allowed(p.ClientID, "users.act")) { + return "", "", false + } + return subject, email, true +} + type adminTenant struct { store.Tenant Activity store.TenantActivity `json:"activity"` @@ -241,13 +260,14 @@ func (s *Server) adminExtendTrial(w http.ResponseWriter, r *http.Request) { }) // portal#64 §10: the extension of the trial contract as its own event, // without a new contract (§ 3 Abs. 3 Testbedingungen). - ext := map[string]interface{}{"new_expiry": end, "granted_by": r.Header.Get("X-Portal-User-Email"), "granted_at": now} + _, grantedBy, _ := s.portalUser(r) + ext := map[string]interface{}{"new_expiry": end, "granted_by": grantedBy, "granted_at": now} if previous != nil { ext["old_expiry"] = *previous } s.emitAudit(ctx, r, store.AuditEvent{ TenantID: t.ID, Action: "trial.extension_granted", TargetID: t.ID, TargetType: "tenant", TargetName: t.Slug, - ActorType: "staff", ActorName: r.Header.Get("X-Portal-User-Email"), Metadata: ext, + ActorType: "staff", ActorName: grantedBy, Metadata: ext, }) writeJSON(w, http.StatusOK, t) } diff --git a/internal/server/authz_middleware_test.go b/internal/server/authz_middleware_test.go index 8b0e29a..3aeff97 100644 --- a/internal/server/authz_middleware_test.go +++ b/internal/server/authz_middleware_test.go @@ -98,3 +98,49 @@ func TestStaffActor_onlyFromAdminClients(t *testing.T) { t.Fatalf("route group %q", g) } } + +func TestPortalUser_onlyFromThePortalClient(t *testing.T) { + s := &Server{Authz: ParseAuthzRules("portal-svc:tenants,users;werkmeister-svc:tenants.read;ops-svc:tenants")} + req := func(client string, user bool) *http.Request { + r := httptest.NewRequest("GET", "/v1/tenants/x/documents/d/download", nil) + r.Header.Set(headerPortalUserSubject, "u-1") + r.Header.Set(headerPortalUserEmail, "cxo@acme.test") + if client == "" { + return r + } + p := &authn.Principal{Subject: "svc", ClientID: client} + if user { + p.TenantID = "t1" + } + return r.WithContext(authn.WithPrincipal(r.Context(), p)) + } + if sub, email, ok := s.portalUser(req("portal-svc", false)); !ok || sub != "u-1" || email != "cxo@acme.test" { + t.Fatalf("portal client: %v %q %q", ok, sub, email) + } + for _, c := range []string{"werkmeister-svc", "ops-svc", "unknown-svc"} { + if _, _, ok := s.portalUser(req(c, false)); ok { + t.Errorf("%s must not name a user", c) + } + if got := s.projectActor(req(c, false)); got != "" { + t.Errorf("%s: project actor %q", c, got) + } + if got := s.projectActorType(req(c, false)); got != "service" { + t.Errorf("%s: project actor type %q", c, got) + } + if a := s.teamActor(req(c, false)); a.Type != "service" || a.ID != "svc" { + t.Errorf("%s: team actor %+v", c, a) + } + } + if _, _, ok := s.portalUser(req("portal-svc", true)); ok { + t.Fatal("a user token speaks for itself, not through the headers") + } + if _, _, ok := s.portalUser(httptest.NewRequest("GET", "/", nil)); ok { + t.Fatal("no headers") + } + if _, _, ok := s.portalUser(req("", false)); !ok { + t.Fatal("without a principal (auth disabled, dev) the headers are taken as given") + } + if a := s.teamActor(req("portal-svc", false)); a.Type != "user" || a.ID != "u-1" || a.Email != "cxo@acme.test" { + t.Fatalf("portal team actor %+v", a) + } +} diff --git a/internal/server/documents.go b/internal/server/documents.go index 17c59f4..8d1b624 100644 --- a/internal/server/documents.go +++ b/internal/server/documents.go @@ -414,8 +414,8 @@ func (s *Server) listTenantDocuments(w http.ResponseWriter, r *http.Request) { // downloadTenantDocument is GET /v1/tenants/{id}/documents/{doc}/download: // a presigned URL valid for five minutes, and an audit entry naming who -// downloaded it (the user token's subject, or the person the portal names in -// X-Portal-User-Email). +// downloaded it (the user token's subject, or the person the portal's service +// client names in X-Portal-User-Email, see portalUser). func (s *Server) downloadTenantDocument(w http.ResponseWriter, r *http.Request) { if !s.documentsReady(w) { return @@ -442,7 +442,7 @@ func (s *Server) downloadTenantDocument(w http.ResponseWriter, r *http.Request) ev.ActorID, ev.ActorName, ev.ActorType = p.Subject, p.Email, "service" if p.IsUser() { ev.ActorType = "user" - } else if email := strings.TrimSpace(r.Header.Get("X-Portal-User-Email")); email != "" { + } else if _, email, ok := s.portalUser(r); ok && email != "" { ev.ActorName, ev.ActorType = email, "user" } } diff --git a/internal/server/projects.go b/internal/server/projects.go index 3b4da89..a4df5aa 100644 --- a/internal/server/projects.go +++ b/internal/server/projects.go @@ -58,7 +58,8 @@ func (s *Server) projectActor(r *http.Request) string { if sub, email, ok := s.staffActor(r); ok { return firstNonEmpty(email, sub) } - return strings.TrimSpace(r.Header.Get(headerPortalUserEmail)) + _, email, _ := s.portalUser(r) + return email } // projectActorType is "staff" for BreakPilot staff, "user" when the portal @@ -67,7 +68,7 @@ func (s *Server) projectActorType(r *http.Request) string { if _, _, ok := s.staffActor(r); ok { return "staff" } - if strings.TrimSpace(r.Header.Get(headerPortalUserEmail)) != "" { + if _, email, ok := s.portalUser(r); ok && email != "" { return "user" } return "service" diff --git a/internal/server/team.go b/internal/server/team.go index 01e495e..ee169ec 100644 --- a/internal/server/team.go +++ b/internal/server/team.go @@ -28,7 +28,9 @@ import ( // Who may: a user token needs IT_ADMIN/OWNER in its own tenant; a service // client needs tenants.write. The portal calls as its service client and // names the person in X-Portal-User-Subject / X-Portal-User-Email (staff: -// X-Staff-*), which the guards and the audit trail use. +// X-Staff-*), which the guards and the audit trail use. Both header sets are +// honoured only from a client that holds the matching group (portalUser, +// staffActor), never from any other product client. // // Guards (409): nobody acts on themselves ("self"); the last active // IT_ADMIN/OWNER is never demoted, blocked or removed ("last_admin"). @@ -81,8 +83,8 @@ func (s *Server) teamActor(r *http.Request) teamActor { return teamActor{ID: p.Subject, Email: p.Email, Type: "user"} } // The portal acting for a signed-in tenant admin. - if sub := strings.TrimSpace(r.Header.Get(headerPortalUserSubject)); sub != "" { - return teamActor{ID: sub, Email: strings.TrimSpace(r.Header.Get(headerPortalUserEmail)), Type: "user"} + if sub, email, ok := s.portalUser(r); ok && sub != "" { + return teamActor{ID: sub, Email: email, Type: "user"} } a := teamActor{Type: "service"} if p != nil {