11#! /usr/bin/env bash
22
33# Secret Scanning Script for Dotfiles
4- # This script searches for common patterns that might indicate secrets or sensitive info.
4+ # This script searches for common patterns that might indicate secrets or sensitive info using git grep .
55
66# Define the colors
77RED=' \033[0;31m'
88GREEN=' \033[0;32m'
99YELLOW=' \033[1;33m'
1010NC=' \033[0m' # No Color
1111
12- # Define the patterns to search for
12+ # Define the patterns to search for (using extended regex for git grep)
13+ # Note: we use -E for extended regex in git grep
1314PATTERNS=(
14- # AWS Access Key ID
15- ' AKIA[0-9A-Z]{16}'
16- # AWS Secret Access Key
17- ' [^a-zA-Z0-9/+=][a-zA-Z0-9/+=]{40}[^a-zA-Z0-9/+=]'
18- # GitHub Personal Access Token
19- ' ghp_[a-zA-Z0-9]{36}'
20- # GitHub OAuth Token
21- ' gho_[a-zA-Z0-9]{36}'
22- # Slack Token
23- ' xox[bap]-[a-zA-Z0-9-]{10,}'
24- # Stripe API Key
25- ' sk_live_[0-9a-zA-Z]{24}'
26- # Google API Key
27- ' AIza[0-9A-Za-z\\-_]{35}'
28- # Generic Private Key
29- ' -----BEGIN [A-Z ]*PRIVATE KEY-----'
30- # Potential passwords/secrets in config files
31- ' (password|passphrase|token|secret|key|api|auth|cred|ident|account|user|private|ssh-)[^=]*[=:][^ \n]{8,}'
15+ ' AKIA[0-9A-Z]{16}' # AWS Access Key ID
16+ ' ghp_[a-zA-Z0-9]{36}' # GitHub Personal Access Token
17+ ' gho_[a-zA-Z0-9]{36}' # GitHub OAuth Token
18+ ' xox[bap]-[a-zA-Z0-9-]{10,}' # Slack Token
19+ ' sk_live_[0-9a-zA-Z]{24}' # Stripe API Key
20+ ' AIza[0-9A-Za-z\-_]{35}' # Google API Key
21+ ' -----BEGIN [A-Z ]*PRIVATE KEY-----' # Generic Private Key
22+ ' (password|passphrase|token|secret|api|auth|cred|ssh-)[^=]*[=:][^ \/\n]{12,}' # Potential secrets (excluding URLs)
3223)
3324
34- # Files to exclude (like this script itself)
35- EXCLUDE_PATTERNS=(
36- ' scripts/security/scan_secrets.sh'
37- ' *.png'
38- ' *.jpg'
39- ' *.jpeg'
40- ' *.gif'
41- ' *.svg'
42- ' *.ico'
43- ' *.woff'
44- ' *.woff2'
45- ' *.ttf'
46- ' *.otf'
47- ' *.lock'
48- ' *.json'
25+ # Files/Directories to exclude (relative to repo root)
26+ EXCLUDE=(
27+ ' :(exclude).git'
28+ ' :(exclude)oh-my-zsh'
29+ ' :(exclude)node_modules'
30+ ' :(exclude).cache'
31+ ' :(exclude)scripts/security/scan_secrets.sh'
32+ ' :(exclude).ignore_stow/default-cursor'
33+ ' :(exclude).ignore_stow/default-vscode'
34+ ' :(exclude)*.png'
35+ ' :(exclude)*.jpg'
36+ ' :(exclude)*.jpeg'
37+ ' :(exclude)*.gif'
38+ ' :(exclude)*.svg'
39+ ' :(exclude)*.ico'
40+ ' :(exclude)*.lock'
41+ ' :(exclude)*.json'
4942)
5043
51- # Directories to exclude
52- EXCLUDE_DIRS=(
53- ' .git'
54- ' oh-my-zsh'
55- ' node_modules'
56- ' .cache'
57- )
58-
59- # Build the exclude arguments for grep
60- EXCLUDE_ARGS=()
61- for pattern in " ${EXCLUDE_PATTERNS[@]} " ; do
62- EXCLUDE_ARGS+=(--exclude=" $pattern " )
63- done
64-
65- for dir in " ${EXCLUDE_DIRS[@]} " ; do
66- EXCLUDE_ARGS+=(--exclude-dir=" $dir " )
67- done
68-
69- echo -e " ${YELLOW} Scanning for potential secrets...${NC} "
44+ echo -e " ${YELLOW} Scanning tracked files for potential secrets...${NC} "
7045
7146FOUND_SECRETS=0
7247
7348for pattern in " ${PATTERNS[@]} " ; do
74- # Search for the pattern in all files, excluding some
75- # Use -E for extended regex
76- # Use -r for recursive
77- # Use -n for line numbers
78- # Use -I to skip binary files
79- MATCHES=$( grep -rEnI " ${EXCLUDE_ARGS [@]} " " $pattern " . 2> /dev/null)
49+ # Use git grep to search only tracked files
50+ # -E: extended regex
51+ # -n: line numbers
52+ # -i: ignore case for some patterns if needed (but here we stay case sensitive for tokens)
53+ # -I: don't match in binary files
54+ MATCHES=$( git grep -EnI " $pattern " -- . " ${EXCLUDE [@]}" 2> /dev/null)
8055
8156 if [[ -n " $MATCHES " ]]; then
8257 echo -e " ${RED} Found potential secret for pattern: $pattern ${NC} "
@@ -89,6 +64,6 @@ if [[ $FOUND_SECRETS -eq 1 ]]; then
8964 echo -e " ${RED} ERROR: Potential secrets found! Please review before pushing.${NC} "
9065 exit 1
9166else
92- echo -e " ${GREEN} No potential secrets found.${NC} "
67+ echo -e " ${GREEN} No potential secrets found in tracked files .${NC} "
9368 exit 0
9469fi
0 commit comments