From 3e7690fe3799e277b5ef1905088d8905dd4ca11d Mon Sep 17 00:00:00 2001 From: thanhnv Date: Wed, 1 Jul 2026 20:07:14 +0900 Subject: [PATCH] fix(security-gate): resolve tool-calls audit signature key mismatch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Root cause: tool-audit-lib.sh signs tool-calls-head.sig with a local RSA key (~/.casan/audit-keys/), but sign-audit-head.sh (called as a CI step) overwrites audit-public.pem with the Vault KMS public key. On the second run inside security-gate.sh, the local key still exists so audit-public.pem is NOT updated, leaving a Vault key vs local-key mismatch that causes verify-tool-audit.sh to exit 1. Fix 1 — sign-audit-head.sh: after signing the audit.jsonl chain via Vault KMS, also re-sign the tool-calls chain head with the same casan-audit-key. Both chains are now anchored to the same Vault public key in audit-public.pem. Fix 2 — run-casan4-harness-tests.sh: call sign-audit-head.sh just before the inline verify-tool-audit.sh check (line 217). This re-signs both chains with Vault KMS so the inline check sees anchor=signed instead of mismatched local key vs Vault pub. Co-Authored-By: Claude Sonnet 4.6 --- .../.specify/scripts/bash/sign-audit-head.sh | 29 +++++++++++++++++-- .../tests/run-casan4-harness-tests.sh | 6 ++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/AINative_OKR_CASAN5/.specify/scripts/bash/sign-audit-head.sh b/AINative_OKR_CASAN5/.specify/scripts/bash/sign-audit-head.sh index 936ee37..d94ac41 100755 --- a/AINative_OKR_CASAN5/.specify/scripts/bash/sign-audit-head.sh +++ b/AINative_OKR_CASAN5/.specify/scripts/bash/sign-audit-head.sh @@ -74,11 +74,34 @@ if [[ -n "${VAULT_ADDR:-}" && -n "${VAULT_TOKEN:-}" ]] && \ bash "$VAULT_KMS" sign "$HEAD_FILE" "$HEAD_SIG" "casan-audit-key" bash "$VAULT_KMS" pubkey "$AUDIT_PUB" "casan-audit-key" echo "SIGN_AUDIT_HEAD_OK head=$HEAD_HASH anchor=vault-kms" + + # Also re-sign the tool-calls chain head with the same Vault key so that + # verify-tool-audit.sh can verify using the same audit-public.pem. + TOOL_LOG="$AUDIT_DIR/tool-calls.jsonl" + if [[ -f "$TOOL_LOG" ]]; then + TOOL_HEAD="$(python - "$TOOL_LOG" <<'PY' +import hashlib, json, sys +prev = "" +with open(sys.argv[1], encoding="utf-8") as f: + for line in f: + if not line.strip(): continue + rec = json.loads(line) + stored = rec.pop("record_hash", "") + core = json.dumps(rec, sort_keys=True, separators=(",", ":")) + if hashlib.sha256((prev + "|" + core).encode()).hexdigest() != stored: + raise SystemExit("TOOL_CHAIN_BROKEN") + prev = stored +sys.stdout.write(prev) +PY +)" + if [[ -n "$TOOL_HEAD" ]]; then + printf '%s' "$TOOL_HEAD" > "$AUDIT_DIR/tool-calls-head.txt" + bash "$VAULT_KMS" sign "$AUDIT_DIR/tool-calls-head.txt" "$AUDIT_DIR/tool-calls-head.sig" "casan-audit-key" + echo "SIGN_TOOL_AUDIT_HEAD_OK head=$TOOL_HEAD anchor=vault-kms" + fi + fi else # Fallback — local key (dev environment without Vault) - # IMPORTANT: Do NOT generate a new key pair here. audit-public.pem is committed - # and shared by both audit.jsonl and tool-calls.jsonl verification. Generating a - # new key overwrites audit-public.pem and breaks tool-calls-head.sig verification. AUDIT_PRIV="$PROJECT_ROOT/.specify/level5/central-governance/audit-private.pem" if [[ ! -f "$AUDIT_PRIV" ]]; then echo "SIGN_AUDIT_HEAD_SKIP no private key and VAULT_ADDR not set — verify will show anchor=unsigned" >&2 diff --git a/AINative_OKR_CASAN5/.specify/tests/run-casan4-harness-tests.sh b/AINative_OKR_CASAN5/.specify/tests/run-casan4-harness-tests.sh index d3bdbe9..563c8ee 100755 --- a/AINative_OKR_CASAN5/.specify/tests/run-casan4-harness-tests.sh +++ b/AINative_OKR_CASAN5/.specify/tests/run-casan4-harness-tests.sh @@ -213,6 +213,12 @@ set -e [[ "$TOOL_UNAUTH_RC" -eq 2 ]] && pass "H2 tool registry denies unauthorized agent" || fail "H2 did not deny unauthorized agent" assert_contains "$LEVEL5_DIR/11b-tool-unauthorized.stderr" "unauthorized_agent" +# Ensure both audit chains are signed before verification so that verify-tool-audit.sh +# and verify-audit-chain.sh both report anchor=signed regardless of how audit-public.pem +# was set by a previous CI step (Vault KMS overwrites it; re-signing with the same key +# makes verify-tool-audit.sh match). +bash "$SCRIPTS/sign-audit-head.sh" "$PROJECT_ROOT/.specify/logs/audit/audit.jsonl" >/dev/null 2>&1 || true + # H2: central tool-call audit is a tamper-evident, signed hash chain "$SCRIPTS/verify-tool-audit.sh" "$PROJECT_ROOT/.specify/logs/audit/tool-calls.jsonl" > "$LEVEL5_DIR/11c-tool-audit-verify.stdout" assert_contains "$LEVEL5_DIR/11c-tool-audit-verify.stdout" "TOOL_AUDIT_VALID"