#!/bin/sh
# The credential store end to end against a fake server, with a private HOME:
# pic login seals a sentinel key (typed on a terminal with echo off, or piped
# on stdin), a request sends exactly that key, the terminal interface asks for
# the passphrase once and hands the key to its request child, a wrong
# passphrase fails cleanly, /login in the interface serves its requests, a
# stored key beats the environment variable (Pi's order: stored, models.json,
# environment) and auth status names the source in use, pic keys
# shows names only, logout removes the key, and the sentinel's bytes appear
# nowhere else: not in ~/.pic/pic.sqlite (or its WAL), the journal, any
# output, a terminal transcript, or pic export. --no-faults (the release
# build): the cases that feed the passphrase through PIC_TEST_PASSPHRASE,
# which only the dev build reads, are skipped; the terminal cases run.
set -eu
PIC=${PIC:?set PIC to the pic binary}
SERVER=${SERVER:?set SERVER to the fake_server binary}
DRIVE=${DRIVE:?set DRIVE to the pty_drive binary}
FAULTS=yes
[ "${1:-}" = --no-faults ] && FAULTS=no
S=$(cd "$(dirname "$0")/streams" && pwd)
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-creds.XXXXXX")
T=$(cd "$T" && pwd -P)
spid=
cleanup() { [ -n "$spid" ] && kill "$spid" 2>/dev/null || :; rm -rf -- "$T"; }
trap cleanup EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
mkdir "$T/home" "$T/ws" "$T/log" "$T/out"
unset FAKE_API_KEY PIC_TEST_PASSPHRASE ANTHROPIC_API_KEY
export HOME="$T/home" PIC_MODEL_CATALOG=offline PIC_PI_EXPORT=0 PIC_MODELS_FILE="$T/models.json" T
export PIC_NO_NATIVE_CLIPBOARD=1
SENTINEL="pic-creds-sentinel-$$-5e1f0c"
PASS="pic-creds-passphrase-$$"
OTHER="pic-creds-env-key-$$"
"$SERVER" "$T/port" "$T/log" 120 "sse:$S/text.sse" &
spid=$!
i=0
while [ ! -s "$T/port" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server did not start"; sleep 0.05; done
printf '{"providers":{"fake":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$FAKE_API_KEY","models":[{"id":"m","contextWindow":200000}]}}}' \
"$(cat "$T/port")" > "$T/models.json"
# (A variable set in front of a shell function stays set after it in some
# shells, so those cases set theirs in a subshell.)
# Without a terminal of its own (its own session): a stray passphrase prompt
# fails at once instead of waiting on whatever terminal runs the suite.
notty() { perl -MPOSIX=setsid -e 'setsid() or die "setsid"; exec @ARGV or die "exec"' "$@"; }
n=0
pic() { # NAME ARGS...: pic with no terminal; stdout and stderr kept in $T/out/NAME.{out,err}
name=$1
shift
notty "$PIC" --db "$T/j" --workspace "$T/ws" --model fake/m "$@" > "$T/out/$name.out" 2> "$T/out/$name.err"
}
drive() { # NAME ARGS...: pic on a pseudo-terminal, the script on stdin; the transcript in $T/out/NAME.tx
name=$1
shift
"$DRIVE" "$T/out/$name.tx" 80 24 "$PIC" --db "$T/j" --workspace "$T/ws" --model fake/m "$@"
}
screen() { perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$1" | grep -v '^\s*$' | tail -8; }
requests() { ls "$T/log" | wc -l | tr -d ' '; }
sent_key() { sed -n 's/^x-api-key: //p' "$T/log/$1.req" | tr -d '\r'; }
# Nothing stored: pic keys says so, and a request names pic login.
pic keys0 keys || fail "keys: $(cat "$T/out/keys0.err")"
grep -q 'No stored keys' "$T/out/keys0.out" || fail "keys: $(cat "$T/out/keys0.out")"
pic nokey -p hello < /dev/null && fail "nokey: a request ran without a key"
grep -q 'FAKE_API_KEY is not set and no key is stored for fake: run `pic login fake`' "$T/out/nokey.err" ||
fail "nokey: $(cat "$T/out/nokey.err")"
[ "$(requests)" = 0 ] || fail "nokey: a request reached the server"
printf 'PASS: with nothing stored, a request names pic login\n'
# pic login on a terminal: the key and the new passphrase (twice) are typed
# with echo off, so none of them is in the transcript.
drive login login fake <<EOF || fail "login: $(screen "$T/out/login.tx")"
expect API key for fake:
send $SENTINEL\r
expect New passphrase for pic's credential store:
send $PASS\r
expect Same passphrase again:
send $PASS\r
expect Saved the API key for fake
EOF
printf 'PASS: pic login on a terminal, key and passphrase typed with echo off\n'
# Ctrl-C at an echo-off prompt ends pic with the terminal's echo back on (the
# shell around it ignores SIGINT so it can report the terminal afterwards).
export PIC
"$DRIVE" "$T/out/ctrlc.tx" 80 24 sh -c 'trap "" INT; "$PIC" login fake; if stty -a | grep -q -- "-echo "; then echo ECHO-IS-OFF; else echo ECHO-IS-ON; fi' <<EOF ||
expect API key for fake:
send \x03
expect ECHO-IS-
EOF
fail "ctrl-c: $(screen "$T/out/ctrlc.tx")"
grep -q ECHO-IS-ON "$T/out/ctrlc.tx" || fail "ctrl-c: echo was left off"
printf 'PASS: Ctrl-C at a prompt leaves the terminal echoing\n'
# The terminal interface: the first message asks for the passphrase (on the
# terminal, before the request child starts); the child has no terminal and
# no passphrase in its environment, so the key reaches the server only by
# the inherited pipe.
drive tui --tui-mode regular <<EOF || fail "tui: $(screen "$T/out/tui.tx")"
expect pic fake/m
send hello\r
expect your message is sent once
expect Passphrase for pic's credential store:
send not-$PASS\r
expect wrong passphrase for the credential store
send $PASS\r
expect Hello, "world"
send \x04\x04
expect pic: conversation
EOF
[ "$(requests)" = 1 ] || fail "tui: $(requests) requests"
! grep -a -q "The API key for fake is in pic's credential store" "$T/out/tui.tx" || fail "tui: a prompt on the plain terminal"
! grep -a -q -- "$PASS" "$T/out/tui.tx" || fail "tui: the passphrase reached the screen"
[ "$(sent_key 1)" = "$SENTINEL" ] || fail "tui: the server got another key"
printf 'PASS: the terminal interface asks once and hands the key to its request child\n'
# A wrong passphrase fails cleanly: no request, an error naming it.
drive wrong -p again <<EOF && fail "wrong: the request ran"
expect Passphrase for pic's credential store:
send not-$PASS\r
expect wrong passphrase for the credential store
EOF
grep -q 'wrong passphrase for the credential store' "$T/out/wrong.tx" || fail "wrong: $(screen "$T/out/wrong.tx")"
[ "$(requests)" = 1 ] || fail "wrong: a request reached the server"
printf 'PASS: a wrong passphrase fails cleanly\n'
# A stored key wins over the environment variable, as in Pi, and auth status
# says which is in use (it opens nothing, so it asks for no passphrase).
(FAKE_API_KEY="$OTHER" && export FAKE_API_KEY && drive envwin -p again) <<EOF || fail "envwin: $(screen "$T/out/envwin.tx")"
expect Passphrase for pic's credential store:
send $PASS\r
expect Hello, "world"
EOF
[ "$(requests)" = 2 ] || fail "envwin: $(requests) requests"
[ "$(sent_key 2)" = "$SENTINEL" ] || fail "envwin: the variable beat the stored key"
! grep -aqF -- "$OTHER" "$T/out/envwin.tx" || fail "envwin: the variable's value reached the screen"
(FAKE_API_KEY="$OTHER" && export FAKE_API_KEY && pic status1 auth status < /dev/null) || fail "status1: $(cat "$T/out/status1.err")"
grep -q '^fake *default\* *API key' "$T/out/status1.out" || fail "status1: $(cat "$T/out/status1.out")"
grep -q '^fake *env *FAKE_API_KEY *not used: a stored key wins' "$T/out/status1.out" || fail "status1: $(cat "$T/out/status1.out")"
! grep -qF -- "$OTHER" "$T/out/status1.out" || fail "status1: printed a key"
! grep -qF -- "$SENTINEL" "$T/out/status1.out" || fail "status1: printed a key"
pic status2 auth status < /dev/null || fail "status2: $(cat "$T/out/status2.err")"
grep -q '^fake *default\* *API key' "$T/out/status2.out" || fail "status2: $(cat "$T/out/status2.out")"
! grep -q 'FAKE_API_KEY' "$T/out/status2.out" || fail "status2: $(cat "$T/out/status2.out")"
printf 'PASS: a stored key beats the environment variable, and auth status names the source\n'
if [ "$FAULTS" = yes ]; then
# Piped: the key on stdin, the passphrase from the dev build's test hook.
printf '%s\n' "$SENTINEL" | PIC_TEST_PASSPHRASE="$PASS" notty "$PIC" login fake > "$T/out/pipe.out" 2> "$T/out/pipe.err" ||
fail "pipe login: $(cat "$T/out/pipe.err")"
(PIC_TEST_PASSPHRASE="$PASS" && export PIC_TEST_PASSPHRASE && pic print -p 'once more' < /dev/null) || fail "print: $(cat "$T/out/print.err")"
[ "$(requests)" = 3 ] || fail "print: $(requests) requests"
[ "$(sent_key 3)" = "$SENTINEL" ] || fail "print: the server got another key"
(PIC_TEST_PASSPHRASE="wrong-$PASS" && export PIC_TEST_PASSPHRASE && pic wrong2 -p 'no' < /dev/null) && fail "wrong2: the request ran"
grep -q 'wrong passphrase for the credential store' "$T/out/wrong2.err" || fail "wrong2: $(cat "$T/out/wrong2.err")"
[ "$(requests)" = 3 ] || fail "wrong2: a request reached the server"
printf 'PASS: a piped key, a request with it, a wrong passphrase (test hook)\n'
# A stored key also beats a literal apiKey in the models file.
sed 's/"apiKey":"\$FAKE_API_KEY"/"apiKey":"literal-'"$$"'"/' "$T/models.json" > "$T/literal.json"
grep -q "literal-$$" "$T/literal.json" || fail "literal: the models file was not rewritten"
(PIC_TEST_PASSPHRASE="$PASS" && PIC_MODELS_FILE="$T/literal.json" && export PIC_TEST_PASSPHRASE PIC_MODELS_FILE &&
pic lit1 -p 'literal' < /dev/null) || fail "lit1: $(cat "$T/out/lit1.err")"
[ "$(requests)" = 4 ] || fail "lit1: $(requests) requests"
[ "$(sent_key 4)" = "$SENTINEL" ] || fail "lit1: the models file's literal beat the stored key"
printf 'PASS: a stored key beats the models file apiKey\n'
fi
# /login in the terminal interface: the provider picker (the model's provider
# chosen), then the key and the passphrase typed in place, in its own process,
# so the message after it asks for nothing.
before=$(requests)
drive tuilogin --tui-mode regular <<EOF || fail "tui /login: $(screen "$T/out/tuilogin.tx")"
expect pic fake/m
send /login\r
expect Log in
send \r
expect API key for fake:
send $SENTINEL\r
expect Passphrase for pic's credential store:
send $PASS\r
expect Saved the API key for fake
send hi\r
expect Hello, "world"
send \x04\x04
expect pic: conversation
EOF
perl -0ne 'exit(/Saved the API key for fake.*Passphrase for/s ? 1 : 0)' "$T/out/tuilogin.tx" || fail "tui /login: asked twice"
[ "$(requests)" = $((before + 1)) ] || fail "tui /login: $(requests) requests"
[ "$(sent_key "$(requests)")" = "$SENTINEL" ] || fail "tui /login: the server got another key"
printf 'PASS: /login in the terminal interface, then a request without a second prompt\n'
# No terminal and a stored key: one sentence naming the ways out, printed once, and no
# conversation journaled for a request never sent. Following the sentence works.
before=$(requests)
notty "$PIC" --db "$T/jlocked" --workspace "$T/ws" --model fake/m -p hello < /dev/null > "$T/out/locked.out" 2> "$T/out/locked.err" &&
fail "locked: the request ran"
[ "$(cat "$T/out/locked.err")" = 'pic: the key for fake is sealed in the credential store and there is no terminal to ask for its passphrase: export FAKE_API_KEY and run `pic auth use fake env`, or see `pic help credentials`' ] ||
fail "locked: $(cat "$T/out/locked.err")"
[ -z "$(notty "$PIC" --db "$T/jlocked" --workspace "$T/ws" sessions < /dev/null)" ] || fail "locked: a session was journaled"
[ "$(requests)" = "$before" ] || fail "locked: a request reached the server"
(FAKE_API_KEY="$OTHER" && export FAKE_API_KEY && pic useenv auth use fake env < /dev/null) || fail "useenv: $(cat "$T/out/useenv.err")"
(FAKE_API_KEY="$OTHER" && export FAKE_API_KEY && pic envnow -p 'from the variable' < /dev/null) || fail "envnow: $(cat "$T/out/envnow.err")"
[ "$(sent_key "$(requests)")" = "$OTHER" ] || fail "envnow: the variable was not sent"
pic usedefault auth use fake default < /dev/null || fail "usedefault: $(cat "$T/out/usedefault.err")"
printf 'PASS: with no terminal, a sealed key fails at once with the ways out, which work\n'
# RPC never asks on the terminal, even with one: credential_locked and a failed prompt, at once;
# unlock takes the passphrase over the protocol (the transcript holds it, so it is removed).
mkdir -p "$T/tx"
"$DRIVE" "$T/tx/rpc.tx" 200 24 "$PIC" --db "$T/j" --workspace "$T/ws" --model fake/m --mode rpc <<EOF || fail "rpc: $(screen "$T/tx/rpc.tx")"
send {"id":"p1","type":"prompt","message":"hi"}\r
expect {"type":"credential_locked","provider":"fake"}
expect {"id":"p1","type":"response","command":"prompt","success":false
send {"id":"u1","type":"unlock","passphrase":"not-$PASS"}\r
expect wrong passphrase for the credential store
send {"id":"u2","type":"unlock","passphrase":"$PASS"}\r
expect {"id":"u2","type":"response","command":"unlock","success":true}
send {"id":"p2","type":"prompt","message":"hi"}\r
expect {"type":"agent_settled","aborted":false}
send \x04
EOF
! grep -aq "Passphrase for" "$T/tx/rpc.tx" || fail "rpc: asked on the terminal"
grep -aq 'Hello, \\"world\\"' "$T/tx/rpc.tx" || fail "rpc: no reply after unlock"
[ "$(sent_key "$(requests)")" = "$SENTINEL" ] || fail "rpc: the server got another key"
rm -rf "$T/tx"
printf 'PASS: RPC reports a locked store instead of asking, and unlock opens it\n'
# pic attach -i: a line typed while nothing runs starts a request; the passphrase is asked
# once on attach's terminal and the key handed to the request.
before=$(requests)
drive attach attach -i -c --format text <<EOF || fail "attach: $(screen "$T/out/attach.tx")"
send once more\r
expect Passphrase for pic's credential store:
send $PASS\r
expect Hello, "world"
send again\r
expect (starting a request)
expect Hello, "world"
send \x04
EOF
[ "$(requests)" = $((before + 2)) ] || fail "attach: $(requests) requests"
[ "$(grep -ac "Passphrase for" "$T/out/attach.tx")" = 1 ] || fail "attach: asked more than once"
[ "$(sent_key "$(requests)")" = "$SENTINEL" ] || fail "attach: the server got another key"
printf 'PASS: attach -i asks once on its terminal and hands the key to its requests\n'
# pic keys: the name and dates, never the value.
pic keys1 keys || fail "keys1: $(cat "$T/out/keys1.err")"
grep -q '^fake *added [0-9-]* [0-9:]* last used ' "$T/out/keys1.out" || fail "keys1: $(cat "$T/out/keys1.out")"
printf 'PASS: pic keys lists names and dates\n'
# The sentinel was sent (the test is live) and is nowhere but the server's
# log: everything that could hold a copy, scanned while the row is still
# there and again after logout removed it (freed pages keep old bytes).
grep -rqF -- "$SENTINEL" "$T/log" || fail "leak: the sentinel never reached the server (test is broken)"
scan() { # WHEN
pic "export-$1" export < /dev/null || fail "export: $(cat "$T/out/export-$1.err")"
pic "exportpi-$1" export --pi - < /dev/null || fail "export --pi: $(cat "$T/out/exportpi-$1.err")"
pic "dump-$1" dump < /dev/null || fail "dump: $(cat "$T/out/dump-$1.err")"
[ -s "$T/out/export-$1.out" ] || fail "export: empty"
[ -f "$HOME/.pic/pic.sqlite" ] || fail "leak check: no pic.sqlite"
find "$HOME" "$T"/j* "$T/ws" "$T/out" -type f > "$T/files"
while IFS= read -r f; do
if grep -aqF -- "$SENTINEL" "$f"; then fail "leak ($1): the key is in $f"; fi
if grep -aqF -- "$PASS" "$f"; then fail "leak ($1): the passphrase is in $f"; fi
done < "$T/files"
}
scan stored
printf 'PASS: with the key stored, it appears nowhere but the request\n'
pic logout logout fake || fail "logout: $(cat "$T/out/logout.err")"
grep -q 'Removed the stored API key for fake' "$T/out/logout.out" || fail "logout: $(cat "$T/out/logout.out")"
pic keys2 keys || fail "keys2"
grep -q 'No stored keys' "$T/out/keys2.out" || fail "keys2: $(cat "$T/out/keys2.out")"
pic gone -p 'after logout' < /dev/null && fail "gone: a request ran after logout"
grep -q 'run `pic login fake`' "$T/out/gone.err" || fail "gone: $(cat "$T/out/gone.err")"
printf 'PASS: logout removes the key\n'
# Nothing stored: the models file's literal is used, else the variable.
sed 's/"apiKey":"\$FAKE_API_KEY"/"apiKey":"literal-'"$$"'"/' "$T/models.json" > "$T/literal.json"
before=$(requests)
(PIC_MODELS_FILE="$T/literal.json" && export PIC_MODELS_FILE && pic lit2 -p 'literal' < /dev/null) || fail "lit2: $(cat "$T/out/lit2.err")"
[ "$(sent_key $((before + 1)))" = "literal-$$" ] || fail "lit2: the models file's literal was not sent"
(FAKE_API_KEY="$OTHER" && export FAKE_API_KEY && pic env2 -p 'variable' < /dev/null) || fail "env2: $(cat "$T/out/env2.err")"
[ "$(sent_key $((before + 2)))" = "$OTHER" ] || fail "env2: the variable was not sent"
(FAKE_API_KEY="$OTHER" && export FAKE_API_KEY && pic status3 auth status < /dev/null) || fail "status3"
grep -q '^fake *env\* *FAKE_API_KEY' "$T/out/status3.out" || fail "status3: $(cat "$T/out/status3.out")"
printf 'PASS: with nothing stored, the models file apiKey, then the variable\n'
scan removed
printf 'PASS: after logout too, the key appears nowhere but the request\n'
# pic auth reset: the escape from a forgotten passphrase. With no terminal it needs --yes;
# on a terminal it asks first. It forgets the master key and every sealed row (keys,
# sign-ins, remote control's keys and phones), keeps settings and trust, and the next login
# makes a new store.
drive relogin login fake <<EOT || fail "relogin: $(screen "$T/out/relogin.tx")"
expect API key for fake:
send $SENTINEL\r
expect Passphrase for pic's credential store:
send $PASS\r
expect Saved the API key for fake
EOT
pic trust trust yes < /dev/null || fail "trust: $(cat "$T/out/trust.err")"
(FAKE_API_KEY="$OTHER" && export FAKE_API_KEY && pic useenv2 auth use fake env < /dev/null) || fail "useenv2: $(cat "$T/out/useenv2.err")"
pic reset0 auth reset < /dev/null && fail "reset0: reset without a terminal or --yes"
grep -q -- '--yes' "$T/out/reset0.err" || fail "reset0: $(cat "$T/out/reset0.err")"
pic keys3 keys < /dev/null
grep -q '^fake ' "$T/out/keys3.out" || fail "reset0: removed something: $(cat "$T/out/keys3.out")"
drive resetno auth reset <<EOT || fail "resetno: $(screen "$T/out/resetno.tx")"
expect [y/N]
send n\r
expect Nothing was changed
EOT
pic keys4 keys < /dev/null
grep -q '^fake ' "$T/out/keys4.out" || fail "resetno: removed something: $(cat "$T/out/keys4.out")"
drive reset auth reset <<EOT || fail "reset: $(screen "$T/out/reset.tx")"
expect [y/N]
send y\r
expect Forgot the credential store
EOT
pic keys5 keys < /dev/null
grep -q 'No stored keys' "$T/out/keys5.out" || fail "reset: $(cat "$T/out/keys5.out")"
pic trust2 trust < /dev/null
[ "$(cat "$T/out/trust2.out")" = trusted ] || fail "reset: trust lost: $(cat "$T/out/trust2.out")"
(FAKE_API_KEY="$OTHER" && export FAKE_API_KEY && pic status4 auth status < /dev/null) || fail "status4"
grep -q '^fake *env\* *FAKE_API_KEY' "$T/out/status4.out" || fail "reset: the key choice was lost: $(cat "$T/out/status4.out")"
drive fresh login fake <<EOT || fail "fresh: $(screen "$T/out/fresh.tx")"
expect API key for fake:
send $SENTINEL\r
expect New passphrase for pic's credential store:
send new-$PASS\r
expect Same passphrase again:
send new-$PASS\r
expect Saved the API key for fake
EOT
pic reset2 auth reset --yes < /dev/null || fail "reset2: $(cat "$T/out/reset2.err")"
pic keys6 keys < /dev/null
grep -q 'No stored keys' "$T/out/keys6.out" || fail "reset2: $(cat "$T/out/keys6.out")"
printf 'PASS: auth reset asks, forgets the store and its sealed rows, keeps trust, and login starts afresh\n'