Skip to content

Agent Mesh

fix(sec): prevent OIDC role impersonation and use deterministic proto marshaling for mesh signatures

Antonio Ojea20d6c3945f87
diff --git a/api/datalog.go b/api/datalog.go
index ddb05088..c12303b7 100644
--- a/api/datalog.go
+++ b/api/datalog.go
@@ -368,6 +368,9 @@ func BuildServiceDatalogFact(serviceStr string) biscuit.Fact {
// BuildTargetDatalogFact translates a target pattern string into a Datalog Fact.
func BuildTargetDatalogFact(targetStr string) biscuit.Fact {
tFact, tVal := ParseServiceTarget(targetStr)
+ if tFact == "" {
+ tFact = "node"
+ }
if tFact == "*" && tVal == "*" {
return biscuit.Fact{Predicate: biscuit.Predicate{
Name: FactGrantedTargetAllFacts,
diff --git a/charts/sam-mesh/templates/bootstrap-job.yaml b/charts/sam-mesh/templates/bootstrap-job.yaml
index 8b92be99..5c784cb1 100644
--- a/charts/sam-mesh/templates/bootstrap-job.yaml
+++ b/charts/sam-mesh/templates/bootstrap-job.yaml
@@ -72,7 +72,7 @@ spec:
CP_URL="http://{{ include "sam-mesh.fullname" . }}-control-plane:8080"
echo "Waiting for Control Plane to be ready..."
- until curl -s "${CP_URL}/info" > /dev/null; do
+ until curl -sf "${CP_URL}/info" > /dev/null; do
echo "Control plane not ready yet, sleeping..."
sleep 2
done
diff --git a/internal/controlplane/mesh.go b/internal/controlplane/mesh.go
index 8714c25b..0f708bde 100644
--- a/internal/controlplane/mesh.go
+++ b/internal/controlplane/mesh.go
@@ -145,14 +145,14 @@ func (p *P2PMeshAdapter) PublishEvent(ctx context.Context, eventType api.MeshEve
}
if privKey != nil {
- eventData, err := proto.Marshal(event)
+ eventData, err := proto.MarshalOptions{Deterministic: true}.Marshal(event)
if err != nil {
return fmt.Errorf("failed to marshal event for signing: %w", err)
}
event.Signature = ed25519.Sign(privKey, eventData)
}
- data, err := proto.Marshal(event)
+ data, err := proto.MarshalOptions{Deterministic: true}.Marshal(event)
if err != nil {
return fmt.Errorf("failed to marshal signed mesh event: %w", err)
}
diff --git a/internal/controlplane/server.go b/internal/controlplane/server.go
index 02c3a2fc..9cbcf205 100644
--- a/internal/controlplane/server.go
+++ b/internal/controlplane/server.go
@@ -1791,9 +1791,6 @@ func resolveRoles(peerID string, claims jwt.MapClaims, bindings []*api.PolicyBin
}
}
}
- for _, r := range oidcRoles {
- resolvedRoles[r] = true
- }
var res []string
for r := range resolvedRoles {
diff --git a/internal/controlplane/server_test.go b/internal/controlplane/server_test.go
index 54938d6b..473fb998 100644
--- a/internal/controlplane/server_test.go
+++ b/internal/controlplane/server_test.go
@@ -1122,3 +1122,79 @@ func TestUserStatusAndTenancy(t *testing.T) {
t.Error("expected node A to be banned/revoked in DB")
}
}
+
+func TestResolveRolesAndRoleImpersonationProtection(t *testing.T) {
+ bindings := []*api.PolicyBinding{
+ {
+ Role: api.RoleRouter,
+ Members: []string{"group:routers", "role:oidc-router-role"},
+ },
+ {
+ Role: api.RoleSamBox,
+ Members: []string{"user:sambox-admin-sub"},
+ },
+ }
+
+ t.Run("OIDC claims role is not blindly trusted without explicit binding", func(t *testing.T) {
+ claims := jwt.MapClaims{
+ "sub": "attacker-sub",
+ "roles": []string{api.RoleRouter, api.RoleSamBox, "unbound-role"},
+ }
+ roles := resolveRoles("peer-123", claims, bindings)
+ for _, r := range roles {
+ if r == api.RoleRouter || r == api.RoleSamBox {
+ t.Errorf("Security flaw: resolveRoles granted capability role %q from raw OIDC claims without explicit binding", r)
+ }
+ }
+ })
+
+ t.Run("Explicit role mapping in binding grants capability role", func(t *testing.T) {
+ claims := jwt.MapClaims{
+ "sub": "router-sub",
+ "roles": []string{"oidc-router-role"},
+ }
+ roles := resolveRoles("peer-123", claims, bindings)
+ hasRouter := false
+ for _, r := range roles {
+ if r == api.RoleRouter {
+ hasRouter = true
+ }
+ }
+ if !hasRouter {
+ t.Errorf("Expected role %q to be granted via explicit role binding mapping", api.RoleRouter)
+ }
+ })
+
+ t.Run("Group membership grants bound capability role", func(t *testing.T) {
+ claims := jwt.MapClaims{
+ "sub": "router-user",
+ "groups": []string{"routers"},
+ }
+ roles := resolveRoles("peer-456", claims, bindings)
+ hasRouter := false
+ for _, r := range roles {
+ if r == api.RoleRouter {
+ hasRouter = true
+ }
+ }
+ if !hasRouter {
+ t.Errorf("Expected role %q to be granted via group binding", api.RoleRouter)
+ }
+ })
+
+ t.Run("User sub grants bound capability role", func(t *testing.T) {
+ claims := jwt.MapClaims{
+ "sub": "sambox-admin-sub",
+ }
+ roles := resolveRoles("peer-789", claims, bindings)
+ hasSamBox := false
+ for _, r := range roles {
+ if r == api.RoleSamBox {
+ hasSamBox = true
+ }
+ }
+ if !hasSamBox {
+ t.Errorf("Expected role %q to be granted via user sub binding", api.RoleSamBox)
+ }
+ })
+}
diff --git a/internal/node/node.go b/internal/node/node.go
index 0c2f0a3f..ca62c90b 100644
--- a/internal/node/node.go
+++ b/internal/node/node.go
@@ -1221,7 +1221,7 @@ func (n *SamNode) startKeyPruning(ctx context.Context, gracePeriod time.Duration
func (n *SamNode) verifyEvent(event *api.MeshEvent) bool {
sig := event.Signature
event.Signature = nil
- data, err := proto.Marshal(event)
+ data, err := proto.MarshalOptions{Deterministic: true}.Marshal(event)
event.Signature = sig // Restore
if err != nil {
logger.Errorf("[Mesh Event] Failed to marshal event for verification: %v", err)
diff --git a/internal/node/policy.go b/internal/node/policy.go
index 74d42f33..6c667e4c 100644
--- a/internal/node/policy.go
+++ b/internal/node/policy.go
@@ -50,61 +50,13 @@ func BuildPolicyRules(roles []*api.PolicyRole, bindings []*api.PolicyBinding) []
roleName := role.Name
for _, svc := range role.AllowedServices {
- svcType, svcName := api.ParseServiceTarget(svc)
-
- if svcType == "*" && svcName == "*" {
- rules = append(rules, biscuit.Rule{
- Head: biscuit.Predicate{
- Name: api.FactGrantedServiceAllTypes,
- IDs: []biscuit.Term{},
- },
- Body: []biscuit.Predicate{
- {Name: api.FactRole, IDs: []biscuit.Term{biscuit.String(roleName)}},
- },
- })
- } else if svcName == "*" {
- rules = append(rules, biscuit.Rule{
- Head: biscuit.Predicate{
- Name: api.FactGrantedServiceAll,
- IDs: []biscuit.Term{biscuit.String(svcType)},
- },
- Body: []biscuit.Predicate{
- {Name: api.FactRole, IDs: []biscuit.Term{biscuit.String(roleName)}},
- },
- })
- } else if strings.HasPrefix(svcName, "*.") {
- suffix := svcName[1:]
- rules = append(rules, biscuit.Rule{
- Head: biscuit.Predicate{
- Name: api.FactGrantedServiceSuffix,
- IDs: []biscuit.Term{biscuit.String(svcType), biscuit.String(suffix)},
- },
- Body: []biscuit.Predicate{
- {Name: api.FactRole, IDs: []biscuit.Term{biscuit.String(roleName)}},
- },
- })
- } else if strings.HasSuffix(svcName, ".*") {
- prefix := svcName[:len(svcName)-1]
- rules = append(rules, biscuit.Rule{
- Head: biscuit.Predicate{
- Name: api.FactGrantedServicePrefix,
- IDs: []biscuit.Term{biscuit.String(svcType), biscuit.String(prefix)},
- },
- Body: []biscuit.Predicate{
- {Name: api.FactRole, IDs: []biscuit.Term{biscuit.String(roleName)}},
- },
- })
- } else {
- rules = append(rules, biscuit.Rule{
- Head: biscuit.Predicate{
- Name: api.FactGrantedServiceExact,
- IDs: []biscuit.Term{biscuit.String(svcType), biscuit.String(svcName)},
- },
- Body: []biscuit.Predicate{
- {Name: api.FactRole, IDs: []biscuit.Term{biscuit.String(roleName)}},
- },
- })
- }
+ fact := api.BuildServiceDatalogFact(svc)
+ rules = append(rules, biscuit.Rule{
+ Head: fact.Predicate,
+ Body: []biscuit.Predicate{
+ {Name: api.FactRole, IDs: []biscuit.Term{biscuit.String(roleName)}},
+ },
+ })
}
hasUnrestricted := false
@@ -145,36 +97,13 @@ func BuildPolicyRules(roles []*api.PolicyRole, bindings []*api.PolicyBinding) []
if t == "*" {
continue
}
-
- // Try to parse as fact:value
- parts := strings.SplitN(t, ":", 2)
- if len(parts) == 2 {
- factName := parts[0]
- factVal := parts[1]
- rules = append(rules, biscuit.Rule{
- Head: biscuit.Predicate{
- Name: api.FactGrantedTargetExact,
- IDs: []biscuit.Term{biscuit.String(factName), biscuit.String(factVal)},
- },
- Body: []biscuit.Predicate{
- {Name: api.FactRole, IDs: []biscuit.Term{biscuit.String(roleName)}},
- },
- })
- } else {
- // Fallback, if it doesn't have a colon, we'll just treat it as an unrestricted node check?
- // Wait, earlier tests might use "node:foo", or maybe just a peerID?
- // For compatibility, if no colon, assume it's a domain/prefix check?
- // Actually, Phase 2 tests mostly use FactGrantedTargetExact.
- rules = append(rules, biscuit.Rule{
- Head: biscuit.Predicate{
- Name: api.FactGrantedTargetExact,
- IDs: []biscuit.Term{biscuit.String("node"), biscuit.String(t)},
- },
- Body: []biscuit.Predicate{
- {Name: api.FactRole, IDs: []biscuit.Term{biscuit.String(roleName)}},
- },
- })
- }
+ fact := api.BuildTargetDatalogFact(t)
+ rules = append(rules, biscuit.Rule{
+ Head: fact.Predicate,
+ Body: []biscuit.Predicate{
+ {Name: api.FactRole, IDs: []biscuit.Term{biscuit.String(roleName)}},
+ },
+ })
}
for _, dl := range role.CustomDatalog {
diff --git a/internal/router/router.go b/internal/router/router.go
index a6696706..10b42620 100644
--- a/internal/router/router.go
+++ b/internal/router/router.go
@@ -519,7 +519,7 @@ func (r *Router) getTrustedPublicKeys() []ed25519.PublicKey {
func (r *Router) verifyEvent(event *api.MeshEvent) bool {
sig := event.Signature
event.Signature = nil
- data, err := proto.Marshal(event)
+ data, err := proto.MarshalOptions{Deterministic: true}.Marshal(event)
event.Signature = sig
if err != nil {
logger.Errorf("[Router Event] Failed to marshal event for verification: %v", err)