GlobalNetworkPolicy: empty `nets: []` list unexpectedly matches all traffic (behaves as "match all")
Expected Behavior
When the nets list inside a GlobalNetworkPolicy rule's destination field is explicitly set to empty (nets: []), it should be treated as an empty match set. This means it should match nothing, preventing the rule from firing and causing the traffic to be dropped by subsequent Deny rules.
Current Behavior
An empty list nets: [] behaves as if the field were completely unset, which evaluates to a "match all" semantic. Instead of blocking the traffic, the rule unexpectedly allows all TCP traffic to pass through, bypassing targeted restrictions.
Possible Solution
The schema validation or the policy engine evaluation should explicitly differentiate between a missing/nil nets field (which means match all) and an explicitly defined empty array nets: [] (which should mean match none). If this behavior is intentional due to backward compatibility or API design, it must be explicitly documented in the API reference guide.
Steps to Reproduce (for bugs)
- Create a test namespace and launch a client workload:
kubectl create ns calico-lab
kubectl -n calico-lab create deploy netshoot --image=nicolaka/netshoot -- sleep infinity- Apply the control policy. This policy restricts TCP egress traffic to only one host (
1.1.1.1/32):
apiVersion: projectcalico.org/v3
kind: GlobalNetworkPolicy
metadata:
name: calico-test-control
spec:
order: 100
namespaceSelector: kubernetes.io/metadata.name == "calico-lab"
selector: app == "netshoot"
types: [Egress]
egress:
- action: Allow
protocol: UDP
destination:
selector: k8s-app == "kube-dns"
namespaceSelector: kubernetes.io/metadata.name == "kube-system"
ports: [53]
- action: Allow
protocol: TCP
destination:
nets: ["1.1.1.1/32"]
- action: Log
- action: Deny- Modify the policy to use an empty list (
nets: []) instead:
apiVersion: projectcalico.org/v3
kind: GlobalNetworkPolicy
metadata:
name: calico-test-variant
spec:
order: 100
namespaceSelector: kubernetes.io/metadata.name == "calico-lab"
selector: app == "netshoot"
types: [Egress]
egress:
- action: Allow
protocol: UDP
destination:
selector: k8s-app == "kube-dns"
namespaceSelector: kubernetes.io/metadata.name == "kube-system"
ports: [53]
- action: Allow
protocol: TCP
destination:
nets: []
- action: Log
- action: Deny- Run a probe command to test connectivity. Note that DNS is used as a control indicator to ensure the policy applies to the pod correctly:
kubectl -n calico-lab exec deploy/netshoot -- sh -c '
curl -sS -m3 -k -o /dev/null -w "tcp: %{http_code}\n" https://1.1.1.1 || echo "tcp: DENIED"
dig +short +time=2 kubernetes.default.svc.cluster.local >/dev/null && echo "dns: ok" || echo "dns: DENIED"'Observed Results
| Policy | tcp | dns |
|---|---|---|
| none (baseline) | Allow | Allow |
nets: ["1.1.1.1/32"] |
Allow | Allow |
nets: [] |
Allow (unexpectedly matches all traffic) | Allow |
Context
I am writing automated network policies and trying to understand the exact semantics of the nets API parameters. Relying on undocumented behavior where an empty slice overrides security rules to match all traffic introduces unexpected security risks.
Calico version: v3.31.5
Source: projectcalico/calico