Files
CASAN/AINative_OKR_CASAN5/.specify/tests/phase-sec03-tests.sh
T
thanhnvandClaude Opus 4.8 2c765c9a45 feat(plan-01): Phase 0.5 — path indirection via casan-paths.sh (no file moves)
Task 1.2: introduce a single path resolver so no harness script hardcodes
`.specify/...` scattered across the tree. casan-paths.sh resolves four roots
(HARNESS/STATE/GOVERNANCE/APP) by marker-based walk-up from its own location —
never `git rev-parse` (git root is the repo PARENT here, not the app dir).

- 101 bash scripts/tests: 238 hardcoded `$PROJECT_ROOT/.specify/...` refs rewritten
  to CASAN_HARNESS_ROOT (code) / CASAN_STATE_ROOT (logs,state) / CASAN_GOVERNANCE_ROOT.
  Sandbox test vars ($WORK/$TP/$FP/$T1_WORK) left untouched.
- Roots are NOT exported: each script/subprocess self-resolves from its own tree,
  matching the original per-script semantics and preserving hermetic sandbox isolation
  (node casan-step.mjs, copied telemetry/rollback scripts must not inherit real roots).
- Sandbox tests that copy a harness script now also copy casan-paths.sh (its new
  sibling dependency): adversarial (verify-audit-chain/verify-tool-audit/rollback) +
  track-a (security-check/telemetry-integrity).
- control-plane-settings.json reclassified as STATE (untracked runtime store).

Roots all still resolve to `.specify` in this monolithic layout, so behavior is
unchanged. Full gate: PASS=64 FAIL=0 SKIP=3 (adversarial 44/0, track-a 25/0).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-07 22:07:41 +09:00

79 lines
3.4 KiB
Bash

#!/usr/bin/env bash
set -uo pipefail
# CASAN Plan-16 SEC-03 (H-03) — rollback-manager: no `bash -c`, structured argv only.
#
# The rollback `execute` path used to `bash -c "$rollback_command"` where the
# command was read from the unsigned transaction log — arbitrary code execution
# for anyone who can append a line. This proves:
# * a genuine checkpoint still restores the file (regression),
# * a forged free-form record with an RCE payload is REFUSED (not executed),
# * a forged structured record whose backup points outside the controlled
# backup dir is REFUSED (cannot copy an arbitrary source file).
#
# Deterministic; no model/app/network. Hermetic tx log via a temp workspace.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../scripts/bash/casan-paths.sh"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
RM="$CASAN_HARNESS_ROOT/scripts/bash/rollback-manager.sh"
TX_LOG="$CASAN_STATE_ROOT/logs/level5/rollback-transactions.jsonl"
WORK="$(mktemp -d)"
# rollback-manager uses a fixed tx-log path; back it up and restore on exit.
[[ -f "$TX_LOG" ]] && cp -p "$TX_LOG" "$WORK/tx.bak"
restore_tx() { if [[ -f "$WORK/tx.bak" ]]; then cp -p "$WORK/tx.bak" "$TX_LOG"; else rm -f "$TX_LOG"; fi; }
trap 'restore_tx; rm -f "$WORK/PWNED"; rm -rf "$WORK"' EXIT
PASS=0; FAIL=0
pass() { echo "PASS: $1"; PASS=$((PASS + 1)); }
fail() { echo "FAIL: $1"; FAIL=$((FAIL + 1)); }
rc_of() { set +e; "$@" >/dev/null 2>&1; echo $?; set -e 2>/dev/null || true; }
echo "===== Plan-16 SEC-03: rollback-manager no bash -c (RCE) ====="
# 1) Regression: genuine checkpoint -> execute restores exact content.
TARGET="$WORK/plan.txt"; echo "ORIGINAL" > "$TARGET"
TX="$(bash "$RM" checkpoint "$TARGET" | sed -n 's/.*transaction_id=\([^ ]*\).*/\1/p')"
echo "OVERWRITTEN" > "$TARGET"
bash "$RM" execute "$TX" >/dev/null 2>&1
[[ "$(cat "$TARGET")" == "ORIGINAL" ]] \
&& pass "genuine checkpoint restores exact pre-overwrite content" \
|| fail "checkpoint did not restore (got: $(cat "$TARGET"))"
# 2) Fail-able: forged free-form record carrying an RCE payload must be REFUSED.
rm -f "$WORK/PWNED"
python3 - "$TX_LOG" "$WORK/PWNED" <<'PY'
import json, sys
log, marker = sys.argv[1], sys.argv[2]
open(log, "a", encoding="utf-8").write(json.dumps({
"transaction_id": "evil-rce", "action": "checkpoint",
"rollback_command": f"touch {marker}"}) + "\n")
PY
RC="$(rc_of bash "$RM" execute evil-rce)"
if [[ "$RC" -ne 0 && ! -f "$WORK/PWNED" ]]; then
pass "forged free-form rollback_command REFUSED, no code executed (rc=$RC)"
else
fail "RCE not prevented (rc=$RC, marker exists=$([[ -f "$WORK/PWNED" ]] && echo yes || echo no))"
fi
# 3) Fail-able: forged restore_file whose backup is outside the controlled dir.
SECRET="$WORK/secret.txt"; echo "SECRET" > "$SECRET"
python3 - "$TX_LOG" "$SECRET" "$WORK/stolen.txt" <<'PY'
import json, sys
log, backup, target = sys.argv[1], sys.argv[2], sys.argv[3]
open(log, "a", encoding="utf-8").write(json.dumps({
"transaction_id": "evil-src", "action": "checkpoint", "op": "restore_file",
"backup": backup, "target": target}) + "\n")
PY
RC="$(rc_of bash "$RM" execute evil-src)"
if [[ "$RC" -ne 0 && ! -f "$WORK/stolen.txt" ]]; then
pass "forged restore source outside backup dir REFUSED (rc=$RC)"
else
fail "arbitrary-source restore not prevented (rc=$RC)"
fi
echo ""
echo "===== SEC-03 SUMMARY: PASS=$PASS FAIL=$FAIL ====="
[[ "$FAIL" -eq 0 ]] || exit 1