diff --git a/validate-action/tests/test-validate-k9.sh b/validate-action/tests/test-validate-k9.sh index ae4bf7a..b0b5ae4 100755 --- a/validate-action/tests/test-validate-k9.sh +++ b/validate-action/tests/test-validate-k9.sh @@ -193,4 +193,191 @@ if INPUT_PATH="$FIXTURE_DIR" INPUT_PATHS_IGNORE='' \ fi grep -q 'coordination.k9' "$FIXTURE_DIR/no-ignore.log" +# --- Regression cases from the 2026-11 review of the vendored copies --- +# Each case runs in a directory of its own so it is judged alone. +run_case() { + local name="$1" + CASE_DIR="$FIXTURE_DIR/case-$name" + mkdir -p "$CASE_DIR" +} + +# A template placeholder level, quoted or not, reaches the placeholder notice +# whole. The capture used to stop at the first '}', so "{{LEVEL}}" became +# '{{level' and failed as an invalid level. +run_case placeholder +cat > "$CASE_DIR/quoted.k9.ncl" <<'EOF' +# SPDX-License-Identifier: MPL-2.0 +{ + magic_number = "K9!", + pedigree = { name = "{{SERVICE}}", version = "1.0.0", leash = "{{LEVEL}}" }, +} +EOF +cat > "$CASE_DIR/plain.k9" <<'EOF' +K9! +# SPDX-License-Identifier: MPL-2.0 +metadata: + name: templated + version: 1.0.0 + security_level: {{ level }} +EOF +placeholder_output=$(INPUT_PATH="$CASE_DIR" "$ACTION_DIR/validate-k9.sh") +grep -q 'Errors: 0' <<< "$placeholder_output" +[[ $(grep -c 'template placeholder' <<< "$placeholder_output") -ge 2 ]] + +# A last line without a trailing newline is still read. It used to be +# dropped, so a final-line signature was invisible to the Hunt check. +run_case no-trailing-newline +printf 'K9!\n# SPDX-License-Identifier: MPL-2.0\nmetadata:\n name: last-line\n version: 1.0.0\n leash: hunt\n signature: abc123' \ + > "$CASE_DIR/last-line.k9" +INPUT_PATH="$CASE_DIR" "$ACTION_DIR/validate-k9.sh" > "$CASE_DIR/out.log" +grep -q 'Errors: 0' "$CASE_DIR/out.log" + +# A Hunt level is not cancelled by a later, lower level elsewhere in the +# file. Every level seen is validated, and any Hunt needs a signature. +run_case hunt-then-kennel +cat > "$CASE_DIR/hunt.k9.ncl" <<'EOF' +# SPDX-License-Identifier: MPL-2.0 +{ + magic_number = "K9!", + pedigree = { name = "unsigned-hunt", version = "1.0.0", leash = 'Hunt }, + app = { leash = 'Kennel }, +} +EOF +if INPUT_PATH="$CASE_DIR" "$ACTION_DIR/validate-k9.sh" > "$CASE_DIR/out.log" 2>&1; then + echo 'expected a later Kennel leash not to waive the Hunt signature rule' >&2 + exit 1 +fi +grep -q "must include a 'signature'" "$CASE_DIR/out.log" + +# Nickel contract application with a pipe, `{ ... } | X.K9Pedigree`, is a +# pedigree and a format marker, as the marker error message promises. +run_case pipe-application +cat > "$CASE_DIR/pipe.k9.ncl" <<'EOF' +# SPDX-License-Identifier: MPL-2.0 +let schemas = import "./schemas.ncl" in +{ + metadata = { name = "piped", version = "1.0.0" }, + leash = 'Yard, +} | schemas.K9Pedigree +EOF +INPUT_PATH="$CASE_DIR" "$ACTION_DIR/validate-k9.sh" > "$CASE_DIR/out.log" +grep -q 'Errors: 0' "$CASE_DIR/out.log" + +# A brace inside a quoted string does not end the policy block, so a later +# policy trust_level of Hunt is still seen and still needs a signature. +run_case quoted-brace +cat > "$CASE_DIR/policy.k9.ncl" <<'EOF' +# SPDX-License-Identifier: MPL-2.0 +{ + magic_number = "K9!", + pedigree = { name = "quoted-brace", version = "1.0.0" }, + policy = { + description = "}", + trust_level = 'Hunt, + }, +} +EOF +if INPUT_PATH="$CASE_DIR" "$ACTION_DIR/validate-k9.sh" > "$CASE_DIR/out.log" 2>&1; then + echo 'expected a quoted brace not to hide a Hunt policy trust_level' >&2 + exit 1 +fi +grep -q "must include a 'signature'" "$CASE_DIR/out.log" + +# `let leash = import ...` binds a module; it is not a leash value. It used to +# be read as the level 'import ../leash.ncl in' and fail as invalid. +run_case let-binding +cat > "$CASE_DIR/let.k9.ncl" <<'EOF' +# SPDX-License-Identifier: MPL-2.0 +let leash = import "../leash.ncl" in +{ + magic_number = "K9!", + pedigree = { name = "let-binding", version = "1.0.0", leash = 'Yard }, +} +EOF +INPUT_PATH="$CASE_DIR" "$ACTION_DIR/validate-k9.sh" > "$CASE_DIR/out.log" +grep -q 'Errors: 0' "$CASE_DIR/out.log" + +# A binding and its body on one line: only the binding is dropped, so the +# body's Hunt level is still seen and still needs a signature. +run_case let-binding-same-line +cat > "$CASE_DIR/inline.k9.ncl" <<'EOF' +# SPDX-License-Identifier: MPL-2.0 +let leash = import "../leash.ncl" in { leash = 'Hunt, + magic_number = "K9!", + metadata = { name = "inline", version = "1.0.0" }, +} | K9Pedigree +EOF +if INPUT_PATH="$CASE_DIR" "$ACTION_DIR/validate-k9.sh" > "$CASE_DIR/out.log" 2>&1; then + echo 'expected a same-line binding not to hide the body Hunt level' >&2 + exit 1 +fi +grep -q "must include a 'signature'" "$CASE_DIR/out.log" + +# Every level field on a line is recorded: a Hunt after a Kennel on the same +# line still needs a signature. +run_case two-levels-one-line +cat > "$CASE_DIR/oneline.k9.ncl" <<'EOF' +# SPDX-License-Identifier: MPL-2.0 +{ + magic_number = "K9!", + pedigree = { name = "x", version = "1", leash = 'Kennel }, app = { leash = 'Hunt }, +} +EOF +if INPUT_PATH="$CASE_DIR" "$ACTION_DIR/validate-k9.sh" > "$CASE_DIR/out.log" 2>&1; then + echo 'expected a second same-line level of Hunt to need a signature' >&2 + exit 1 +fi +grep -q "must include a 'signature'" "$CASE_DIR/out.log" + +# A schema name inside a quoted string is prose, not a pipe application, so +# it does not count as a pedigree. +run_case quoted-pipe +cat > "$CASE_DIR/prose.k9.ncl" <<'EOF' +# SPDX-License-Identifier: MPL-2.0 +{ + magic_number = "K9!", + description = "example | schemas.K9Pedigree", + name = "prose", + version = "1.0.0", + leash = 'Yard, +} +EOF +if INPUT_PATH="$CASE_DIR" "$ACTION_DIR/validate-k9.sh" > "$CASE_DIR/out.log" 2>&1; then + echo 'expected a quoted pipe application not to count as a pedigree' >&2 + exit 1 +fi +grep -q 'Missing pedigree' "$CASE_DIR/out.log" + +# A level written inside a quoted string is prose, not a field: it must not +# turn a Yard file into one that needs a signature. +run_case quoted-level-prose +cat > "$CASE_DIR/prose-level.k9.ncl" <<'EOF' +# SPDX-License-Identifier: MPL-2.0 +{ + magic_number = "K9!", + pedigree = { name = "x", version = "1", leash = 'Yard }, description = "example leash = Hunt", +} +EOF +INPUT_PATH="$CASE_DIR" "$ACTION_DIR/validate-k9.sh" > "$CASE_DIR/out.log" +grep -q 'Errors: 0' "$CASE_DIR/out.log" + +# A policy trust_level after a leash is still recorded, so an unsigned Hunt +# policy is caught whichever order the declarations come in. +run_case leash-then-policy +cat > "$CASE_DIR/order.k9.ncl" <<'EOF' +# SPDX-License-Identifier: MPL-2.0 +{ + magic_number = "K9!", + pedigree = { name = "order", version = "1.0.0", leash = 'Kennel }, + policy = { + trust_level = 'Hunt, + }, +} +EOF +if INPUT_PATH="$CASE_DIR" "$ACTION_DIR/validate-k9.sh" > "$CASE_DIR/out.log" 2>&1; then + echo 'expected a Hunt policy after a Kennel leash to need a signature' >&2 + exit 1 +fi +grep -q "must include a 'signature'" "$CASE_DIR/out.log" + echo 'K9 validator regression tests passed' diff --git a/validate-action/validate-k9.sh b/validate-action/validate-k9.sh index 24d4f56..a4f681f 100755 --- a/validate-action/validate-k9.sh +++ b/validate-action/validate-k9.sh @@ -119,7 +119,7 @@ strip_k9_comment() { comment_free_file() { local file="$1" line - while IFS= read -r line; do + while IFS= read -r line || [[ -n "$line" ]]; do strip_k9_comment "$line" done < "$file" } @@ -157,7 +157,7 @@ mask_k9_quoted_text() { structural_syntax_file() { local file="$1" line - while IFS= read -r line; do + while IFS= read -r line || [[ -n "$line" ]]; do mask_k9_quoted_text "$(strip_k9_comment "$line")" done < "$file" } @@ -218,6 +218,15 @@ report_issue() { # --------------------------------------------------------------------------- # Strips quotes, leading/trailing whitespace, Nickel enum tick prefix. # Handles both separators: `leash = 'Hunt` (Nickel) and `leash: hunt` (plain). +# Remember every security level a file declares, with its line, so that a +# later field cannot overwrite an earlier one: a Hunt level followed by an +# unrelated Kennel leash must still need a signature. Appends to the +# caller's `seen_levels` and `seen_level_lines` arrays (bash dynamic scope). +record_security_level() { + seen_levels+=("$1") + seen_level_lines+=("$2") +} + normalise_level() { local raw="$1" # Remove surrounding quotes, tick prefix ('Kennel -> Kennel), whitespace @@ -243,8 +252,11 @@ normalise_level() { # --------------------------------------------------------------------------- validate_k9() { local file="$1" - local syntax_content + local syntax_content structural_content syntax_content=$(comment_free_file "$file") + # Quote-masked copy: a schema name inside a string value is prose, not a + # schema application, so the application checks read this copy. + structural_content=$(structural_syntax_file "$file") FILES_SCANNED=$((FILES_SCANNED + 1)) # Dialect: .k9.ncl is Nickel source; bare .k9 is the plain dialect. @@ -258,7 +270,7 @@ validate_k9() { local first_content_line_num=0 local line_num=0 - while IFS= read -r line; do + while IFS= read -r line || [[ -n "$line" ]]; do line_num=$((line_num + 1)) # Skip empty lines if [[ -z "${line// /}" ]]; then @@ -283,7 +295,9 @@ validate_k9() { has_marker=true elif grep -Eq '^[[:space:]]*magic_number[[:space:]]*=[[:space:]]*"K9!"' <<< "$syntax_content"; then has_marker=true - elif grep -Eq '(K9Pedigree|pedigree_schema)([[:space:]]*&|[[:space:]]*\{)|&[[:space:]]*(.*\.)?(K9Pedigree|pedigree_schema)|import[[:space:]]*"[^"]*(pedigree|\.k9)\.ncl"' <<< "$syntax_content"; then + elif grep -Eq '(K9Pedigree|pedigree_schema)([[:space:]]*&|[[:space:]]*\{)|[&|][[:space:]]*(.*\.)?(K9Pedigree|pedigree_schema)' <<< "$structural_content"; then + has_marker=true + elif grep -Eq 'import[[:space:]]*"[^"]*(pedigree|\.k9)\.ncl"' <<< "$syntax_content"; then has_marker=true fi @@ -296,7 +310,7 @@ validate_k9() { # --- Check 2: SPDX header --- local has_spdx=false line_num=0 - while IFS= read -r line; do + while IFS= read -r line || [[ -n "$line" ]]; do line_num=$((line_num + 1)) if [[ $line_num -gt 10 ]]; then break @@ -325,6 +339,8 @@ validate_k9() { local security_level_value="" local security_level_line=0 local has_signature_field=false + local -a seen_levels=() + local -a seen_level_lines=() local in_policy=false local policy_depth=0 @@ -338,11 +354,15 @@ validate_k9() { fi # Pedigree construct, Nickel forms: direct, let-bound, schema - # application (`K9Pedigree { ... }`), or schema merge. - if [[ "$line" =~ ^[[:space:]]*(let[[:space:]]+)?[A-Za-z_]*pedigree[[:space:]]*= ]] \ - || [[ "$line" =~ (K9Pedigree|pedigree_schema)[[:space:]]*\{ ]] \ - || [[ "$line" =~ (K9Pedigree|pedigree_schema)[[:space:]]*\& ]] \ - || [[ "$line" =~ \&[[:space:]]*([A-Za-z_][A-Za-z0-9_]*\.)?(K9Pedigree|pedigree_schema) ]]; then + # application (`K9Pedigree { ... }`), or schema merge. Matched on the + # quote-masked line, so `description = "a | schemas.K9Pedigree"` is + # not taken for an application. + local masked + masked="$(mask_k9_quoted_text "$line")" + if [[ "$masked" =~ ^[[:space:]]*(let[[:space:]]+)?[A-Za-z_]*pedigree[[:space:]]*= ]] \ + || [[ "$masked" =~ (K9Pedigree|pedigree_schema)[[:space:]]*\{ ]] \ + || [[ "$masked" =~ (K9Pedigree|pedigree_schema)[[:space:]]*\& ]] \ + || [[ "$masked" =~ [\&\|][[:space:]]*([A-Za-z_][A-Za-z0-9_]*\.)?(K9Pedigree|pedigree_schema) ]]; then has_pedigree=true fi @@ -361,13 +381,45 @@ validate_k9() { has_pedigree_version=true fi - # Security level (leash field) - if [[ "$line" =~ (^|[[:space:]\{,])(leash|security_level)[[:space:]]*[=:][[:space:]]*([^,\}\#]+) ]]; then - has_security_level=true - has_primary_security_level=true - security_level_value="$(normalise_level "${BASH_REMATCH[0]}")" - security_level_line=$line_num - elif [[ "$has_primary_security_level" == "false" ]]; then + # Security level (leash field). A `{{...}}` template placeholder, + # quoted or not, is captured whole: the generic value class stops at + # the first `}` and would cut it to `{{level`. + # A Nickel binding such as `let leash = import "../leash.ncl" in` + # names a module, not a level. Only the binding itself is dropped: + # a body on the same line (`in { leash = 'Hunt, ... }`) is still + # scanned. Every level field on the line is recorded, not only the + # first. + # Field names are found in the quote-masked line, so prose such as + # `description = "leash = Hunt"` is not a field; each value is then + # read from the same offset in the original line (masking keeps + # every character in place), so a quoted placeholder still works. + local scan_m="$masked" scan_o="$line" level_key key_prefix key_end + local line_has_level=false + if [[ "$scan_m" =~ ^([[:space:]]*let[[:space:]]+(leash|security_level)[[:space:]]*=) ]]; then + scan_m="${scan_m:${#BASH_REMATCH[1]}}" + scan_o="${scan_o:${#BASH_REMATCH[1]}}" + fi + while [[ "$scan_m" =~ (^|[[:space:]\{,])(leash|security_level)[[:space:]]*[=:] ]]; do + level_key="${BASH_REMATCH[0]}" + key_prefix="${scan_m%%"$level_key"*}" + key_end=$(( ${#key_prefix} + ${#level_key} )) + scan_m="${scan_m:key_end}" + scan_o="${scan_o:key_end}" + if [[ "$scan_o" =~ ^[[:space:]]*([\"\']?\{\{[^\}]*\}\}[\"\']?|[^,\}\#]+) ]]; then + line_has_level=true + has_security_level=true + has_primary_security_level=true + security_level_value="$(normalise_level "${level_key}${BASH_REMATCH[0]}")" + security_level_line=$line_num + record_security_level "$security_level_value" "$line_num" + fi + done + # A policy trust_level is recorded wherever it appears, whether or + # not a leash came earlier: the order of declarations must not + # decide whether a Hunt level needs a signature. + if [[ "$line_has_level" == "true" ]]; then + : + else local trust_is_policy=false if [[ "$line" =~ policy[[:space:]]*\.[[:space:]]*trust_level[[:space:]]*[=:] ]] \ || [[ "$in_policy" == "true" && "$line" =~ (^|[[:space:]\{,])trust_level[[:space:]]*[=:] ]] \ @@ -375,12 +427,13 @@ validate_k9() { trust_is_policy=true fi if [[ "$trust_is_policy" == "true" ]] \ - && [[ "$line" =~ trust_level[[:space:]]*[=:][[:space:]]*([^,\}]+) ]]; then + && [[ "$line" =~ trust_level[[:space:]]*[=:][[:space:]]*([\"\']?\{\{[^\}]*\}\}[\"\']?|[^,\}]+) ]]; then # Only schema-v1 policy.trust_level is a leash. Legacy # pedigree-level trust_level is descriptive metadata. has_security_level=true security_level_value="$(normalise_level "${BASH_REMATCH[0]}")" security_level_line=$line_num + record_security_level "$security_level_value" "$line_num" fi fi @@ -390,9 +443,14 @@ validate_k9() { fi if [[ "$in_policy" == "true" ]]; then - local without_open="${line//\{/}" - local without_close="${line//\}/}" - policy_depth=$((policy_depth + ${#line} - ${#without_open} - ${#line} + ${#without_close})) + # Count braces outside quoted strings only: `description = "}"` + # must not end the policy block early and hide a later + # trust_level. + local structural + structural="$(mask_k9_quoted_text "$line")" + local without_open="${structural//\{/}" + local without_close="${structural//\}/}" + policy_depth=$((policy_depth + ${#structural} - ${#without_open} - ${#structural} + ${#without_close})) if (( policy_depth <= 0 )); then in_policy=false policy_depth=0 @@ -416,26 +474,38 @@ validate_k9() { fi # --- Check 4: Security level validation --- - if [[ "$has_security_level" == "true" && "$security_level_value" =~ ^\{\{.*\}\}$ ]]; then - # Scaffold file: the level is a template placeholder to be filled at - # instantiation time. Note it, but a template cannot validate as - # concrete and flagging it every run just trains people to ignore - # the gate. - annotate "notice" "$file" "$security_level_line" \ - "Security level is a template placeholder (${security_level_value}); skipping level validation" - elif [[ "$has_security_level" == "true" ]]; then - local level_valid=false - for valid in $VALID_LEVELS; do - if [[ "$security_level_value" == "$valid" ]]; then - level_valid=true - break + # Every level the file declares is checked, not only the last one. + local hunt_line=0 + if [[ "$has_security_level" == "true" ]]; then + local i level level_line + for i in "${!seen_levels[@]}"; do + level="${seen_levels[$i]}" + level_line="${seen_level_lines[$i]}" + if [[ "$level" =~ ^\{\{.*\}\}$ ]]; then + # Scaffold file: the level is a template placeholder to be + # filled at instantiation time. Note it, but a template + # cannot validate as concrete and flagging it every run just + # trains people to ignore the gate. + annotate "notice" "$file" "$level_line" \ + "Security level is a template placeholder (${level}); skipping level validation" + continue fi - done - if [[ "$level_valid" == "false" ]]; then - report_issue "error" "$file" "$security_level_line" \ - "Invalid security level '${security_level_value}'. Must be one of: kennel, yard, hunt" - fi + local level_valid=false + for valid in $VALID_LEVELS; do + if [[ "$level" == "$valid" ]]; then + level_valid=true + break + fi + done + + if [[ "$level_valid" == "false" ]]; then + report_issue "error" "$file" "$level_line" \ + "Invalid security level '${level}'. Must be one of: kennel, yard, hunt" + elif [[ "$level" == "hunt" && "$hunt_line" -eq 0 ]]; then + hunt_line="$level_line" + fi + done else if [[ "$has_pedigree" == "true" ]]; then report_issue "warning" "$file" 1 \ @@ -444,8 +514,9 @@ validate_k9() { fi # --- Check 5: Hunt-level signature requirement --- - if [[ "$security_level_value" == "hunt" && "$has_signature_field" == "false" ]]; then - report_issue "error" "$file" "$security_level_line" \ + # Any Hunt level needs a signature, wherever it sits in the file. + if [[ "$hunt_line" -ne 0 && "$has_signature_field" == "false" ]]; then + report_issue "error" "$file" "$hunt_line" \ "Hunt-level K9 file must include a 'signature' or 'signature_required' field" fi }