#!/usr/bin/env bash
# Vulnerable/fixed end-to-end validation for CVE-2026-61500
# Author: A. Ramos <aramosf@gmail.com> (@aramosf), 2026-09-26
set -euo pipefail

repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
compose=(docker compose -f "$repo_root/lab/docker-compose.yml")
runtime="$repo_root/lab/runtime"
marker="cve-2026-61500-rce-marker-$$.txt"

cleanup() {
    if [[ "${KEEP_LAB:-0}" != "1" ]]; then
        "${compose[@]}" down --remove-orphans >/dev/null 2>&1 || true
    fi
}
trap cleanup EXIT

prepare_variant() {
    local variant="$1"
    mkdir -p "$runtime/$variant/data" "$runtime/$variant/shares"
    cp "$repo_root/lab/config-$variant.yaml" "$runtime/$variant/data/config.yaml"
    rm -f "$runtime/$variant/data/$marker"
}

wait_http() {
    local port="$1"
    local attempt
    for ((attempt = 0; attempt < 80; attempt++)); do
        if curl -fsS "http://127.0.0.1:$port/" >/dev/null 2>&1; then
            return 0
        fi
        sleep 0.25
    done
    printf 'HFS did not become ready on port %s\n' "$port" >&2
    return 1
}

wait_account() {
    local variant="$1"
    local port="$2"
    local status
    local attempt
    for ((attempt = 0; attempt < 80; attempt++)); do
        if grep -q '^accounts:' "$runtime/$variant/data/config.yaml" 2>/dev/null; then
            status="$(curl -sS -o /dev/null -w '%{http_code}' \
                -H 'Content-Type: application/json' \
                -H 'X-Hfs-Anti-Csrf: 1' \
                --data '{"username":"admin"}' \
                "http://127.0.0.1:$port/~/api/loginSrp1" || true)"
            [[ "$status" == "200" ]] && return 0
        fi
        sleep 0.25
    done
    printf 'HFS admin account did not become ready on port %s\n' "$port" >&2
    return 1
}

python3 -m py_compile "$repo_root/cve-2026-61500-poc.py"
"${compose[@]}" down --remove-orphans >/dev/null 2>&1 || true
mkdir -p "$runtime"
docker run --rm --user 0:0 \
    -v "$runtime:/runtime" \
    --entrypoint /bin/sh \
    rejetto/hfs@sha256:d6765e93b68de222583be7788afad699695fd08aa2f56377337f5139779e0746 \
    -c 'rm -rf -- /runtime/vulnerable /runtime/fixed'
prepare_variant vulnerable
prepare_variant fixed
"${compose[@]}" up -d vulnerable fixed
wait_http 28182
wait_http 28183
wait_account vulnerable 28182
wait_account fixed 28183

printf 'vulnerable_server_header=%s\n' "$(curl -fsSI http://127.0.0.1:28182/ | awk -F': ' 'tolower($1)=="server" {sub("\\r$", "", $2); print $2}')"
python3 "$repo_root/cve-2026-61500-poc.py" \
    --target http://127.0.0.1:28182 --marker "$marker"
test "$(cat "$runtime/vulnerable/data/$marker")" = CVE_2026_61500_RCE_CONFIRMED
printf '%s\n' 'vulnerable_result=PASS forged admin session and executed benign server_code marker'

set +e
fixed_output="$(python3 "$repo_root/cve-2026-61500-poc.py" \
    --target http://127.0.0.1:28183 --marker "$marker" 2>&1)"
fixed_status=$?
set -e
printf '%s\n' "$fixed_output"
test "$fixed_status" -eq 3
grep -q '^not_vulnerable=' <<<"$fixed_output"
test ! -e "$runtime/fixed/data/$marker"
printf 'fixed_server_header=%s\n' "$(curl -fsSI http://127.0.0.1:28183/ | awk -F': ' 'tolower($1)=="server" {sub("\\r$", "", $2); print $2}')"
printf '%s\n' 'fixed_result=PASS opaque UUID login identifier stopped exploitation'
