summaryrefslogtreecommitdiff
path: root/tests/secrets-guard.test.sh
blob: 338206a926d7790f915f6213862aa1b7b8668c14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env bash
# Fail if tracked files look like Apple/iCloud session material or secrets.
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"

fail() { echo "FAIL: $*" >&2; exit 1; }
pass() { echo "ok: $*"; }

# Filenames that must never be tracked
BAD_NAME_RE='(storage-state\.json|session\.ok|cookies\.json|(^|/)Cookies$|\.env$|\.pem$|\.key$|credentials\.json|secrets\.ya?ml|browser-profile/)'

mapfile -t TRACKED < <(git ls-files)
[[ "${#TRACKED[@]}" -gt 0 ]] || fail "no tracked files"

for f in "${TRACKED[@]}"; do
  if [[ "$f" =~ $BAD_NAME_RE ]]; then
    fail "tracked forbidden path: $f"
  fi
done
pass "no forbidden secret filenames tracked"

# Content heuristics on tracked text (skip lockfile binary-ish noise)
CONTENT_RE='X-APPLE-WEBAUTH|X-APPLE-DS-WEB-SESSION|myacinfo|BEGIN (RSA |OPENSSH )?PRIVATE KEY|appleidpassword|ICLOUD_PASSWORD\s*='
hits="$(git grep -nI -E "$CONTENT_RE" -- . ':(exclude)Cargo.lock' ':(exclude)SECURITY.md' ':(exclude)tests/secrets-guard.test.sh' || true)"
if [[ -n "$hits" ]]; then
  echo "$hits" >&2
  fail "tracked content matches secret heuristics"
fi
pass "no secret-like content in tracked files"

# Ensure session dir is outside the repo by default
if [[ -e "$ROOT/browser-profile" || -e "$ROOT/storage-state.json" || -e "$ROOT/session.ok" ]]; then
  fail "session material present in repo working tree (use ~/.config/icloud-notes/)"
fi
pass "no session material in repo working tree"

echo "All secrets-guard checks passed."