Files
CASAN/AINative_OKR_CASAN5/packages/casan-harness/tests/phase-sec03-tests.sh
T
thanhnvandClaude Opus 4.8 664bd1f00c feat(plan-01): Phase 1 — relocate harness code to packages/casan-harness (symlink facade)
Physically move the pure-code subtrees out of .specify into the package, leaving
compat symlinks at the old .specify/<dir> paths so every existing reference (internal
CASAN_HARNESS_ROOT + external CI/docker/mjs) keeps resolving. Runtime state stays put.

Moved (git mv): scripts/ tests/ security/ templates/ config/ governance/ memory/
  .specify/<dir>  ->  packages/casan-harness/<dir>   (+ .specify/<dir> symlink)
Stays in .specify (state/governance/domain, handled later): logs/ agentops/ level5/
  init-options.json traceability-map.json

Python `.resolve()` self-location followed the compat symlink into packages and lost
the app root; generate-casan-demo-context.py, generate-agentops-dashboard.py and
dashboard-server.py now walk UP for the `.specify` state marker instead of a fixed
parent depth (fixes "missing trace files" in run-casan4).

Full gate: PASS=64 FAIL=0 SKIP=3 (CASAN_CI_STEP_TIMEOUT_SEC=1200 — track-a ~450s runs
close to the 600s default and can tip over under load; this is timing variance, not a
regression — it passed cleanly with headroom). Runtime log/audit artifacts kept unstaged.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 00:06:00 +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