|
| 1 | +#!/bin/bash |
| 2 | +# Socket Security Commit-msg Hook |
| 3 | +# Additional security layer - validates commit even if pre-commit was bypassed. |
| 4 | + |
| 5 | +set -e |
| 6 | + |
| 7 | +# Colors for output. |
| 8 | +RED='\033[0;31m' |
| 9 | +GREEN='\033[0;32m' |
| 10 | +NC='\033[0m' |
| 11 | + |
| 12 | +# Allowed public API key (used in socket-lib). |
| 13 | +ALLOWED_PUBLIC_KEY="sktsec_t_--RAN5U4ivauy4w37-6aoKyYPDt5ZbaT5JBVMqiwKo_api" |
| 14 | + |
| 15 | +ERRORS=0 |
| 16 | + |
| 17 | +# Get files in this commit (for security checks). |
| 18 | +COMMITTED_FILES=$(git diff --cached --name-only --diff-filter=ACM 2>/dev/null || echo "") |
| 19 | + |
| 20 | +# Quick checks for critical issues in committed files. |
| 21 | +if [ -n "$COMMITTED_FILES" ]; then |
| 22 | + for file in $COMMITTED_FILES; do |
| 23 | + if [ -f "$file" ]; then |
| 24 | + # Check for Socket API keys (except allowed). |
| 25 | + if grep -E 'sktsec_[a-zA-Z0-9_-]+' "$file" 2>/dev/null | grep -v "$ALLOWED_PUBLIC_KEY" | grep -v 'your_api_key_here' | grep -v 'fake-token' | grep -v 'test-token' | grep -v '\.example' | grep -q .; then |
| 26 | + echo "${RED}✗ SECURITY: Potential API key detected in commit!${NC}" |
| 27 | + echo "File: $file" |
| 28 | + ERRORS=$((ERRORS + 1)) |
| 29 | + fi |
| 30 | + |
| 31 | + # Check for .env files. |
| 32 | + if echo "$file" | grep -qE '^\.env(\.local)?$'; then |
| 33 | + echo "${RED}✗ SECURITY: .env file in commit!${NC}" |
| 34 | + ERRORS=$((ERRORS + 1)) |
| 35 | + fi |
| 36 | + fi |
| 37 | + done |
| 38 | +fi |
| 39 | + |
| 40 | +# Auto-strip AI attribution from commit message. |
| 41 | +COMMIT_MSG_FILE="$1" |
| 42 | +if [ -f "$COMMIT_MSG_FILE" ]; then |
| 43 | + # Create a temporary file to store the cleaned message. |
| 44 | + TEMP_FILE=$(mktemp) |
| 45 | + REMOVED_LINES=0 |
| 46 | + |
| 47 | + # Read the commit message line by line and filter out AI attribution. |
| 48 | + while IFS= read -r line || [ -n "$line" ]; do |
| 49 | + # Check if this line contains AI attribution patterns. |
| 50 | + if echo "$line" | grep -qiE "(Generated with|Co-Authored-By: Claude|Co-Authored-By: AI|🤖 Generated|AI generated|Claude Code|@anthropic|Assistant:|Generated by Claude|Machine generated)"; then |
| 51 | + REMOVED_LINES=$((REMOVED_LINES + 1)) |
| 52 | + else |
| 53 | + # Line doesn't contain AI attribution, keep it. |
| 54 | + printf '%s\n' "$line" >> "$TEMP_FILE" |
| 55 | + fi |
| 56 | + done < "$COMMIT_MSG_FILE" |
| 57 | + |
| 58 | + # Replace the original commit message with the cleaned version. |
| 59 | + if [ $REMOVED_LINES -gt 0 ]; then |
| 60 | + mv "$TEMP_FILE" "$COMMIT_MSG_FILE" |
| 61 | + echo "${GREEN}✓ Auto-stripped${NC} $REMOVED_LINES AI attribution line(s) from commit message" |
| 62 | + else |
| 63 | + # No lines were removed, just clean up the temp file. |
| 64 | + rm -f "$TEMP_FILE" |
| 65 | + fi |
| 66 | +fi |
| 67 | + |
| 68 | +if [ $ERRORS -gt 0 ]; then |
| 69 | + echo "${RED}✗ Commit blocked by security validation${NC}" |
| 70 | + exit 1 |
| 71 | +fi |
| 72 | + |
| 73 | +exit 0 |
0 commit comments