From 326589282693a0508db50817a0c54ffd8fe925f3 Mon Sep 17 00:00:00 2001 From: Michel Jansen Date: Wed, 11 Nov 2026 12:34:14 +0100 Subject: [PATCH] test(kubernetes): group sysctl safety checks with heredoc tests The heredoc suite owns escaped-input and render-time rejection coverage. Keeping sysctl safety checks there leaves bootstrap-name agreement tests focused on the shared content hash. Assisted-by: LLM Signed-off-by: Michel Jansen --- ...ck-talosconfigtemplate-name-agreement.bats | 82 ------------- hack/talos-reconcile-heredoc_test.bats | 111 +++++++++++++++++- 2 files changed, 105 insertions(+), 88 deletions(-) diff --git a/hack/check-talosconfigtemplate-name-agreement.bats b/hack/check-talosconfigtemplate-name-agreement.bats index 6a3b54b84..6da6212fe 100755 --- a/hack/check-talosconfigtemplate-name-agreement.bats +++ b/hack/check-talosconfigtemplate-name-agreement.bats @@ -185,85 +185,3 @@ assert_agreement() { [ "$(job_tct_name "$TMP/r.yaml")" = "$baseline" ] rm -rf "$TMP" } - -@test "sysctl names and values survive the actual Job heredoc literally without shell execution" { - TMP=$(mktemp -d) - cat > "$TMP/values.yaml" <<'VALUES' -talos: - sysctls: - user.max_user_namespaces: "11255" - net.ipv4.ip_local_port_range: "1024\t65535" - kernel.panic: "0" - '$(touch "$SYSCTL_MARKER")': '$(touch "$SYSCTL_MARKER")' - '`touch "$SYSCTL_MARKER"`': '`touch "$SYSCTL_MARKER"`' - '${SYSCTL_MARKER}': '${SYSCTL_MARKER}' - 'backslash\$(touch "$SYSCTL_MARKER")': 'backslash\$(touch "$SYSCTL_MARKER")' - "quotes\"\\\nEOF\n$(touch \"$SYSCTL_MARKER\")": "quotes\"\\\nEOF\n$(touch \"$SYSCTL_MARKER\")" - "unicode\u0085EOF\u2028EOF\u2029": "unicode\u0085EOF\u2028EOF\u2029" - "empty": "" -VALUES - render_pool "$TMP/r.yaml" -f "$TMP/values.yaml" - yq -r 'select(.kind == "Job") | .spec.template.spec.containers[0].command[2]' "$TMP/r.yaml" > "$TMP/job.sh" - # Execute only the real manifest-producing heredoc, never the surrounding - # kubectl/Talos operations. YAML is parsed on both sides of the shell so - # escaping that prevents execution but corrupts the machine config also fails. - awk '/^TCT_MANIFEST=/{emit=1} emit{print} emit && /^\)"$/{exit}' "$TMP/job.sh" > "$TMP/heredoc.sh" - [ -s "$TMP/heredoc.sh" ] - printf '\nprintf "%%s\\n" "$TCT_MANIFEST"\n' >> "$TMP/heredoc.sh" - yq -o=json '.talos.sysctls | sort_keys(.)' "$TMP/values.yaml" > "$TMP/expected.json" - for shell in bash dash; do - SYSCTL_MARKER="$TMP/executed" TALOS_TOKEN=runtime-token "$shell" "$TMP/heredoc.sh" > "$TMP/tct.yaml" - [ ! -e "$TMP/executed" ] - yq -o=json '.spec.template.spec.data | from_yaml | .machine.sysctls | sort_keys(.)' "$TMP/tct.yaml" > "$TMP/actual.json" - diff -u "$TMP/expected.json" "$TMP/actual.json" - [ "$(yq '.spec.template.spec.data | from_yaml | .machine.token' "$TMP/tct.yaml")" = "runtime-token" ] - done - rm -rf "$TMP" -} - -@test "sysctl types are rejected by both the schema and the render-time guard" { - TMP=$(mktemp -d) - for value in '11255' 'false' 'null' '["1"]' '{"evil":"$(id)"}'; do - input="talos.sysctls={\"kernel.panic\":$value}" - if render_pool "$TMP/r.yaml" --set-json "$input"; then - echo "FAIL: schema accepted a non-string sysctl: $value" - false - fi - grep -q 'values don.t meet the specifications' "$TMP/r.yaml.err" - if render_pool "$TMP/r.yaml" --skip-schema-validation --set-json "$input"; then - echo "FAIL: template accepted a non-string sysctl: $value" - false - fi - grep -Fq 'talos.sysctls["kernel.panic"] must be a string' "$TMP/r.yaml.err" - done - for value in '"$(id)"' 'false' '0' '[]' '["1"]'; do - input="talos.sysctls=$value" - if render_pool "$TMP/r.yaml" --set-json "$input"; then - echo "FAIL: schema accepted a non-map sysctls: $value" - false - fi - grep -q 'values don.t meet the specifications' "$TMP/r.yaml.err" - if render_pool "$TMP/r.yaml" --skip-schema-validation --set-json "$input"; then - echo "FAIL: template accepted a non-map sysctls: $value" - false - fi - grep -Fq 'talos.sysctls must be a map of strings' "$TMP/r.yaml.err" - 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/hack/talos-reconcile-heredoc_test.bats b/hack/talos-reconcile-heredoc_test.bats index 6fd19e850..e3251fe3e 100644 --- a/hack/talos-reconcile-heredoc_test.bats +++ b/hack/talos-reconcile-heredoc_test.bats @@ -2,8 +2,8 @@ # ----------------------------------------------------------------------------- # Execution-level regression test for the worker TalosConfigTemplate heredoc: # it pins that the reconcile Job's unquoted heredoc still emits a valid config -# when talos.registryMirrors (and the Talos image coordinates) carry hostile -# free-form input. +# when talos.registryMirrors, talos.sysctls and the Talos image coordinates +# carry hostile free-form input. # # Since the Phase 2 split the worker pool, its talos-reconcile Job, and the # TalosConfigTemplate it applies live only in the kubernetes-nodes chart; the @@ -13,8 +13,8 @@ # The reconcile Job applies the TalosConfigTemplate via an UNQUOTED `cat < "$out" 2>"$out.err" + [ -s "$out" ] || { cat "$out.err" >&2; return 1; } +} + @test "kubernetes-nodes refuses hostile Talos image coordinates at render" { work=$(mktemp -d) cat > "$work/vals.yaml" <<'VALS' @@ -334,3 +351,85 @@ VALS || { echo "kernelModules parameter was not preserved literally" >&2; printf '%s\n' "$out" | grep -A6 'kernel:' >&2; rm -rf "$work"; exit 1; } rm -rf "$work" } + +@test "sysctl names and values survive the actual Job heredoc literally without shell execution" { + TMP=$(mktemp -d) + cat > "$TMP/values.yaml" <<'VALUES' +talos: + sysctls: + user.max_user_namespaces: "11255" + net.ipv4.ip_local_port_range: "1024\t65535" + kernel.panic: "0" + '$(touch "$SYSCTL_MARKER")': '$(touch "$SYSCTL_MARKER")' + '`touch "$SYSCTL_MARKER"`': '`touch "$SYSCTL_MARKER"`' + '${SYSCTL_MARKER}': '${SYSCTL_MARKER}' + 'backslash\$(touch "$SYSCTL_MARKER")': 'backslash\$(touch "$SYSCTL_MARKER")' + "quotes\"\\\nEOF\n$(touch \"$SYSCTL_MARKER\")": "quotes\"\\\nEOF\n$(touch \"$SYSCTL_MARKER\")" + "unicode\u0085EOF\u2028EOF\u2029": "unicode\u0085EOF\u2028EOF\u2029" + "empty": "" +VALUES + render_sysctl_pool "$TMP/r.yaml" -f "$TMP/values.yaml" + yq -r 'select(.kind == "Job") | .spec.template.spec.containers[0].command[2]' "$TMP/r.yaml" > "$TMP/job.sh" + # Execute only the real manifest-producing heredoc, never the surrounding + # kubectl/Talos operations. YAML is parsed on both sides of the shell so + # escaping that prevents execution but corrupts the machine config also fails. + awk '/^TCT_MANIFEST=/{emit=1} emit{print} emit && /^\)"$/{exit}' "$TMP/job.sh" > "$TMP/heredoc.sh" + [ -s "$TMP/heredoc.sh" ] + printf '\nprintf "%%s\\n" "$TCT_MANIFEST"\n' >> "$TMP/heredoc.sh" + yq -o=json '.talos.sysctls | sort_keys(.)' "$TMP/values.yaml" > "$TMP/expected.json" + for shell in bash dash; do + SYSCTL_MARKER="$TMP/executed" TALOS_TOKEN=runtime-token "$shell" "$TMP/heredoc.sh" > "$TMP/tct.yaml" + [ ! -e "$TMP/executed" ] + yq -o=json '.spec.template.spec.data | from_yaml | .machine.sysctls | sort_keys(.)' "$TMP/tct.yaml" > "$TMP/actual.json" + diff -u "$TMP/expected.json" "$TMP/actual.json" + [ "$(yq '.spec.template.spec.data | from_yaml | .machine.token' "$TMP/tct.yaml")" = "runtime-token" ] + done + rm -rf "$TMP" +} + +@test "sysctl types are rejected by both the schema and the render-time guard" { + TMP=$(mktemp -d) + for value in '11255' 'false' 'null' '["1"]' '{"evil":"$(id)"}'; do + input="talos.sysctls={\"kernel.panic\":$value}" + if render_sysctl_pool "$TMP/r.yaml" --set-json "$input"; then + echo "FAIL: schema accepted a non-string sysctl: $value" + false + fi + grep -q 'values don.t meet the specifications' "$TMP/r.yaml.err" + if render_sysctl_pool "$TMP/r.yaml" --skip-schema-validation --set-json "$input"; then + echo "FAIL: template accepted a non-string sysctl: $value" + false + fi + grep -Fq 'talos.sysctls["kernel.panic"] must be a string' "$TMP/r.yaml.err" + done + for value in '"$(id)"' 'false' '0' '[]' '["1"]'; do + input="talos.sysctls=$value" + if render_sysctl_pool "$TMP/r.yaml" --set-json "$input"; then + echo "FAIL: schema accepted a non-map sysctls: $value" + false + fi + grep -q 'values don.t meet the specifications' "$TMP/r.yaml.err" + if render_sysctl_pool "$TMP/r.yaml" --skip-schema-validation --set-json "$input"; then + echo "FAIL: template accepted a non-map sysctls: $value" + false + fi + grep -Fq 'talos.sysctls must be a map of strings' "$TMP/r.yaml.err" + 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_sysctl_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_sysctl_pool "$TMP/r.yaml" --skip-schema-validation --set-file "talos.sysctls.kernel\\.panic=$TMP/value" + rm -rf "$TMP" +}