#!/usr/bin/env bash # Scan files (or the staged set) for high-signal secret material. # # This is a safety net, not a guarantee. It deliberately matches only # high-confidence patterns so it does not false-positive on sha256 digests in # the OpenCode manifest or on documentation that merely mentions secrets. # # Usage: scripts/check-secrets.sh [FILE...] # With no arguments, scans files staged for commit. set -uo pipefail if [[ $# -gt 0 ]]; then files=("$@") else mapfile -t files < <(git diff --cached --name-only --diff-filter=ACM) fi patterns=( '-----BEGIN [A-Z ]*PRIVATE KEY-----' 'AKIA[0-9A-Z]{16}' 'ASIA[0-9A-Z]{16}' 'sk-[A-Za-z0-9]{20,}' 'ghp_[A-Za-z0-9]{36}' 'github_pat_[A-Za-z0-9_]{20,}' 'xox[baprs]-[A-Za-z0-9-]{10,}' 'AIza[0-9A-Za-z_-]{35}' ) found=0 for file in "${files[@]}"; do [[ -n "$file" ]] || continue [[ -f "$file" ]] || continue case "$file" in *.iso|*.qcow2|*.img|*.raw) continue ;; esac for pattern in "${patterns[@]}"; do if grep -EqI -- "$pattern" "$file"; then printf 'potential secret: %s matches /%s/\n' "$file" "$pattern" >&2 found=1 fi done done if (( found )); then printf '\nRefusing to proceed: remove the secret from the staged content.\n' >&2 exit 1 fi exit 0