Skip to content

Agent Mesh

controlplane: reject unknown fields in a posted policy

Antonio Ojead1a66f3866b0
diff --git a/internal/controlplane/server.go b/internal/controlplane/server.go
index 359d5aa5..0d8e6bcc 100644
--- a/internal/controlplane/server.go
+++ b/internal/controlplane/server.go
@@ -977,8 +977,9 @@ func (s *Server) HandlePolicies(w http.ResponseWriter, r *http.Request) {
req := &api.PolicyConfigUpdateRequest{}
if strings.HasPrefix(r.Header.Get("Content-Type"), "application/json") {
- unmarshaler := protojson.UnmarshalOptions{DiscardUnknown: true}
- if err := unmarshaler.Unmarshal(body, req); err != nil {
+ // Strict: an unknown field here is a typo like "allowed_service", and
+ // discarding it would quietly drop the permission it was meant to grant.
+ if err := protojson.Unmarshal(body, req); err != nil {
http.Error(w, "Invalid JSON format: "+err.Error(), http.StatusBadRequest)
return
}
diff --git a/internal/controlplane/server_test.go b/internal/controlplane/server_test.go
index 0ced9089..57b592a2 100644
--- a/internal/controlplane/server_test.go
+++ b/internal/controlplane/server_test.go
@@ -573,6 +573,47 @@ func TestPoliciesAcceptConsoleJSON(t *testing.T) {
}
}
+// A misspelled field is the difference between a permission being granted and
+// silently not existing, so it has to be an error rather than a discarded key.
+func TestPoliciesRejectUnknownJSONFields(t *testing.T) {
+ issuer, _ := startCustomMockOIDC(t)
+ srv, store, baseURL := setupTestServer(t, issuer)
+ defer func() {
+ _ = srv.Close()
+ _ = store.Close()
+ }()
+
+ srv.config.AdminToken = "super-secret-admin-token"
+ client := &http.Client{Timeout: 5 * time.Second}
+
+ for name, policy := range map[string]string{
+ "misspelled role field": `{"roles": [{"name": "dev", "allowed_service": ["mcp://git"]}]}`,
+ "misspelled top level": `{"role": [{"name": "dev"}]}`,
+ "legacy version key": `{"version": "v1alpha1", "roles": [{"name": "dev"}]}`,
+ } {
+ req, _ := http.NewRequest(http.MethodPost, baseURL+"/policies", strings.NewReader(policy))
+ req.Header.Set("Content-Type", "application/json")
+ req.Header.Set("Authorization", "Bearer super-secret-admin-token")
+ resp, err := client.Do(req)
+ if err != nil {
+ t.Fatalf("%s: POST /policies: %v", name, err)
+ }
+ _ = resp.Body.Close()
+ if resp.StatusCode != http.StatusBadRequest {
+ t.Errorf("%s: got status %d, want %d", name, resp.StatusCode, http.StatusBadRequest)
+ }
+ }
+
+ // Nothing should have been stored by any of those.
+ roles, bindings, err := store.GetMeshPolicy(context.Background())
+ if err != nil && err != storage.ErrNotFound {
+ t.Fatalf("reading back the stored policy: %v", err)
+ }
+ if len(roles) != 0 || len(bindings) != 0 {
+ t.Errorf("a rejected policy was stored anyway: roles=%v bindings=%v", roles, bindings)
+ }
+}
+
func TestPoliciesConfigurationREST(t *testing.T) {
issuer, _ := startCustomMockOIDC(t)
srv, store, baseURL := setupTestServer(t, issuer)
diff --git a/tests/integration/failover_test.go b/tests/integration/failover_test.go
index d536cdc0..2b1241de 100644
--- a/tests/integration/failover_test.go
+++ b/tests/integration/failover_test.go
@@ -46,8 +46,7 @@ func TestFailoverUpdatesRelay(t *testing.T) {
// Create a mock policy file
policyFile := filepath.Join(tmpDir, "policies.yaml")
- policyContent := `version: "v1alpha1"
-bindings: []
+ policyContent := `bindings: []
roles: []
`
writePolicyWithRouter(t, policyFile, policyContent)
diff --git a/tests/integration/federation_test.go b/tests/integration/federation_test.go
index faba4507..f587d212 100644
--- a/tests/integration/federation_test.go
+++ b/tests/integration/federation_test.go
@@ -44,8 +44,7 @@ func TestRouterFederationAndRelay(t *testing.T) {
// Create a mock policy file
policyFile := filepath.Join(tmpDir, "policies.yaml")
- policyContent := `version: "v1alpha1"
-bindings:
+ policyContent := `bindings:
- members: ["user:mock-user"]
role: admin
roles:
diff --git a/tests/integration/local_policy_test.go b/tests/integration/local_policy_test.go
index 44300ba6..c04e0f68 100644
--- a/tests/integration/local_policy_test.go
+++ b/tests/integration/local_policy_test.go
@@ -19,8 +19,7 @@ func TestLocalPolicyCanGrantPermissions(t *testing.T) {
// Control plane policy that grants NOTHING.
controlPlanePolicyFile := filepath.Join(tmpDir, "policies.yaml")
- controlPlanePolicyYAML := `version: "v1alpha1"
-roles:
+ controlPlanePolicyYAML := `roles:
- name: none
allowed_services: []
allowed_targets: ["*"]
@@ -163,8 +162,7 @@ func TestLocalPolicyCannotBypassControlPlaneTargetConstraint(t *testing.T) {
// Control plane policy that grants service access but RESTRICTS target to "group:admin-only".
controlPlanePolicyFile := filepath.Join(tmpDir, "policies.yaml")
- controlPlanePolicyYAML := `version: "v1alpha1"
-roles:
+ controlPlanePolicyYAML := `roles:
- name: restricted-role
allowed_services: ["*"]
allowed_targets: ["group:admin-only"]
diff --git a/tests/integration/mcp_local_tools_test.go b/tests/integration/mcp_local_tools_test.go
index 1f50992b..e85ec891 100644
--- a/tests/integration/mcp_local_tools_test.go
+++ b/tests/integration/mcp_local_tools_test.go
@@ -32,8 +32,7 @@ func TestMCPLocalTools(t *testing.T) {
// Create a mock policy file
policyFile := filepath.Join(tmpDir, "policies.yaml")
- policyContent := `version: "v1alpha1"
-bindings: []
+ policyContent := `bindings: []
roles: []
`
if err := os.WriteFile(policyFile, []byte(policyContent), 0644); err != nil {
diff --git a/tests/integration/multimaster_test.go b/tests/integration/multimaster_test.go
index 8d300ed1..847c0935 100644
--- a/tests/integration/multimaster_test.go
+++ b/tests/integration/multimaster_test.go
@@ -43,8 +43,7 @@ func TestMultiMasterControlPlane(t *testing.T) {
// Create a mock policy file
policyFile := filepath.Join(tmpDir, "policies.yaml")
- policyContent := `version: "v1alpha1"
-bindings: []
+ policyContent := `bindings: []
roles: []
`
writePolicyWithRouter(t, policyFile, policyContent)
diff --git a/tests/integration/policy_permutations_test.go b/tests/integration/policy_permutations_test.go
index bf9267b7..7d3ee6de 100644
--- a/tests/integration/policy_permutations_test.go
+++ b/tests/integration/policy_permutations_test.go
@@ -77,8 +77,7 @@ func TestPolicyPermutations(t *testing.T) {
// 1. Control plane policies
controlPlanePolicyFile := filepath.Join(tmpDir, "policies.yaml")
- controlPlanePolicyYAML := `version: "v1alpha1"
-roles:
+ controlPlanePolicyYAML := `roles:
- name: role-user
allowed_services: ["mcp://test-user"]
allowed_targets: ["user:bob-subject"]
diff --git a/tests/integration/revocation_test.go b/tests/integration/revocation_test.go
index 64db971e..8c35e62a 100644
--- a/tests/integration/revocation_test.go
+++ b/tests/integration/revocation_test.go
@@ -45,8 +45,7 @@ func TestNodeRevocationIntegration(t *testing.T) {
// 1. Create a mock policy file granting "router" role
policyFile := filepath.Join(tmpDir, "policies.yaml")
- policyContent := `version: "v1alpha1"
-bindings:
+ policyContent := `bindings:
- members: ["user:mock-user"]
role: admin
roles:
diff --git a/tests/integration/rotation_test.go b/tests/integration/rotation_test.go
index 9a5a5a7e..a25a90af 100644
--- a/tests/integration/rotation_test.go
+++ b/tests/integration/rotation_test.go
@@ -40,8 +40,7 @@ func TestKeyRotationIntegration(t *testing.T) {
// 1. Create a mock policy file
policyFile := filepath.Join(tmpDir, "policies.yaml")
- policyContent := `version: "v1alpha1"
-bindings:
+ policyContent := `bindings:
- members: ["user:mock-user"]
role: admin
roles: