#!/usr/bin/env bash # Audit a database backup (snapshot, WAL segment, or LMDB file) for # PLAINTEXT credentials. # # WHY THIS EXISTS, AND IT IS NOT THE OBVIOUS SCRIPT. The obvious version is # `grep -F` for a few credential names, and on these files it returns 0 for # EVERYTHING - including strings that are certainly present. Measured # 2026-10-19: `grep -c -F novitax` answered 0 on a WAL segment where # `strings -a | grep -c -F novitax` answered 5957, because the files carry # custom framing (CALWAL01 / CALSNAP1) with embedded NULs, so grep will not # surface text past them. # # WHY A CLEAN RESULT ON A *SNAPSHOT* IS WEAKER THAN THIS SCRIPT ORIGINALLY # CLAIMED. The header used to say the files "are not compressed; gzip still # halves them". That inference was wrong: the database owner confirmed # 2026-10-22 that the snapshot writer compresses with LZ4 whenever # `compression` is on, which is the DEFAULT, and gzip shrinking an LZ4 file # further is expected rather than evidence of anything. `strings -a` still # surfaces plaintext there, but for a different reason - LZ4 output is mostly # literal runs, so plaintext survives in FRAGMENTS. # # The consequence is an ASYMMETRY, and only one direction counts on a # snapshot: a HIT is real evidence of exposure, a MISS is NOT evidence of # safety. A long token - an API key, a 28-character document id - can be # split across an LZ4 match reference, so it is never shown whole and the # absence means "not found in the literal runs" rather than "not there". # So read a clean SNAPSHOT verdict as "nothing surfaced", never as "nothing # is there", and prefer a WAL segment or an LMDB file when an absence has to # carry weight - those are not written through the compressing path. # # The positive control below is still load-bearing: without it the check # cannot report at all. It simply does not upgrade a snapshot miss into an # absence, which is why the verdicts are PRESENT / REVIEW / EXPOSED rather # than a boolean. # # That makes the obvious script a check that CANNOT FAIL: it reports a clean # bill of health on a credential audit while being incapable of reporting an # exposure. A clean report is the most dangerous possible output for this # check, so every absence here is gated behind a POSITIVE CONTROL: unless a # string known to be in the file is found, the script refuses to draw any # conclusion and says so. # # The patterns are DERIVED FROM THE SOURCE that defines the credentials # rather than listed here, so the list cannot drift from the thing it checks. # (Suggested by a consumer who found the same shape in their own bundle # audit, where one pattern happened to be present and was an ACCIDENTAL # positive control - accidental being the problem.) # # Never prints a credential value. Counts and masked shapes only. set -uo pipefail # NOTE: never `strings ... | grep -q` in this script. `grep -q` exits on the # FIRST match, which SIGPIPEs `strings`, and under `pipefail` that makes the # whole pipeline non-zero - so a control that MATCHED reports as failed. That # bug made every real file report CANNOT DETERMINE on the first run of this # script. Use `grep -c` (which consumes all input) and compare the count. SRC_DIR="${SRC_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/src}" die() { printf 'audit: %s\n' "$*" >&2; exit 2; } [ $# -ge 1 ] || die "usage: $0 ..." # --- derive the patterns from the stores that define the credentials -------- derive_patterns() { # Credential-bearing collection names, from the collection constants. grep -rhno 'vectorapi_\(keys\|settings\|secrets\)' "$SRC_DIR" 2>/dev/null \ | cut -d: -f2- | sort -u # Credential VALUE field names, from the stores' own serialisation. grep -rhno '"\(openai_api_key\|api_key\|apiKey\|password\|secret\|token\|credentials\)"' \ "$SRC_DIR" 2>/dev/null | cut -d: -f2- | tr -d '"' | sort -u } mapfile -t PATTERNS < <(derive_patterns) [ "${#PATTERNS[@]}" -gt 0 ] || die "derived NO patterns from $SRC_DIR - refusing to report anything" rc=0 printf 'audit: %d pattern(s) derived from %s\n\n' "${#PATTERNS[@]}" "$SRC_DIR" audit_one() { local f="$1" printf '=== %s ===\n' "$f" # One `strings` pass per file, reused for every pattern. These files run to # hundreds of MB and re-reading per pattern made the audit minutes long. local tok tok=$(mktemp) || die "mktemp failed" strings -a "$f" 2>/dev/null > "$tok" # --- the positive control, before any absence is believed --------------- # The file's own magic is a string we KNOW is at offset 0. If `strings` # cannot surface that, it cannot surface anything, and no zero below means # a thing. local magic magic=$(head -c 8 "$f" 2>/dev/null | tr -dc 'A-Za-z0-9') if [ "${#magic}" -lt 4 ]; then printf ' CONTROL: INCONCLUSIVE - no printable magic; falling back to token yield\n' local tokens tokens=$(head -2000 "$tok" | wc -l) if [ "$tokens" -lt 10 ]; then printf ' RESULT: CANNOT DETERMINE - only %d printable token(s). NO ABSENCE BELOW MEANS ANYTHING.\n\n' "$tokens" rc=3; rm -f "$tok"; return fi printf ' CONTROL: PASS via token yield (%d tokens)\n' "$tokens" elif [ "$(grep -c -F "$magic" "$tok")" -eq 0 ]; then printf ' RESULT: CANNOT DETERMINE - the control string %s is in the file but not findable.\n' "$magic" printf ' NO ABSENCE BELOW MEANS ANYTHING. Do not read this as clean.\n\n' rc=3; rm -f "$tok"; return else printf ' CONTROL: PASS - %s findable, so this file IS searchable\n' "$magic" fi # --- positive evidence of encryption ------------------------------------ local enc encj enc=$(grep -c -F '$ENC$' "$tok") encj=$(grep -c -F '$ENCJ$' "$tok") printf ' ciphertext markers: $ENC$=%s $ENCJ$=%s\n' "$enc" "$encj" # --- the actual question ------------------------------------------------- local found=0 p n for p in "${PATTERNS[@]}"; do n=$(grep -c -F "$p" "$tok") [ "$n" -gt 0 ] && { printf ' PRESENT: %-22s %s hit(s)\n' "$p" "$n"; found=1; } done # A key-SHAPED token is the thing that matters, not a field name: a field # name can appear in tenant content. Anchored at a token start, because # `sk-` occurs mid-blob in ciphertext by chance (seen 2026-10-19). local shaped shaped=$(grep -c -E '(^|[^A-Za-z0-9_-])sk-[A-Za-z0-9_-]{20,}' "$tok") if [ "$shaped" -gt 0 ]; then printf ' *** EXPOSED: %s key-shaped token(s) (sk- at a token start, 20+ chars)\n' "$shaped" rc=1 elif [ "$found" -eq 0 ]; then printf ' RESULT: CLEAN - control passed and no credential pattern present\n' else printf ' RESULT: REVIEW - credential field/collection names present but no key-shaped token\n' [ "$rc" -eq 0 ] && rc=4 fi rm -f "$tok" printf '\n' } for target in "$@"; do [ -e "$target" ] || die "no such path: $target (refusing to report clean for a path that does not exist)" if [ -d "$target" ]; then while IFS= read -r -d '' f; do audit_one "$f"; done \ < <(find "$target" -type f ! -name 'SHA256SUMS' ! -name '*.md' ! -name '*.txt' -print0) else audit_one "$target" fi done case $rc in 0) printf 'audit: CLEAN (every file had a passing control)\n' ;; 1) printf 'audit: EXPOSED - plaintext key-shaped material found\n' ;; 3) printf 'audit: INCONCLUSIVE for at least one file - absences there mean nothing\n' ;; 4) printf 'audit: REVIEW needed\n' ;; esac exit $rc