fix(connections): refuse user tokens on product routes
The two internal routes carry the peer's tenant id, which no person sees; a user token is now refused there whatever groups its client holds. The per-person window for entering codes applies only when a person is named: keyed on the portal's own client, one workspace's attempts would have blocked every other workspace. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q9inRcu428A2iNjDHPEAT9
This commit is contained in:
1 parent
400d1b1678
commit
3ef530b504
3 files changed
+82
-9
No files matched your search
@@ -246,10 +246,14 @@ func (s *Server) redeemConnectionInvite(w http.ResponseWriter, r *http.Request)
|
||||
actor := s.teamActor(r)
|
||||
now := s.now()
|
||||
// Both windows count every attempt, so neither a tenant nor one person
|
||||
// can try codes faster than the rate.
|
||||
tenantOK := s.connLimiter.allow("tenant:"+t.ID, now, connectionRedeemRate)
|
||||
personOK := s.connLimiter.allow("person:"+firstNonEmpty(actor.ID, actor.Email, actor.Type), now, connectionRedeemRate)
|
||||
if !tenantOK || !personOK {
|
||||
// can try codes faster than the rate. A call that names no person (a
|
||||
// service client without X-Portal-User-*) has the tenant window only:
|
||||
// keyed on the client, one workspace's attempts would block every other.
|
||||
allowed := s.connLimiter.allow("tenant:"+t.ID, now, connectionRedeemRate)
|
||||
if person := firstNonEmpty(actor.ID, actor.Email); actor.Type != "service" && person != "" {
|
||||
allowed = s.connLimiter.allow("person:"+person, now, connectionRedeemRate) && allowed
|
||||
}
|
||||
if !allowed {
|
||||
w.Header().Set("Retry-After", "3600")
|
||||
writeError(w, http.StatusTooManyRequests, "rate_limited", "too many codes entered; try again later")
|
||||
return
|
||||
@@ -371,10 +375,24 @@ type productConnection struct {
|
||||
PeerName string `json:"peer_name"`
|
||||
}
|
||||
|
||||
// productOnly refuses a user token on the product routes: they carry the
|
||||
// peer's tenant id, which no person ever sees, whatever groups the token's
|
||||
// client holds.
|
||||
func productOnly(w http.ResponseWriter, r *http.Request) bool {
|
||||
if p, _ := authn.PrincipalFrom(r.Context()); p.IsUser() {
|
||||
writeError(w, http.StatusForbidden, "FORBIDDEN_CLIENT", "products only")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// internalTenantConnections is GET /v1/internal/tenants/{id}/connections
|
||||
// ?state=active: the tenant's active connections with the peer's tenant id,
|
||||
// for a product to deliver over. 404 for an unknown tenant.
|
||||
func (s *Server) internalTenantConnections(w http.ResponseWriter, r *http.Request) {
|
||||
if !productOnly(w, r) {
|
||||
return
|
||||
}
|
||||
if st := r.URL.Query().Get("state"); st != "" && st != "active" {
|
||||
writeError(w, http.StatusBadRequest, "invalid_state", "state must be active")
|
||||
return
|
||||
@@ -409,6 +427,9 @@ func (s *Server) internalTenantConnections(w http.ResponseWriter, r *http.Reques
|
||||
// it is active and the tenants it joins. A product asks it before every
|
||||
// delivery. 404 for an unknown id.
|
||||
func (s *Server) checkConnection(w http.ResponseWriter, r *http.Request) {
|
||||
if !productOnly(w, r) {
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
||||
defer cancel()
|
||||
c, err := s.Store.GetConnection(ctx, r.PathValue("connection"))
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"git.breakpilot.com/platform/tenant-registry/internal/authn"
|
||||
"git.breakpilot.com/platform/tenant-registry/internal/store"
|
||||
)
|
||||
|
||||
// Only a workspace administrator may create, enter or end, and only in its
|
||||
@@ -52,3 +53,54 @@ func TestConnections_userRoles(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The product routes refuse a person's token, whatever its client holds;
|
||||
// they carry the peer's tenant id.
|
||||
func TestConnections_productRoutesRefuseUsers(t *testing.T) {
|
||||
p := &authn.Principal{Subject: "u-1", ClientID: "portal", TenantID: "00000000-0000-0000-0000-000000000001", OrgRoles: []string{"IT_ADMIN"}}
|
||||
_, h, tenant := scopedServer(t, p, "portal:*")
|
||||
for _, path := range []string{
|
||||
"/v1/internal/tenants/" + tenant.ID + "/connections?state=active",
|
||||
"/v1/internal/connections/00000000-0000-0000-0000-000000000002",
|
||||
} {
|
||||
rr := httptest.NewRecorder()
|
||||
h.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, path, nil))
|
||||
if rr.Code != http.StatusForbidden {
|
||||
t.Errorf("%s: %d %s", path, rr.Code, rr.Body.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The person window follows the person the portal names, across
|
||||
// workspaces; the portal's own client is never a person.
|
||||
func TestConnections_personWindow(t *testing.T) {
|
||||
portal := &authn.Principal{Subject: "svc-portal", ClientID: "portal-svc"}
|
||||
s, h, acme := scopedServer(t, portal, "portal-svc:tenants,users.act")
|
||||
other, err := s.Store.CreateTenant(t.Context(), store.TenantCreate{Slug: "beta", Name: "Beta GmbH", Plan: "professional"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
redeem := func(tenantID, subject string) int {
|
||||
req := httptest.NewRequest(http.MethodPost, "/v1/tenants/"+tenantID+"/connections", strings.NewReader(`{"code":"x"}`))
|
||||
if subject != "" {
|
||||
req.Header.Set(headerPortalUserSubject, subject)
|
||||
}
|
||||
rr := httptest.NewRecorder()
|
||||
h.ServeHTTP(rr, req)
|
||||
return rr.Code
|
||||
}
|
||||
for i := 0; i < connectionRedeemRate; i++ {
|
||||
if got := redeem(acme.ID, "u-7"); got != http.StatusNotFound {
|
||||
t.Fatalf("attempt %d: %d", i, got)
|
||||
}
|
||||
}
|
||||
if got := redeem(other.ID, "u-7"); got != http.StatusTooManyRequests {
|
||||
t.Fatalf("the same person in another workspace: %d", got)
|
||||
}
|
||||
if got := redeem(other.ID, "u-8"); got != http.StatusNotFound {
|
||||
t.Fatalf("another person there: %d", got)
|
||||
}
|
||||
if got := redeem(other.ID, ""); got != http.StatusNotFound {
|
||||
t.Fatalf("the portal naming nobody: %d", got)
|
||||
}
|
||||
}
|
||||
@@ -347,7 +347,7 @@ func TestConnections_productRoutes(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// Entering codes is limited per tenant, working or not.
|
||||
// Entering codes is limited per workspace, working or not.
|
||||
func TestConnections_redeemRateLimit(t *testing.T) {
|
||||
h := newMemoryHarness(t)
|
||||
defer h.Close()
|
||||
@@ -360,11 +360,11 @@ func TestConnections_redeemRateLimit(t *testing.T) {
|
||||
if status, raw := h.redeem(b.ID, "AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAAA"); status != http.StatusTooManyRequests {
|
||||
t.Fatalf("21st attempt: %d %s", status, raw)
|
||||
}
|
||||
// Another tenant has its own window, but the same caller does not:
|
||||
// without a principal every request is the same caller here.
|
||||
// Another tenant has its own window; a call naming no person has no
|
||||
// person window, so one workspace's attempts never block another's.
|
||||
c, _ := h.store.CreateTenant(context.Background(), store.TenantCreate{Slug: "gamma", Name: "Gamma AG", Plan: "professional"})
|
||||
if status, _ := h.redeem(c.ID, "x"); status != http.StatusTooManyRequests {
|
||||
t.Fatalf("per caller: %d", status)
|
||||
if status, raw := h.redeem(c.ID, "x"); status != http.StatusNotFound {
|
||||
t.Fatalf("another workspace: %d %s", status, raw)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user