|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | +# |
| 4 | +# Copyright 2026 ResQ |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | +# pre-commit |
| 19 | +# |
| 20 | +# Runs checks before allowing a commit: |
| 21 | +# resq CLI (if installed) → secrets scan → OSV vuln scan → C# format check |
| 22 | +# |
| 23 | +# Install resq CLI: cargo install resq-cli |
| 24 | +# |
| 25 | +# Exit codes: |
| 26 | +# 0 All checks passed. |
| 27 | +# 1 Blocking check failed. |
| 28 | + |
| 29 | +# ── Path resolution ─────────────────────────────────────────────────────────── |
| 30 | +if [ ${#BASH_SOURCE[@]} -gt 0 ] && [ -n "${BASH_SOURCE[0]:-}" ]; then |
| 31 | + HOOK_PATH="${BASH_SOURCE[0]}" |
| 32 | +else |
| 33 | + HOOK_PATH="$0" |
| 34 | +fi |
| 35 | +HOOK_PATH=$(readlink -f "$HOOK_PATH") |
| 36 | +SCRIPT_DIR=$(dirname "$HOOK_PATH") |
| 37 | +PROJECT_ROOT=$(cd "$SCRIPT_DIR/.." && pwd) |
| 38 | + |
| 39 | +# ── Nix environment entry (if osv-scanner or dotnet missing) ────────────────── |
| 40 | +if [[ -z "${IN_NIX_SHELL:-}" ]] && [[ -z "${RESQ_NIX_GUARD:-}" ]]; then |
| 41 | + NEED_NIX=0 |
| 42 | + for _tool in osv-scanner dotnet; do |
| 43 | + if ! command -v "$_tool" >/dev/null 2>&1; then NEED_NIX=1; break; fi |
| 44 | + done |
| 45 | + if [[ "$NEED_NIX" -eq 1 ]] && command -v nix >/dev/null 2>&1 && [ -f "$PROJECT_ROOT/flake.nix" ]; then |
| 46 | + echo "❄️ Entering Nix environment for pre-commit checks..." |
| 47 | + export RESQ_NIX_GUARD=1 |
| 48 | + exec nix develop "$PROJECT_ROOT" --command bash "$HOOK_PATH" "$@" |
| 49 | + fi |
| 50 | +fi |
| 51 | + |
| 52 | +# Source shared utilities if available |
| 53 | +if [ -f "$PROJECT_ROOT/scripts/lib/shell-utils.sh" ]; then |
| 54 | + source "$PROJECT_ROOT/scripts/lib/shell-utils.sh" |
| 55 | +fi |
| 56 | + |
| 57 | +[ -n "${GIT_HOOKS_SKIP:-}" ] && exit 0 |
| 58 | + |
| 59 | +# ── Try resq CLI (handles all checks including OSV, secrets, formatting) ────── |
| 60 | +if command -v resq >/dev/null 2>&1; then |
| 61 | + exec resq pre-commit --root "$PROJECT_ROOT" "$@" |
| 62 | +fi |
| 63 | +echo "ℹ️ resq CLI not found — running inline checks. Install: cargo install resq-cli" |
| 64 | + |
| 65 | +# ── Large file check ────────────────────────────────────────────────────────── |
| 66 | +echo "🔍 Checking staged file sizes..." |
| 67 | +STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM) |
| 68 | +for f in $STAGED_FILES; do |
| 69 | + if [ -f "$f" ]; then |
| 70 | + SIZE=$(wc -c < "$f") |
| 71 | + if [ "$SIZE" -gt 1048576 ]; then |
| 72 | + echo "❌ '$f' exceeds 1 MB. Large files must not be committed." |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | + fi |
| 76 | +done |
| 77 | +echo "✅ File sizes OK" |
| 78 | + |
| 79 | +# ── Secrets scan ────────────────────────────────────────────────────────────── |
| 80 | +echo "🔍 Scanning for secrets..." |
| 81 | +if command -v gitleaks >/dev/null 2>&1; then |
| 82 | + if ! gitleaks protect --staged --redact -v 2>/dev/null; then |
| 83 | + echo "❌ Potential secrets detected. Remove them before committing." |
| 84 | + exit 1 |
| 85 | + fi |
| 86 | +else |
| 87 | + STAGED_CONTENT=$(git diff --cached --unified=0 2>/dev/null || true) |
| 88 | + echo "$STAGED_CONTENT" | grep -qE 'AKIA[0-9A-Z]{16}' && { echo "❌ AWS key detected."; exit 1; } || true |
| 89 | + echo "$STAGED_CONTENT" | grep -qE '(ghp_|ghs_|gho_)[A-Za-z0-9_]{36,}' && { echo "❌ GitHub token detected."; exit 1; } || true |
| 90 | + echo "$STAGED_CONTENT" | grep -qF 'BEGIN PRIVATE KEY' && { echo "❌ Private key detected."; exit 1; } || true |
| 91 | + echo "$STAGED_CONTENT" | grep -qiE 'api[_-]?key\s*=\s*"[A-Za-z0-9_\-]{16,}"' && { echo "❌ API key detected."; exit 1; } || true |
| 92 | + echo "$STAGED_CONTENT" | grep -qiE 'password\s*=\s*"[^"]{4,}"' && { echo "❌ Password detected."; exit 1; } || true |
| 93 | +fi |
| 94 | +echo "✅ Secrets scan OK" |
| 95 | + |
| 96 | +# ── OSV vulnerability scan ──────────────────────────────────────────────────── |
| 97 | +if command -v osv-scanner >/dev/null 2>&1; then |
| 98 | + echo "🔍 Running OSV vulnerability scan..." |
| 99 | + OSV_ARGS=() |
| 100 | + [ -f "$PROJECT_ROOT/packages.lock.json" ] && OSV_ARGS+=(--lockfile "$PROJECT_ROOT/packages.lock.json") |
| 101 | + if [ ${#OSV_ARGS[@]} -gt 0 ]; then |
| 102 | + if ! osv-scanner scan "${OSV_ARGS[@]}" 2>/dev/null; then |
| 103 | + echo "❌ OSV scan found vulnerabilities. Run: osv-scanner scan --lockfile packages.lock.json" |
| 104 | + echo " To skip: GIT_HOOKS_SKIP=1 git commit" |
| 105 | + exit 1 |
| 106 | + fi |
| 107 | + echo "✅ OSV scan OK" |
| 108 | + fi |
| 109 | +fi |
| 110 | + |
| 111 | +# ── C# formatting check ─────────────────────────────────────────────────────── |
| 112 | +STAGED_CS=$(git diff --cached --name-only --diff-filter=ACM | grep '\.cs$' || true) |
| 113 | +if [ -n "$STAGED_CS" ] && command -v dotnet >/dev/null 2>&1; then |
| 114 | + echo "🔍 Checking C# formatting..." |
| 115 | + if ! dotnet format --verify-no-changes --no-restore 2>/dev/null; then |
| 116 | + echo "❌ C# formatting issues. Run: dotnet format" |
| 117 | + exit 1 |
| 118 | + fi |
| 119 | + echo "✅ C# formatting OK" |
| 120 | +fi |
| 121 | + |
| 122 | +echo "✅ Pre-commit checks passed" |
0 commit comments