-
Notifications
You must be signed in to change notification settings - Fork 245
/
Copy pathpattern_detector_test.go
144 lines (124 loc) · 6.58 KB
/
pattern_detector_test.go
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
package pattern
import (
"regexp"
"strings"
"talisman/detector/helpers"
"talisman/detector/severity"
"talisman/gitrepo"
"talisman/talismanrc"
"talisman/utility"
"testing"
"github.com/stretchr/testify/assert"
)
var talismanRC = &talismanrc.TalismanRC{}
var defaultChecksumCompare = helpers.NewChecksumCompare(nil, utility.MakeHasher("default", "."), talismanRC)
var dummyCallback = func() {}
var (
customPatterns []talismanrc.PatternString
)
func TestShouldDetectPasswordPatterns(t *testing.T) {
filename := "secret.txt"
values := [7]string{"password", "secret", "key", "pwd", "pass", "pword", "passphrase"}
for i := 0; i < len(values); i++ {
shouldPassDetectionOfSecretPattern(filename, []byte(strings.ToTitle(values[i])+":UnsafeString"), t)
shouldPassDetectionOfSecretPattern(filename, []byte(values[i]+"=UnsafeString"), t)
shouldPassDetectionOfSecretPattern(filename, []byte("."+values[i]+"=randomStringGoesHere}"), t)
shouldPassDetectionOfSecretPattern(filename, []byte(":"+values[i]+" randomStringGoesHere"), t)
shouldPassDetectionOfSecretPattern(filename, []byte(values[i]+" ,\"randomStringGoesHere\""), t)
shouldPassDetectionOfSecretPattern(filename, []byte("'" + values[i]+"' ,\"randomStringGoesHere\""), t)
shouldPassDetectionOfSecretPattern(filename, []byte("\"" + values[i]+"\" ,\"randomStringGoesHere\""), t)
shouldPassDetectionOfSecretPattern(filename,
[]byte("\"SERVER_"+strings.ToUpper(values[i])+"\" : UnsafeString"),
t)
shouldPassDetectionOfSecretPattern(filename, []byte(values[i]+"2-string : UnsafeString"), t)
shouldPassDetectionOfSecretPattern(filename,
[]byte("<"+values[i]+" data=123> randomStringGoesHere </"+values[i]+">"),
t)
shouldPassDetectionOfSecretPattern(filename,
[]byte("<admin "+values[i]+"> randomStringGoesHere </my"+values[i]+">"),
t)
}
shouldPassDetectionOfSecretPattern(filename, []byte("\"pw\" : UnsafeString"), t)
shouldPassDetectionOfSecretPattern(filename, []byte("Pw=UnsafeString"), t)
shouldPassDetectionOfSecretPattern(filename, []byte("<ConsumerKey>alksjdhfkjaklsdhflk12345adskjf</ConsumerKey>"), t)
shouldPassDetectionOfSecretPattern(filename, []byte("AWS key :"), t)
shouldPassDetectionOfSecretPattern(filename, []byte(`BEGIN RSA PRIVATE KEY-----
aghjdjadslgjagsfjlsgjalsgjaghjldasja
-----END RSA PRIVATE KEY`), t)
shouldPassDetectionOfSecretPattern(filename, []byte(`PWD=appropriate`), t)
shouldPassDetectionOfSecretPattern(filename, []byte(`pass=appropriate`), t)
shouldPassDetectionOfSecretPattern(filename, []byte(`adminpwd=appropriate`), t)
shouldFailDetectionOfSecretPattern(filename, []byte("\"pAsSWoRD\" :1234567"), t)
shouldFailDetectionOfSecretPattern(filename, []byte(`setPassword("12345678")`), t)
shouldFailDetectionOfSecretPattern(filename, []byte(`setenv(password,123456)`), t)
shouldFailDetectionOfSecretPattern(filename, []byte(`random=12345678)`), t)
}
func TestShouldIgnorePasswordPatterns(t *testing.T) {
results := helpers.NewDetectionResults(talismanrc.HookMode)
content := []byte("\"password\" : UnsafePassword")
filename := "secret.txt"
additions := []gitrepo.Addition{gitrepo.NewAddition(filename, content)}
fileIgnoreConfig := &talismanrc.FileIgnoreConfig{
FileName: filename,
Checksum: "833b6c24c8c2c5c7e1663226dc401b29c005492dc76a1150fc0e0f07f29d4cc3",
IgnoreDetectors: []string{"filecontent"},
AllowedPatterns: []string{}}
ignores := &talismanrc.TalismanRC{IgnoreConfigs: []talismanrc.IgnoreConfig{fileIgnoreConfig}}
NewPatternDetector(customPatterns).Test(defaultChecksumCompare, additions, ignores, results, dummyCallback)
assert.True(t, results.Successful(), "Expected file %s to be ignored by pattern", filename)
}
func TestShouldIgnoreAllowedPattern(t *testing.T) {
results := helpers.NewDetectionResults(talismanrc.HookMode)
content := []byte("\"key\" : \"This is an allowed keyword\"\npassword=y0uw1lln3v3rgu3ssmyP@55w0rd")
filename := "allowed_pattern.txt"
additions := []gitrepo.Addition{gitrepo.NewAddition(filename, content)}
fileIgnoreConfig := &talismanrc.FileIgnoreConfig{
FileName: filename, Checksum: "",
IgnoreDetectors: []string{},
AllowedPatterns: []string{"key"}}
ignores := &talismanrc.TalismanRC{
IgnoreConfigs: []talismanrc.IgnoreConfig{fileIgnoreConfig},
AllowedPatterns: []*regexp.Regexp{regexp.MustCompile("password")}}
NewPatternDetector(customPatterns).Test(defaultChecksumCompare, additions, ignores, results, dummyCallback)
assert.True(t,
results.Successful(),
"Expected keywords %v %v to be ignored by Talisman", fileIgnoreConfig.AllowedPatterns, ignores.AllowedPatterns)
}
func TestShouldOnlyWarnSecretPatternIfBelowThreshold(t *testing.T) {
results := helpers.NewDetectionResults(talismanrc.HookMode)
content := []byte(`password=UnsafeString`)
filename := "secret.txt"
additions := []gitrepo.Addition{gitrepo.NewAddition(filename, content)}
talismanRCWithThreshold := &talismanrc.TalismanRC{Threshold: severity.High}
checksumCompare := helpers.NewChecksumCompare(nil, utility.MakeHasher("default", "."), talismanRCWithThreshold)
NewPatternDetector(customPatterns).Test(checksumCompare, additions, talismanRCWithThreshold, results, dummyCallback)
assert.False(t, results.HasFailures(), "Expected file %s to not have failures", filename)
assert.True(t, results.HasWarnings(), "Expected file %s to have warnings", filename)
}
func DetectionOfSecretPattern(filename string, content []byte) (*helpers.DetectionResults, []gitrepo.Addition, string) {
results := helpers.NewDetectionResults(talismanrc.HookMode)
additions := []gitrepo.Addition{gitrepo.NewAddition(filename, content)}
NewPatternDetector(customPatterns).Test(defaultChecksumCompare, additions, talismanRC, results, dummyCallback)
expected := "Potential secret pattern : " + string(content)
return results, additions, expected
}
func shouldPassDetectionOfSecretPattern(filename string, content []byte, t *testing.T) {
results, additions, expected := DetectionOfSecretPattern(filename, content)
assert.Equal(t, expected, getFailureMessage(results, additions))
assert.Len(t, results.Results, 1)
}
func shouldFailDetectionOfSecretPattern(filename string, content []byte, t *testing.T) {
results, additions, expected := DetectionOfSecretPattern(filename, content)
assert.NotEqual(t, expected, getFailureMessage(results, additions))
assert.Len(t, results.Results, 0)
}
func getFailureMessage(results *helpers.DetectionResults, additions []gitrepo.Addition) string {
failureMessages := []string{}
for _, failureDetails := range results.GetFailures(additions[0].Path) {
failureMessages = append(failureMessages, failureDetails.Message)
}
if len(failureMessages) == 0 {
return ""
}
return failureMessages[0]
}