Skip to content

Instantly share code, notes, and snippets.

@DarkMatterMatt
Created March 21, 2020 11:37
Show Gist options
  • Save DarkMatterMatt/9e725fb1d7c5a562a584c1198b7d1a06 to your computer and use it in GitHub Desktop.
Save DarkMatterMatt/9e725fb1d7c5a562a584c1198b7d1a06 to your computer and use it in GitHub Desktop.
A pre-commit hook to prevent uploading secrets (API keys) by accident
#!/bin/bash
#
# A pre-commit hook to prevent uploading secrets (API keys) by accident
read -r -d "" SECRETS <<"EOF"
YOUR_API_KEY
SECRET2
EOF
# find changed files
changed_files=$(git diff --cached --name-only --diff-filter=ACM)
# loop through each file
while IFS= read -r file; do
# check for each secret
while IFS= read -r secret; do
line_number=$(grep -n "$secret" "$file" | cut -d : -f 1)
if [ ! -z "$line_number" ]; then
echo "Found $secret in $file:$line_number"
exit 1
fi
done <<< "$SECRETS"
done <<< "$changed_files"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment