|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +SENSITIVE_RPC_PATTERNS=( |
| 4 | + 'https:\/\/[a-z0-9-]+\.quiknode\.pro\/[a-f0-9]{32,}' |
| 5 | + 'https:\/\/blastapi\.io\/dashboard\/project\/[a-f0-9\-]{36}' |
| 6 | + 'https:\/\/[a-z0-9-]+\.infura\.io\/v3\/[a-f0-9]{32}' |
| 7 | + 'https:\/\/\d+\.rpc\.thirdweb\.com\/[a-f0-9]{32}' |
| 8 | + 'https:\/\/[^ ]*\/[a-f0-9]{32,}\/?' |
| 9 | + 'https:\/\/[^ ]*\?(.*key|token|auth)=.+' |
| 10 | + 'https:\/\/[^ ]+:[^ @]+@[^ ]+' |
| 11 | +) |
| 12 | + |
| 13 | +FOUND=0 |
| 14 | + |
| 15 | +# Get list of staged files that are Added, Copied, or Modified |
| 16 | +FILES=$(git diff --cached --name-only --diff-filter=ACM) |
| 17 | + |
| 18 | +for file in $FILES; do |
| 19 | + # --- EOL Check --- |
| 20 | + if [ -f "$file" ]; then |
| 21 | + last_byte=$(tail -c 1 "$file" | od -An -t u1 | tr -d ' ') |
| 22 | + if [ "$last_byte" != "10" ]; then |
| 23 | + echo "File '$file' does not end with a newline (EOL)." |
| 24 | + FOUND=1 |
| 25 | + fi |
| 26 | + fi |
| 27 | + |
| 28 | + # -- Private RPC Check -- |
| 29 | + while IFS= read -r line; do |
| 30 | + # Skip empty lines or lines starting with '+' |
| 31 | + if [[ -z "$line" || "$line" =~ ^\+[^+] ]]; then |
| 32 | + # Extract the actual content (remove leading '+') |
| 33 | + line_content=$(echo "$line" | sed 's/^+//') |
| 34 | + # Skip bypassable lines |
| 35 | + if echo "$line_content" | grep -q 'skip-check'; then |
| 36 | + continue |
| 37 | + fi |
| 38 | + |
| 39 | + for pattern in "${SENSITIVE_RPC_PATTERNS[@]}"; do |
| 40 | + if echo "$line_content" | grep -E -i "$pattern" > /dev/null; then |
| 41 | + echo "Sensitive pattern detected: $pattern" |
| 42 | + echo "File: $file" |
| 43 | + echo "Line: $line_content" |
| 44 | + echo "Add '# skip-check' if intentional" |
| 45 | + echo "" |
| 46 | + FOUND=1 |
| 47 | + fi |
| 48 | + done |
| 49 | + fi |
| 50 | + done < <(git diff --cached --unified=0 "$file" | grep '^+[^+]') |
| 51 | +done |
| 52 | + |
| 53 | +if [[ "$FOUND" -eq 1 ]]; then |
| 54 | + echo "Commit blocked." |
| 55 | + exit 1 |
| 56 | +fi |
| 57 | + |
| 58 | +exit 0 |
0 commit comments