| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- #!/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. They are not
- # compressed; gzip still halves them. So the text is right there and grep
- # will not surface it.
- #
- # 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 <backup-file-or-directory>..."
- # --- 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
|