| 1 | package keyword |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/stretchr/testify/assert" |
| 8 | |
| 9 | "github.com/DefangLabs/secret-detector/pkg/detectors/tests" |
| 10 | ) |
| 11 | |
| 12 | var testCases = []tests.TestCase{ |
| 13 | {"api_key", "api_key: 4P1_K3Y", true}, |
| 14 | {"apikey", "someAPIKey=50m34p1k3y", true}, |
| 15 | {"api-key", "api-key-prod=4p1-k3y-pr0d", true}, |
| 16 | {"private_key", "MY_APP_PRIVATE_KEY := my_4pp_pr1v473_k3y", true}, |
| 17 | {"password", "password: p455w0rd", true}, |
| 18 | {"pwd", `"myDatabasePwd" = "myd474b453pwd"`, true}, |
| 19 | {"secret", "'my_secret_code'='my_53cr37_c0d3'", true}, |
| 20 | {"account-key", `AWS-ACCOUNT-KEY: "4w5-4cc0un7-k3y"`, true}, |
| 21 | {"export token", "export NPM_TOKEN=npm_70k3n", true}, |
| 22 | {"keyword in value", "not_a_keyword = not_a_secret", false}, |
| 23 | {"value only", "secret_without_a_key", false}, |
| 24 | {"value is a variable", "password1: $MY_PASSWORD", false}, |
| 25 | {"value is an expression", "password2: {{ .secrets.password }}", false}, |
| 26 | {"empty input", "", false}, |
| 27 | } |
| 28 | |
| 29 | var keywordDetector = NewDetector() |
| 30 | |
| 31 | func TestScan(t *testing.T) { |
| 32 | tests.TestScan(t, keywordDetector, testCases) |
| 33 | } |
| 34 | |
| 35 | func TestScanWithMultipleMatches(t *testing.T) { |
| 36 | tests.TestScanWithMultipleMatchesWithoutKey(t, keywordDetector, testCases) |
| 37 | } |
| 38 | |
| 39 | func TestScanMap(t *testing.T) { |
| 40 | t.Run("Test ScanMap", func(t *testing.T) { |
| 41 | input := make(map[string]string, len(testCases)) |
| 42 | expectedKeys := make([]string, 0) |
| 43 | for _, test := range testCases { |
| 44 | expectedKey, expectedValue := extractKeyValue(test.Input) |
| 45 | input[expectedKey] = expectedValue |
| 46 | if test.ExpectDetection { |
| 47 | expectedKeys = append(expectedKeys, expectedKey) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | detectedSecrets, err := keywordDetector.ScanMap(input) |
| 52 | |
| 53 | assert.NoError(t, err) |
| 54 | tests.AssertKeysDetected(t, expectedKeys, keywordDetector.SecretType(), detectedSecrets) |
| 55 | }) |
| 56 | |
| 57 | t.Run("Test ScanMap with no detections", func(t *testing.T) { |
| 58 | input := make(map[string]string, len(testCases)) |
| 59 | for _, test := range testCases { |
| 60 | if !test.ExpectDetection { |
| 61 | k, v := extractKeyValue(test.Input) |
| 62 | input[k] = v |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | detectedSecrets, err := keywordDetector.ScanMap(input) |
| 67 | |
| 68 | assert.NoError(t, err) |
| 69 | assert.Len(t, detectedSecrets, 0) |
| 70 | }) |
| 71 | } |
| 72 | |
| 73 | func extractKeyValue(in string) (key, value string) { |
| 74 | var parts []string |
| 75 | for _, sep := range []string{":=", ":", "="} { |
| 76 | parts = strings.SplitN(in, sep, 2) |
| 77 | if len(parts) == 2 { |
| 78 | break |
| 79 | } |
| 80 | } |
| 81 | if len(parts) == 1 { |
| 82 | value = parts[0] |
| 83 | } |
| 84 | if len(parts) == 2 { |
| 85 | key = parts[0] |
| 86 | value = parts[1] |
| 87 | } |
| 88 | return |
| 89 | } |