#!/bin/bash
set -euo pipefail

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

cd "$ROOT"

TEST_URI="https://allowed.com%40attacker.com/"

echo "[*] Setting up test directories..."
VULN_DIR="$ROOT/.repro_scratch/vuln"
FIXED_DIR="$ROOT/.repro_scratch/fixed"
mkdir -p "$VULN_DIR" "$FIXED_DIR"

# Install vulnerable version if not already present
if [[ ! -d "$VULN_DIR/node_modules" ]]; then
  echo "[*] Installing fast-uri@3.1.1 (vulnerable)..."
  (cd "$VULN_DIR" && npm install fast-uri@3.1.1 --silent)
fi

# Install fixed version if not already present
if [[ ! -d "$FIXED_DIR/node_modules" ]]; then
  echo "[*] Installing fast-uri@3.1.2 (fixed)..."
  (cd "$FIXED_DIR" && npm install fast-uri@3.1.2 --silent)
fi

echo "[*] Running PoC against vulnerable version..."
node -e "
const fastUri = require('$VULN_DIR/node_modules/fast-uri');
const normalized = fastUri.normalize('$TEST_URI');
const parsed = fastUri.parse(normalized);
console.log('VULNERABLE BUILD (3.1.1)');
console.log('  Input URI:       $TEST_URI');
console.log('  Normalized URI:  ' + normalized);
console.log('  Parsed host:     ' + parsed.host);
console.log('  Parsed userinfo: ' + (parsed.userinfo || 'undefined'));

const fs = require('fs');
fs.writeFileSync('$LOGS/vuln_result.json', JSON.stringify({
  version: '3.1.1',
  input: '$TEST_URI',
  normalized: normalized,
  host: parsed.host,
  userinfo: parsed.userinfo || null
}, null, 2));
"

echo "[*] Running PoC against fixed version..."
node -e "
const fastUri = require('$FIXED_DIR/node_modules/fast-uri');
const normalized = fastUri.normalize('$TEST_URI');
const parsed = fastUri.parse(normalized);
console.log('FIXED BUILD (3.1.2)');
console.log('  Input URI:       $TEST_URI');
console.log('  Normalized URI:  ' + normalized);
console.log('  Parsed host:     ' + parsed.host);
console.log('  Parsed userinfo: ' + (parsed.userinfo || 'undefined'));

const fs = require('fs');
fs.writeFileSync('$LOGS/fixed_result.json', JSON.stringify({
  version: '3.1.2',
  input: '$TEST_URI',
  normalized: normalized,
  host: parsed.host,
  userinfo: parsed.userinfo || null
}, null, 2));
"

echo "[*] Evaluating results..."
VULN_HOST=$(node -e "console.log(JSON.parse(require('fs').readFileSync('$LOGS/vuln_result.json')).host)")
FIXED_HOST=$(node -e "console.log(JSON.parse(require('fs').readFileSync('$LOGS/fixed_result.json')).host)")
VULN_USERINFO=$(node -e "console.log(JSON.parse(require('fs').readFileSync('$LOGS/vuln_result.json')).userinfo)")
FIXED_USERINFO=$(node -e "console.log(JSON.parse(require('fs').readFileSync('$LOGS/fixed_result.json')).userinfo)")

echo ""
echo "=== RESULTS ==="
echo "Vulnerable (3.1.1): host='$VULN_HOST' userinfo='$VULN_USERINFO'"
echo "Fixed      (3.1.2): host='$FIXED_HOST' userinfo='$FIXED_USERINFO'"
echo ""

if [[ "$VULN_HOST" == "attacker.com" && "$VULN_USERINFO" == "allowed.com" ]]; then
  echo "[+] Vulnerable build confirmed: encoded @ decoded and re-emitted raw"
else
  echo "[-] Vulnerable build did NOT show expected host confusion"
  exit 1
fi

if [[ "$FIXED_HOST" == "allowed.com%40attacker.com" && "$FIXED_USERINFO" == "null" ]]; then
  echo "[+] Fixed build confirmed: encoded authority delimiter preserved"
else
  echo "[-] Fixed build did NOT show expected behavior"
  exit 1
fi

echo ""
echo "[+] CVE-2026-6322 REPRODUCED AND VERIFIED"
exit 0
