Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

13.5 KB
Raw Blame
#!/bin/bash
# Pre-commit hook adapted from GitHub workflow file-whitelist-check.yml
# The agent-guidance guard scans the full Git index; the remaining checks
# (whitelist, large file) run only against the files passed via $@ (resolved
# against the caller's directory below, so a manual invocation with
# cwd-relative paths is checked too).

# Load allowed file patterns from shared whitelist (single source of truth)
REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || echo ".")"
# Helpers ship next to this hook (../.github/scripts/), so locate them relative
# to the hook itself rather than to the repository being scanned.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
WHITELIST_FILE="$REPO_ROOT/.file-whitelist.txt"

# Everything below uses repository-root-relative paths (the agent-guidance
# index scan, bridge lookups, and the paths pre-commit passes via $@), so run
# from the repository root regardless of the invocation directory; `git
# ls-files` is also called with `-C "$REPO_ROOT"` so the scan is the full Git
# index from any working directory, not a cwd-scoped subtree.
CALLER_DIR="$PWD"
cd "$REPO_ROOT" || exit 1

# pre-commit runs at the repository root and passes root-relative paths, but
# a manual invocation can pass paths relative to the CALLER's directory. The
# cd above would leave those unresolvable and they would take the
# deleted-file skip below (silent fail-open: a violating file is never
# checked). Resolve each arg that exists only relative to the caller's
# directory into its repository-root spelling so it is checked exactly like
# the pre-commit form. An arg that already resolves from the root keeps that
# root-relative meaning; one that exists nowhere (deleted file) keeps its
# spelling and is still skipped below.
CALLER_ARGS=()
for arg in "$@"; do
  if [ ! -e "$arg" ] && [ -e "$CALLER_DIR/$arg" ]; then
    case "$CALLER_DIR" in
      "$REPO_ROOT") resolved_arg="$arg" ;;
      "$REPO_ROOT"/*) resolved_arg="${CALLER_DIR#"$REPO_ROOT"/}/$arg" ;;
      *) resolved_arg="$arg" ;;
    esac
    CALLER_ARGS+=("$resolved_arg")
  else
    CALLER_ARGS+=("$arg")
  fi
done

if [ ! -f "$WHITELIST_FILE" ]; then
  echo "❌ Missing .file-whitelist.txt — cannot run whitelist check."
  exit 1
fi

# Agent guidance path guard: reject tracked AGENT.md / AGENTS.md / CLAUDE.md / GEMINI.md
# outside the exact repository root. Scans the full Git index (not just $@) so a
# forced `git add -f` cannot land forbidden agent-guidance paths anywhere except
# the blessed root AGENTS.md, CLAUDE.md, and GEMINI.md. What is enforced, in full:
#   (1) a basename equal to AGENT(S).md / CLAUDE.md / GEMINI.md in any letter
#       case is rejected at any depth except the exact exempt root paths —
#       "any case" is Unicode caseless matching (uppercase-then-casefold:
#       ASCII case plus U+017F LONG S, U+0131 DOTLESS I, ligature folds),
#       ignoring trailing dots/spaces ('CLAUDE.md.', 'AGENTS.md '), and
#       CLAUDE.local.md is protected the same way with no root exemption;
#   (2) every tracked path must be pure printable ASCII (U+0020..U+007E) —
#       hidden-Unicode filenames (ZWSP, bidi marks, tag characters,
#       fullwidth/Cyrillic/Greek homoglyphs) and control characters in names
#       are rejected outright, so lookalike basenames cannot land at all;
#   (3) the root bridges must be regular files whose blob is exactly
#       '@AGENTS.md' plus a trailing newline (below).
# (1) and (2) are implemented in .github/scripts/agent_guidance_index_filter.py
# (one python3 process for the whole index scan). python3 is the same
# interpreter the content classifier below (delegated to
# .github/scripts/file-whitelist-check.sh) already requires. If python3 or the
# filter is unavailable the guard FAILS CLOSED. The NUL-delimited read handles
# paths containing spaces.
AGENT_GUIDANCE_VIOLATIONS=()
AGENT_GUIDANCE_REASONS=()
GUIDANCE_FILTER="$SCRIPT_DIR/../.github/scripts/agent_guidance_index_filter.py"
if [ ! -f "$GUIDANCE_FILTER" ] || ! command -v python3 >/dev/null 2>&1; then
  echo "❌ AGENT GUIDANCE GUARD cannot run (python3 or $GUIDANCE_FILTER missing) — failing closed."
  exit 1
fi
GUIDANCE_TMP="$(mktemp "${TMPDIR:-/tmp}/agent-guidance.XXXXXX")"
# This hook does not run under `set -o pipefail`, so check BOTH sides of the
# pipeline explicitly: a failing `git ls-files` (e.g. not inside a repository)
# would otherwise feed the filter an empty list and the guard would pass.
git -C "$REPO_ROOT" ls-files -z | python3 "$GUIDANCE_FILTER" > "$GUIDANCE_TMP"
GUIDANCE_PIPE_STATUS=("${PIPESTATUS[@]}")
if [ "${GUIDANCE_PIPE_STATUS[0]}" -ne 0 ] || [ "${GUIDANCE_PIPE_STATUS[1]}" -ne 0 ]; then
  rm -f "$GUIDANCE_TMP"
  echo "❌ AGENT GUIDANCE GUARD could not scan the Git index — failing closed."
  exit 1
fi
while IFS= read -r -d '' record || [ -n "$record" ]; do
  # Records are "<path>\t<reason>"; split at the LAST tab with parameter
  # expansion (not `read`) so paths containing newlines or tabs survive
  # intact and are reported verbatim (a reason is "; "-joined rule text and
  # never contains a tab).
  AGENT_GUIDANCE_VIOLATIONS+=("${record%$'\t'*}")
  AGENT_GUIDANCE_REASONS+=("${record##*$'\t'}")
done < "$GUIDANCE_TMP"
rm -f "$GUIDANCE_TMP"

if [ ${#AGENT_GUIDANCE_VIOLATIONS[@]} -gt 0 ]; then
  echo ""
  echo "❌ AGENT GUIDANCE FILES - Forbidden tracked AGENT(S).md / CLAUDE.md / CLAUDE.local.md / GEMINI.md paths or unsafe filenames detected:"
  echo "   Only the exact repository-root 'AGENTS.md', 'CLAUDE.md', and 'GEMINI.md' are permitted; agent-guidance"
  echo "   files anywhere else (any case — Unicode letter-case variants included — any depth, trailing dots or"
  echo "   spaces ignored; CLAUDE.local.md is never allowed) bypass .gitignore and"
  echo "   must be removed, and tracked paths must be pure printable ASCII (hidden-Unicode or lookalike filenames"
  echo "   are rejected outright)."
  for i in "${!AGENT_GUIDANCE_VIOLATIONS[@]}"; do
    echo "  🚫 ${AGENT_GUIDANCE_VIOLATIONS[$i]}"
    echo "     → Issue: ${AGENT_GUIDANCE_REASONS[$i]}"
  done
  echo ""
  echo "💡 To fix these issues:"
  echo "   - Run: git rm --cached '<path>' for each forbidden path above"
  echo "   - Only the exact repository-root 'AGENTS.md', 'CLAUDE.md', or 'GEMINI.md' is allowed"
  echo ""
  exit 1
fi

# Verify bridge files are regular files containing only the @AGENTS.md import
# directive. The check is on the INDEX ENTRY: a tracked symlink whose link
# target STRING is the 11-byte filename '@AGENTS.md\n' would satisfy a
# content+size comparison of the blob while every reader of CLAUDE.md follows
# the link to arbitrary text — so the entry must be a regular file (mode
# 100644), the worktree file must not be a symlink, and the blob must
# byte-equal '@AGENTS.md\n' exactly (blob-identity compare: no NUL splicing,
# no command-substitution newline stripping, and the 10/12-byte, CRLF and
# double-LF edges are covered by the same equality).
BRIDGE_VIOLATIONS=()
bridge_expected_blob="$(printf '@AGENTS.md\n' | git -C "$REPO_ROOT" hash-object --stdin)"
# The root AGENTS.md they import gets the same entry-level hardening (regular
# file, mode 100644, no symlink) — otherwise a symlinked AGENTS.md would
# redirect every bridge to arbitrary text — but its content is free-form, so
# only the bridges are held to the exact '@AGENTS.md\n' blob.
for bridge_file in AGENTS.md CLAUDE.md GEMINI.md; do
  if git -C "$REPO_ROOT" ls-files --error-unmatch -- "$bridge_file" >/dev/null 2>&1; then
    bridge_mode=""
    while IFS= read -r -d '' entry; do
      if [ "${entry#*$'\t'}" = "$bridge_file" ]; then
        bridge_mode="${entry%% *}"
      fi
    done < <(git -C "$REPO_ROOT" ls-files -s -z -- "$bridge_file")
    if [ "$bridge_mode" != "100644" ] || [ -L "$REPO_ROOT/$bridge_file" ]; then
      BRIDGE_VIOLATIONS+=("$bridge_file")
      continue
    fi
    if [ "$bridge_file" = "AGENTS.md" ]; then
      continue
    fi
    bridge_blob="$(git -C "$REPO_ROOT" rev-parse ":$bridge_file" 2>/dev/null || echo invalid)"
    if [ "$bridge_blob" != "$bridge_expected_blob" ]; then
      BRIDGE_VIOLATIONS+=("$bridge_file")
    fi
  fi
done

if [ ${#BRIDGE_VIOLATIONS[@]} -gt 0 ]; then
  echo ""
  echo "❌ INVALID AGENT GUIDANCE BRIDGES:"
  echo "   Root AGENTS.md, CLAUDE.md and GEMINI.md must be regular files (tracked mode 100644, no symlink),"
  echo "   and the CLAUDE.md / GEMINI.md bridges must contain exactly '@AGENTS.md' plus a trailing newline."
  echo ""
  for bridge_violation in "${BRIDGE_VIOLATIONS[@]}"; do
    echo "  🚫 $bridge_violation"
  done
  echo ""
  exit 1
fi

ALLOWED_PATTERNS=()
while IFS= read -r line; do
  [[ -z "$line" || "$line" =~ ^[[:space:]]*# ]] && continue
  ALLOWED_PATTERNS+=("$line")
done < "$WHITELIST_FILE"

# Index mode (100644, 100755, 120000, 160000, ...) of the exact tracked path
# "$1", or empty when the path is not in the index. `:(literal)` keeps glob
# characters in a name from widening the pathspec, and the exact-path compare
# keeps a directory argument from matching the files beneath it.
tracked_index_mode() {
  local want="$1" entry
  while IFS= read -r -d '' entry; do
    if [ "${entry#*$'\t'}" = "$want" ]; then
      printf '%s' "${entry%% *}"
      return 0
    fi
  done < <(git -C "$REPO_ROOT" ls-files -s -z -- ":(literal)$want")
  return 0
}

WHITELIST_VIOLATIONS=()
NON_REGULAR_VIOLATIONS=()
LARGE_FILES=()
BINARY_FILES=()

echo "🔍 Running file whitelist security checks..."

# Process each file passed as argument
for file in ${CALLER_ARGS[@]+"${CALLER_ARGS[@]}"}; do
  # Same rule as the CI gate: a symlink whose target is missing or is a
  # directory, or a tracked entry that exists but is not a regular file
  # (submodule gitlink, directory), is rejected rather than skipped; a
  # symlink to a regular file is checked like any file; only a path that no
  # longer exists at all (a deleted file) is skipped.
  if [ -L "$file" ]; then
    if [ ! -e "$file" ] || [ -d "$file" ]; then
      NON_REGULAR_VIOLATIONS+=("$file (symlink whose target is missing or is a directory)")
      continue
    fi
  elif [ ! -e "$file" ]; then
    continue
  elif [ ! -f "$file" ]; then
    nonregular_mode="$(tracked_index_mode "$file")"
    if [ -n "$nonregular_mode" ]; then
      NON_REGULAR_VIOLATIONS+=("$file (tracked mode $nonregular_mode is not a regular file)")
    fi
    continue
  fi

  # 1. Whitelist check
  ALLOWED=false
  for pattern in "${ALLOWED_PATTERNS[@]}"; do
    if echo "$file" | grep -qE "$pattern"; then
      ALLOWED=true
      break
    fi
  done

  if [ "$ALLOWED" = "false" ]; then
    WHITELIST_VIOLATIONS+=("$file")
  fi

  # 2. Large file check (>1MB)
  FILE_SIZE=$(stat -c%s "$file" 2>/dev/null || stat -f%z "$file" 2>/dev/null || echo 0)
  if [ "$FILE_SIZE" -gt 1048576 ]; then
    LARGE_FILES+=("$file ($(echo "$FILE_SIZE" | awk '{printf "%.1fMB", $1/1024/1024}'))")
  fi
done

# Use the same content classifier as CI. Whitelisting a .py/.json pathname
# must not allow a binary merely renamed to that suffix. The classifier also
# verifies that the six explicit PNG/MP3 exceptions retain their expected
# signatures.
CONTENT_CHECKER="$SCRIPT_DIR/../.github/scripts/file-whitelist-check.sh"
CONTENT_SCAN_OUTPUT=""
if [ ! -f "$CONTENT_CHECKER" ]; then
  BINARY_FILES+=("<content scanner> (missing $CONTENT_CHECKER)")
elif ! CONTENT_SCAN_OUTPUT=$(
  "$CONTENT_CHECKER" --content-only ${CALLER_ARGS[@]+"${CALLER_ARGS[@]}"}
); then
  if [ -z "$CONTENT_SCAN_OUTPUT" ]; then
    BINARY_FILES+=("<content scanner> (failed without diagnostics)")
  else
    while IFS=$'\t' read -r binary_path reason; do
      [ -z "$binary_path" ] && continue
      BINARY_FILES+=("$binary_path ($reason)")
    done <<< "$CONTENT_SCAN_OUTPUT"
  fi
fi

# Report violations
TOTAL_VIOLATIONS=0

if [ ${#NON_REGULAR_VIOLATIONS[@]} -gt 0 ]; then
  echo ""
  echo "❌ NON-REGULAR TRACKED PATHS - Every tracked path must be a checkable file:"
  echo "   Symlinks to a missing path or a directory, submodule gitlinks and other"
  echo "   non-regular entries cannot be checked against .file-whitelist.txt."
  for violation in "${NON_REGULAR_VIOLATIONS[@]}"; do
    echo "  🚫 $violation"
  done
  TOTAL_VIOLATIONS=$((TOTAL_VIOLATIONS + ${#NON_REGULAR_VIOLATIONS[@]}))
fi

if [ ${#WHITELIST_VIOLATIONS[@]} -gt 0 ]; then
  echo ""
  echo "❌ WHITELIST VIOLATIONS - File types not allowed in repository:"
  echo "   Binary files (images, audio, etc.) bloat the repo and should NOT be committed."
  echo "   Only explicitly listed binary files are allowed — store others externally."
  for violation in "${WHITELIST_VIOLATIONS[@]}"; do
    echo "  🚫 $violation"
  done
  TOTAL_VIOLATIONS=$((TOTAL_VIOLATIONS + ${#WHITELIST_VIOLATIONS[@]}))
fi

if [ ${#LARGE_FILES[@]} -gt 0 ]; then
  echo ""
  echo "❌ LARGE FILES (>1MB) - Files too big for repository:"
  for violation in "${LARGE_FILES[@]}"; do
    echo "  📏 $violation"
  done
  TOTAL_VIOLATIONS=$((TOTAL_VIOLATIONS + ${#LARGE_FILES[@]}))
fi

if [ ${#BINARY_FILES[@]} -gt 0 ]; then
  echo ""
  echo "❌ UNEXPECTED BINARY CONTENT - Files must be reviewable UTF-8 text:"
  echo "   Only the six exact, pre-existing PNG/MP3 assets are content exceptions."
  for violation in "${BINARY_FILES[@]}"; do
    echo "  🔒 $violation"
  done
  TOTAL_VIOLATIONS=$((TOTAL_VIOLATIONS + ${#BINARY_FILES[@]}))
fi

if [ $TOTAL_VIOLATIONS -eq 0 ]; then
  echo "✅ All file whitelist checks passed!"
  exit 0
else
  echo ""
  echo "💡 To fix these issues:"
  echo "   - For text/config files: add pattern to .file-whitelist.txt (requires maintainer approval)"
  echo "   - For binary files: do NOT rename or add them — they permanently bloat git history"
  echo "   - For large files: use external storage"
  echo ""
  exit 1
fi