Sfoglia il codice sorgente

tools: audit a backup for plaintext credentials, gated behind a positive control

The obvious version of this script is grep -F for a few credential names,
and on these files it returns 0 for EVERYTHING including strings that are
certainly present - custom framing (CALWAL01/CALSNAP1) with embedded NULs.
That makes it a check that cannot fail, reporting a clean bill of health
on a credential audit while being incapable of reporting an exposure.

So every absence is gated behind a positive control on the file's own
magic, falling back to token yield for LMDB; without it the script refuses
to conclude and says no absence means anything. Patterns are derived from
the stores that define the credentials rather than listed, so they cannot
drift (a consumer's suggestion, after finding the same shape in their
bundle audit where one pattern was an accidental control).

Falsified four ways rather than assumed: a file that really leaks a
key-shaped token exits 1, a file strings cannot read exits 3, a
nonexistent path exits 2, and a real pre-2.17 prod backup holding genuine
plaintext keys is correctly reported EXPOSED while the post-2.17.2 rescue
copy reports REVIEW with ciphertext markers.

Found a pipefail trap while testing: strings | grep -q SIGPIPEs strings,
and under pipefail the control reported failure on a file it had just
matched. Noted in the script so it is not reintroduced.
fszontagh 1 mese fa
parent
commit
e5cdb743a2
1 ha cambiato i file con 137 aggiunte e 0 eliminazioni
  1. 137 0
      scripts/audit-backup-credentials.sh

+ 137 - 0
scripts/audit-backup-credentials.sh

@@ -0,0 +1,137 @@
+#!/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