Skip to content

Commit 79451fc

Browse files
feat: add pre-commit for private RPC & EOL check (axelarnetwork#746)
1 parent a2165a8 commit 79451fc

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

.husky/pre-commit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22

33
echo "Running 'npm run build' pre-commit..."
44
npm run build
5+
6+
bash ./.husky/scripts/detect-private-rpc.sh
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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

Comments
 (0)