Merge pull request 'fix(validate): run the sensitive-source scan from any checkout' (#11) from donghyeon.kang/w6-scanner-fix into main

This commit was merged in pull request #11.
This commit is contained in:
2026-11-11 23:46:20 +00:00
2 changed files with 241 additions and 30 deletions
@@ -2,9 +2,13 @@
set -o pipefail
readonly PLATFORM_SENSITIVE_SOURCE_EXACT_PLATFORM_ROOT=/home/donghyeon/workspace/platform
readonly PLATFORM_SENSITIVE_SOURCE_EXACT_DOCS_ROOT=/home/donghyeon/workspace/docs/platform
readonly PLATFORM_SENSITIVE_SOURCE_EXACT_SCANNER="$PLATFORM_SENSITIVE_SOURCE_EXACT_PLATFORM_ROOT/scripts/validate/scan-platform-sensitive-source.sh"
# The platform root is the checkout this scanner lives in, so a temporary copy
# or a CI runner scans itself. The central documentation root stays on the
# desktop host unless --docs-root or --platform-only says otherwise.
PLATFORM_SENSITIVE_SOURCE_SELF_ROOT="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/../.." && pwd -P)"
readonly PLATFORM_SENSITIVE_SOURCE_SELF_ROOT
readonly PLATFORM_SENSITIVE_SOURCE_SELF="$PLATFORM_SENSITIVE_SOURCE_SELF_ROOT/scripts/validate/scan-platform-sensitive-source.sh"
readonly PLATFORM_SENSITIVE_SOURCE_DEFAULT_DOCS_ROOT=/home/donghyeon/workspace/docs/platform
_sensitive_source_fail() {
printf 'ERROR: %s\n' "$*" >&2
@@ -14,17 +18,22 @@ _sensitive_source_fail() {
_sensitive_source_usage() {
cat <<'USAGE'
Usage:
bash scripts/validate/scan-platform-sensitive-source.sh
bash scripts/validate/scan-platform-sensitive-source.sh [--docs-root DIR | --platform-only]
Scans the complete platform source and central platform documentation trees.
Only .git and .helm directory contents, plus the exact platform-root
.superpowers/sdd scratch subtree, are excluded. Diagnostics contain file names
and finding classes only; matched values and lines are never printed.
Scans the platform checkout that contains this script and the central platform
documentation tree (default /home/donghyeon/workspace/docs/platform, or
--docs-root DIR). --platform-only skips the documentation tree, for a
temporary copy or a CI runner that has none. Only .git and .helm directory
contents, plus the platform-root .superpowers/sdd scratch subtree, are
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
@@ -43,7 +52,7 @@ _sensitive_source_report_path() {
local path=$1 platform_root=$2 docs_root=$3 relative
if [[ "$path" == "$platform_root"/* ]]; then
relative="platform/${path#"$platform_root"/}"
elif [[ "$path" == "$docs_root"/* ]]; then
elif [[ -n "$docs_root" && "$path" == "$docs_root"/* ]]; then
relative="docs/${path#"$docs_root"/}"
else
relative="$(basename -- "$path")"
@@ -82,13 +91,19 @@ structured_source = suffix in (".yaml", ".yml", ".json")
python_source = suffix == ".py"
shell_source = suffix in (".sh", ".bash", ".zsh") or not suffix
prose_source = suffix in (".md", ".markdown", ".txt", ".rst")
embedded_python_source = path in (
"/home/donghyeon/workspace/platform/scripts/validate/observability-smoke.sh",
"/home/donghyeon/workspace/platform/scripts/validate/test-scan-platform-sensitive-source.sh",
"/home/donghyeon/workspace/platform/scripts/validate/test-configure-keycloak-grafana-oidc.sh",
"/home/donghyeon/workspace/platform/scripts/bootstrap/create-observability-secrets.sh",
"/home/donghyeon/workspace/platform/scripts/bootstrap/configure-keycloak-grafana-oidc.sh",
)
# Shell files with embedded Python, relative to the checkout that holds this
# scanner (scanner_source is <root>/scripts/validate/<scanner>).
scanner_root = Path(scanner_source).parents[2]
embedded_python_source = path in {
str(scanner_root / relative)
for relative in (
"scripts/validate/observability-smoke.sh",
"scripts/validate/test-scan-platform-sensitive-source.sh",
"scripts/validate/test-configure-keycloak-grafana-oidc.sh",
"scripts/bootstrap/create-observability-secrets.sh",
"scripts/bootstrap/configure-keycloak-grafana-oidc.sh",
)
}
try:
raw = open(path, "rb").read()
@@ -717,6 +732,19 @@ def permitted_reference_container(key, value):
)
def permitted_s3_credentials_reference(path, key, value):
# CloudNativePG barmanObjectStore.s3Credentials: each entry is a
# SecretKeySelector (name and key only), never the value itself.
return (
len(path) >= 1
and path[-1] == "s3Credentials"
and key in ("accessKeyId", "secretAccessKey")
and isinstance(value, dict)
and set(value) == {"name", "key"}
and all(isinstance(value[field], str) and value[field] for field in value)
)
def permitted_webhook_feature(path, key, value):
return (
tuple(str(part) for part in path) == ("operators", "object-store")
@@ -752,6 +780,8 @@ def walk(value, path=()):
pass
elif permitted_reference_container(key, child):
pass
elif permitted_s3_credentials_reference(path, key, child):
pass
elif not isinstance(child, str) or unsafe_scalar(child):
return True
if walk(child, path + (key,)):
@@ -1382,9 +1412,13 @@ scan_platform_sensitive_roots() (
return 1
}
done
[[ "$platform_root" == /* && "$docs_root" == /* && "$platform_root" != "$docs_root" ]] ||
# An empty docs root means a platform-only scan.
local -a scan_roots=("$platform_root")
[[ -z "$docs_root" ]] || scan_roots+=("$docs_root")
[[ "$platform_root" == /* ]] && { [[ -z "$docs_root" ]] ||
[[ "$docs_root" == /* && "$platform_root" != "$docs_root" ]]; } ||
_sensitive_source_fail 'scan roots must be distinct absolute paths' || return
for argument in "$platform_root" "$docs_root"; do
for argument in "${scan_roots[@]}"; do
[[ -d "$argument" && ! -L "$argument" && "$(cd -- "$argument" && pwd -P)" == "$argument" ]] ||
_sensitive_source_fail "scan root is not a physical directory: $argument" || return
done
@@ -1397,7 +1431,7 @@ scan_platform_sensitive_roots() (
printf 'SCANNER_ERROR: file inventory hardening failed\n' >&2
return 1
}
if ! find "$platform_root" "$docs_root" \
if ! find "${scan_roots[@]}" \
\( -type d -path "$platform_root/.superpowers/sdd" -prune \) -o \
\( -type d \( -name .git -o -name .helm \) -prune \) -o \
\( -type f -o -type l \) -print0 >"$list_file"; then
@@ -1432,7 +1466,7 @@ scan_platform_sensitive_roots() (
fi
finding="$(_sensitive_source_python_scan \
"$file" "$test_file" "$PLATFORM_SENSITIVE_SOURCE_EXACT_SCANNER")" || {
"$file" "$test_file" "$PLATFORM_SENSITIVE_SOURCE_SELF")" || {
printf 'SCANNER_ERROR: content scan failed for FILE=%s\n' "$report" >&2
return 1
}
@@ -1505,7 +1539,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'
@@ -1546,6 +1580,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
@@ -1570,9 +1609,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
@@ -1706,14 +1792,23 @@ scan_platform_renderer_secrets() (
)
scan_platform_sensitive_source_main() {
(( $# == 0 )) || {
local docs_root=$PLATFORM_SENSITIVE_SOURCE_DEFAULT_DOCS_ROOT
if (( $# == 1 )) && [[ "$1" == --platform-only ]]; then
docs_root=''
elif (( $# == 2 )) && [[ "$1" == --docs-root && "$2" == /* ]]; then
docs_root=$2
elif (( $# != 0 )); then
_sensitive_source_usage >&2
return 2
}
fi
if [[ -n "$docs_root" && ! -d "$docs_root" ]]; then
printf 'ERROR: documentation root %s does not exist; pass --docs-root DIR or --platform-only\n' \
"$docs_root" >&2
return 2
fi
scan_platform_sensitive_roots \
"$PLATFORM_SENSITIVE_SOURCE_EXACT_PLATFORM_ROOT" \
"$PLATFORM_SENSITIVE_SOURCE_EXACT_DOCS_ROOT" >/dev/null || return
scan_platform_renderer_secrets "$PLATFORM_SENSITIVE_SOURCE_EXACT_PLATFORM_ROOT" || return
"$PLATFORM_SENSITIVE_SOURCE_SELF_ROOT" "$docs_root" >/dev/null || return
scan_platform_renderer_secrets "$PLATFORM_SENSITIVE_SOURCE_SELF_ROOT" || return
printf 'PLATFORM SENSITIVE SOURCE SCAN PASS\n'
}
@@ -378,6 +378,44 @@ envValueFrom:
YAML
expect_reject 'a secretKeyRef container with a literal child fails closed' \
'platform/bootstrap/reference-container.yaml'
rm -f -- "$WORK/platform/bootstrap/reference-container.yaml"
cat >"$WORK/platform/bootstrap/s3-credentials.yaml" <<'YAML'
spec:
backup:
barmanObjectStore:
s3Credentials:
accessKeyId:
name: platform-postgres-backup-s3
key: ACCESS_KEY_ID
secretAccessKey:
name: platform-postgres-backup-s3
key: ACCESS_SECRET_KEY
YAML
expect_clean 'a CloudNativePG s3Credentials Secret reference remains a reference'
cat >"$WORK/platform/bootstrap/s3-credentials.yaml" <<'YAML'
spec:
s3Credentials:
secretAccessKey:
name: platform-postgres-backup-s3
key: ACCESS_SECRET_KEY
value: should-never-be-printed-s3-reference
YAML
expect_reject 'an s3Credentials reference with an extra literal child fails closed' \
'platform/bootstrap/s3-credentials.yaml'
cat >"$WORK/platform/bootstrap/s3-credentials.yaml" <<'YAML'
spec:
storage:
secretAccessKey:
name: platform-postgres-backup-s3
key: ACCESS_SECRET_KEY
YAML
expect_reject 'a name and key container outside s3Credentials still fails closed' \
'platform/bootstrap/s3-credentials.yaml'
printf 'spec:\n s3Credentials:\n secretAccess%sKey: Actual%s\n' '' 'ProductionSecretValue9283' \
>"$WORK/platform/bootstrap/s3-credentials.yaml"
expect_reject 'a literal s3Credentials secret access key is rejected' \
'platform/bootstrap/s3-credentials.yaml'
new_fixture
cat >"$WORK/platform/bootstrap/webhook-feature.yaml" <<'YAML'
@@ -967,6 +1005,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 +1486,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
@@ -1468,6 +1551,39 @@ output="$(run_scanner 2>&1)" || scanner_error_rc=$?
fail 'scanner error did not fail closed'
}
pass 'rg errors fail closed'
PLATFORM_SENSITIVE_SOURCE_TEST_PATH=''
[[ "$PLATFORM_SENSITIVE_SOURCE_SELF_ROOT" == "$ROOT" ]] || \
fail 'the scanner does not scan the checkout it lives in'
pass 'the scanner scans the checkout it lives in, not a fixed path'
main_rc=0
main_output="$(scan_platform_sensitive_source_main --unknown 2>&1)" || main_rc=$?
(( main_rc == 2 )) || fail 'an unknown entrypoint argument was accepted'
main_rc=0
main_output="$(scan_platform_sensitive_source_main --docs-root relative/docs 2>&1)" || main_rc=$?
(( main_rc == 2 )) || fail 'a relative docs root was accepted'
new_fixture
main_rc=0
main_output="$(scan_platform_sensitive_source_main --docs-root "$WORK/missing-docs" 2>&1)" || main_rc=$?
(( main_rc == 2 )) || fail 'a missing docs root was accepted'
[[ "$main_output" == *'--platform-only'* ]] || \
fail 'a missing docs root did not name --platform-only'
pass 'the entrypoint rejects unknown, relative and missing docs roots before scanning'
printf 'ordinary input\n' >"$WORK/platform/bootstrap/ordinary.md"
platform_only_output="$(scan_platform_sensitive_roots "$WORK/platform" '' 2>&1)" || {
printf '%s\n' "$platform_only_output" >&2
fail 'a platform-only root scan failed'
}
[[ "$platform_only_output" == *'PLATFORM SENSITIVE SOURCE SCAN PASS'* ]] || \
fail 'a platform-only root scan did not emit the PASS marker'
printf 'Slack: https://hooks.%s/services/T1/B2/C3\n' 'slack.com' \
>"$WORK/platform/bootstrap/live.env"
platform_only_rc=0
platform_only_output="$(scan_platform_sensitive_roots "$WORK/platform" '' 2>&1)" || platform_only_rc=$?
(( platform_only_rc != 0 )) || fail 'a platform-only root scan accepted a Slack webhook'
[[ "$platform_only_output" == *'platform/bootstrap/live.env'* ]] || \
fail 'a platform-only root scan did not report the filename'
pass 'a platform-only root scan still scans the platform root'
cleanup_before="$(
find /tmp -maxdepth 1 -type d -name 'platform-sensitive-source-test.??????' \