@@ -132,6 +132,16 @@ metadata:
132132 name : gateway-foreign-hostname-rejected
133133spec :
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---
508652apiVersion : chainsaw.kyverno.io/v1alpha1
509653kind : 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.
0 commit comments