Artifact e9df83619c15c5c07bc2001c4f895bec474a62c8f48f82e7625d0e961873ba40:
- File
test/pic/creds.sh
— part of check-in
[bcb82b941e]
at
2026-11-23 11:55:36
on branch keysource
— Keep the credential store's key in the Keychain or a key file, not behind a passphrase
A new store gets a random 32-byte master key kept in the login Keychain (macOS, /usr/bin/security run as a child with the command on stdin) or the keyring (secret-tool), else ~/.pic/master.key at 0600. The source is recorded in picu_credential_key.source; a passphrase store keeps working as before. pic auth rekey keychain|file|passphrase reseals every sealed row in one transaction, and a typed passphrase is followed once by the offer to stop asking. --passphrase-fd and PIC_PASSPHRASE_COMMAND (setting passphraseCommand) give a passphrase with no terminal. pic remote opens a keychain or file store with no prompt. Every OS service stays in platform.c behind secret_key, secret_keep and secret_drop. (user: dmestas, size: 13828) [annotate] [blame] [check-ins using]
#!/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' # A passphrase store (a new store keeps its key in the Keychain or a key file; # keysource.sh tests those): pic auth rekey passphrase makes one, the new # passphrase typed twice with echo off. Then pic login on a terminal: the key # and the passphrase typed with echo off, so none of them is in the # transcript, and the offer to stop asking for it, declined once for good. drive mkstore auth rekey passphrase <<EOF || fail "mkstore: $(screen "$T/out/mkstore.tx")" expect New passphrase for pic's credential store: send $PASS\r expect Same passphrase again: send $PASS\r expect now comes from a passphrase EOF drive login login fake <<EOF || fail "login: $(screen "$T/out/login.tx")" expect API key for fake: send $SENTINEL\r expect Passphrase for pic's credential store: send $PASS\r expect Stop asking for this passphrase? send n\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' # 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'