#!/bin/bash
# Runner for the StorageAccessAPITabHelper credentialless-renewal POC.
#
# Prerequisite: chrome built at out/asan with chromium-changes.patch applied.
#
# What this does:
#   1. Starts an HTTPS server on :8443.
#   2. Launches Chrome with a.test/b.test resolved to 127.0.0.1.
#   3. The patched browser process seeds a pre-existing 30-day
#      STORAGE_ACCESS(b.test, a.test)=ALLOW grant created 10 days ago (i.e.
#      expiry = now + 20 days). This stands in for "user previously accepted
#      the rSA prompt".
#   4. Loads https://a.test:8443/top.html which contains a *credentialless*
#      iframe to https://b.test:8443/embed.html.
#   5. The patched renderer (simulating a compromised renderer) sends
#      LocalFrameHost.UpdateUserActivationState(kNotifyActivation) over Mojo
#      from inside the credentialless subframe with NO real user gesture.
#   6. The browser process logs the state of the STORAGE_ACCESS grant before
#      and after RenewPermissionGrant, demonstrating that the credentialless
#      / nonce-partitioned frame extended the unpartitioned grant's expiry.

set -e
HERE="$(cd "$(dirname "$0")" && pwd)"
CHROME="${CHROME:-/chromium/chromium/src/out/asan/chrome}"
PROFILE="$(mktemp -d /tmp/chrome-poc-profile.XXXXXX)"

cleanup() {
  [[ -n "$SERVER_PID" ]] && kill "$SERVER_PID" 2>/dev/null || true
  rm -rf "$PROFILE"
}
trap cleanup EXIT

python3 "$HERE/server.py" &
SERVER_PID=$!
sleep 2

URL="${1:-https://a.test:8443/top.html}"

echo "=== Launching Chrome (URL=$URL) ==="
timeout 30 "$CHROME" \
  --no-first-run --no-default-browser-check \
  --no-sandbox \
  --disable-gpu \
  --user-data-dir="$PROFILE" \
  --ignore-certificate-errors \
  --host-resolver-rules='MAP *.test 127.0.0.1' \
  --enable-logging=stderr \
  "$URL" 2>&1 | grep -E 'SAA-POC|FrameReceivedUserActivation' || true

echo "=== Done ==="
