From c1ab522d8d4f6c6a4feb350658df4cccda1f8b2f Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 16:50:31 +0000 Subject: [PATCH 1/3] fix(validate-action): stop K9 levels being cut, lost or overwritten Review of the copies of validate-k9.sh vendored into five repositories found these faults in the validator itself. Each one now has a regression case in tests/test-validate-k9.sh, and each case was run against the old and the new script. Security: the Hunt signature rule could be bypassed. - A later leash field overwrote an earlier one. A pedigree with `leash = 'Hunt` and an unrelated `leash = 'Kennel` further down passed unsigned. Every level a file declares is now recorded and validated, and any Hunt level needs a signature. - Policy-block depth was counted on raw text, so `description = "}"` ended the block early and hid a later `trust_level = 'Hunt`, which then passed unsigned. Braces inside quoted strings are no longer counted. False errors: - The level capture stopped at the first `}`, so a template placeholder `"{{LEVEL}}"` arrived as `{{level` and failed as an invalid level. It now reaches the placeholder notice whole, quoted or not. - A last line without a trailing newline was dropped, so a final-line `signature:` was not seen. - Contract application with a pipe, `{ ... } | X.K9Pedigree`, was not recognised as a pedigree or a format marker, although the marker error message lists it. - `let leash = import "../leash.ncl" in` was read as the level `import ../leash.ncl in`. This repository's own examples/deploy.k9.ncl and examples/container-deploy.k9.ncl hit it. Run old and new across 25 checked-out estate repositories (about 480 K9 files): no repository gains an error. Here, the two examples files stop failing. release-k9.k9.ncl still fails, correctly: it is a Hunt contract with no signature and no name. Not changed: - Skipping K9-suffixed files with no pedigree signal stays as it is; the suite pins that on purpose. - Name and version are still scanned file-wide in the plain dialect, as the header documents. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0132Cdz3iwk2QGKv2sUhhsye --- validate-action/tests/test-validate-k9.sh | 104 ++++++++++++++++++++++ validate-action/validate-k9.sh | 101 ++++++++++++++------- 2 files changed, 173 insertions(+), 32 deletions(-) diff --git a/validate-action/tests/test-validate-k9.sh b/validate-action/tests/test-validate-k9.sh index ae4bf7a..3810058 100755 --- a/validate-action/tests/test-validate-k9.sh +++ b/validate-action/tests/test-validate-k9.sh @@ -193,4 +193,108 @@ 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" + echo 'K9 validator regression tests passed' diff --git a/validate-action/validate-k9.sh b/validate-action/validate-k9.sh index 24d4f56..569e7e4 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 @@ -258,7 +267,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 +292,7 @@ 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)|import[[:space:]]*"[^"]*(pedigree|\.k9)\.ncl"' <<< "$syntax_content"; then has_marker=true fi @@ -296,7 +305,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 +334,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 @@ -342,7 +353,7 @@ validate_k9() { 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 + || [[ "$line" =~ [\&\|][[:space:]]*([A-Za-z_][A-Za-z0-9_]*\.)?(K9Pedigree|pedigree_schema) ]]; then has_pedigree=true fi @@ -361,12 +372,19 @@ validate_k9() { has_pedigree_version=true fi - # Security level (leash field) - if [[ "$line" =~ (^|[[:space:]\{,])(leash|security_level)[[:space:]]*[=:][[:space:]]*([^,\}\#]+) ]]; 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, so it is not a leash field. + if [[ "$line" =~ ^[[:space:]]*let[[:space:]]+(leash|security_level)[[:space:]]*= ]]; then + : + elif [[ "$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 + record_security_level "$security_level_value" "$line_num" elif [[ "$has_primary_security_level" == "false" ]]; then local trust_is_policy=false if [[ "$line" =~ policy[[:space:]]*\.[[:space:]]*trust_level[[:space:]]*[=:] ]] \ @@ -375,12 +393,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 +409,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 +440,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 +480,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 } From bd953b37dbe091ca4a002c0db8f9cb071a15f165 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 17:11:50 +0000 Subject: [PATCH 2/3] fix(validate-action): scan every level on a line, keep let bodies, mask quoted pipes - Record every leash/security_level field on a line, not only the first, so `leash = 'Kennel }, app = { leash = 'Hunt` still triggers the signature rule. - Drop only the `let leash =` binding, not the whole line, so a body on the same line (`in { leash = 'Hunt, ...`) is still scanned. - Match K9Pedigree/pedigree_schema applications on quote-masked text, so a schema name inside a string value is neither a pedigree nor a format marker. The quoted-import check still reads the unmasked text. Regression cases added for each; the previous validator passes all three bad files, this one rejects them. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0132Cdz3iwk2QGKv2sUhhsye --- validate-action/tests/test-validate-k9.sh | 51 +++++++++++++++++++++++ validate-action/validate-k9.sh | 44 +++++++++++++------ 2 files changed, 83 insertions(+), 12 deletions(-) diff --git a/validate-action/tests/test-validate-k9.sh b/validate-action/tests/test-validate-k9.sh index 3810058..7bc8e1c 100755 --- a/validate-action/tests/test-validate-k9.sh +++ b/validate-action/tests/test-validate-k9.sh @@ -297,4 +297,55 @@ 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" + echo 'K9 validator regression tests passed' diff --git a/validate-action/validate-k9.sh b/validate-action/validate-k9.sh index 569e7e4..f3b0808 100755 --- a/validate-action/validate-k9.sh +++ b/validate-action/validate-k9.sh @@ -252,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. @@ -292,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 @@ -349,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 @@ -376,15 +385,26 @@ validate_k9() { # 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, so it is not a leash field. - if [[ "$line" =~ ^[[:space:]]*let[[:space:]]+(leash|security_level)[[:space:]]*= ]]; then - : - elif [[ "$line" =~ (^|[[:space:]\{,])(leash|security_level)[[:space:]]*[=:][[:space:]]*([\"\']?\{\{[^\}]*\}\}[\"\']?|[^,\}\#]+) ]]; then + # 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. + local level_scan="$line" level_match line_has_level=false + if [[ "$level_scan" =~ ^([[:space:]]*let[[:space:]]+(leash|security_level)[[:space:]]*=) ]]; then + level_scan="${level_scan:${#BASH_REMATCH[1]}}" + fi + while [[ "$level_scan" =~ (^|[[:space:]\{,])(leash|security_level)[[:space:]]*[=:][[:space:]]*([\"\']?\{\{[^\}]*\}\}[\"\']?|[^,\}\#]+) ]]; do + level_match="${BASH_REMATCH[0]}" + line_has_level=true has_security_level=true has_primary_security_level=true - security_level_value="$(normalise_level "${BASH_REMATCH[0]}")" + security_level_value="$(normalise_level "$level_match")" security_level_line=$line_num record_security_level "$security_level_value" "$line_num" + level_scan="${level_scan#*"$level_match"}" + done + if [[ "$line_has_level" == "true" ]]; then + : elif [[ "$has_primary_security_level" == "false" ]]; then local trust_is_policy=false if [[ "$line" =~ policy[[:space:]]*\.[[:space:]]*trust_level[[:space:]]*[=:] ]] \ From e24cfb2d3cc9301297590408613a11628bb1a40f Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 18:02:17 +0000 Subject: [PATCH 3/3] fix(validate-action): ignore levels in quoted prose; record policy levels in any order - Level fields are now found in the quote-masked line, and each value is read from the same offset in the original line, so `description = "example leash = Hunt"` no longer counts as a Hunt declaration. Quoted placeholders and two real fields on one line still work. - A policy trust_level is recorded even when a leash appeared earlier in the file. Before, `leash = 'Kennel` followed by `policy.trust_level = 'Hunt` let an unsigned file pass, while the reverse order did not. Both cases have regression tests; the previous validator gets each one wrong. Results on the repository's own K9 files are unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0132Cdz3iwk2QGKv2sUhhsye --- validate-action/tests/test-validate-k9.sh | 32 ++++++++++++++++++ validate-action/validate-k9.sh | 40 +++++++++++++++-------- 2 files changed, 59 insertions(+), 13 deletions(-) diff --git a/validate-action/tests/test-validate-k9.sh b/validate-action/tests/test-validate-k9.sh index 7bc8e1c..b0b5ae4 100755 --- a/validate-action/tests/test-validate-k9.sh +++ b/validate-action/tests/test-validate-k9.sh @@ -348,4 +348,36 @@ if INPUT_PATH="$CASE_DIR" "$ACTION_DIR/validate-k9.sh" > "$CASE_DIR/out.log" 2>& 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 f3b0808..a4f681f 100755 --- a/validate-action/validate-k9.sh +++ b/validate-action/validate-k9.sh @@ -389,23 +389,37 @@ validate_k9() { # a body on the same line (`in { leash = 'Hunt, ... }`) is still # scanned. Every level field on the line is recorded, not only the # first. - local level_scan="$line" level_match line_has_level=false - if [[ "$level_scan" =~ ^([[:space:]]*let[[:space:]]+(leash|security_level)[[:space:]]*=) ]]; then - level_scan="${level_scan:${#BASH_REMATCH[1]}}" + # 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 [[ "$level_scan" =~ (^|[[:space:]\{,])(leash|security_level)[[:space:]]*[=:][[:space:]]*([\"\']?\{\{[^\}]*\}\}[\"\']?|[^,\}\#]+) ]]; do - level_match="${BASH_REMATCH[0]}" - line_has_level=true - has_security_level=true - has_primary_security_level=true - security_level_value="$(normalise_level "$level_match")" - security_level_line=$line_num - record_security_level "$security_level_value" "$line_num" - level_scan="${level_scan#*"$level_match"}" + 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 : - elif [[ "$has_primary_security_level" == "false" ]]; then + else local trust_is_policy=false if [[ "$line" =~ policy[[:space:]]*\.[[:space:]]*trust_level[[:space:]]*[=:] ]] \ || [[ "$in_policy" == "true" && "$line" =~ (^|[[:space:]\{,])trust_level[[:space:]]*[=:] ]] \