@@ -2,6 +2,7 @@ package filecontent
2
2
3
3
import (
4
4
"fmt"
5
+ "regexp"
5
6
"strings"
6
7
"talisman/detector/helpers"
7
8
"talisman/detector/severity"
@@ -20,10 +21,10 @@ var emptyTalismanRC = &talismanrc.TalismanRC{IgnoreConfigs: []talismanrc.IgnoreC
20
21
var defaultChecksumCompareUtility = helpers .
21
22
NewChecksumCompare (nil , utility .MakeHasher ("default" , "." ), emptyTalismanRC )
22
23
var dummyCallback = func () {}
24
+ var filename = "filename"
23
25
24
26
func TestShouldNotFlagSafeText (t * testing.T ) {
25
27
results := helpers .NewDetectionResults (talismanrc .HookMode )
26
- filename := "filename"
27
28
additions := []gitrepo.Addition {gitrepo .NewAddition (filename , []byte ("prettySafe" ))}
28
29
29
30
NewFileContentDetector (emptyTalismanRC ).
@@ -33,7 +34,6 @@ func TestShouldNotFlagSafeText(t *testing.T) {
33
34
34
35
func TestShouldIgnoreFileIfNeeded (t * testing.T ) {
35
36
results := helpers .NewDetectionResults (talismanrc .HookMode )
36
- filename := "filename"
37
37
additions := []gitrepo.Addition {gitrepo .NewAddition (filename , []byte ("prettySafe" ))}
38
38
talismanRCIWithFilenameIgnore := & talismanrc.TalismanRC {
39
39
IgnoreConfigs : []talismanrc.IgnoreConfig {
@@ -59,7 +59,6 @@ func TestShouldNotFlag4CharSafeText(t *testing.T) {
59
59
the encoded value of i· rather just a plain abcd input see
60
60
stackoverflow.com/questions/8571501/how-to-check-whether-the-string-is-base64-encoded-or-not#comment23919648_8571649*/
61
61
results := helpers .NewDetectionResults (talismanrc .HookMode )
62
- filename := "filename"
63
62
additions := []gitrepo.Addition {gitrepo .NewAddition (filename , []byte ("abcd" ))}
64
63
65
64
NewFileContentDetector (emptyTalismanRC ).
@@ -71,7 +70,6 @@ func TestShouldNotFlagLowEntropyBase64Text(t *testing.T) {
71
70
const lowEntropyString string = "YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWEK"
72
71
results := helpers .NewDetectionResults (talismanrc .HookMode )
73
72
content := []byte (lowEntropyString )
74
- filename := "filename"
75
73
additions := []gitrepo.Addition {gitrepo .NewAddition (filename , content )}
76
74
77
75
NewFileContentDetector (emptyTalismanRC ).
@@ -82,7 +80,6 @@ func TestShouldNotFlagLowEntropyBase64Text(t *testing.T) {
82
80
func TestShouldFlagPotentialAWSSecretKeys (t * testing.T ) {
83
81
const awsSecretAccessKey string = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
84
82
results := helpers .NewDetectionResults (talismanrc .HookMode )
85
- filename := "filename"
86
83
additions := []gitrepo.Addition {gitrepo .NewAddition (filename , []byte (awsSecretAccessKey ))}
87
84
filePath := additions [0 ].Path
88
85
@@ -99,7 +96,6 @@ func TestShouldFlagPotentialAWSSecretKeys(t *testing.T) {
99
96
func TestShouldFlagPotentialSecretWithoutTrimmingWhenLengthLessThan50Characters (t * testing.T ) {
100
97
const secret string = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9asdfa"
101
98
results := helpers .NewDetectionResults (talismanrc .HookMode )
102
- filename := "filename"
103
99
additions := []gitrepo.Addition {gitrepo .NewAddition (filename , []byte (secret ))}
104
100
filePath := additions [0 ].Path
105
101
@@ -117,7 +113,6 @@ func TestShouldFlagPotentialJWT(t *testing.T) {
117
113
"OiJDaHJpcyBTZXZpbGxlamEiLCJhZG1pbiI6dHJ1ZX0.03f329983b86f7d9a9f5fef85305880101d5e302afafa20154d094b229f757"
118
114
results := helpers .NewDetectionResults (talismanrc .HookMode )
119
115
content := []byte (jwt )
120
- filename := "filename"
121
116
additions := []gitrepo.Addition {gitrepo .NewAddition (filename , content )}
122
117
filePath := additions [0 ].Path
123
118
@@ -141,7 +136,6 @@ func TestShouldFlagPotentialSecretsWithinJavaCode(t *testing.T) {
141
136
"}"
142
137
results := helpers .NewDetectionResults (talismanrc .HookMode )
143
138
content := []byte (dangerousJavaCode )
144
- filename := "filename"
145
139
additions := []gitrepo.Addition {gitrepo .NewAddition (filename , content )}
146
140
filePath := additions [0 ].Path
147
141
@@ -162,7 +156,6 @@ func TestShouldNotFlagPotentialSecretsWithinSafeJavaCode(t *testing.T) {
162
156
" }\r \n \r \n " +
163
157
"}"
164
158
results := helpers .NewDetectionResults (talismanrc .HookMode )
165
- filename := "filename"
166
159
additions := []gitrepo.Addition {gitrepo .NewAddition (filename , []byte (safeJavaCode ))}
167
160
168
161
NewFileContentDetector (emptyTalismanRC ).
@@ -173,7 +166,6 @@ func TestShouldNotFlagPotentialSecretsWithinSafeJavaCode(t *testing.T) {
173
166
func TestShouldNotFlagPotentialSecretsWithinSafeLongMethodName (t * testing.T ) {
174
167
safeLongMethodName := "TestBase64DetectorShouldNotDetectLongMethodNamesEvenWithRidiculousHighEntropyWordsMightExist"
175
168
results := helpers .NewDetectionResults (talismanrc .HookMode )
176
- filename := "filename"
177
169
additions := []gitrepo.Addition {gitrepo .NewAddition (filename , []byte (safeLongMethodName ))}
178
170
179
171
NewFileContentDetector (emptyTalismanRC ).
@@ -184,7 +176,6 @@ func TestShouldNotFlagPotentialSecretsWithinSafeLongMethodName(t *testing.T) {
184
176
func TestShouldFlagPotentialSecretsEncodedInHex (t * testing.T ) {
185
177
const hex string = "68656C6C6F20776F726C6421"
186
178
results := helpers .NewDetectionResults (talismanrc .HookMode )
187
- filename := "filename"
188
179
additions := []gitrepo.Addition {gitrepo .NewAddition (filename , []byte (hex ))}
189
180
filePath := additions [0 ].Path
190
181
@@ -198,7 +189,6 @@ func TestShouldFlagPotentialSecretsEncodedInHex(t *testing.T) {
198
189
func TestShouldNotFlagPotentialCreditCardNumberIfAboveThreshold (t * testing.T ) {
199
190
const creditCardNumber string = "340000000000009"
200
191
results := helpers .NewDetectionResults (talismanrc .HookMode )
201
- filename := "filename"
202
192
additions := []gitrepo.Addition {gitrepo .NewAddition (filename , []byte (creditCardNumber ))}
203
193
talismanRCWithThreshold := & talismanrc.TalismanRC {Threshold : severity .High }
204
194
checksumCompareWithThreshold := helpers .
@@ -210,12 +200,44 @@ func TestShouldNotFlagPotentialCreditCardNumberIfAboveThreshold(t *testing.T) {
210
200
assert .False (t , results .HasFailures (), "Expected no base64 detection when threshold is higher" )
211
201
}
212
202
203
+ func TestShouldNotFlagPotentialSecretsIfIgnored (t * testing.T ) {
204
+ const hex string = "68656C6C6F20776F726C6421"
205
+ talismanRCWithIgnores := & talismanrc.TalismanRC {
206
+ AllowedPatterns : []* regexp.Regexp {regexp .MustCompile ("[0-9a-fA-F]*" )}}
207
+ results := helpers .NewDetectionResults (talismanrc .HookMode )
208
+ additions := []gitrepo.Addition {gitrepo .NewAddition (filename , []byte (hex ))}
209
+
210
+ NewFileContentDetector (emptyTalismanRC ).
211
+ Test (defaultChecksumCompareUtility , additions , talismanRCWithIgnores , results , dummyCallback )
212
+
213
+ assert .False (t , results .HasFailures (), "Expected file ignore allowed pattern for hex text" )
214
+ }
215
+
216
+ func TestResultsShouldNotFlagCreditCardNumberIfSpecifiedInFileIgnores (t * testing.T ) {
217
+ const creditCardNumber string = "340000000000009"
218
+ results := helpers .NewDetectionResults (talismanrc .HookMode )
219
+ fileIgnoreConfig := & talismanrc.FileIgnoreConfig {
220
+ FileName : filename , Checksum : "" ,
221
+ AllowedPatterns : []string {creditCardNumber },
222
+ }
223
+ talismanRCWithFileIgnore := & talismanrc.TalismanRC {
224
+ IgnoreConfigs : []talismanrc.IgnoreConfig {fileIgnoreConfig },
225
+ }
226
+ additions := []gitrepo.Addition {gitrepo .NewAddition (filename , []byte (creditCardNumber ))}
227
+
228
+ NewFileContentDetector (emptyTalismanRC ).
229
+ Test (defaultChecksumCompareUtility , additions , talismanRCWithFileIgnore , results , dummyCallback )
230
+
231
+ assert .False (t , results .HasFailures (), "Expected the creditcard number to be ignored based on talisman RC" )
232
+
233
+ }
234
+
235
+
213
236
func TestResultsShouldContainHexTextsIfHexAndBase64ExistInFile (t * testing.T ) {
214
237
const hex string = "68656C6C6F20776F726C6421"
215
238
const base64 string = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
216
239
const hexAndBase64 = hex + "\n " + base64
217
240
results := helpers .NewDetectionResults (talismanrc .HookMode )
218
- filename := "filename"
219
241
additions := []gitrepo.Addition {gitrepo .NewAddition (filename , []byte (hexAndBase64 ))}
220
242
filePath := additions [0 ].Path
221
243
@@ -232,7 +254,6 @@ func TestResultsShouldContainBase64TextsIfHexAndBase64ExistInFile(t *testing.T)
232
254
const base64 string = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
233
255
const hexAndBase64 = hex + "\n " + base64
234
256
results := helpers .NewDetectionResults (talismanrc .HookMode )
235
- filename := "filename"
236
257
additions := []gitrepo.Addition {gitrepo .NewAddition (filename , []byte (hexAndBase64 ))}
237
258
filePath := additions [0 ].Path
238
259
@@ -248,7 +269,6 @@ func TestResultsShouldContainBase64TextsIfHexAndBase64ExistInFile(t *testing.T)
248
269
func TestResultsShouldContainCreditCardNumberIfCreditCardNumberExistInFile (t * testing.T ) {
249
270
const creditCardNumber string = "340000000000009"
250
271
results := helpers .NewDetectionResults (talismanrc .HookMode )
251
- filename := "filename"
252
272
additions := []gitrepo.Addition {gitrepo .NewAddition (filename , []byte (creditCardNumber ))}
253
273
filePath := additions [0 ].Path
254
274
0 commit comments