Skip to content

Commit f83e751

Browse files
committed
fix(cozystack-basics): normalize tenant hostname VAP comparisons with lowerAscii
The Ingress, Gateway, and Route hostname ValidatingAdmissionPolicies compare tenant hostnames against the tenant apex with case-sensitive CEL. The resource-side host is DNS-1123 lowercase already, but the apex operand is the namespace.cozystack.io/host label value, which Kubernetes permits to carry uppercase. A mixed-case apex label then wrongly denies a tenant's own legitimate lowercase in-apex hostname, and an uppercase root-host would let a host under the platform apex slip through the Ingress outside-root exclusion. Wrap both operands of every comparison in .lowerAscii() so the match follows DNS case semantics — no behavior change for the all-lowercase default. Covered by helm-unittest assertions pinning the normalization in all three policies and e2e probes that set a mixed-case apex on an isolated namespace and assert a lowercase in-apex HTTPRoute and Ingress are still admitted. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent b6ac249 commit f83e751

7 files changed

Lines changed: 216 additions & 16 deletions

File tree

‎hack/e2e-apps/gateway.bats‎

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,70 @@ EOF
401401
kubectl -n tenant-test delete httproute route-hostname-allow-probe --ignore-not-found
402402
}
403403

404+
@test "cozystack-route-hostname-policy VAP matches the tenant apex case-insensitively" {
405+
# Defense-in-depth: the route hostname is DNS-1123 lowercase, but the apex
406+
# operand is the namespace.cozystack.io/host label VALUE, which Kubernetes
407+
# permits to carry uppercase. Under a mixed-case apex (Test.Example.Org),
408+
# a lowercase in-apex HTTPRoute (probe.test.example.org) must still be
409+
# ALLOWED — the VAP normalizes both operands with .lowerAscii(). Without
410+
# that normalization the VAP compares "probe.test.example.org" against apex
411+
# "Test.Example.Org" and wrongly denies a legitimate route, so this fails
412+
# pre-fix and passes post-fix. This is the only runtime shape that
413+
# exercises .lowerAscii(): an uppercase hostname on the route itself is
414+
# stripped by built-in DNS-1123 validation before the VAP ever runs.
415+
#
416+
# Use a throwaway tenant-* namespace with its own mixed-case apex rather
417+
# than relabelling the shared tenant-test namespace: an interrupted run or
418+
# a failed restore would otherwise leave tenant-test mixed-case and poison
419+
# every later test. The tenant-* prefix is required for the VAP's
420+
# matchCondition to fire; the CREATE with the host label is admitted
421+
# because the e2e identity is a trusted caller for the namespace host-label
422+
# immutability VAP.
423+
# Pre-clean a stale namespace left by an interrupted prior run (e2e convention).
424+
kubectl delete namespace tenant-case-probe --ignore-not-found --wait=true --timeout=60s
425+
kubectl apply -f - <<'EOF'
426+
apiVersion: v1
427+
kind: Namespace
428+
metadata:
429+
name: tenant-case-probe
430+
labels:
431+
namespace.cozystack.io/host: Test.Example.Org
432+
EOF
433+
# Guard against a vacuous pass: the apex must actually be mixed-case. A
434+
# lowercase apex would admit the route regardless of the fix, and an absent
435+
# apex would deny it via the fail-closed nil-guard — either way the test
436+
# would stop proving case-insensitivity.
437+
apex=$(kubectl get namespace tenant-case-probe -o jsonpath='{.metadata.labels.namespace\.cozystack\.io/host}')
438+
if [ "$apex" != "Test.Example.Org" ]; then
439+
echo "SETUP FAILURE: tenant-case-probe apex is not mixed-case (got '$apex')" >&2
440+
kubectl delete namespace tenant-case-probe --ignore-not-found --wait=false
441+
return 1
442+
fi
443+
if output=$(kubectl apply -f - 2>&1 <<'EOF'
444+
apiVersion: gateway.networking.k8s.io/v1
445+
kind: HTTPRoute
446+
metadata:
447+
name: route-hostname-case-probe
448+
namespace: tenant-case-probe
449+
spec:
450+
hostnames:
451+
- "probe.test.example.org"
452+
rules:
453+
- backendRefs:
454+
- name: kubernetes
455+
namespace: default
456+
port: 443
457+
EOF
458+
); then
459+
kubectl delete namespace tenant-case-probe --ignore-not-found --wait=false
460+
else
461+
kubectl delete namespace tenant-case-probe --ignore-not-found --wait=false
462+
echo "BUG: VAP denied a lowercase in-apex HTTPRoute under a mixed-case apex — .lowerAscii() normalization missing or broken" >&2
463+
echo "$output" >&2
464+
return 1
465+
fi
466+
}
467+
404468
@test "cozystack-ingress-hostname-policy VAP and binding are installed" {
405469
# Legacy-Ingress counterpart of the gateway/route hostname VAPs. Gateway
406470
# API is off by default, so tenant apps publish through a legacy
@@ -667,6 +731,78 @@ EOF
667731
kubectl -n tenant-test delete ingress ingress-hostname-allow-probe --ignore-not-found
668732
}
669733

734+
@test "cozystack-ingress-hostname-policy VAP matches the tenant apex case-insensitively" {
735+
# Defense-in-depth: the Ingress rules[].host and tls[].hosts[] are DNS-1123
736+
# lowercase, but the apex operand is the namespace.cozystack.io/host label
737+
# VALUE, which Kubernetes permits to carry uppercase. Under a mixed-case
738+
# apex (Test.Example.Org), a lowercase in-apex Ingress (rule host and tls
739+
# host both probe.test.example.org) must still be ALLOWED — the VAP
740+
# normalizes both operands with .lowerAscii(). Without that normalization
741+
# the VAP compares "probe.test.example.org" against apex "Test.Example.Org"
742+
# and wrongly denies a legitimate Ingress, so this fails pre-fix and passes
743+
# post-fix. Ingress is the default publish path (Gateway API is off by
744+
# default) and carries the most complex CEL (both the rule-host and tls-host
745+
# branches), so it gets its own runtime guard alongside the route test.
746+
#
747+
# Use a throwaway tenant-* namespace with its own mixed-case apex rather
748+
# than relabelling the shared tenant-test namespace: an interrupted run or
749+
# a failed restore would otherwise leave tenant-test mixed-case and poison
750+
# every later test. The tenant-* prefix is required for the VAP's
751+
# matchCondition to fire; the CREATE with the host label is admitted
752+
# because the e2e identity is a trusted caller for the namespace host-label
753+
# immutability VAP.
754+
# Pre-clean a stale namespace left by an interrupted prior run (e2e convention).
755+
kubectl delete namespace tenant-ingress-case-probe --ignore-not-found --wait=true --timeout=60s
756+
kubectl apply -f - <<'EOF'
757+
apiVersion: v1
758+
kind: Namespace
759+
metadata:
760+
name: tenant-ingress-case-probe
761+
labels:
762+
namespace.cozystack.io/host: Test.Example.Org
763+
EOF
764+
# Guard against a vacuous pass: the apex must actually be mixed-case. A
765+
# lowercase apex would admit the Ingress regardless of the fix, and an
766+
# absent apex would deny it via the fail-closed nil-guard.
767+
apex=$(kubectl get namespace tenant-ingress-case-probe -o jsonpath='{.metadata.labels.namespace\.cozystack\.io/host}')
768+
if [ "$apex" != "Test.Example.Org" ]; then
769+
echo "SETUP FAILURE: tenant-ingress-case-probe apex is not mixed-case (got '$apex')" >&2
770+
kubectl delete namespace tenant-ingress-case-probe --ignore-not-found --wait=false
771+
return 1
772+
fi
773+
if output=$(kubectl apply -f - 2>&1 <<'EOF'
774+
apiVersion: networking.k8s.io/v1
775+
kind: Ingress
776+
metadata:
777+
name: ingress-hostname-case-probe
778+
namespace: tenant-ingress-case-probe
779+
spec:
780+
tls:
781+
- hosts:
782+
- "probe.test.example.org"
783+
secretName: noop-tls
784+
rules:
785+
- host: "probe.test.example.org"
786+
http:
787+
paths:
788+
- path: /
789+
pathType: Prefix
790+
backend:
791+
service:
792+
name: noop
793+
port:
794+
number: 80
795+
EOF
796+
); then
797+
kubectl delete namespace tenant-ingress-case-probe --ignore-not-found --wait=false
798+
else
799+
kubectl delete namespace tenant-ingress-case-probe --ignore-not-found --wait=false
800+
echo "BUG: VAP denied a lowercase in-apex Ingress under a mixed-case apex — .lowerAscii() normalization missing or broken" >&2
801+
echo "$output" >&2
802+
return 1
803+
fi
804+
}
805+
670806
@test "cozystack-ingress-hostname-policy VAP allows an Ingress host outside the platform root apex" {
671807
# Narrow rule: a tenant may route its own domain that lies entirely
672808
# outside the platform root apex (example.org). This is the path the

‎packages/system/cozystack-basics/templates/gateway-hostname-policy.yaml‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@ spec:
2525
? namespaceObject.metadata.labels["namespace.cozystack.io/host"]
2626
: ""
2727
validations:
28+
# Both operands are normalized with .lowerAscii() as defense-in-depth:
29+
# the listener hostname is DNS-1123 lowercase already, but tenantHost
30+
# comes from the namespace.cozystack.io/host label value, which
31+
# Kubernetes permits to carry uppercase.
2832
- expression: >-
2933
variables.tenantHost == "" ||
3034
!has(object.spec.listeners) ||
3135
object.spec.listeners.all(l,
3236
!has(l.hostname) ||
33-
l.hostname == variables.tenantHost ||
34-
l.hostname.endsWith("." + variables.tenantHost)
37+
l.hostname.lowerAscii() == variables.tenantHost.lowerAscii() ||
38+
l.hostname.lowerAscii().endsWith("." + variables.tenantHost.lowerAscii())
3539
)
3640
messageExpression: >-
3741
"Gateway listener hostname must equal " + variables.tenantHost +

‎packages/system/cozystack-basics/templates/ingress-hostname-policy.yaml‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,15 @@
4343
has() then walks into metadata.labels safely. See the feedback memory
4444
cel-has-field-only.md.
4545

46-
Case handling: kube-apiserver validates spec.rules[].host and
47-
spec.tls[].hosts[] as DNS-1123 subdomains (lowercase only), so the
48-
apex/subdomain string comparisons below need no case normalization — a
49-
mixed-case host is rejected by built-in validation and never reaches a
50-
created state, so casing cannot be used to sidestep the comparisons.
46+
Case handling: every comparison below normalizes both operands with
47+
.lowerAscii() as defense-in-depth. The resource-side host is already
48+
lowercase — kube-apiserver validates spec.rules[].host and
49+
spec.tls[].hosts[] as DNS-1123 subdomains (lowercase only), so a
50+
mixed-case host is rejected by built-in validation before this VAP
51+
runs — but the apex operand is the namespace.cozystack.io/host label
52+
VALUE, and Kubernetes permits uppercase in a label value. Lowercasing
53+
both sides keeps the match correct even if the apex label (or the
54+
templated root-host) ever carries uppercase.
5155

5256
Defense-in-depth contract: this VAP is fail-closed. A tenant-*
5357
namespace without `namespace.cozystack.io/host` is rejected (not
@@ -59,7 +63,7 @@
5963
{{- $rootHost := index .Values._cluster "root-host" }}
6064
{{- if $rootHost }}
6165
{{- $A := `namespaceObject.metadata.labels["namespace.cozystack.io/host"]` }}
62-
{{- $celValidator := printf `(namespaceObject == null || !has(namespaceObject.metadata.labels) || !("namespace.cozystack.io/host" in namespaceObject.metadata.labels)) ? false : (!has(object.spec.defaultBackend) && (!has(object.spec.rules) || object.spec.rules.all(r, has(r.host) && r.host != "" && (r.host == %s || r.host.endsWith("." + %s) || (!r.host.startsWith("*.") && !(r.host == %q || r.host.endsWith(%q)))))) && (!has(object.spec.tls) || object.spec.tls.all(t, !has(t.hosts) || t.hosts.all(h, h != "" && (h == %s || h.endsWith("." + %s) || (!h.startsWith("*.") && !(h == %q || h.endsWith(%q))))))))` $A $A $rootHost (printf ".%s" $rootHost) $A $A $rootHost (printf ".%s" $rootHost) -}}
66+
{{- $celValidator := printf `(namespaceObject == null || !has(namespaceObject.metadata.labels) || !("namespace.cozystack.io/host" in namespaceObject.metadata.labels)) ? false : (!has(object.spec.defaultBackend) && (!has(object.spec.rules) || object.spec.rules.all(r, has(r.host) && r.host != "" && (r.host.lowerAscii() == %s.lowerAscii() || r.host.lowerAscii().endsWith("." + %s.lowerAscii()) || (!r.host.lowerAscii().startsWith("*.") && !(r.host.lowerAscii() == %q.lowerAscii() || r.host.lowerAscii().endsWith(%q.lowerAscii())))))) && (!has(object.spec.tls) || object.spec.tls.all(t, !has(t.hosts) || t.hosts.all(h, h != "" && (h.lowerAscii() == %s.lowerAscii() || h.lowerAscii().endsWith("." + %s.lowerAscii()) || (!h.lowerAscii().startsWith("*.") && !(h.lowerAscii() == %q.lowerAscii() || h.lowerAscii().endsWith(%q.lowerAscii()))))))))` $A $A $rootHost (printf ".%s" $rootHost) $A $A $rootHost (printf ".%s" $rootHost) -}}
6367
---
6468
apiVersion: admissionregistration.k8s.io/v1
6569
kind: ValidatingAdmissionPolicy

‎packages/system/cozystack-basics/templates/route-hostname-policy.yaml‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@
3535
label is absent is an in-flight namespace creation that hasn't
3636
finished labelling yet — in which case the caller should retry.
3737
*/}}
38-
{{- $celValidator := `(namespaceObject == null || !has(namespaceObject.metadata.labels) || !("namespace.cozystack.io/host" in namespaceObject.metadata.labels)) ? false : (!has(object.spec.hostnames) || object.spec.hostnames.all(h, h == namespaceObject.metadata.labels["namespace.cozystack.io/host"] || h.endsWith("." + namespaceObject.metadata.labels["namespace.cozystack.io/host"])))` -}}
38+
{{- /*
39+
Both operands of the hostname comparison are normalized with
40+
.lowerAscii() as defense-in-depth: the route hostname is DNS-1123
41+
lowercase already, but the apex operand is the
42+
namespace.cozystack.io/host label value, which Kubernetes permits to
43+
carry uppercase.
44+
*/}}
45+
{{- $celValidator := `(namespaceObject == null || !has(namespaceObject.metadata.labels) || !("namespace.cozystack.io/host" in namespaceObject.metadata.labels)) ? false : (!has(object.spec.hostnames) || object.spec.hostnames.all(h, h.lowerAscii() == namespaceObject.metadata.labels["namespace.cozystack.io/host"].lowerAscii() || h.lowerAscii().endsWith("." + namespaceObject.metadata.labels["namespace.cozystack.io/host"].lowerAscii())))` -}}
3946
---
4047
apiVersion: admissionregistration.k8s.io/v1
4148
kind: ValidatingAdmissionPolicy

‎packages/system/cozystack-basics/tests/gateway-hostname-policy_test.yaml‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,18 @@ tests:
6868
- documentIndex: 0
6969
matchRegex:
7070
path: spec.validations[0].expression
71-
pattern: 'l\.hostname\.endsWith'
71+
pattern: 'l\.hostname\.lowerAscii\(\)\.endsWith'
7272
- documentIndex: 0
7373
matchRegex:
7474
path: spec.validations[0].expression
7575
pattern: 'variables\.tenantHost'
76+
# Defense-in-depth: both operands normalized with
77+
# lowerAscii() — tenantHost comes from the label value, which may
78+
# carry uppercase even though the listener hostname is DNS-1123.
79+
- documentIndex: 0
80+
matchRegex:
81+
path: spec.validations[0].expression
82+
pattern: 'l\.hostname\.lowerAscii\(\) == variables\.tenantHost\.lowerAscii\(\)'
7683

7784
- it: cozystack-gateway-hostname-policy Binding is fail-closed (validationActions Deny)
7885
asserts:

‎packages/system/cozystack-basics/tests/ingress-hostname-policy_test.yaml‎

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,32 @@ tests:
8787
path: spec.validations[0].expression
8888
pattern: 'namespace\.cozystack\.io/host'
8989

90+
- it: normalizes both operands with lowerAscii for case-insensitive comparison
91+
# Defense-in-depth: the resource-side host is DNS-1123
92+
# lowercase already, but the apex operand is the
93+
# namespace.cozystack.io/host label VALUE, which Kubernetes permits
94+
# to carry uppercase. Pin .lowerAscii() on the host operand, the apex
95+
# label operand, and the templated root-host literal for both the
96+
# rules[].host and tls[].hosts[] branches so a future edit cannot
97+
# silently drop the normalization.
98+
asserts:
99+
- documentIndex: 0
100+
matchRegex:
101+
path: spec.validations[0].expression
102+
pattern: 'r\.host\.lowerAscii\(\)'
103+
- documentIndex: 0
104+
matchRegex:
105+
path: spec.validations[0].expression
106+
pattern: 'h\.lowerAscii\(\)'
107+
- documentIndex: 0
108+
matchRegex:
109+
path: spec.validations[0].expression
110+
pattern: 'namespaceObject\.metadata\.labels\["namespace\.cozystack\.io/host"\]\.lowerAscii\(\)'
111+
- documentIndex: 0
112+
matchRegex:
113+
path: spec.validations[0].expression
114+
pattern: '"example\.org"\.lowerAscii\(\)'
115+
90116
- it: allows a hostname outside the platform root apex (external custom domain)
91117
# Narrow rule: a tenant may route its own domain that lies entirely
92118
# outside the platform root apex (templated from _cluster.root-host).
@@ -97,11 +123,11 @@ tests:
97123
- documentIndex: 0
98124
matchRegex:
99125
path: spec.validations[0].expression
100-
pattern: '!\(r\.host == "example\.org" \|\| r\.host\.endsWith\("\.example\.org"\)\)'
126+
pattern: '!\(r\.host\.lowerAscii\(\) == "example\.org"\.lowerAscii\(\) \|\| r\.host\.lowerAscii\(\)\.endsWith\("\.example\.org"\.lowerAscii\(\)\)\)'
101127
- documentIndex: 0
102128
matchRegex:
103129
path: spec.validations[0].expression
104-
pattern: '!\(h == "example\.org" \|\| h\.endsWith\("\.example\.org"\)\)'
130+
pattern: '!\(h\.lowerAscii\(\) == "example\.org"\.lowerAscii\(\) \|\| h\.lowerAscii\(\)\.endsWith\("\.example\.org"\.lowerAscii\(\)\)\)'
105131

106132
- it: does not let a wildcard use the outside-root path (would match the platform apex)
107133
# A wildcard on the root's parent label (e.g. *.org when root is
@@ -113,11 +139,11 @@ tests:
113139
- documentIndex: 0
114140
matchRegex:
115141
path: spec.validations[0].expression
116-
pattern: '!r\.host\.startsWith\("\*\."\) && !\(r\.host == "example\.org"'
142+
pattern: '!r\.host\.lowerAscii\(\)\.startsWith\("\*\."\) && !\(r\.host\.lowerAscii\(\) == "example\.org"\.lowerAscii\(\)'
117143
- documentIndex: 0
118144
matchRegex:
119145
path: spec.validations[0].expression
120-
pattern: '!h\.startsWith\("\*\."\) && !\(h == "example\.org"'
146+
pattern: '!h\.lowerAscii\(\)\.startsWith\("\*\."\) && !\(h\.lowerAscii\(\) == "example\.org"\.lowerAscii\(\)'
121147

122148
- it: allows a wildcard under the own apex (own-apex path is not wildcard-gated)
123149
# The !startsWith("*.") gate applies ONLY to the outside-root branch.
@@ -130,11 +156,11 @@ tests:
130156
- documentIndex: 0
131157
matchRegex:
132158
path: spec.validations[0].expression
133-
pattern: 'r\.host\.endsWith\("\." \+ namespaceObject\.metadata\.labels\["namespace\.cozystack\.io/host"\]\) \|\| \(!r\.host\.startsWith\("\*\."\)'
159+
pattern: 'r\.host\.lowerAscii\(\)\.endsWith\("\." \+ namespaceObject\.metadata\.labels\["namespace\.cozystack\.io/host"\]\.lowerAscii\(\)\) \|\| \(!r\.host\.lowerAscii\(\)\.startsWith\("\*\."\)'
134160
- documentIndex: 0
135161
matchRegex:
136162
path: spec.validations[0].expression
137-
pattern: 'h\.endsWith\("\." \+ namespaceObject\.metadata\.labels\["namespace\.cozystack\.io/host"\]\) \|\| \(!h\.startsWith\("\*\."\)'
163+
pattern: 'h\.lowerAscii\(\)\.endsWith\("\." \+ namespaceObject\.metadata\.labels\["namespace\.cozystack\.io/host"\]\.lowerAscii\(\)\) \|\| \(!h\.lowerAscii\(\)\.startsWith\("\*\."\)'
138164

139165
- it: rejects a hostless or empty-host catch-all rule
140166
# A legacy Ingress rule with no .host (absent OR empty string) matches

‎packages/system/cozystack-basics/tests/route-hostname-policy_test.yaml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@ tests:
5353
path: spec.validations[0].expression
5454
pattern: 'namespace\.cozystack\.io/host'
5555

56+
- it: normalizes both operands with lowerAscii for case-insensitive comparison
57+
# Defense-in-depth: the route hostname is DNS-1123 lowercase
58+
# already, but the apex operand is the namespace.cozystack.io/host
59+
# label VALUE, which Kubernetes permits to carry uppercase. Pin
60+
# .lowerAscii() on both operands for HTTPRoute (doc 0) and TLSRoute
61+
# (doc 2), which share the same CEL validator.
62+
asserts:
63+
- documentIndex: 0
64+
matchRegex:
65+
path: spec.validations[0].expression
66+
pattern: 'h\.lowerAscii\(\) == namespaceObject\.metadata\.labels\["namespace\.cozystack\.io/host"\]\.lowerAscii\(\)'
67+
- documentIndex: 2
68+
matchRegex:
69+
path: spec.validations[0].expression
70+
pattern: 'h\.lowerAscii\(\) == namespaceObject\.metadata\.labels\["namespace\.cozystack\.io/host"\]\.lowerAscii\(\)'
71+
5672
- it: validation actions are Deny so requests fail closed
5773
asserts:
5874
- documentIndex: 1

0 commit comments

Comments
 (0)