Skip to content

Commit fea02ef

Browse files
committed
chore(git-hooks): commit pre-commit and pre-push hooks
Adds pre-commit and pre-push hooks to version control. These hooks provide security checks for sensitive data and prevent AI attribution in commits.
1 parent 3560d1b commit fea02ef

File tree

2 files changed

+211
-0
lines changed

2 files changed

+211
-0
lines changed

.git-hooks/pre-commit

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#!/bin/bash
2+
# Socket Security Checks
3+
# Prevents committing sensitive data and common mistakes.
4+
5+
set -e
6+
7+
# Colors for output.
8+
RED='\033[0;31m'
9+
YELLOW='\033[1;33m'
10+
GREEN='\033[0;32m'
11+
NC='\033[0m'
12+
13+
# Allowed public API key (used in socket-lib).
14+
ALLOWED_PUBLIC_KEY="sktsec_t_--RAN5U4ivauy4w37-6aoKyYPDt5ZbaT5JBVMqiwKo_api"
15+
16+
echo "${GREEN}Running Socket Security checks...${NC}"
17+
18+
# Get list of staged files.
19+
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM)
20+
21+
if [ -z "$STAGED_FILES" ]; then
22+
echo "${GREEN}✓ No files to check${NC}"
23+
exit 0
24+
fi
25+
26+
ERRORS=0
27+
28+
# Check for .DS_Store files.
29+
echo "Checking for .DS_Store files..."
30+
if echo "$STAGED_FILES" | grep -q '\.DS_Store'; then
31+
echo "${RED}✗ ERROR: .DS_Store file detected!${NC}"
32+
echo "$STAGED_FILES" | grep '\.DS_Store'
33+
ERRORS=$((ERRORS + 1))
34+
fi
35+
36+
# Check for log files.
37+
echo "Checking for log files..."
38+
if echo "$STAGED_FILES" | grep -E '\.log$' | grep -v 'test.*\.log'; then
39+
echo "${RED}✗ ERROR: Log file detected!${NC}"
40+
echo "$STAGED_FILES" | grep -E '\.log$' | grep -v 'test.*\.log'
41+
ERRORS=$((ERRORS + 1))
42+
fi
43+
44+
# Check for .env files.
45+
echo "Checking for .env files..."
46+
if echo "$STAGED_FILES" | grep -E '^\.env(\.local)?$'; then
47+
echo "${RED}✗ ERROR: .env or .env.local file detected!${NC}"
48+
echo "$STAGED_FILES" | grep -E '^\.env(\.local)?$'
49+
echo "These files should never be committed. Use .env.example instead."
50+
ERRORS=$((ERRORS + 1))
51+
fi
52+
53+
# Check for hardcoded user paths (generic detection).
54+
echo "Checking for hardcoded personal paths..."
55+
for file in $STAGED_FILES; do
56+
if [ -f "$file" ]; then
57+
# Skip test files and hook scripts.
58+
if echo "$file" | grep -qE '\.(test|spec)\.|/test/|/tests/|fixtures/|\.git-hooks/|\.husky/'; then
59+
continue
60+
fi
61+
62+
# Check for common user path patterns.
63+
if grep -E '(/Users/[^/\s]+/|/home/[^/\s]+/|C:\\Users\\[^\\]+\\)' "$file" 2>/dev/null | grep -q .; then
64+
echo "${RED}✗ ERROR: Hardcoded personal path found in: $file${NC}"
65+
grep -n -E '(/Users/[^/\s]+/|/home/[^/\s]+/|C:\\Users\\[^\\]+\\)' "$file" | head -3
66+
echo "Replace with relative paths or environment variables."
67+
ERRORS=$((ERRORS + 1))
68+
fi
69+
fi
70+
done
71+
72+
# Check for Socket API keys.
73+
echo "Checking for API keys..."
74+
for file in $STAGED_FILES; do
75+
if [ -f "$file" ]; then
76+
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 'SOCKET_SECURITY_API_KEY=' | grep -v 'fake-token' | grep -v 'test-token' | grep -q .; then
77+
echo "${YELLOW}⚠ WARNING: Potential API key found in: $file${NC}"
78+
grep -n 'sktsec_' "$file" | grep -v "$ALLOWED_PUBLIC_KEY" | grep -v 'your_api_key_here' | grep -v 'fake-token' | grep -v 'test-token' | head -3
79+
echo "If this is a real API key, DO NOT COMMIT IT."
80+
fi
81+
fi
82+
done
83+
84+
# Check for common secret patterns.
85+
echo "Checking for potential secrets..."
86+
for file in $STAGED_FILES; do
87+
if [ -f "$file" ]; then
88+
# Skip test files, example files, and hook scripts.
89+
if echo "$file" | grep -qE '\.(test|spec)\.(m?[jt]s|tsx?)$|\.example$|/test/|/tests/|fixtures/|\.git-hooks/|\.husky/'; then
90+
continue
91+
fi
92+
93+
# Check for AWS keys.
94+
if grep -iE '(aws_access_key|aws_secret|AKIA[0-9A-Z]{16})' "$file" 2>/dev/null | grep -q .; then
95+
echo "${RED}✗ ERROR: Potential AWS credentials found in: $file${NC}"
96+
grep -n -iE '(aws_access_key|aws_secret|AKIA[0-9A-Z]{16})' "$file" | head -3
97+
ERRORS=$((ERRORS + 1))
98+
fi
99+
100+
# Check for GitHub tokens.
101+
if grep -E 'gh[ps]_[a-zA-Z0-9]{36}' "$file" 2>/dev/null | grep -q .; then
102+
echo "${RED}✗ ERROR: Potential GitHub token found in: $file${NC}"
103+
grep -n -E 'gh[ps]_[a-zA-Z0-9]{36}' "$file" | head -3
104+
ERRORS=$((ERRORS + 1))
105+
fi
106+
107+
# Check for private keys.
108+
if grep -E '-----BEGIN (RSA |EC |DSA )?PRIVATE KEY-----' "$file" 2>/dev/null | grep -q .; then
109+
echo "${RED}✗ ERROR: Private key found in: $file${NC}"
110+
ERRORS=$((ERRORS + 1))
111+
fi
112+
fi
113+
done
114+
115+
if [ $ERRORS -gt 0 ]; then
116+
echo ""
117+
echo "${RED}✗ Security check failed with $ERRORS error(s).${NC}"
118+
echo "Fix the issues above and try again."
119+
exit 1
120+
fi
121+
122+
echo "${GREEN}✓ All security checks passed!${NC}"
123+
exit 0

.git-hooks/pre-push

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/bin/bash
2+
# Socket Security Pre-push Hook
3+
# Final security check before pushing to remote.
4+
5+
set -e
6+
7+
# Colors for output.
8+
RED='\033[0;31m'
9+
YELLOW='\033[1;33m'
10+
GREEN='\033[0;32m'
11+
NC='\033[0m'
12+
13+
echo "${GREEN}Running final security validation before push...${NC}"
14+
15+
# Allowed public API key (used in socket-lib).
16+
ALLOWED_PUBLIC_KEY="sktsec_t_--RAN5U4ivauy4w37-6aoKyYPDt5ZbaT5JBVMqiwKo_api"
17+
18+
# Get the remote name and URL.
19+
remote="$1"
20+
url="$2"
21+
22+
# Read stdin for refs being pushed.
23+
while read local_ref local_sha remote_ref remote_sha; do
24+
# Get the range of commits being pushed.
25+
if [ "$remote_sha" = "0000000000000000000000000000000000000000" ]; then
26+
# New branch - check all commits.
27+
range="$local_sha"
28+
else
29+
# Existing branch - check new commits.
30+
range="$remote_sha..$local_sha"
31+
fi
32+
33+
# Get all files changed in these commits.
34+
CHANGED_FILES=$(git diff --name-only "$range" 2>/dev/null || echo "")
35+
36+
if [ -z "$CHANGED_FILES" ]; then
37+
continue
38+
fi
39+
40+
ERRORS=0
41+
42+
# Check for sensitive files.
43+
if echo "$CHANGED_FILES" | grep -qE '^\.env(\.local)?$'; then
44+
echo "${RED}✗ BLOCKED: Attempting to push .env file!${NC}"
45+
echo "Files: $(echo "$CHANGED_FILES" | grep -E '^\.env(\.local)?$')"
46+
ERRORS=$((ERRORS + 1))
47+
fi
48+
49+
# Check for .DS_Store.
50+
if echo "$CHANGED_FILES" | grep -q '\.DS_Store'; then
51+
echo "${YELLOW}⚠ WARNING: .DS_Store file in push${NC}"
52+
echo "Files: $(echo "$CHANGED_FILES" | grep '\.DS_Store')"
53+
fi
54+
55+
# Sample files for API keys (only check files that exist).
56+
for file in $CHANGED_FILES; do
57+
if [ -f "$file" ] && [ ! -d "$file" ]; then
58+
# Skip test files.
59+
if echo "$file" | grep -qE '\.(test|spec)\.|/test/|/tests/|fixtures/|\.example$'; then
60+
continue
61+
fi
62+
63+
# Check for Socket API keys.
64+
if grep -E 'sktsec_[a-zA-Z0-9_-]{40,}' "$file" 2>/dev/null | grep -v "$ALLOWED_PUBLIC_KEY" | grep -v 'your_api_key_here' | grep -v 'fake-token' | grep -v 'test-token' | grep -q .; then
65+
echo "${RED}✗ BLOCKED: Real API key detected in push!${NC}"
66+
echo "File: $file"
67+
echo "Line(s):"
68+
grep -n 'sktsec_' "$file" | grep -v "$ALLOWED_PUBLIC_KEY" | grep -v 'your_api_key_here' | grep -v 'fake-token' | grep -v 'test-token' | head -3
69+
ERRORS=$((ERRORS + 1))
70+
fi
71+
fi
72+
done
73+
74+
if [ $ERRORS -gt 0 ]; then
75+
echo ""
76+
echo "${RED}✗ Push blocked by security validation!${NC}"
77+
echo "Remove sensitive data from your commits before pushing."
78+
echo ""
79+
echo "To fix:"
80+
echo " 1. Remove sensitive data from files"
81+
echo " 2. Amend or rebase your commits"
82+
echo " 3. Push again"
83+
exit 1
84+
fi
85+
done
86+
87+
echo "${GREEN}✓ Security validation passed!${NC}"
88+
exit 0

0 commit comments

Comments
 (0)