: asapien/signet

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
#!/bin/sh
# Development only: existing native Chrome, no browser-automation dependency.
set -eu
[ "${SIGNET_BETA_BROWSER_DISPOSABLE:-}" = yes ] || exit 2
B=${SIGNET_URL:-http://127.0.0.1:8080}
case "$B" in
    http://127.0.0.1:*) port=${B#http://127.0.0.1:} ;;
    http://localhost:*) port=${B#http://localhost:} ;;
    *) echo 'Use an explicitly disposable loopback preview' >&2; exit 2 ;;
esac
case "$port" in ''|*[!0-9]*) exit 2 ;; esac
[ "$port" -ge 1 ] && [ "$port" -le 65535 ] || exit 2
CHROME=${CHROME_PATH:-/Applications/Google Chrome.app/Contents/MacOS/Google Chrome}
[ -x "$CHROME" ] || { echo 'SKIP - native Chrome unavailable; do not install automatically'; exit 77; }
T=$(mktemp -d /tmp/signet-beta-browser.XXXXXX)
child=
timer=
cleanup() {
    if [ -n "$timer" ]; then kill "$timer" 2>/dev/null || true; wait "$timer" 2>/dev/null || true; fi
    if [ -n "$child" ]; then kill "$child" 2>/dev/null || true; wait "$child" 2>/dev/null || true; fi
    rm -rf "$T"
}
trap cleanup 0
trap 'exit 1' HUP INT TERM
code=$(curl --max-time 10 -sS -o "$T/unauthorized" -w '%{http_code}' "$B/api/me")
[ "$code" = 401 ] || { echo 'FAIL - unauthenticated API did not fail closed' >&2; exit 1; }
"$CHROME" --headless --disable-gpu --no-first-run --no-default-browser-check \
    --no-proxy-server --user-data-dir="$T/profile" --timeout=10000 \
    --virtual-time-budget=5000 --dump-dom "$B/" > "$T/dom.html" 2> "$T/chrome.log" &
child=$!
# Chrome's own timeout does not interrupt a JavaScript modal dialog.
(
    trap - 0
    sleeper=
    cancel() {
        if [ -n "$sleeper" ]; then kill "$sleeper" 2>/dev/null || true; wait "$sleeper" 2>/dev/null || true; fi
        exit 0
    }
    trap cancel HUP INT TERM
    sleep 15 & sleeper=$!
    wait "$sleeper"
    : > "$T/timed-out"
    echo 'FAIL - browser bootstrap exceeded15s (possibly a modal dialog)' >&2
    kill "$child" 2>/dev/null || true
) &
timer=$!
if wait "$child"; then child=; else child=; exit 1; fi
kill "$timer" 2>/dev/null || true
wait "$timer" 2>/dev/null || true
timer=
[ ! -f "$T/timed-out" ] || exit 1
grep -F 'aria-label="Beta release">BETA</span>' "$T/dom.html" >/dev/null
grep -F '<h1>Sign-in required</h1>' "$T/dom.html" >/dev/null
grep -F 'Your account and permissions come from that service' "$T/dom.html" >/dev/null
echo 'beta browser: real DOM shows BETA and a prompt-free sign-in state; API remains401 without identity'