Created
March 21, 2020 11:37
-
-
Save DarkMatterMatt/9e725fb1d7c5a562a584c1198b7d1a06 to your computer and use it in GitHub Desktop.
A pre-commit hook to prevent uploading secrets (API keys) by accident
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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