#!/bin/bash
set -euo pipefail

# Portable root detection - works anywhere
ROOT="${PRUVA_ROOT:-$(cd "$(dirname "$0")/.." && pwd)}"
LOGS="$ROOT/logs"
mkdir -p "$LOGS"

cd "$ROOT"

echo "=== Crawl4AI RCE via Hooks Parameter - Reproduction Script ==="
echo "CVE-2026-26216 / GHSA-5882-5rx9-xgxp"
echo ""

# Cleanup function
cleanup() {
    echo "[+] Cleaning up..."
    pkill -f "crawl4ai_rce_demo.py" 2>/dev/null || true
    # Removed unused line
}
trap cleanup EXIT

# Run the Python demonstration
python3 "$ROOT/repro/crawl4ai_rce_demo.py" 2>&1 | tee "$LOGS/reproduction_output.log"

exit_code=${PIPESTATUS[0]}

echo ""
echo "=========================================="
if [ $exit_code -eq 0 ]; then
    echo "[VULNERABILITY CONFIRMED]"
    echo "=========================================="
    echo ""
    echo "The reproduction script successfully demonstrated:"
    echo "  - RCE via hooks parameter using __import__ builtin"
    echo "  - Environment variable exfiltration"
    echo ""
    echo "Logs saved to: $LOGS/"
    exit 0
else
    echo "[!] Vulnerability reproduction failed"
    exit 1
fi
