200 lines
7.9 KiB
Bash
200 lines
7.9 KiB
Bash
#!/usr/bin/env bash
|
|||
|
|
# End-to-end check for the Brave-only bird wrapper.
|
||
|
|
#
|
||
|
|
# The check runs the real wrapper from this repo against a throwaway HOME with:
|
||
|
|
# - a fake Brave cookie database with encrypted x.com auth_token and ct0;
|
||
|
|
# - a fake Brave Safe Storage password served by a `security` spy on PATH;
|
||
|
|
# - fake Safari, Chrome, and Firefox cookie databases with decoy tokens;
|
||
|
|
# - a fake credentials file with a decoy token;
|
||
|
|
# - a fake "real bird" that records argv and the environment it receives.
|
||
|
|
#
|
||
|
|
# It proves the wrapper decrypts Brave cookies with the Brave key, passes the
|
||
|
|
# two cookies to bird as AUTH_TOKEN and CT0, prints no cookie or credential
|
||
|
|
# value, and never opens Safari, Chrome, Firefox, or the credentials file.
|
||
|
|
set -euo pipefail
|
||
|
|
|
||
|
|
ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)
|
||
|
|
WRAPPER="$ROOT/home/.pi/agent/bin/bird"
|
||
|
|
TMP=$(mktemp -d "${TMPDIR:-/tmp}/bird-brave-e2e.XXXXXX")
|
||
|
|
trap 'chmod -R u+rwX "$TMP" 2>/dev/null || true; rm -rf "$TMP"' EXIT
|
||
|
|
|
||
|
|
fail() { printf 'FAIL: %s\n' "$1" >&2; exit 1; }
|
||
|
|
pass() { printf 'PASS: %s\n' "$1"; }
|
||
|
|
|
||
|
|
[ -f "$WRAPPER" ] || fail "wrapper missing at $WRAPPER"
|
||
|
|
[ -x "$WRAPPER" ] || fail "wrapper is not executable at $WRAPPER"
|
||
|
|
|
||
|
|
FIX="$TMP/home"
|
||
|
|
BRAVE_DIR="$FIX/Library/Application Support/BraveSoftware/Brave-Browser/Default"
|
||
|
|
CHROME_DIR="$FIX/Library/Application Support/Google/Chrome/Default"
|
||
|
|
FIREFOX_DIR="$FIX/Library/Application Support/Firefox/Profiles/default"
|
||
|
|
SAFARI_DIR="$FIX/Library/Safari"
|
||
|
|
CRED_FILE="$FIX/.config/bird/credentials"
|
||
|
|
|
||
|
|
mkdir -p "$TMP/bin" "$BRAVE_DIR" "$CHROME_DIR" "$FIREFOX_DIR" "$SAFARI_DIR" \
|
||
|
|
"$(dirname "$CRED_FILE")"
|
||
|
|
|
||
|
|
AUTH_VALUE='BRAVE_AUTH_SENTINEL_7f3a'
|
||
|
|
CT0_VALUE='BRAVE_CT0_SENTINEL_91c2'
|
||
|
|
FAKE_KEY='brave-fixture-password'
|
||
|
|
|
||
|
|
# Build the fake cookie databases. The Brave values use the same v10 scheme as
|
||
|
|
# real Brave: PBKDF2-HMAC-SHA1(saltysalt, 1003) and AES-128-CBC with a space IV.
|
||
|
|
python3 - "$BRAVE_DIR/Cookies" "$CHROME_DIR/Cookies" "$FIREFOX_DIR/cookies.sqlite" \
|
||
|
|
"$SAFARI_DIR/Cookies.binarycookies" "$AUTH_VALUE" "$CT0_VALUE" "$FAKE_KEY" <<'PY'
|
||
|
|
import ctypes
|
||
|
|
import hashlib
|
||
|
|
import sqlite3
|
||
|
|
import sys
|
||
|
|
|
||
|
|
brave_db, chrome_db, firefox_db, safari_db, auth_value, ct0_value, password = sys.argv[1:]
|
||
|
|
lib = ctypes.CDLL("/usr/lib/libSystem.B.dylib")
|
||
|
|
lib.CCCrypt.argtypes = [
|
||
|
|
ctypes.c_uint32, ctypes.c_uint32, ctypes.c_uint32,
|
||
|
|
ctypes.c_char_p, ctypes.c_size_t, ctypes.c_char_p,
|
||
|
|
ctypes.c_char_p, ctypes.c_size_t, ctypes.c_void_p, ctypes.c_size_t,
|
||
|
|
ctypes.POINTER(ctypes.c_size_t),
|
||
|
|
]
|
||
|
|
lib.CCCrypt.restype = ctypes.c_int32
|
||
|
|
key = hashlib.pbkdf2_hmac("sha1", password.encode(), b"saltysalt", 1003, 16)
|
||
|
|
iv = b" " * 16
|
||
|
|
|
||
|
|
|
||
|
|
def encrypted(host_key, value):
|
||
|
|
payload = hashlib.sha256(host_key.encode()).digest() + value.encode()
|
||
|
|
out = ctypes.create_string_buffer(len(payload) + 32)
|
||
|
|
moved = ctypes.c_size_t(0)
|
||
|
|
status = lib.CCCrypt(0, 0, 1, key, len(key), iv, payload, len(payload),
|
||
|
|
out, len(out), ctypes.byref(moved))
|
||
|
|
assert status == 0, status
|
||
|
|
return b"v10" + out.raw[:moved.value]
|
||
|
|
|
||
|
|
|
||
|
|
def make_cookie_db(path, rows, version=24):
|
||
|
|
conn = sqlite3.connect(path)
|
||
|
|
conn.execute("CREATE TABLE meta (key TEXT PRIMARY KEY, value TEXT)")
|
||
|
|
conn.execute("INSERT INTO meta (key, value) VALUES ('version', ?)", (str(version),))
|
||
|
|
conn.execute(
|
||
|
|
"CREATE TABLE cookies (host_key TEXT, name TEXT, value TEXT, encrypted_value BLOB)"
|
||
|
|
)
|
||
|
|
conn.executemany("INSERT INTO cookies VALUES (?, ?, '', ?)", rows)
|
||
|
|
conn.commit()
|
||
|
|
conn.close()
|
||
|
|
|
||
|
|
|
||
|
|
make_cookie_db(brave_db, [
|
||
|
|
("x.com", "auth_token", encrypted("x.com", auth_value)),
|
||
|
|
(".x.com", "ct0", encrypted(".x.com", ct0_value)),
|
||
|
|
("example.com", "auth_token", encrypted("example.com", "OTHER_DOMAIN_DECOY")),
|
||
|
|
])
|
||
|
|
make_cookie_db(chrome_db, [
|
||
|
|
("x.com", "auth_token", b"CHROME_AUTH_DECOY"),
|
||
|
|
("x.com", "ct0", b"CHROME_CT0_DECOY"),
|
||
|
|
])
|
||
|
|
make_cookie_db(firefox_db, [
|
||
|
|
("x.com", "auth_token", b"FIREFOX_AUTH_DECOY"),
|
||
|
|
("x.com", "ct0", b"FIREFOX_CT0_DECOY"),
|
||
|
|
])
|
||
|
|
with open(safari_db, "wb") as handle:
|
||
|
|
handle.write(b"SAFARI_AUTH_DECOY")
|
||
|
|
PY
|
||
|
|
|
||
|
|
printf 'CREDENTIALS_DECOY_NEVER_READ\n' > "$CRED_FILE"
|
||
|
|
|
||
|
|
cat > "$TMP/bin/security" <<'SH'
|
||
|
|
#!/usr/bin/env bash
|
||
|
|
printf '%s\n' "$*" >> "$BIRD_E2E_KEYCHAIN_LOG"
|
||
|
|
case " $* " in
|
||
|
|
*"add-generic-password"*|*"delete-generic-password"*|*"Chrome Safe Storage"*)
|
||
|
|
printf 'unexpected keychain operation\n' >&2
|
||
|
|
exit 9 ;;
|
||
|
|
esac
|
||
|
|
if [ "$1" = "find-generic-password" ] && [ "$3" = "Brave Safe Storage" ] \
|
||
|
|
&& [ "$5" = "Brave" ]; then
|
||
|
|
printf '%s' "$BIRD_E2E_FAKE_KEY"
|
||
|
|
exit 0
|
||
|
|
fi
|
||
|
|
exit 8
|
||
|
|
SH
|
||
|
|
|
||
|
|
cat > "$TMP/bin/fake-bird" <<'SH'
|
||
|
|
#!/usr/bin/env bash
|
||
|
|
{
|
||
|
|
printf 'args:'
|
||
|
|
for arg in "$@"; do printf ' <%s>' "$arg"; done
|
||
|
|
printf '\n'
|
||
|
|
printf 'auth=%s\n' "${AUTH_TOKEN-<unset>}"
|
||
|
|
printf 'ct0=%s\n' "${CT0-<unset>}"
|
||
|
|
} > "$BIRD_E2E_BIRD_LOG"
|
||
|
|
printf 'fake bird ok\n'
|
||
|
|
SH
|
||
|
|
chmod +x "$TMP/bin/security" "$TMP/bin/fake-bird"
|
||
|
|
|
||
|
|
: > "$TMP/keychain.log"
|
||
|
|
: > "$TMP/bird.log"
|
||
|
|
: > "$TMP/stdout"
|
||
|
|
: > "$TMP/stderr"
|
||
|
|
|
||
|
|
before_brave=$(shasum -a 256 "$BRAVE_DIR/Cookies" | awk '{print $1}')
|
||
|
|
before_chrome=$(shasum -a 256 "$CHROME_DIR/Cookies" | awk '{print $1}')
|
||
|
|
before_firefox=$(shasum -a 256 "$FIREFOX_DIR/cookies.sqlite" | awk '{print $1}')
|
||
|
|
before_safari=$(shasum -a 256 "$SAFARI_DIR/Cookies.binarycookies" | awk '{print $1}')
|
||
|
|
before_creds=$(shasum -a 256 "$CRED_FILE" | awk '{print $1}')
|
||
|
|
|
||
|
|
# A read attempt on any of these files must fail. The wrapper only needs Brave.
|
||
|
|
chmod 000 "$CHROME_DIR/Cookies" "$FIREFOX_DIR/cookies.sqlite" \
|
||
|
|
"$SAFARI_DIR/Cookies.binarycookies" "$CRED_FILE"
|
||
|
|
|
||
|
|
set +e
|
||
|
|
env -i \
|
||
|
|
HOME="$FIX" \
|
||
|
|
PATH="$TMP/bin:/opt/homebrew/bin:/usr/bin:/bin" \
|
||
|
|
BIRD_E2E_FAKE_KEY="$FAKE_KEY" \
|
||
|
|
BIRD_E2E_KEYCHAIN_LOG="$TMP/keychain.log" \
|
||
|
|
BIRD_E2E_BIRD_LOG="$TMP/bird.log" \
|
||
|
|
BIRD_BRAVE_REAL_BIRD="$TMP/bin/fake-bird" \
|
||
|
|
"$WRAPPER" read https://x.com/i/status/123 --json \
|
||
|
|
>"$TMP/stdout" 2>"$TMP/stderr"
|
||
|
|
status=$?
|
||
|
|
set -e
|
||
|
|
|
||
|
|
chmod 644 "$CHROME_DIR/Cookies" "$FIREFOX_DIR/cookies.sqlite" \
|
||
|
|
"$SAFARI_DIR/Cookies.binarycookies" "$CRED_FILE"
|
||
|
|
|
||
|
|
[ "$status" -eq 0 ] || fail "wrapper exited $status: $(cat "$TMP/stderr")"
|
||
|
|
grep -q 'fake bird ok' "$TMP/stdout" || fail 'real bird was not executed'
|
||
|
|
grep -Fxq 'args: <read> <https://x.com/i/status/123> <--json>' "$TMP/bird.log" \
|
||
|
|
|| fail "bird argv was not passed through: $(cat "$TMP/bird.log")"
|
||
|
|
grep -Fxq "auth=$AUTH_VALUE" "$TMP/bird.log" \
|
||
|
|
|| fail 'bird did not receive the Brave auth_token'
|
||
|
|
grep -Fxq "ct0=$CT0_VALUE" "$TMP/bird.log" \
|
||
|
|
|| fail 'bird did not receive the Brave ct0'
|
||
|
|
pass 'wrapper decrypts Brave cookies and passes them to bird'
|
||
|
|
|
||
|
|
grep -Fxq 'find-generic-password -s Brave Safe Storage -a Brave -w' "$TMP/keychain.log" \
|
||
|
|
|| fail "unexpected keychain calls: $(cat "$TMP/keychain.log")"
|
||
|
|
[ "$(wc -l < "$TMP/keychain.log" | tr -d ' ')" -eq 1 ] \
|
||
|
|
|| fail 'wrapper made more than one keychain call'
|
||
|
|
pass 'wrapper reads Brave Safe Storage only and writes no keychain item'
|
||
|
|
|
||
|
|
for value in "$AUTH_VALUE" "$CT0_VALUE" 'CREDENTIALS_DECOY_NEVER_READ' \
|
||
|
|
'CHROME_AUTH_DECOY' 'FIREFOX_AUTH_DECOY' 'SAFARI_AUTH_DECOY'; do
|
||
|
|
if grep -Fq "$value" "$TMP/stdout" || grep -Fq "$value" "$TMP/stderr"; then
|
||
|
|
fail "wrapper printed a secret value: $value"
|
||
|
|
fi
|
||
|
|
done
|
||
|
|
pass 'wrapper prints no cookie or credential value'
|
||
|
|
|
||
|
|
after_chrome=$(shasum -a 256 "$CHROME_DIR/Cookies" | awk '{print $1}')
|
||
|
|
after_firefox=$(shasum -a 256 "$FIREFOX_DIR/cookies.sqlite" | awk '{print $1}')
|
||
|
|
after_safari=$(shasum -a 256 "$SAFARI_DIR/Cookies.binarycookies" | awk '{print $1}')
|
||
|
|
after_creds=$(shasum -a 256 "$CRED_FILE" | awk '{print $1}')
|
||
|
|
after_brave=$(shasum -a 256 "$BRAVE_DIR/Cookies" | awk '{print $1}')
|
||
|
|
[ "$before_brave" = "$after_brave" ] || fail 'Brave cookie database changed'
|
||
|
|
[ "$before_chrome" = "$after_chrome" ] || fail 'Chrome cookies were touched'
|
||
|
|
[ "$before_firefox" = "$after_firefox" ] || fail 'Firefox cookies were touched'
|
||
|
|
[ "$before_safari" = "$after_safari" ] || fail 'Safari cookies were touched'
|
||
|
|
[ "$before_creds" = "$after_creds" ] || fail 'credentials file was touched'
|
||
|
|
pass 'wrapper touches Brave only, never Safari, Chrome, Firefox, or credentials'
|
||
|
|
|
||
|
|
printf 'OK: bird brave-only end-to-end check passed\n'
|