Skip to content

Agent Mesh

Reject policy configs that could exceed the biscuit fact limit at validation time

Antonio Ojead230f8c07499
diff --git a/internal/controlplane/policy_scale_test.go b/internal/controlplane/policy_scale_test.go
index 451c7be5..197ce24d 100644
--- a/internal/controlplane/policy_scale_test.go
+++ b/internal/controlplane/policy_scale_test.go
@@ -37,12 +37,14 @@ import (
// increasing numbers of roles/bindings and logs sizes and timings, so we have
// a record of where the current implementation starts to strain.
//
-// The default tiers are kept under the biscuit-go authorizer's default world
-// fact-count limit (1000 facts, see datalog.WithMaxFacts) so this test stays
-// green in CI. Hitting that limit is an expected, informative outcome of
-// pushing the scale further rather than a bug: once reached, the test logs it
-// and stops escalating instead of failing. Any other error (validation, role
-// resolution, or minting) still fails the test. Probe further with e.g.:
+// validatePolicyConfig now rejects configs whose worst-case fact budget
+// (summed across all roles, see maxIdentityFactBudget) approaches biscuit-go's
+// default 1000-fact authorizer world limit, so scaling further is expected to
+// be caught there rather than at mint/authorize time. Hitting either limit is
+// an expected, informative outcome of pushing the scale further rather than a
+// bug: once reached, the test logs it and stops escalating instead of failing.
+// Any other error (validation, role resolution, or minting) still fails the
+// test. Probe further with e.g.:
//
// SAM_POLICY_SCALE_MAX=5000 go test ./internal/controlplane -run TestPolicyScale -v
func TestPolicyScale(t *testing.T) {
@@ -95,7 +97,11 @@ func TestPolicyScale(t *testing.T) {
start := time.Now()
if err := validatePolicyConfig(req); err != nil {
- t.Fatalf("validatePolicyConfig failed at n=%d: %v", n, err)
+ if !strings.Contains(err.Error(), "exceeding the safe budget") {
+ t.Fatalf("validatePolicyConfig failed at n=%d for an unexpected reason: %v", n, err)
+ }
+ t.Logf("roles=%-6d validate: hit admin-time fact budget guard (%v) - stopping", n, err)
+ break
}
validateDur := time.Since(start)
@@ -184,6 +190,14 @@ func TestPolicyScaleSetEncoding(t *testing.T) {
AllowedTargets: targets,
}}
+ // A single role with many exact entries should stay well under the
+ // admin-time fact budget guard, since Set-based encoding collapses it
+ // to a couple of facts regardless of entry count.
+ bindings := []*api.PolicyBinding{{Role: "bulk-role", Members: []string{"node:" + nodePeer.String()}}}
+ if err := validatePolicyConfig(&api.PolicyConfigUpdateRequest{Roles: roles, Bindings: bindings}); err != nil {
+ t.Fatalf("validatePolicyConfig unexpectedly rejected a single bulk role with n=%d exact entries: %v", n, err)
+ }
+
claims := jwt.MapClaims{}
start := time.Now()
biscuitBytes, _, err := identity.MintBiscuitToken(priv, claims, nil, nodePeer, time.Now().Add(time.Hour), []string{"bulk-role"}, roles)
@@ -202,3 +216,45 @@ func TestPolicyScaleSetEncoding(t *testing.T) {
t.Logf("exact_entries_per_role=%-6d mint=%-12s biscuit_size=%-10dB authorize=%-12s", n, mintDur, len(biscuitBytes), authorizeDur)
}
}
+
+// TestValidatePolicyConfigFactBudget verifies that validatePolicyConfig itself
+// rejects configs whose worst-case per-identity fact budget would approach
+// biscuit-go's default authorizer fact limit, instead of letting the problem
+// surface later as an authorization failure for real users.
+func TestValidatePolicyConfigFactBudget(t *testing.T) {
+ newRoles := func(n int) ([]*api.PolicyRole, []*api.PolicyBinding) {
+ roles := make([]*api.PolicyRole, 0, n)
+ bindings := make([]*api.PolicyBinding, 0, n)
+ for i := 0; i < n; i++ {
+ name := fmt.Sprintf("role-%d", i)
+ roles = append(roles, &api.PolicyRole{
+ Name: name,
+ AllowedServices: []string{fmt.Sprintf("mcp://svc-%d.example", i)},
+ AllowedTargets: []string{fmt.Sprintf("group:team-%d", i)},
+ })
+ bindings = append(bindings, &api.PolicyBinding{
+ Role: name,
+ Members: []string{"group:everyone"},
+ })
+ }
+ return roles, bindings
+ }
+
+ t.Run("rejects a config that could push a single identity over the fact budget", func(t *testing.T) {
+ roles, bindings := newRoles(500)
+ err := validatePolicyConfig(&api.PolicyConfigUpdateRequest{Roles: roles, Bindings: bindings})
+ if err == nil {
+ t.Fatal("expected validatePolicyConfig to reject an over-budget config, got nil error")
+ }
+ if !strings.Contains(err.Error(), "exceeding the safe budget") {
+ t.Errorf("expected a fact budget error, got: %v", err)
+ }
+ })
+
+ t.Run("accepts a config comfortably under the fact budget", func(t *testing.T) {
+ roles, bindings := newRoles(50)
+ if err := validatePolicyConfig(&api.PolicyConfigUpdateRequest{Roles: roles, Bindings: bindings}); err != nil {
+ t.Errorf("expected a small config to pass validation, got: %v", err)
+ }
+ })
+}
diff --git a/internal/controlplane/server.go b/internal/controlplane/server.go
index 9561f71e..07d0dda7 100644
--- a/internal/controlplane/server.go
+++ b/internal/controlplane/server.go
@@ -1833,8 +1833,17 @@ func toStringSlice(val any) []string {
return nil
}
+// maxIdentityFactBudget bounds the worst-case number of Datalog facts a policy
+// config could let a single identity accumulate across all of its resolved
+// roles. biscuit-go's authorizer defaults to rejecting worlds beyond ~1000
+// facts (datalog.ErrWorldRunLimitMaxFacts); this stays safely under that limit
+// so an admin gets a clear validation error instead of users hitting
+// unexplained authorization failures later.
+const maxIdentityFactBudget = 900
+
func validatePolicyConfig(req *api.PolicyConfigUpdateRequest) error {
roleNames := make(map[string]bool)
+ factBudget := 0
for _, r := range req.Roles {
if r == nil {
continue
@@ -1868,6 +1877,18 @@ func validatePolicyConfig(req *api.PolicyConfigUpdateRequest) error {
}
}
}
+
+ // Worst case for fact-count purposes assumes a single identity could be
+ // granted every role (e.g. via overlapping group/claim bindings), so we
+ // sum each role's contribution rather than relying on assumptions about
+ // which roles are mutually exclusive for a given identity.
+ factBudget += len(api.BuildServiceDatalogFacts(r.AllowedServices))
+ factBudget += len(api.BuildTargetDatalogFacts(r.AllowedTargets))
+ factBudget += len(r.CustomDatalog)
+ }
+
+ if factBudget > maxIdentityFactBudget {
+ return fmt.Errorf("policy config would allow a single identity (via overlapping bindings) to accumulate up to %d Datalog facts across all roles, exceeding the safe budget of %d; biscuit-go's authorizer rejects tokens/checks beyond ~1000 world facts, so requests would start failing at authorization time instead of at config validation. Reduce the number of roles, grants, or custom_datalog entries", factBudget, maxIdentityFactBudget)
}
validPrefixes := map[string]bool{