Files
CASAN/AINative_OKR_CASAN5/.specify/scripts/bash/path-guard.sh
T
thanhnvandClaude Opus 4.8 8c06a55aed feat: plan 16 P2 batch 1 (SEC-13 SSRF, SEC-27 log-escape, SEC-28 path-traversal)
- SEC-13 (M-09): SSRF allowlist on provider-usage-fetch (always block non-http(s)
  schemes; enforced mode blocks internal/link-local IPs + non-allowlisted hosts,
  dev keeps loopback mocks); dashboard refuses non-loopback bind in enforced mode.
- SEC-27 (X-02): casan-log strips ESC/CSI + CR/LF (terminal-escape + fake-log-line
  injection) while keeping tab and visible text.
- SEC-28 (X-04): new path-guard.sh — realpath resolve + reject symlink/.. escapes
  outside the allowed root.

Verify: SEC-13 6/0, SEC-27 3/0, SEC-28 4/0, adversarial 44/44, run-casan4 0-FAIL.

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

40 lines
1.3 KiB
Bash

#!/usr/bin/env bash
set -uo pipefail
# CASAN Plan-16 SEC-28 (X-04) — path-traversal / symlink guard.
#
# A tool that takes a file path as input/output can be pointed at an arbitrary
# location via `..` or a symlink (e.g. a symlink named "input.txt" -> /etc/passwd),
# reading or writing outside the workspace. This resolves the REAL path (following
# every symlink) and refuses anything that escapes the allowed root.
#
# Usage: path-guard.sh <path> [allowed-root] (default root: repo workspace)
# Exit: 0 inside the root, 1 outside / unresolvable.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)"
TARGET="${1:-}"
ROOT="${2:-$PROJECT_ROOT}"
if [[ -z "$TARGET" ]]; then
echo "Usage: path-guard.sh <path> [allowed-root]" >&2
exit 64
fi
python3 - "$TARGET" "$ROOT" <<'PY'
import os
import sys
target, root = sys.argv[1], sys.argv[2]
# realpath resolves symlinks in every existing path component and normalizes "..";
# for a not-yet-created leaf it resolves the existing parent chain.
real_target = os.path.realpath(target)
real_root = os.path.realpath(root)
if real_target == real_root or real_target.startswith(real_root + os.sep):
print(f"PATH_OK {real_target}")
sys.exit(0)
sys.stderr.write(f"PATH_ESCAPES_ROOT target={target} real={real_target} root={real_root}\n")
sys.exit(1)
PY