1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
#!/usr/bin/env bats
load test_helper
@test "no arguments prints usage instructions" {
repo_run git-secrets
[ $status -eq 0 ]
[ $(expr "${lines[0]}" : "usage: git secrets") -ne 0 ]
}
@test "-h prints help" {
repo_run git-secrets -h
[ $(expr "${lines[0]}" : "usage: git secrets") -ne 0 ]
}
@test "Invalid scan filename fails" {
repo_run git-secrets --scan /path/to/not/there
[ $status -eq 2 ]
echo "$output" | grep "No such file"
}
@test "Does not require secrets" {
git config --unset-all secrets.patterns || true
repo_run git-secrets --scan $BATS_TEST_FILENAME
[ $status -eq 0 ]
}
@test "No prohibited matches exits 0" {
echo 'it is ok' > "$BATS_TMPDIR/test.txt"
repo_run git-secrets --scan "$BATS_TMPDIR/test.txt"
[ $status -eq 0 ]
}
@test "Scans all files when no file provided" {
setup_bad_repo
repo_run git-secrets --scan
[ $status -eq 1 ]
}
@test "Scans all files including history" {
setup_bad_repo
repo_run git-secrets --scan-history
[ $status -eq 1 ]
}
@test "Scans all files when no file provided with secret in history" {
setup_bad_repo_history
repo_run git-secrets --scan
[ $status -eq 0 ]
}
@test "Scans all files including history with secret in history" {
setup_bad_repo_history
repo_run git-secrets --scan-history
[ $status -eq 1 ]
}
@test "Scans history with secrets distributed among branches in history" {
cd $TEST_REPO
echo '@todo' > $TEST_REPO/history_failure.txt
git add -A
git commit -m "Testing history"
echo 'todo' > $TEST_REPO/history_failure.txt
git add -A
git commit -m "Testing history"
git checkout -b testbranch
echo '@todo' > $TEST_REPO/history_failure.txt
git add -A
git commit -m "Testing history"
git checkout master
cd -
repo_run git-secrets --scan-history
[ $status -eq 1 ]
}
@test "Scans recursively" {
setup_bad_repo
mkdir -p $TEST_REPO/foo/bar/baz
echo '@todo more stuff' > $TEST_REPO/foo/bar/baz/data.txt
repo_run git-secrets --scan -r $TEST_REPO/foo
[ $status -eq 1 ]
}
@test "Scans recursively only if -r is given" {
setup_bad_repo
mkdir -p $TEST_REPO/foo/bar/baz
echo '@todo more stuff' > $TEST_REPO/foo/bar/baz/data.txt
repo_run git-secrets --scan $TEST_REPO/foo
[ $status -eq 0 ]
}
@test "Excludes allowed patterns from failures" {
git config --add secrets.patterns 'foo="baz{1,5}"'
git config --add secrets.allowed 'foo="bazzz"'
echo 'foo="bazzz" is ok because 3 "z"s' > "$BATS_TMPDIR/test.txt"
repo_run git-secrets --scan "$BATS_TMPDIR/test.txt"
[ $status -eq 0 ]
echo 'This is NOT: ok foo="bazzzz"' > "$BATS_TMPDIR/test.txt"
repo_run git-secrets --scan "$BATS_TMPDIR/test.txt"
[ $status -eq 1 ]
}
@test "Prohibited matches exits 1" {
file="$TEST_REPO/test.txt"
echo '@todo stuff' > $file
echo 'this is forbidden right?' >> $file
repo_run git-secrets --scan $file
[ $status -eq 1 ]
[ "${lines[0]}" == "$file:1:@todo stuff" ]
[ "${lines[1]}" == "$file:2:this is forbidden right?" ]
}
@test "Only matches on word boundaries" {
file="$TEST_REPO/test.txt"
# Note that the following does not match as it is not a word.
echo 'mesa Jar Jar Binks' > $file
# The following do match because they are in word boundaries.
echo 'foo.me' >> $file
echo '"me"' >> $file
repo_run git-secrets --scan $file
[ $status -eq 1 ]
[ "${lines[0]}" == "$file:2:foo.me" ]
[ "${lines[1]}" == "$file:3:\"me\"" ]
}
@test "Can scan from stdin using -" {
echo "foo" | "${BATS_TEST_DIRNAME}/../git-secrets" --scan -
echo "me" | "${BATS_TEST_DIRNAME}/../git-secrets" --scan - && exit 1 || true
}
@test "installs hooks for repo" {
setup_bad_repo
repo_run git-secrets --install $TEST_REPO
[ -f $TEST_REPO/.git/hooks/pre-commit ]
[ -f $TEST_REPO/.git/hooks/prepare-commit-msg ]
[ -f $TEST_REPO/.git/hooks/commit-msg ]
}
@test "fails if hook exists and no -f" {
repo_run git-secrets --install $TEST_REPO
repo_run git-secrets --install $TEST_REPO
[ $status -eq 1 ]
}
@test "Overwrites hooks if -f is given" {
repo_run git-secrets --install $TEST_REPO
repo_run git-secrets --install -f $TEST_REPO
[ $status -eq 0 ]
}
@test "installs hooks for repo with Debian style directories" {
setup_bad_repo
mkdir $TEST_REPO/.git/hooks/pre-commit.d
mkdir $TEST_REPO/.git/hooks/prepare-commit-msg.d
mkdir $TEST_REPO/.git/hooks/commit-msg.d
run git-secrets --install $TEST_REPO
[ -f $TEST_REPO/.git/hooks/pre-commit.d/git-secrets ]
[ -f $TEST_REPO/.git/hooks/prepare-commit-msg.d/git-secrets ]
[ -f $TEST_REPO/.git/hooks/commit-msg.d/git-secrets ]
}
@test "installs hooks to template directory" {
setup_bad_repo
run git-secrets --install $TEMPLATE_DIR
[ $status -eq 0 ]
run git init --template $TEMPLATE_DIR
[ $status -eq 0 ]
[ -f "${TEST_REPO}/.git/hooks/pre-commit" ]
[ -f "${TEST_REPO}/.git/hooks/prepare-commit-msg" ]
[ -f "${TEST_REPO}/.git/hooks/commit-msg" ]
}
@test "Scans using keys from credentials file" {
echo 'aws_access_key_id = abc123' > $BATS_TMPDIR/test.ini
echo 'aws_secret_access_key=foobaz' >> $BATS_TMPDIR/test.ini
echo 'aws_access_key_id = "Bernard"' >> $BATS_TMPDIR/test.ini
echo 'aws_secret_access_key= "Laverne"' >> $BATS_TMPDIR/test.ini
echo 'aws_access_key_id= Hoagie+man' >> $BATS_TMPDIR/test.ini
cd $TEST_REPO
run git secrets --aws-provider $BATS_TMPDIR/test.ini
[ $status -eq 0 ]
echo "$output" | grep -F "foobaz"
echo "$output" | grep -F "abc123"
echo "$output" | grep -F "Bernard"
echo "$output" | grep -F "Laverne"
echo "$output" | grep -F 'Hoagie\+man'
run git secrets --add-provider -- git secrets --aws-provider $BATS_TMPDIR/test.ini
[ $status -eq 0 ]
echo '(foobaz) test' > $TEST_REPO/bad_file
echo "abc123 test" >> $TEST_REPO/bad_file
echo 'Bernard test' >> $TEST_REPO/bad_file
echo 'Laverne test' >> $TEST_REPO/bad_file
echo 'Hoagie+man test' >> $TEST_REPO/bad_file
repo_run git-secrets --scan $TEST_REPO/bad_file
[ $status -eq 1 ]
echo "$output" | grep "foobaz"
echo "$output" | grep "abc123"
echo "$output" | grep "Bernard"
echo "$output" | grep "Laverne"
echo "$output" | grep -F 'Hoagie+man'
}
@test "Lists secrets for a repo" {
repo_run git-secrets --list
[ $status -eq 0 ]
echo "$output" | grep -F 'secrets.patterns @todo'
echo "$output" | grep -F 'secrets.patterns forbidden|me'
}
@test "Adds secrets to a repo and de-dedupes" {
repo_run git-secrets --add 'testing+123'
[ $status -eq 0 ]
repo_run git-secrets --add 'testing+123'
[ $status -eq 1 ]
repo_run git-secrets --add --literal 'testing+abc'
[ $status -eq 0 ]
repo_run git-secrets --add -l 'testing+abc'
[ $status -eq 1 ]
repo_run git-secrets --list
echo "$output" | grep -F 'secrets.patterns @todo'
echo "$output" | grep -F 'secrets.patterns forbidden|me'
echo "$output" | grep -F 'secrets.patterns testing+123'
echo "$output" | grep -F 'secrets.patterns testing\+abc'
}
@test "Adds allowed patterns to a repo and de-dedupes" {
repo_run git-secrets --add -a 'testing+123'
[ $status -eq 0 ]
repo_run git-secrets --add --allowed 'testing+123'
[ $status -eq 1 ]
repo_run git-secrets --add -a -l 'testing+abc'
[ $status -eq 0 ]
repo_run git-secrets --add -a -l 'testing+abc'
[ $status -eq 1 ]
repo_run git-secrets --list
echo "$output" | grep -F 'secrets.patterns @todo'
echo "$output" | grep -F 'secrets.patterns forbidden|me'
echo "$output" | grep -F 'secrets.allowed testing+123'
echo "$output" | grep -F 'secrets.allowed testing\+abc'
}
@test "Empty lines must be ignored in .gitallowed files" {
setup_bad_repo
echo '' >> $TEST_REPO/.gitallowed
repo_run git-secrets --scan
[ $status -eq 1 ]
}
@test "Comment lines must be ignored in .gitallowed files" {
setup_bad_repo_with_hash
repo_run git-secrets --scan
[ $status -eq 1 ]
echo '#hash' > $TEST_REPO/.gitallowed
repo_run git-secrets --scan
[ $status -eq 1 ]
echo 'hash' > $TEST_REPO/.gitallowed
repo_run git-secrets --scan
[ $status -eq 0 ]
}
@test "Scans all files and allowing none of the bad patterns in .gitallowed" {
setup_bad_repo
echo 'hello' > $TEST_REPO/.gitallowed
repo_run git-secrets --scan
[ $status -eq 1 ]
}
@test "Scans all files and allowing all bad patterns in .gitallowed" {
setup_bad_repo
echo '@todo' > $TEST_REPO/.gitallowed
echo 'forbidden' >> $TEST_REPO/.gitallowed
echo 'me' >> $TEST_REPO/.gitallowed
repo_run git-secrets --scan
[ $status -eq 0 ]
}
@test "Adds common AWS patterns" {
repo_run git config --unset-all secrets
repo_run git-secrets --register-aws
git config --local --get secrets.providers
repo_run git-secrets --list
echo "$output" | grep -F '(A3T[A-Z0-9]|AKIA|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)[A-Z0-9]{16}'
echo "$output" | grep "AKIAIOSFODNN7EXAMPLE"
echo "$output" | grep "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
}
@test "Adds providers" {
repo_run git-secrets --add-provider -- echo foo baz bar
[ $status -eq 0 ]
repo_run git-secrets --add-provider -- echo bam
[ $status -eq 0 ]
repo_run git-secrets --list
echo "$output" | grep -F 'echo foo baz bar'
echo "$output" | grep -F 'echo bam'
echo 'foo baz bar' > $TEST_REPO/bad_file
echo 'bam' >> $TEST_REPO/bad_file
repo_run git-secrets --scan $TEST_REPO/bad_file
[ $status -eq 1 ]
echo "$output" | grep -F 'foo baz bar'
echo "$output" | grep -F 'bam'
}
@test "Strips providers that return nothing" {
repo_run git-secrets --add-provider -- 'echo'
[ $status -eq 0 ]
repo_run git-secrets --add-provider -- 'echo 123'
[ $status -eq 0 ]
repo_run git-secrets --list
echo "$output" | grep -F 'echo 123'
echo 'foo' > $TEST_REPO/bad_file
repo_run git-secrets --scan $TEST_REPO/bad_file
[ $status -eq 0 ]
}
@test "--recursive cannot be used with SCAN_*" {
repo_run git-secrets --scan -r --cached
[ $status -eq 1 ]
repo_run git-secrets --scan -r --no-index
[ $status -eq 1 ]
repo_run git-secrets --scan -r --untracked
[ $status -eq 1 ]
}
@test "--recursive can be used with --scan" {
repo_run git-secrets --scan -r
[ $status -eq 0 ]
}
@test "--recursive can't be used with --list" {
repo_run git-secrets --list -r
[ $status -eq 1 ]
}
@test "-f can only be used with --install" {
repo_run git-secrets --scan -f
[ $status -eq 1 ]
}
@test "-a can only be used with --add" {
repo_run git-secrets --scan -a
[ $status -eq 1 ]
}
@test "-l can only be used with --add" {
repo_run git-secrets --scan -l
[ $status -eq 1 ]
}
@test "--cached can only be used with --scan" {
repo_run git-secrets --list --cached
[ $status -eq 1 ]
}
@test "--no-index can only be used with --scan" {
repo_run git-secrets --list --no-index
[ $status -eq 1 ]
}
@test "--untracked can only be used with --scan" {
repo_run git-secrets --list --untracked
[ $status -eq 1 ]
}
|