diff --git a/scripts/validate/scan-platform-sensitive-source.sh b/scripts/validate/scan-platform-sensitive-source.sh index 63e65d4..1f84765 100644 --- a/scripts/validate/scan-platform-sensitive-source.sh +++ b/scripts/validate/scan-platform-sensitive-source.sh @@ -29,9 +29,11 @@ excluded. Diagnostics contain file names and finding classes only; matched values and lines are never printed. The production run invokes all five renderer entrypoints in private temporary -contexts. Four output-producing renderers publish private handoffs. The access -renderer uses its output-free Grafana verifier because it does not require -historical metric inventories; its exact two-line success contract is checked. +contexts. In a git checkout they run from a private copy of the tracked and +not-ignored files, so an ignored chart cache cannot stop them. Four +output-producing renderers publish private handoffs. The access renderer uses +its output-free Grafana verifier because it does not require historical metric +inventories; its exact two-line success contract is checked. It still traverses the pinned core renderer, whose separately published handoff contains the only allowlisted credential-free Secret and is decoded here. USAGE @@ -1522,7 +1524,7 @@ scan_platform_sensitive_roots() ( scan_platform_renderer_secrets() ( set -o pipefail local platform_root=$1 renderer label output_prefix publishes log file results line rc entry artifact - local helm_bin list_file='' + local helm_bin list_file='' render_root source_copy='' local failed=false local -a renderers=( 'phase1|render-phase1.sh|platform-phase1-apply|yes' @@ -1563,6 +1565,11 @@ scan_platform_renderer_secrets() ( rm -f -- "$list_file" ;; esac + case "$source_copy" in + /tmp/platform-sensitive-source-render-src.??????) + rm -rf -- "$source_copy" + ;; + esac } trap cleanup_renderer_scan EXIT HUP INT TERM @@ -1587,9 +1594,56 @@ scan_platform_renderer_secrets() ( return 1 } + # In a git checkout, render from a private copy of the tracked and not-ignored + # files (minus files deleted from the working tree). Ignored state such as a + # leftover .helm chart cache would otherwise make the renderers refuse to run. + render_root=$platform_root + if [[ -e "$platform_root/.git" ]]; then + for label in git tar; do + command -v "$label" >/dev/null 2>&1 || { + printf 'SCANNER_ERROR: renderer command unavailable: %s\n' "$label" >&2 + return 1 + } + done + [[ "$(git -C "$platform_root" rev-parse --show-toplevel 2>/dev/null)" == "$platform_root" ]] || { + printf 'SCANNER_ERROR: renderer root is not the top of its git checkout\n' >&2 + return 1 + } + source_copy="$(mktemp -d /tmp/platform-sensitive-source-render-src.XXXXXX)" || { + printf 'SCANNER_ERROR: renderer source copy allocation failed\n' >&2 + return 1 + } + chmod 0700 "$source_copy" || return 1 + if ! python3 -I - "$platform_root" <<'PY' | +import subprocess +import sys + +root = sys.argv[1] + + +def listed(*options): + result = subprocess.run( + ["git", "-C", root, "ls-files", "-z", *options], + check=True, + stdout=subprocess.PIPE, + ) + return [entry for entry in result.stdout.split(b"\0") if entry] + + +keep = set(listed("--cached", "--others", "--exclude-standard")) - set(listed("--deleted")) +for entry in sorted(keep): + sys.stdout.buffer.write(entry + b"\0") +PY + tar -C "$platform_root" --null -T - -cf - | tar -x -C "$source_copy"; then + printf 'SCANNER_ERROR: renderer source copy failed\n' >&2 + return 1 + fi + render_root=$source_copy + fi + for renderer in "${renderers[@]}"; do IFS='|' read -r label renderer output_prefix publishes <<<"$renderer" - renderer="$platform_root/scripts/validate/$renderer" + renderer="$render_root/scripts/validate/$renderer" [[ -f "$renderer" && ! -L "$renderer" ]] || { printf 'SCANNER_ERROR: required renderer is unsafe: %s\n' "$label" >&2 return 1 diff --git a/scripts/validate/test-scan-platform-sensitive-source.sh b/scripts/validate/test-scan-platform-sensitive-source.sh index 0f5ad6c..3b382ef 100644 --- a/scripts/validate/test-scan-platform-sensitive-source.sh +++ b/scripts/validate/test-scan-platform-sensitive-source.sh @@ -967,6 +967,16 @@ label="$(basename -- "$0")" handoff=no [[ -z "$out" ]] || handoff=yes printf '%s\n' "$label${component:+:$component}:handoff=$handoff" >>"$PLATFORM_SENSITIVE_SOURCE_RENDER_LOG" +root="$(cd -- "$(dirname -- "$0")/../.." && pwd -P)" +if [[ "${PLATFORM_SENSITIVE_SOURCE_FAKE_REJECT_CACHE:-0}" != 0 ]] && \ + [[ -n "$(find "$root" -name .helm -type d -print -quit)" ]]; then + printf 'REJECT: exact chart cache already exists\n' >&2 + exit 23 +fi +if [[ -n "${PLATFORM_SENSITIVE_SOURCE_FAKE_REQUIRE_INPUT:-}" && \ + ! -f "$root/$PLATFORM_SENSITIVE_SOURCE_FAKE_REQUIRE_INPUT" ]]; then + exit 42 +fi emit_phase1_log() { local publish=$1 name @@ -1438,6 +1448,41 @@ for injected_renderer in \ fail "$injected_renderer unexpected stdout leaked content" pass "$injected_renderer stdout is restricted to its bounded grammar" done + +# A git checkout renders from a private copy of its tracked and not-ignored files, +# so an ignored chart cache left in the checkout cannot stop the renderers (the +# real renderers refuse a pre-existing exact cache with RC=23). +command -v git >/dev/null 2>&1 || fail 'git is required for the clean render copy test' +printf '.helm/\n' >"$WORK/platform/.gitignore" +git -C "$WORK/platform" init -q +git -C "$WORK/platform" add -A +git -C "$WORK/platform" -c user.name=fixture -c user.email=fixture@example.invalid \ + commit -q -m fixture +mkdir -p "$WORK/platform/services/observability/.helm/charts/stale-1.0.0" +printf 'untracked\n' >"$WORK/platform/services/observability/untracked-input.yaml" +: >"$WORK/renderer.log" +renderer_rc=0 +renderer_output="$(PLATFORM_SENSITIVE_SOURCE_FAKE_REJECT_CACHE=1 \ + PLATFORM_SENSITIVE_SOURCE_FAKE_REQUIRE_INPUT=services/observability/untracked-input.yaml \ + scan_platform_renderer_secrets "$WORK/platform" 2>&1)" || renderer_rc=$? +(( renderer_rc == 0 )) || { + printf '%s\n' "$renderer_output" >&2 + fail 'an ignored chart cache in a git checkout blocked the renderers' +} +[[ -d "$WORK/platform/services/observability/.helm/charts/stale-1.0.0" ]] || \ + fail 'the renderer scan changed the ignored cache in the scanned checkout' +pass 'a git checkout renders from a clean copy, so an ignored chart cache cannot block it' +pass 'the clean render copy keeps untracked files that git does not ignore' +rm -f -- "$WORK/platform/services/observability/untracked-input.yaml" +rm -f -- "$WORK/platform/scripts/validate/render-phase2.sh" +renderer_rc=0 +renderer_output="$(scan_platform_renderer_secrets "$WORK/platform" 2>&1)" || renderer_rc=$? +(( renderer_rc != 0 )) || fail 'a renderer deleted from the working tree was still run' +[[ "$renderer_output" == *'required renderer is unsafe: phase2'* ]] || { + printf '%s\n' "$renderer_output" >&2 + fail 'a renderer deleted from the working tree did not fail at the renderer check' +} +pass 'the clean render copy leaves out files deleted from the working tree' unset PLATFORM_SENSITIVE_SOURCE_RENDER_LOG new_fixture