Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

test(np-08): reject broad internal policy selectors

Alfredo Lopezea0f7f988ba8
diff --git a/scripts/security/check-np08-mcp-host-networkpolicy.rb b/scripts/security/check-np08-mcp-host-networkpolicy.rb
index 986d7fa84..3d4f9ef17 100755
--- a/scripts/security/check-np08-mcp-host-networkpolicy.rb
+++ b/scripts/security/check-np08-mcp-host-networkpolicy.rb
@@ -89,6 +89,9 @@ broad_internal_peer = lambda do |peer, policy|
labels = namespace_selector["matchLabels"]
namespace_name = labels.is_a?(Hash) ? labels["kubernetes.io/metadata.name"] : nil
+ exact_namespace_selector = namespace_selector.keys.sort == ["matchLabels"] &&
+ labels == { "kubernetes.io/metadata.name" => namespace_name }
+ next true unless exact_namespace_selector
pod_selector = peer["podSelector"]
pod_labels = pod_selector.is_a?(Hash) ? pod_selector["matchLabels"] : nil
pod_expressions = pod_selector.is_a?(Hash) ? pod_selector["matchExpressions"] : nil
@@ -97,12 +100,16 @@ broad_internal_peer = lambda do |peer, policy|
if namespace_name == "mcp-server"
server_name = pod_selector.dig("matchLabels", "clerum.io/mcpserver") if pod_selector.is_a?(Hash)
policy_labels = policy.dig("metadata", "labels")
- source_labels = policy.dig("spec", "podSelector", "matchLabels")
+ source_selector = policy.dig("spec", "podSelector")
+ source_labels = source_selector.is_a?(Hash) ? source_selector["matchLabels"] : nil
context_bound_policy = policy_labels.is_a?(Hash) &&
+ policy_labels["clerum.io/managed-by"] == "host-context-controller" &&
policy_labels["clerum.io/policy-type"] == "context-allow" &&
policy_labels["clerum.io/context"].is_a?(String) && !policy_labels["clerum.io/context"].empty? &&
policy_labels["clerum.io/mcpserver"].is_a?(String) && !policy_labels["clerum.io/mcpserver"].empty? &&
+ source_selector.is_a?(Hash) && source_selector.keys.sort == ["matchLabels"] &&
source_labels.is_a?(Hash) &&
+ source_labels.keys.sort == ["clerum.io/context", "clerum.io/managed-by"] &&
source_labels["clerum.io/managed-by"] == "host-context-controller" &&
source_labels["clerum.io/context"] == policy_labels["clerum.io/context"]
exact_server_selector = server_name.is_a?(String) && !server_name.empty? &&
@@ -111,9 +118,27 @@ broad_internal_peer = lambda do |peer, policy|
next false if exact_server_selector
next true
end
- next true if !labels.is_a?(Hash) || labels.empty?
+ if namespace_name == "control-plane"
+ exact_gateway = pod_selector.is_a?(Hash) &&
+ pod_selector.keys.sort == ["matchLabels"] &&
+ pod_selector["matchLabels"].is_a?(Hash) &&
+ Array(pod_selector["matchExpressions"]).empty? &&
+ %w[host-context-controller-api-gateway nginx-workflow-approval-gateway].include?(pod_selector.dig("matchLabels", "app")) &&
+ pod_selector["matchLabels"].keys == ["app"]
+ next false if exact_gateway
+ next true
+ end
+ if namespace_name == "kube-system"
+ exact_dns_namespace = pod_selector.nil?
+ exact_dns_pods = pod_selector.is_a?(Hash) &&
+ pod_selector.keys.sort == ["matchLabels"] &&
+ pod_selector["matchLabels"] == { "k8s-app" => "kube-dns" } &&
+ Array(pod_selector["matchExpressions"]).empty?
+ next false if exact_dns_namespace || exact_dns_pods
+ next true
+ end
- !has_pod_selector && namespace_name != "kube-system"
+ next true
end
egress_contract_ok = policy_rules.all? do |policy, rule|
diff --git a/scripts/tests/test-np08-live-networkpolicy-contract.sh b/scripts/tests/test-np08-live-networkpolicy-contract.sh
index 88eb1e467..b3dfd56ff 100755
--- a/scripts/tests/test-np08-live-networkpolicy-contract.sh
+++ b/scripts/tests/test-np08-live-networkpolicy-contract.sh
@@ -63,7 +63,11 @@ wide="$(mutate wide 'data["items"].find { |d| d["metadata"]["name"] == "mcp-host
assert_result "${wide}" '{"egress_contract_ok":false,"hcc_lane":true,"proxy_8083":false}'
echo "PASS: live policy helper rejects wide internal selectors"
-mcp_server="$(mutate mcp-server 'data["items"] << { "apiVersion" => "networking.k8s.io/v1", "kind" => "NetworkPolicy", "metadata" => { "name" => "ctx-a-server-a-egress", "namespace" => "mcp-host", "labels" => { "clerum.io/policy-type" => "context-allow", "clerum.io/context" => "context-a", "clerum.io/mcpserver" => "server-a" } }, "spec" => { "podSelector" => { "matchLabels" => { "clerum.io/managed-by" => "host-context-controller", "clerum.io/context" => "context-a" } }, "policyTypes" => ["Egress"], "egress" => [{ "to" => [{ "namespaceSelector" => { "matchLabels" => { "kubernetes.io/metadata.name" => "mcp-server" } }, "podSelector" => { "matchLabels" => { "clerum.io/mcpserver" => "server-a" } } }], "ports" => [{ "port" => 3000, "protocol" => "TCP" }] }] } }')"
+broad_expression="$(mutate broad-expression 'data["items"].find { |d| d["metadata"]["name"] == "mcp-host" }["spec"]["egress"] << { "to" => [{ "namespaceSelector" => { "matchLabels" => { "kubernetes.io/metadata.name" => "control-plane" }, "matchExpressions" => [{ "key" => "app", "operator" => "Exists" }] }, "podSelector" => { "matchExpressions" => [{ "key" => "app", "operator" => "Exists" }] } }], "ports" => [{ "port" => 443, "protocol" => "TCP" }] }')"
+assert_result "${broad_expression}" '{"egress_contract_ok":false,"hcc_lane":true,"proxy_8083":false}'
+echo "PASS: live policy helper rejects broad internal matchExpressions"
+
+mcp_server="$(mutate mcp-server 'data["items"] << { "apiVersion" => "networking.k8s.io/v1", "kind" => "NetworkPolicy", "metadata" => { "name" => "ctx-a-server-a-egress", "namespace" => "mcp-host", "labels" => { "clerum.io/managed-by" => "host-context-controller", "clerum.io/policy-type" => "context-allow", "clerum.io/context" => "context-a", "clerum.io/mcpserver" => "server-a" } }, "spec" => { "podSelector" => { "matchLabels" => { "clerum.io/managed-by" => "host-context-controller", "clerum.io/context" => "context-a" } }, "policyTypes" => ["Egress"], "egress" => [{ "to" => [{ "namespaceSelector" => { "matchLabels" => { "kubernetes.io/metadata.name" => "mcp-server" } }, "podSelector" => { "matchLabels" => { "clerum.io/mcpserver" => "server-a" } } }], "ports" => [{ "port" => 3000, "protocol" => "TCP" }] }] } }')"
assert_result "${mcp_server}" '{"egress_contract_ok":true,"hcc_lane":true,"proxy_8083":false}'
echo "PASS: live policy helper accepts a context-scoped MCP server peer"
diff --git a/scripts/tests/test-np08-render-contract-fixtures.sh b/scripts/tests/test-np08-render-contract-fixtures.sh
index a61feb8ea..a4bdc53b0 100755
--- a/scripts/tests/test-np08-render-contract-fixtures.sh
+++ b/scripts/tests/test-np08-render-contract-fixtures.sh
@@ -70,6 +70,27 @@ when 'wide-internal-egress'
],
'ports' => [{ 'port' => 443, 'protocol' => 'TCP' }],
}
+when 'broad-internal-expression'
+ policy = documents.find do |document|
+ document['kind'] == 'NetworkPolicy' &&
+ document.dig('metadata', 'namespace') == 'mcp-host' &&
+ document.dig('metadata', 'name') == 'mcp-host'
+ end
+ abort('fixture source is missing mcp-host NetworkPolicy') unless policy
+ policy.fetch('spec').fetch('egress') << {
+ 'to' => [
+ {
+ 'namespaceSelector' => {
+ 'matchLabels' => { 'kubernetes.io/metadata.name' => 'control-plane' },
+ 'matchExpressions' => [{ 'key' => 'app', 'operator' => 'Exists' }],
+ },
+ 'podSelector' => {
+ 'matchExpressions' => [{ 'key' => 'app', 'operator' => 'Exists' }],
+ },
+ },
+ ],
+ 'ports' => [{ 'port' => 443, 'protocol' => 'TCP' }],
+ }
when 'named-port'
policy = documents.find do |document|
document['kind'] == 'NetworkPolicy' &&
@@ -175,6 +196,13 @@ assert_rejected \
'mcp-host must not gain broad internal or mcp-server egress' \
'wide internal egress selector'
+broad_internal_expression="${tmpdir}/broad-internal-expression.yaml"
+mutate_render broad-internal-expression "${broad_internal_expression}"
+assert_rejected \
+ "${broad_internal_expression}" \
+ 'mcp-host must not gain broad internal or mcp-server egress' \
+ 'broad internal matchExpression selector'
+
named_port="${tmpdir}/named-port.yaml"
mutate_render named-port "${named_port}"
assert_rejected \
diff --git a/scripts/tests/test-np08-render-contract.sh b/scripts/tests/test-np08-render-contract.sh
index 1a403f108..13b355268 100755
--- a/scripts/tests/test-np08-render-contract.sh
+++ b/scripts/tests/test-np08-render-contract.sh
@@ -149,6 +149,9 @@ for rendered in "$@"; do
labels = namespace_selector["matchLabels"]
namespace_name = labels.is_a?(Hash) ? labels["kubernetes.io/metadata.name"] : nil
+ exact_namespace_selector = namespace_selector.keys.sort == ["matchLabels"] &&
+ labels == { "kubernetes.io/metadata.name" => namespace_name }
+ next true unless exact_namespace_selector
pod_selector = peer["podSelector"]
pod_labels = pod_selector.is_a?(Hash) ? pod_selector["matchLabels"] : nil
pod_expressions = pod_selector.is_a?(Hash) ? pod_selector["matchExpressions"] : nil
@@ -157,12 +160,16 @@ for rendered in "$@"; do
if namespace_name == "mcp-server"
server_name = pod_selector.dig("matchLabels", "clerum.io/mcpserver") if pod_selector.is_a?(Hash)
policy_labels = policy.dig("metadata", "labels")
- source_labels = policy.dig("spec", "podSelector", "matchLabels")
+ source_selector = policy.dig("spec", "podSelector")
+ source_labels = source_selector.is_a?(Hash) ? source_selector["matchLabels"] : nil
context_bound_policy = policy_labels.is_a?(Hash) &&
+ policy_labels["clerum.io/managed-by"] == "host-context-controller" &&
policy_labels["clerum.io/policy-type"] == "context-allow" &&
policy_labels["clerum.io/context"].is_a?(String) && !policy_labels["clerum.io/context"].empty? &&
policy_labels["clerum.io/mcpserver"].is_a?(String) && !policy_labels["clerum.io/mcpserver"].empty? &&
+ source_selector.is_a?(Hash) && source_selector.keys.sort == ["matchLabels"] &&
source_labels.is_a?(Hash) &&
+ source_labels.keys.sort == ["clerum.io/context", "clerum.io/managed-by"] &&
source_labels["clerum.io/managed-by"] == "host-context-controller" &&
source_labels["clerum.io/context"] == policy_labels["clerum.io/context"]
exact_server_selector = server_name.is_a?(String) && !server_name.empty? &&
@@ -171,9 +178,27 @@ for rendered in "$@"; do
next false if exact_server_selector
next true
end
- next true if !labels.is_a?(Hash) || labels.empty?
+ if namespace_name == "control-plane"
+ exact_gateway = pod_selector.is_a?(Hash) &&
+ pod_selector.keys.sort == ["matchLabels"] &&
+ pod_selector["matchLabels"].is_a?(Hash) &&
+ Array(pod_selector["matchExpressions"]).empty? &&
+ %w[host-context-controller-api-gateway nginx-workflow-approval-gateway].include?(pod_selector.dig("matchLabels", "app")) &&
+ pod_selector["matchLabels"].keys == ["app"]
+ next false if exact_gateway
+ next true
+ end
+ if namespace_name == "kube-system"
+ exact_dns_namespace = pod_selector.nil?
+ exact_dns_pods = pod_selector.is_a?(Hash) &&
+ pod_selector.keys.sort == ["matchLabels"] &&
+ pod_selector["matchLabels"] == { "k8s-app" => "kube-dns" } &&
+ Array(pod_selector["matchExpressions"]).empty?
+ next false if exact_dns_namespace || exact_dns_pods
+ next true
+ end
- !has_pod_selector && namespace_name != "kube-system"
+ next true
end
broad_internal_egress = policies.any? do |policy|
Array(policy.dig("spec", "egress")).any? do |rule|