fix: portal user headers only from the portal's service client (#91)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014aRnCGqELxoix5TfpX8Ejf
This commit was merged in pull request #91.
This commit is contained in:
1 parent
208f3d4c7e
commit
9129e95538
5 files changed
+79
-10
No files matched your search
@@ -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
|
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 {
|
type adminTenant struct {
|
||||||
store.Tenant
|
store.Tenant
|
||||||
Activity store.TenantActivity `json:"activity"`
|
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,
|
// portal#64 §10: the extension of the trial contract as its own event,
|
||||||
// without a new contract (§ 3 Abs. 3 Testbedingungen).
|
// 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 {
|
if previous != nil {
|
||||||
ext["old_expiry"] = *previous
|
ext["old_expiry"] = *previous
|
||||||
}
|
}
|
||||||
s.emitAudit(ctx, r, store.AuditEvent{
|
s.emitAudit(ctx, r, store.AuditEvent{
|
||||||
TenantID: t.ID, Action: "trial.extension_granted", TargetID: t.ID, TargetType: "tenant", TargetName: t.Slug,
|
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)
|
writeJSON(w, http.StatusOK, t)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -98,3 +98,49 @@ func TestStaffActor_onlyFromAdminClients(t *testing.T) {
|
|||||||
t.Fatalf("route group %q", g)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -414,8 +414,8 @@ func (s *Server) listTenantDocuments(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// downloadTenantDocument is GET /v1/tenants/{id}/documents/{doc}/download:
|
// downloadTenantDocument is GET /v1/tenants/{id}/documents/{doc}/download:
|
||||||
// a presigned URL valid for five minutes, and an audit entry naming who
|
// 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
|
// downloaded it (the user token's subject, or the person the portal's service
|
||||||
// X-Portal-User-Email).
|
// client names in X-Portal-User-Email, see portalUser).
|
||||||
func (s *Server) downloadTenantDocument(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) downloadTenantDocument(w http.ResponseWriter, r *http.Request) {
|
||||||
if !s.documentsReady(w) {
|
if !s.documentsReady(w) {
|
||||||
return
|
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"
|
ev.ActorID, ev.ActorName, ev.ActorType = p.Subject, p.Email, "service"
|
||||||
if p.IsUser() {
|
if p.IsUser() {
|
||||||
ev.ActorType = "user"
|
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"
|
ev.ActorName, ev.ActorType = email, "user"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,7 +58,8 @@ func (s *Server) projectActor(r *http.Request) string {
|
|||||||
if sub, email, ok := s.staffActor(r); ok {
|
if sub, email, ok := s.staffActor(r); ok {
|
||||||
return firstNonEmpty(email, sub)
|
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
|
// 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 {
|
if _, _, ok := s.staffActor(r); ok {
|
||||||
return "staff"
|
return "staff"
|
||||||
}
|
}
|
||||||
if strings.TrimSpace(r.Header.Get(headerPortalUserEmail)) != "" {
|
if _, email, ok := s.portalUser(r); ok && email != "" {
|
||||||
return "user"
|
return "user"
|
||||||
}
|
}
|
||||||
return "service"
|
return "service"
|
||||||
|
|||||||
@@ -28,7 +28,9 @@ import (
|
|||||||
// Who may: a user token needs IT_ADMIN/OWNER in its own tenant; a service
|
// 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
|
// 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:
|
// 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
|
// Guards (409): nobody acts on themselves ("self"); the last active
|
||||||
// IT_ADMIN/OWNER is never demoted, blocked or removed ("last_admin").
|
// 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"}
|
return teamActor{ID: p.Subject, Email: p.Email, Type: "user"}
|
||||||
}
|
}
|
||||||
// The portal acting for a signed-in tenant admin.
|
// The portal acting for a signed-in tenant admin.
|
||||||
if sub := strings.TrimSpace(r.Header.Get(headerPortalUserSubject)); sub != "" {
|
if sub, email, ok := s.portalUser(r); ok && sub != "" {
|
||||||
return teamActor{ID: sub, Email: strings.TrimSpace(r.Header.Get(headerPortalUserEmail)), Type: "user"}
|
return teamActor{ID: sub, Email: email, Type: "user"}
|
||||||
}
|
}
|
||||||
a := teamActor{Type: "service"}
|
a := teamActor{Type: "service"}
|
||||||
if p != nil {
|
if p != nil {
|
||||||
|
|||||||
Reference in new issue
Block a user