From 9b6128d3ca5e63fe5855b334792d21f40620197d Mon Sep 17 00:00:00 2001 From: Michel Jansen Date: Tue, 10 Nov 2026 10:41:18 +0100 Subject: [PATCH] fix(kubernetes): reject invalid UTF-8 sysctl file values Raw Helm file inputs can carry invalid UTF-8 that Go quoting turns into different Unicode characters. Reject these bytes rather than silently changing the requested worker configuration. Assisted-by: LLM Signed-off-by: Michel Jansen --- ...check-talosconfigtemplate-name-agreement.bats | 16 ++++++++++++++++ .../templates/_talosconfigtemplate.tpl | 7 +++++++ 2 files changed, 23 insertions(+) diff --git a/hack/check-talosconfigtemplate-name-agreement.bats b/hack/check-talosconfigtemplate-name-agreement.bats index 810f13175..6a3b54b84 100755 --- a/hack/check-talosconfigtemplate-name-agreement.bats +++ b/hack/check-talosconfigtemplate-name-agreement.bats @@ -251,3 +251,19 @@ VALUES done rm -rf "$TMP" } + +@test "sysctl file values reject invalid UTF-8 rather than changing their bytes" { + TMP=$(mktemp -d) + # --set-file reads raw bytes, unlike the YAML/JSON-facing application API. + for bytes in '\377' '\300\257' '\342\202'; do + printf '%b' "$bytes" > "$TMP/value" + if render_pool "$TMP/r.yaml" --skip-schema-validation --set-file "talos.sysctls.kernel\\.panic=$TMP/value"; then + echo "FAIL: accepted invalid UTF-8 sysctl bytes: $bytes" + false + fi + grep -Fq 'must be valid UTF-8' "$TMP/r.yaml.err" + done + printf '\357\277\275' > "$TMP/value" + render_pool "$TMP/r.yaml" --skip-schema-validation --set-file "talos.sysctls.kernel\\.panic=$TMP/value" + rm -rf "$TMP" +} diff --git a/packages/apps/kubernetes-nodes/templates/_talosconfigtemplate.tpl b/packages/apps/kubernetes-nodes/templates/_talosconfigtemplate.tpl index 5f8c294cf..c33d43f16 100644 --- a/packages/apps/kubernetes-nodes/templates/_talosconfigtemplate.tpl +++ b/packages/apps/kubernetes-nodes/templates/_talosconfigtemplate.tpl @@ -51,6 +51,13 @@ {{- if not (kindIs "string" $value) }} {{- fail (printf "talos.sysctls[%q] must be a string" $key) }} {{- end }} +{{- /* JSON replaces invalid UTF-8 with replacement runes. Comparing the + round-trip detects raw --set-file bytes that quote would change into + different Unicode characters in the machine config. */}} +{{- $roundTrip := dict "value" $value | toJson | fromJson }} +{{- if ne $value $roundTrip.value }} +{{- fail (printf "talos.sysctls[%q] must be valid UTF-8" $key) }} +{{- end }} {{- end }} {{- end }} {{- $kubeletVersion := include "kubernetes.versionMap" $root | trim }}