Files
CASAN/packages/casan-harness/tests/phase-sec15-tests.sh
T
thanhnvandClaude Opus 4.8 36a4812ef3 refactor(structure): promote app to repo root + remove redundant workspace cruft
Standard production layout: the OKR app (was nested under AINative_OKR_CASAN5/) is now
the repository root. No more wrapper directory.

- Promote AINative_OKR_CASAN5/* -> repo root (backend/ frontend/ packages/ apps/
  .specify/ docs/ infra/ nginx/ scripts/ + configs). Merge tool dirs: .gitea (kept the
  active deploy ci.yml, added harness-ci.yml + runbooks), .claude (agents/commands +
  launch.json), .github moved up.
- Remove redundant: 00_SUBMISSION_PACKAGE, scattered root notes (FPT_CASAN_Full.md,
  tu-tuong-casan.md, casan-tu-sinh..., casan_harness_assessment.md, source-review...,
  README_CASAN5_REFINED.md), casan-next-plans/ and optimize-docs/ (competition/planning
  artifacts — roadmap + design history preserved in git log / commit messages).
- Update all references to the old layout:
  - .gitea/workflows/{ci,harness-ci}.yml, .github/workflows/{ci,deploy}.yml:
    working-directory .; drop AINative_OKR_CASAN5/ prefix; .specify/{tests,scripts}
    -> packages/casan-harness/... (.specify/logs state kept)
  - .claude/launch.json, .gitea/*-runbook.md: path prefixes
  - CLAUDE.md, README.md: docs/input -> apps/okr/domain/input
  - policy-bundle.yaml: 8 policy paths -> packages/casan-harness/...; manifest re-signed
- secrets-scan.sh: fixture excludes -> new package/domain paths.

Full gate from the new root: PASS=64 FAIL=0 SKIP=3.

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

73 lines
3.7 KiB
Bash

#!/usr/bin/env bash
set -uo pipefail
# CASAN Plan-16 SEC-15 — low-cluster hardening.
# * supply-chain typosquat: catch distance<=2 (was distance==1), no false positives
# (levenshtein length sentinel fixed so it no longer collides with the threshold),
# * tool-exec: fail CLOSED in enforced mode when no timeout backend exists,
# * validate-tool-input: recurse into nested objects/arrays (was one level deep).
#
# Deterministic; hermetic.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../scripts/bash/casan-paths.sh"
PROJECT_ROOT="$CASAN_APP_ROOT"
S="$CASAN_HARNESS_ROOT/scripts/bash"
WORK="$(mktemp -d)"
trap '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-15: low-cluster hardening ====="
# ---- typosquat (distance<=2) ----
printf '{"dependencies":{}}' > "$WORK/base.json"
printf 'requests\nnumpy\nexpress\n' > "$WORK/known.txt"; : > "$WORK/deny.txt"
scan() { python3 "$S/supply-chain-scan.py" "$1" "$WORK/base.json" "$WORK/known.txt" "$WORK/deny.txt" "$WORK/r.json" "" >/dev/null 2>&1; }
printf '{"dependencies":{"reqeusts":"1.0.0"}}' > "$WORK/m1.json"; scan "$WORK/m1.json"
grep -q "typosquat_of:requests" "$WORK/r.json" \
&& pass "2-char typosquat (reqeusts→requests) flagged" || fail "2-char typosquat missed"
printf '{"dependencies":{"fastapi":"1.0.0"}}' > "$WORK/m2.json"; scan "$WORK/m2.json"
grep -q "typosquat" "$WORK/r.json" \
&& fail "legit package fastapi false-flagged as typosquat" \
|| pass "legit distant package not false-flagged (levenshtein sentinel fixed)"
# ---- tool-exec fail-closed when no timeout backend ----
# Hermetic: build a PATH that contains ONLY bash (no timeout, no perl) so the
# no-backend branch is reached regardless of the host's /usr layout. On
# merged-/usr systems /bin is a symlink to /usr/bin, so PATH=/bin would still
# find timeout/perl — the old heuristic only worked on split-/usr (e.g. macOS).
ONLYBIN="$WORK/onlybin"; mkdir -p "$ONLYBIN"
BASH_BIN="$(command -v bash)"
ln -sf "$BASH_BIN" "$ONLYBIN/bash"
[[ "$(set +e; PATH="$ONLYBIN" CASAN_TOOL_EXEC_STRICT=1 "$BASH_BIN" "$S/tool-exec.sh" 2 -- echo hi >/dev/null 2>&1; echo $?)" -eq 2 ]] \
&& pass "tool-exec refuses (fail-closed) with no timeout backend in enforced mode" \
|| fail "tool-exec did not fail closed without a timeout backend"
[[ "$(set +e; PATH="$ONLYBIN" "$BASH_BIN" "$S/tool-exec.sh" 2 -- echo hi >/dev/null 2>&1; echo $?)" -eq 0 ]] \
&& pass "tool-exec dev: runs without backend (backward compatible)" \
|| fail "tool-exec dev mode broke"
# ---- validate-tool-input recursion ----
cat > "$WORK/schema.json" <<'J'
{"type":"object","additionalProperties":false,"properties":{
"cfg":{"type":"object","additionalProperties":false,"properties":{"port":{"type":"integer"}}}}}
J
printf '{"cfg":{"port":8080}}' > "$WORK/ok.json"
printf '{"cfg":{"port":"NOPE"}}' > "$WORK/badtype.json"
printf '{"cfg":{"port":80,"evil":"x"}}' > "$WORK/badextra.json"
[[ "$(rc_of bash "$S/validate-tool-input.sh" "$WORK/schema.json" "$WORK/ok.json")" -eq 0 ]] \
&& pass "valid nested object accepted" || fail "valid nested object rejected"
[[ "$(rc_of bash "$S/validate-tool-input.sh" "$WORK/schema.json" "$WORK/badtype.json")" -eq 2 ]] \
&& pass "nested wrong type rejected (recursion)" || fail "nested wrong type slipped through"
[[ "$(rc_of bash "$S/validate-tool-input.sh" "$WORK/schema.json" "$WORK/badextra.json")" -eq 2 ]] \
&& pass "nested unexpected field rejected (recursion)" || fail "nested unexpected field slipped through"
echo ""
echo "===== SEC-15 SUMMARY: PASS=$PASS FAIL=$FAIL ====="
[[ "$FAIL" -eq 0 ]] || exit 1