Skip to content

Commit e94481c

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 already lowercase — the apiserver's hostname validation permits nothing else — but the apex operand is the namespace.cozystack.io/host label value, which Kubernetes permits to carry uppercase, and the Ingress policy additionally compares against the templated root host, a free-form platform value that nothing validates for case. A mixed-case apex label wrongly denies a tenant its own legitimate lowercase in-apex hostname. An uppercase root host is the dangerous direction: the Ingress outside-root exclusion compares a lowercase host against the uppercase literal, never matches, so the exclusion stops excluding and the policy degenerates into admitting any non-wildcard host — every non-wildcard platform-apex name a tenant asks for, including the root apex itself. Only the anti-wildcard gate still holds. Wrap both operands of every comparison in .lowerAscii() so the match follows DNS case semantics — no behavior change for the all-lowercase default. Each policy reads a label it does not own: the namespace host label is settable out-of-band by a trusted caller, so none of them may assume the apex arrives normalized. The Gateway policy admits any listener hostname when the host label is absent or empty, unlike its two siblings. That is the platform's one documented fail-open exception, recorded in the threat model together with its compensating control, so changing the posture is a security-model decision rather than a side effect of normalizing case. This leaves the behavior untouched and pins its current shape in the tests. Covered by helm-unittest assertions pinning each comparison as a whole operand pair, so dropping the normalization from any single operand is caught; a rendering case that pins the uppercase-root-host escape shut; and e2e probes that set a mixed-case apex on an isolated namespace and assert both halves of the match — a lowercase in-apex HTTPRoute and Ingress are still admitted, while a foreign apex and a hostname that ends with the apex without a dot boundary are still denied. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent df541b9 commit e94481c

7 files changed

Lines changed: 483 additions & 16 deletions

File tree

‎hack/e2e-chainsaw/gateway/chainsaw-test.yaml‎

Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ metadata:
132132
name: gateway-foreign-hostname-rejected
133133
spec:
134134
steps:
135+
# No mixed-case-apex counterpart here, unlike the Route and Ingress suites.
136+
# Deliberate: this policy applies the same rule as the Route one — own apex
137+
# or a dot-bounded subdomain of it, with no outside-root branch — so a
138+
# Gateway probe would re-exercise the same two comparisons that
139+
# route-hostname-policy already drives against a mixed-case apex at runtime,
140+
# in both directions. The two differ only in how the apex reaches the
141+
# comparison: a `variables.tenantHost` binding here, the label inlined there.
142+
# Because that binding is what this suite does NOT cover, both of its
143+
# comparisons are pinned operand-by-operand in
144+
# tests/gateway-hostname-policy_test.yaml instead.
135145
- name: foreign-listener-hostname-denied
136146
try:
137147
- script:
@@ -504,6 +514,140 @@ spec:
504514
kubectl -n "$ns" delete httproute route-hostname-allow-probe --ignore-not-found
505515
check:
506516
($error == null): true
517+
# Case-insensitivity. The route hostname is always lowercase — the CRD's
518+
# hostname pattern permits only lowercase, so an uppercase hostname is rejected
519+
# before the VAP runs — but the apex operand is the namespace.cozystack.io/host
520+
# label VALUE, and Kubernetes permits uppercase in a label value. Under a
521+
# mixed-case apex a lowercase in-apex route must still be admitted, so the VAP
522+
# normalizes both operands with .lowerAscii(). Pre-fix it compares
523+
# "probe.case-probe.route.invalid" against "Case-Probe.route.invalid", misses,
524+
# and denies a legitimate route — this step is red pre-fix, green post-fix.
525+
# A mixed-case apex is the only runtime shape that exercises the
526+
# normalization.
527+
#
528+
# The apex here does not need to sit under the platform root: unlike the
529+
# Ingress policy this VAP compares against the tenant apex only and has no
530+
# outside-root branch, so a reserved .invalid apex (RFC 2606) keeps the case
531+
# host-independent and unable to collide with a dev cluster's real apex.
532+
#
533+
# The mixed-case apex goes on a throwaway tenant-* namespace rather than on
534+
# tenant-test: relabelling the shared namespace would leave it mixed-case for
535+
# every later test if a restore were missed. The tenant-* prefix is what makes
536+
# the VAP's matchCondition fire, and the runner is a trusted caller for the
537+
# host-label immutability VAP, so the labelled CREATE is admitted (see
538+
# untrusted-sa-cannot-set-host-label-at-create above). Applied declaratively:
539+
# an admission denial fails the apply, and Chainsaw auto-cleanup deletes the
540+
# route and the namespace.
541+
- name: mixed-case-apex-route-allowed
542+
try:
543+
# Pre-clean a namespace left by an interrupted prior run: a leftover
544+
# tenant-case-probe stuck in Terminating fails the apply below, and because
545+
# the label here is a fixed literal a surviving namespace would otherwise be
546+
# silently reused rather than rebuilt.
547+
- script:
548+
content: |
549+
set -eu
550+
kubectl delete namespace tenant-case-probe --ignore-not-found --wait=true --timeout=60s
551+
check:
552+
($error == null): true
553+
- apply:
554+
resource:
555+
apiVersion: v1
556+
kind: Namespace
557+
metadata:
558+
name: tenant-case-probe
559+
labels:
560+
namespace.cozystack.io/host: Case-Probe.route.invalid
561+
# Guard against a vacuous pass: re-read the persisted label. Had anything
562+
# normalized it to lowercase, the route below would be admitted with or
563+
# without the fix and the step would stop proving case-insensitivity.
564+
- assert:
565+
resource:
566+
apiVersion: v1
567+
kind: Namespace
568+
metadata:
569+
name: tenant-case-probe
570+
labels:
571+
namespace.cozystack.io/host: Case-Probe.route.invalid
572+
- apply:
573+
resource:
574+
apiVersion: gateway.networking.k8s.io/v1
575+
kind: HTTPRoute
576+
metadata:
577+
name: route-hostname-case-probe
578+
namespace: tenant-case-probe
579+
spec:
580+
hostnames:
581+
- "probe.case-probe.route.invalid"
582+
rules:
583+
- backendRefs:
584+
- name: kubernetes
585+
namespace: default
586+
port: 443
587+
# The deny half of the mixed-case apex, pairing with the step above the way
588+
# foreign-apex-route-denied pairs with own-apex-route-allowed. The allow half
589+
# alone only proves the normalized match does not UNDER-admit; over-admission
590+
# is the direction that hijacks a hostname, and normalizing both operands is
591+
# exactly the edit that can widen a match by accident. Reuses the namespace
592+
# the previous step created — Chainsaw cleans up at test scope, not step
593+
# scope. Scripted rather than an apply/expect pair because the grep for
594+
# "ValidatingAdmissionPolicy" is what proves the VAP did the denying: a bare
595+
# "the apply failed" assertion would be satisfied by a typo in the manifest.
596+
- name: mixed-case-apex-route-denied
597+
try:
598+
- script:
599+
content: |
600+
set -eu
601+
# deny <case> <name> reads a manifest on stdin and expects the VAP to
602+
# reject it, mirroring the deny() helper in the Ingress suite below.
603+
deny() {
604+
kubectl -n tenant-case-probe delete httproute "$2" --ignore-not-found >/dev/null
605+
out=$(kubectl apply -n tenant-case-probe -f - 2>&1) && rc=0 || rc=$?
606+
echo "case=$1 rc=$rc"
607+
if [ "$rc" -eq 0 ]; then
608+
kubectl -n tenant-case-probe delete httproute "$2" --ignore-not-found
609+
echo "BUG: admission accepted $1: $out" >&2
610+
exit 1
611+
fi
612+
echo "$out" | grep -qi "ValidatingAdmissionPolicy"
613+
}
614+
# A wholly foreign apex stays denied once both operands are lowercased.
615+
deny foreign-apex route-case-foreign-probe <<'EOF'
616+
apiVersion: gateway.networking.k8s.io/v1
617+
kind: HTTPRoute
618+
metadata:
619+
name: route-case-foreign-probe
620+
spec:
621+
hostnames:
622+
- "evil.other.invalid"
623+
rules:
624+
- backendRefs:
625+
- name: kubernetes
626+
namespace: default
627+
port: 443
628+
EOF
629+
# Dot-boundary guard under the mixed-case apex: "evilcase-probe.route.invalid"
630+
# ends with the lowercased apex "case-probe.route.invalid" but is NOT a
631+
# subdomain of it. The CEL only denies it because the suffix test is
632+
# endsWith("." + apex); drop that leading dot and this hostname is
633+
# admitted and hijacks the apex. This is the only case in this suite
634+
# that would notice.
635+
deny prefix-adjacent route-case-adjacent-probe <<'EOF'
636+
apiVersion: gateway.networking.k8s.io/v1
637+
kind: HTTPRoute
638+
metadata:
639+
name: route-case-adjacent-probe
640+
spec:
641+
hostnames:
642+
- "evilcase-probe.route.invalid"
643+
rules:
644+
- backendRefs:
645+
- name: kubernetes
646+
namespace: default
647+
port: 443
648+
EOF
649+
check:
650+
($error == null): true
507651
---
508652
apiVersion: chainsaw.kyverno.io/v1alpha1
509653
kind: Test
@@ -1048,6 +1192,151 @@ spec:
10481192
kubectl -n "$ns" delete ingress ingress-ownapex-wildcard-probe --ignore-not-found
10491193
check:
10501194
($error == null): true
1195+
# Case-insensitivity. spec.rules[].host and spec.tls[].hosts[] are always
1196+
# lowercase — the apiserver's host validation permits only lowercase, so an
1197+
# uppercase host is rejected before the VAP runs — but the apex operand is the
1198+
# namespace.cozystack.io/host label VALUE, and Kubernetes permits uppercase in
1199+
# a label value. Under a mixed-case apex a lowercase in-apex Ingress must
1200+
# still be admitted, so the VAP normalizes both operands with .lowerAscii().
1201+
# Ingress is the default publish path and carries the most complex CEL (a
1202+
# rule-host and a tls-host branch), so it gets its own runtime guard beside
1203+
# the route one; both branches are exercised at once because the tls branch
1204+
# denies on its own.
1205+
#
1206+
# The probe apex is derived as Case-Probe.<root> rather than hardcoded, and it
1207+
# has to sit UNDER the platform root to mean anything: the outside-root branch
1208+
# admits any non-wildcard host outside the root apex whatever the tenant apex
1209+
# says, so a probe on an unrelated domain would be admitted pre-fix and prove
1210+
# nothing. Under the root that escape is closed — pre-fix the own-apex branch
1211+
# misses on case and the outside-root branch is excluded, so admission denies
1212+
# and this step is red; post-fix the own-apex branch matches and it is green.
1213+
#
1214+
# The mixed-case apex goes on a throwaway tenant-* namespace rather than on
1215+
# tenant-test, for the same reason as the route case. Built in a script since
1216+
# it needs ${root} interpolation, so the namespace is torn down in finally
1217+
# rather than by Chainsaw auto-cleanup. That teardown is also why the allow and
1218+
# deny halves share ONE step here while the route case splits them in two:
1219+
# finally runs at step scope, so a second step would find the namespace gone.
1220+
# The route namespace is applied declaratively and reclaimed at test scope, so
1221+
# it survives into its own deny step.
1222+
- name: mixed-case-apex-allowed-and-denied
1223+
try:
1224+
- script:
1225+
content: |
1226+
set -eu
1227+
root=$(kubectl get namespace tenant-root -o jsonpath='{.metadata.labels.namespace\.cozystack\.io/host}')
1228+
[ -n "$root" ] || { echo "SETUP FAILURE: tenant-root lacks the namespace.cozystack.io/host label" >&2; exit 1; }
1229+
apex="Case-Probe.${root}"
1230+
host="probe.case-probe.${root}"
1231+
# Guard against a vacuous pass: the probe apex must really be
1232+
# mixed-case. An all-lowercase apex would admit the Ingress with or
1233+
# without the fix — this catches a later edit that lowercases the
1234+
# literal above, which the persisted-label check below cannot see.
1235+
[ "$apex" != "$(echo "$apex" | tr '[:upper:]' '[:lower:]')" ] || { echo "SETUP FAILURE: probe apex '$apex' is not mixed-case" >&2; exit 1; }
1236+
# Pre-clean a namespace left behind by an interrupted prior run.
1237+
kubectl delete namespace tenant-ingress-case-probe --ignore-not-found --wait=true --timeout=60s
1238+
kubectl apply -f - <<EOF
1239+
apiVersion: v1
1240+
kind: Namespace
1241+
metadata:
1242+
name: tenant-ingress-case-probe
1243+
labels:
1244+
namespace.cozystack.io/host: ${apex}
1245+
EOF
1246+
# Second vacuous-pass guard: re-read the persisted label, in case
1247+
# anything normalized the value on the way in.
1248+
got=$(kubectl get namespace tenant-ingress-case-probe -o jsonpath='{.metadata.labels.namespace\.cozystack\.io/host}')
1249+
[ "$got" = "$apex" ] || { echo "SETUP FAILURE: apex label persisted as '$got', want '$apex'" >&2; exit 1; }
1250+
kubectl apply -n tenant-ingress-case-probe -f - <<EOF
1251+
apiVersion: networking.k8s.io/v1
1252+
kind: Ingress
1253+
metadata:
1254+
name: ingress-hostname-case-probe
1255+
spec:
1256+
tls:
1257+
- hosts:
1258+
- "${host}"
1259+
secretName: noop-tls
1260+
rules:
1261+
- host: "${host}"
1262+
http:
1263+
paths:
1264+
- path: /
1265+
pathType: Prefix
1266+
backend:
1267+
service:
1268+
name: noop
1269+
port:
1270+
number: 80
1271+
EOF
1272+
# The deny half, pairing with the allow above the way
1273+
# foreign-and-adjacent-hosts-denied pairs with own-apex-allowed. The
1274+
# allow half alone only proves the normalized match does not
1275+
# UNDER-admit; over-admission is what hijacks a hostname, and
1276+
# normalizing both operands is exactly the edit that can widen a match
1277+
# by accident.
1278+
deny() {
1279+
kubectl -n tenant-ingress-case-probe delete ingress "$2" --ignore-not-found >/dev/null
1280+
out=$(kubectl apply -n tenant-ingress-case-probe -f - 2>&1) && rc=0 || rc=$?
1281+
echo "case=$1 rc=$rc"
1282+
if [ "$rc" -eq 0 ]; then
1283+
kubectl -n tenant-ingress-case-probe delete ingress "$2" --ignore-not-found
1284+
echo "BUG: admission accepted $1: $out" >&2
1285+
exit 1
1286+
fi
1287+
echo "$out" | grep -qi "ValidatingAdmissionPolicy"
1288+
}
1289+
# Under the platform root but outside the probe's own apex: the
1290+
# own-apex branch must miss on more than case, and the outside-root
1291+
# branch must stay closed under the root.
1292+
deny foreign-apex-host ingress-case-foreign-probe <<EOF
1293+
apiVersion: networking.k8s.io/v1
1294+
kind: Ingress
1295+
metadata:
1296+
name: ingress-case-foreign-probe
1297+
spec:
1298+
rules:
1299+
- host: "dashboard.${root}"
1300+
http:
1301+
paths:
1302+
- path: /
1303+
pathType: Prefix
1304+
backend:
1305+
service:
1306+
name: noop
1307+
port:
1308+
number: 80
1309+
EOF
1310+
# Dot-boundary guard on the normalized path: "evilcase-probe.<root>"
1311+
# ends with the lowercased apex "case-probe.<root>" without being a
1312+
# subdomain of it. Only endsWith("." + apex) denies it; drop that
1313+
# leading dot and the apex is hijackable.
1314+
deny prefix-adjacent ingress-case-adjacent-probe <<EOF
1315+
apiVersion: networking.k8s.io/v1
1316+
kind: Ingress
1317+
metadata:
1318+
name: ingress-case-adjacent-probe
1319+
spec:
1320+
rules:
1321+
- host: "evilcase-probe.${root}"
1322+
http:
1323+
paths:
1324+
- path: /
1325+
pathType: Prefix
1326+
backend:
1327+
service:
1328+
name: noop
1329+
port:
1330+
number: 80
1331+
EOF
1332+
check:
1333+
($error == null): true
1334+
finally:
1335+
- delete:
1336+
ref:
1337+
apiVersion: v1
1338+
kind: Namespace
1339+
name: tenant-ingress-case-probe
10511340
# Static accepts — concrete external domains outside the platform root apex
10521341
# on both spec.rules[].host and spec.tls[].hosts. Declarative apply; Chainsaw
10531342
# auto-cleanup deletes the probes.

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,26 @@ spec:
2525
? namespaceObject.metadata.labels["namespace.cozystack.io/host"]
2626
: ""
2727
validations:
28+
# Both operands of the hostname match are normalized with .lowerAscii():
29+
# the listener hostname is already lowercase (the CRD pattern permits
30+
# nothing else), but tenantHost
31+
# comes from the namespace.cozystack.io/host label value, which
32+
# Kubernetes permits to carry uppercase. Unlike the Ingress policy this
33+
# one never compares against the platform root-host.
34+
#
35+
# The leading `tenantHost == "" ||` makes this policy fail OPEN when the
36+
# host label is absent or empty, unlike its fail-closed siblings. That is
37+
# the platform's documented fail-open exception, with its compensating
38+
# control and its limits — see docs/security/threat-model.md. Changing
39+
# that posture is a security-model decision, not a side effect of
40+
# normalizing case, so the behavior is left as it was and pinned by test.
2841
- expression: >-
2942
variables.tenantHost == "" ||
3043
!has(object.spec.listeners) ||
3144
object.spec.listeners.all(l,
3245
!has(l.hostname) ||
33-
l.hostname == variables.tenantHost ||
34-
l.hostname.endsWith("." + variables.tenantHost)
46+
l.hostname.lowerAscii() == variables.tenantHost.lowerAscii() ||
47+
l.hostname.lowerAscii().endsWith("." + variables.tenantHost.lowerAscii())
3548
)
3649
messageExpression: >-
3750
"Gateway listener hostname must equal " + variables.tenantHost +

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,27 @@
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(), and for this policy that is load-bearing rather than
48+
hardening. The resource-side host is always lowercase — kube-apiserver
49+
permits only lowercase in spec.rules[].host and spec.tls[].hosts[],
50+
wildcards included, and rejects the rest before this VAP runs — but BOTH apex operands can carry
51+
uppercase: the namespace.cozystack.io/host label VALUE (Kubernetes
52+
permits uppercase in a label value) and the templated root-host, a
53+
free-form platform value that nothing validates for case.
54+
55+
An uppercase root-host is the dangerous direction. The outside-root
56+
branch below admits a host lying entirely outside the platform apex,
57+
and it decides that by comparing the lowercase host against this
58+
templated literal. Un-normalized, that comparison can never match, the
59+
exclusion silently stops excluding, and the policy degenerates into
60+
"admit any non-wildcard host" — every non-wildcard platform-apex name a
61+
tenant asks for is granted, including the root apex itself. Only the
62+
anti-wildcard gate still holds.
63+
64+
A mixed-case apex LABEL is the milder direction: it wrongly denies a
65+
tenant its own legitimate in-apex hostname rather than granting it
66+
someone else's.
5167

5268
Defense-in-depth contract: this VAP is fail-closed. A tenant-*
5369
namespace without `namespace.cozystack.io/host` is rejected (not
@@ -59,7 +75,7 @@
5975
{{- $rootHost := index .Values._cluster "root-host" }}
6076
{{- if $rootHost }}
6177
{{- $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) -}}
78+
{{- $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) -}}
6379
---
6480
apiVersion: admissionregistration.k8s.io/v1
6581
kind: ValidatingAdmissionPolicy

0 commit comments

Comments
 (0)