@@ -2,9 +2,7 @@ import test from 'ava'
2
2
3
3
import { findLikelySecrets } from '../../lib/plugins_core/secrets_scanning/utils.js'
4
4
5
- const testFile = 'test.txt'
6
-
7
- test ( 'findLikelySecrets - should find secrets with common prefixes at the beginning of a line' , async ( t ) => {
5
+ test ( 'findLikelySecrets - should not find secrets without quotes or delimiters' , async ( t ) => {
8
6
const lines = [
9
7
'aws_123456789012345678' ,
10
8
'ghp_1234567890123456789' ,
@@ -14,29 +12,22 @@ test('findLikelySecrets - should find secrets with common prefixes at the beginn
14
12
15
13
lines . forEach ( ( text ) => {
16
14
const matches = findLikelySecrets ( { text } )
17
- t . is ( matches . length , 1 )
18
- t . like ( matches [ 0 ] , {
19
- // match found at the beginning of the line
20
- index : 0 ,
21
- } )
15
+ t . is ( matches . length , 0 )
22
16
} )
23
17
} )
24
18
25
- test ( 'findLikelySecrets - should find secrets with various delimiters at the beginning ' , async ( t ) => {
19
+ test ( 'findLikelySecrets - should find secrets with quotes or equals ' , async ( t ) => {
26
20
const matchingLines = [
27
21
'my_secret_key=aws_123456789012345678' ,
28
- 'awsKey: aws_123456789012345678' ,
29
22
'mySecretKey = aws_123456789012345678' ,
30
23
'secretKey="aws_123456789012345678"' ,
24
+ 'secretKey = "aws_123456789012345678"' ,
31
25
"secretKey='aws_123456789012345678'" ,
32
26
'secretKey=`aws_123456789012345678`' ,
33
- 'someKey, aws_123456789012345678, otherKey' ,
34
27
]
35
28
matchingLines . forEach ( ( text ) => {
36
29
const matches = findLikelySecrets ( { text } )
37
30
t . is ( matches . length , 1 )
38
-
39
- t . true ( matches [ 0 ] . index > 0 , 'Match should not be at the start of the line' )
40
31
} )
41
32
} )
42
33
@@ -47,12 +38,12 @@ test('findLikelySecrets - should not match values with spaces after prefix', asy
47
38
} )
48
39
49
40
test ( 'findLikelySecrets - should not match values that are too short' , async ( t ) => {
50
- const matches = findLikelySecrets ( { text : 'aws_key=12345678901' } )
41
+ const matches = findLikelySecrets ( { text : 'aws_key=" 12345678901" ' } )
51
42
t . is ( matches . length , 0 )
52
43
} )
53
44
54
45
test ( 'findLikelySecrets - should return the matched prefix as the key' , async ( t ) => {
55
- const matches = findLikelySecrets ( { text : 'github_pat_123456789012345678' } )
46
+ const matches = findLikelySecrets ( { text : 'mykey = " github_pat_123456789012345678" ' } )
56
47
t . is ( matches . length , 1 )
57
48
t . is ( matches [ 0 ] . prefix , 'github_pat_' )
58
49
} )
@@ -67,17 +58,13 @@ test('findLikelySecrets - should handle empty or invalid input', async (t) => {
67
58
} )
68
59
69
60
test ( 'findLikelySecrets - should match exactly minimum chars after prefix' , async ( t ) => {
70
- const exactMinChars = 'aws_123456789012' // Exactly 12 chars after prefix
61
+ const exactMinChars = 'value = " aws_123456789012" ' // Exactly 12 chars after prefix
71
62
const matches = findLikelySecrets ( { text : exactMinChars } )
72
63
t . is ( matches . length , 1 )
73
64
} )
74
65
75
66
test ( 'findLikelySecrets - should match different prefixes from LIKELY_SECRET_PREFIXES' , async ( t ) => {
76
- const lines = [
77
- 'ghp_123456789012345678' , // GitHub personal access token
78
- 'sk_live_123456789012345678' , // Stripe key
79
- 'AKIAXXXXXXXXXXXXXXXX' , // AWS access key
80
- ]
67
+ const lines = [ 'key="ghp_123456789012345678"' , 'key="sk_123456789012345678"' , 'key="aws_123456789012345678"' ]
81
68
82
69
lines . forEach ( ( text ) => {
83
70
const matches = findLikelySecrets ( { text } )
@@ -91,34 +78,35 @@ test('findLikelySecrets - should skip safe-listed values', async (t) => {
91
78
t . is ( matches . length , 0 )
92
79
} )
93
80
94
- test ( 'findLikelySecrets - should match secrets with special characters' , async ( t ) => {
95
- const lines = [
96
- 'aws_abc123!@#$%^&*()_+' , // Special chars
97
- 'ghp_abc-123_456.789' , // Common separator chars
98
- 'sk_live_123-456_789.000' , // Mix of numbers and separators
99
- ]
81
+ test ( 'findLikelySecrets - should allow dashes and alphanumeric characters only' , async ( t ) => {
82
+ const validLines = [ 'key="aws_abc123-456-789"' , 'key="ghp_abc-123-def-456"' ]
100
83
101
- lines . forEach ( ( text ) => {
102
- const matches = findLikelySecrets ( { text } )
103
- t . is ( matches . length , 1 )
84
+ validLines . forEach ( ( line ) => {
85
+ const matches = findLikelySecrets ( { text : line } )
86
+ t . is ( matches . length , 1 , `Should match line with dashes: ${ line } ` )
87
+ } )
88
+
89
+ const invalidLines = [ 'key="aws_abc123!@#$%^&*()_+"' , 'key="ghp_abc.123_456.789"' , 'key="sk_live_123_456_789"' ]
90
+
91
+ invalidLines . forEach ( ( line ) => {
92
+ const matches = findLikelySecrets ( { text : line } )
93
+ t . is ( matches . length , 0 , `Should not match line with special characters: ${ line } ` )
104
94
} )
105
95
} )
106
96
107
97
test ( 'findLikelySecrets - should match full secret value against omitValues' , async ( t ) => {
108
98
// Test both partial and full matches to ensure proper behavior
109
99
const partialMatch = findLikelySecrets ( {
110
- text : 'aws_123456789012_extra_chars_here ' ,
100
+ text : 'key="aws_123456789012extracharshere" ' ,
111
101
// The omitValue only partially matches the secret - we should still detect the secret
112
102
omitValuesFromEnhancedScan : [ 'aws_123456789012' ] ,
113
103
} )
114
104
t . is ( partialMatch . length , 1 )
115
105
116
106
const fullMatch = findLikelySecrets ( {
117
- line : 'aws_123456789012_extra_chars_here' ,
118
- file : testFile ,
119
- lineNumber : 1 ,
107
+ text : 'key="aws_123456789012extracharshere"' ,
120
108
// Omit the full secret value - we should not detect the secret
121
- omitValuesFromEnhancedScan : [ 'aws_123456789012_extra_chars_here ' ] ,
109
+ omitValuesFromEnhancedScan : [ 'aws_123456789012extracharshere ' ] ,
122
110
} )
123
111
t . is ( fullMatch . length , 0 )
124
112
} )
0 commit comments